Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/shim/telemetryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,34 @@ export class TelemetryClient {
const attributes: Attributes = {
...telemetry.properties,
};
attributes[SEMATTRS_HTTP_METHOD] = "HTTP";
attributes[SEMATTRS_HTTP_URL] = telemetry.url;
attributes[SEMATTRS_HTTP_STATUS_CODE] = telemetry.resultCode;
// Only set HTTP attributes if we have the relevant data
if (!telemetry.name) {
attributes[SEMATTRS_HTTP_METHOD] = "HTTP";
}
if (telemetry.url) {
attributes[SEMATTRS_HTTP_URL] = telemetry.url;
}
if (telemetry.resultCode) {
attributes[SEMATTRS_HTTP_STATUS_CODE] = telemetry.resultCode;
}
const options: SpanOptions = {
kind: SpanKind.SERVER,
attributes: attributes,
startTime: startTime,
};
const span: any = trace.getTracer("ApplicationInsightsTracer")
.startSpan(telemetry.name, options, ctx);

if (telemetry.id) {
try {
if (span._spanContext) {
span._spanContext.traceId = telemetry.id;
}
} catch (error) {
diag.warn('Unable to set custom traceId on span:', error);
}
}

span.setStatus({
code: telemetry.success ? SpanStatusCode.OK : SpanStatusCode.ERROR,
});
Expand Down Expand Up @@ -219,9 +237,13 @@ export class TelemetryClient {
};
if (telemetry.dependencyTypeName) {
if (telemetry.dependencyTypeName.toLowerCase().indexOf("http") > -1) {
attributes[SEMATTRS_HTTP_METHOD] = "HTTP";
attributes[SEMATTRS_HTTP_URL] = telemetry.data;
attributes[SEMATTRS_HTTP_STATUS_CODE] = telemetry.resultCode;
// Only set HTTP URL and status code for HTTP dependencies
if (telemetry.data) {
attributes[SEMATTRS_HTTP_URL] = telemetry.data;
}
if (telemetry.resultCode) {
attributes[SEMATTRS_HTTP_STATUS_CODE] = telemetry.resultCode;
}
} else if (Util.getInstance().isDbDependency(telemetry.dependencyTypeName)) {
attributes[SEMATTRS_DB_SYSTEM] = telemetry.dependencyTypeName;
attributes[SEMATTRS_DB_STATEMENT] = telemetry.data;
Expand All @@ -241,6 +263,17 @@ export class TelemetryClient {
};
const span: any = trace.getTracer("ApplicationInsightsTracer")
.startSpan(telemetry.name, options, ctx);

if (telemetry.id) {
try {
if (span._spanContext) {
span._spanContext.traceId = telemetry.id;
}
} catch (error) {
diag.warn('Unable to set custom traceId on span:', error);
}
}

span.setStatus({
code: telemetry.success ? SpanStatusCode.OK : SpanStatusCode.ERROR,
});
Expand Down
41 changes: 40 additions & 1 deletion test/unitTests/shim/telemetryClient.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,50 @@ describe("shim/TelemetryClient", () => {
assert.equal(spans[0].name, "TestName");
assert.equal(spans[0].endTime[0] - spans[0].startTime[0], 2); // hrTime UNIX Epoch time in seconds
assert.equal(spans[0].kind, 1, "Span Kind"); // Incoming
assert.equal(spans[0].attributes["http.method"], "HTTP");
assert.equal(spans[0].attributes["http.status_code"], "401");
assert.equal(spans[0].attributes["http.url"], "http://test.com");
});

it("trackRequest with custom id sets traceId", async () => {
const customId = "custom-trace-id-123456789abcdef0";
const telemetry: RequestTelemetry = {
id: customId,
name: "CustomTraceRequest",
duration: 1000,
resultCode: "200",
url: "http://example.com",
success: true,
};
client.trackRequest(telemetry);
await tracerProvider.forceFlush();
const spans = testProcessor.spansProcessed;
assert.equal(spans.length, 1);
assert.equal(spans[0].name, "CustomTraceRequest");
// Verify that the span's traceId matches the custom id provided
assert.equal(spans[0].spanContext().traceId, customId);
});

it("trackDependency with custom id sets traceId", async () => {
const customId = "custom-dependency-trace-id-abcdef";
const telemetry: DependencyTelemetry = {
id: customId,
name: "CustomTraceDependency",
duration: 500,
resultCode: "200",
data: "http://api.example.com",
dependencyTypeName: "HTTP",
target: "api.example.com",
success: true,
};
client.trackDependency(telemetry);
await tracerProvider.forceFlush();
const spans = testProcessor.spansProcessed;
assert.equal(spans.length, 1);
assert.equal(spans[0].name, "CustomTraceDependency");
// Verify that the span's traceId matches the custom id provided
assert.equal(spans[0].spanContext().traceId, customId);
});

it("trackMetric", async () => {
const telemetry = {
name: "TestName",
Expand Down
Loading