Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid accessing Id on activity start #2659

4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Prevent accessing activity Id before sampler runs in case of legacy
activities.
([2659](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2659))

* Added `ReadOnlyTagCollection` and expose `Tags` on `MetricPoint` instead of
`Keys`+`Values`
([#2642](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2642))
Expand Down
9 changes: 8 additions & 1 deletion src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ public void ActivityStarted(Activity activity)
{
if (this.IsEnabled(EventLevel.Verbose, EventKeywords.All))
{
this.ActivityStarted(activity.OperationName, activity.Id);
// Accessing activity.Id here will cause the Id to be initialized
// before the sampler runs in case where the activity is created using legacy way
// i.e. new Activity("Operation name"). This will result in Id not reflecting the
// correct sampling flags
// https://github.com/dotnet/runtime/issues/61857
var activityId = string.Concat("00-", activity.TraceId.ToHexString(), "-", activity.SpanId.ToHexString());
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
activityId = string.Concat(activityId, activity.ActivityTraceFlags.HasFlag(ActivityTraceFlags.Recorded) ? "-01" : "-00");
this.ActivityStarted(activity.OperationName, activityId);
}
}

Expand Down