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

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,21 @@ public void ActivityStarted(Activity activity)
{
if (this.IsEnabled(EventLevel.Verbose, EventKeywords.All))
{
this.ActivityStarted(activity?.Id);
// Accessing activity.Id here will cause the Id to be calculated and cached
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
// before the sampler runs. This will result in Id not reflecting the
// correct sampling flags.
// https://github.com/dotnet/runtime/issues/61857
string activityId = null;
if (activity == null)
{
this.ActivityStarted(activityId);
}
else
{
activityId = string.Concat("00-", activity.TraceId.ToHexString(), "-", activity.SpanId.ToHexString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using one concat instead of two (might be good to to benchmark and see what's the actual perf/alloc difference).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// * Summary *

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
Intel Core i7-8850H CPU 2.60GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET SDK=6.0.100
[Host] : .NET 5.0.12 (5.0.1221.52207), X64 RyuJIT
DefaultJob : .NET 5.0.12 (5.0.1221.52207), X64 RyuJIT

Method Mean Error StdDev
OneConcat 66.48 ns 1.381 ns 2.455 ns
TwoConcat 60.69 ns 1.014 ns 0.792 ns

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like two concat works better.

activityId = string.Concat(activityId, (activity.ActivityTraceFlags & ActivityTraceFlags.Recorded) != 0 ? "-01" : "-00");
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
this.ActivityStarted(activityId);
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ 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. This will result in Id not reflecting the
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
// 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 & ActivityTraceFlags.Recorded) != 0 ? "-01" : "-00");
this.ActivityStarted(activity.OperationName, activityId);
}
}

Expand Down