Skip to content

Commit

Permalink
Correctly set activityKey
Browse files Browse the repository at this point in the history
  • Loading branch information
bouwkast committed Jul 10, 2024
1 parent d06d8fa commit c66e302
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ public static void ActivityStarted<T>(string sourceName, T activity, OpenTelemet
var activityTraceId = w3cActivity.TraceId;
var activitySpanId = w3cActivity.SpanId;

if (activityTraceId != null! && activitySpanId != null!)
{
activityKey = activityTraceId + activitySpanId;
}

// If the user has specified a parent context, get the parent Datadog SpanContext
if (w3cActivity is { ParentSpanId: { } parentSpanId, ParentId: { } parentId })
{
Expand Down Expand Up @@ -110,6 +105,7 @@ public static void ActivityStarted<T>(string sourceName, T activity, OpenTelemet
{
// TraceId (always 32 chars long even when using 64-bit ids)
w3cActivity.TraceId = activeSpan.Context.RawTraceId;
activityTraceId = w3cActivity.TraceId;

// SpanId (always 16 chars long)
w3cActivity.ParentSpanId = activeSpan.Context.RawSpanId;
Expand Down Expand Up @@ -137,6 +133,11 @@ public static void ActivityStarted<T>(string sourceName, T activity, OpenTelemet
rawTraceId = activityTraceId;
rawSpanId = activitySpanId;
}

if (activityTraceId != null! && activitySpanId != null!)
{
activityKey = activityTraceId + activitySpanId;
}
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,8 @@ public async Task SubmitsTraces(string packageVersion, bool legacyOperationNames
var otelSpans = spans.Where(s => s.Service == "MyServiceName");
var activitySourceSpans = spans.Where(s => s.Service == CustomServiceName);

if (string.IsNullOrEmpty(packageVersion) || new Version(packageVersion) >= new Version("1.7.0"))
{
otelSpans.Count().Should().Be(expectedSpanCount - 3); // there is another span w/ service == ServiceNameOverride
activitySourceSpans.Count().Should().Be(2);
}
else
{
otelSpans.Count().Should().Be(expectedSpanCount - 2); // there is another span w/ service == ServiceNameOverride
activitySourceSpans.Count().Should().Be(1);
}
otelSpans.Count().Should().Be(expectedSpanCount - 2); // there is another span w/ service == ServiceNameOverride
activitySourceSpans.Count().Should().Be(1);

ValidateIntegrationSpans(otelSpans, metadataSchemaVersion: "v0", expectedServiceName: "MyServiceName", isExternalSpan: false);
ValidateIntegrationSpans(activitySourceSpans, metadataSchemaVersion: "v0", expectedServiceName: CustomServiceName, isExternalSpan: false);
Expand Down Expand Up @@ -173,14 +165,7 @@ public async Task SubmitsTracesWithActivitySource(string packageVersion)
using var s = new AssertionScope();
var otelSpans = spans.Where(s => s.Service == "MyServiceName");

if (string.IsNullOrEmpty(packageVersion) || new Version(packageVersion) >= new Version("1.7.0"))
{
otelSpans.Count().Should().Be(expectedSpanCount - 2); // there is another span w/ service == ServiceNameOverride
}
else
{
otelSpans.Count().Should().Be(expectedSpanCount - 1); // there is another span w/ service == ServiceNameOverride
}
otelSpans.Count().Should().Be(expectedSpanCount - 1); // there is another span w/ service == ServiceNameOverride

ValidateIntegrationSpans(otelSpans, metadataSchemaVersion: "v0", expectedServiceName: "MyServiceName", isExternalSpan: false);

Expand Down
14 changes: 8 additions & 6 deletions tracer/test/snapshots/OpenTelemetrySdkTests.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@
SpanId: Id_29,
Name: internal,
Resource: StartRootSpan,
Service: CustomServiceName,
Service: MyServiceName,
Type: custom,
ParentId: Id_26,
Tags: {
Expand All @@ -698,12 +698,14 @@
otel.library.name: MyServiceName,
otel.status_code: STATUS_CODE_UNSET,
otel.trace_id: Guid_4,
runtime-id: Guid_2,
service.instance.id: Guid_3,
service.name: MyServiceName,
service.version: 1.0.x,
span.kind: internal,
version: 1.0.x
},
Metrics: {
_dd.top_level: 1.0
telemetry.sdk.language: dotnet,
telemetry.sdk.name: opentelemetry,
telemetry.sdk.version: sdk-version,
_dd.base_service: CustomServiceName
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@
SpanId: Id_29,
Name: internal,
Resource: StartRootSpan,
Service: CustomServiceName,
Service: MyServiceName,
Type: custom,
ParentId: Id_26,
Tags: {
Expand All @@ -698,12 +698,14 @@
otel.library.name: MyServiceName,
otel.status_code: STATUS_CODE_UNSET,
otel.trace_id: Guid_4,
runtime-id: Guid_2,
service.instance.id: Guid_3,
service.name: MyServiceName,
service.version: 1.0.x,
span.kind: internal,
version: 1.0.x
},
Metrics: {
_dd.top_level: 1.0
telemetry.sdk.language: dotnet,
telemetry.sdk.name: opentelemetry,
telemetry.sdk.version: sdk-version,
_dd.base_service: CustomServiceName
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@
SpanId: Id_29,
Name: MyServiceName.internal,
Resource: StartRootSpan,
Service: CustomServiceName,
Service: MyServiceName,
Type: custom,
ParentId: Id_26,
Tags: {
Expand All @@ -698,12 +698,14 @@
otel.library.name: MyServiceName,
otel.status_code: STATUS_CODE_UNSET,
otel.trace_id: Guid_4,
runtime-id: Guid_2,
service.instance.id: Guid_3,
service.name: MyServiceName,
service.version: 1.0.x,
span.kind: internal,
version: 1.0.x
},
Metrics: {
_dd.top_level: 1.0
telemetry.sdk.language: dotnet,
telemetry.sdk.name: opentelemetry,
telemetry.sdk.version: sdk-version,
_dd.base_service: CustomServiceName
}
},
{
Expand Down

0 comments on commit c66e302

Please sign in to comment.