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

Conversation

vishweshbankwar
Copy link
Member

@vishweshbankwar vishweshbankwar commented Nov 22, 2021

Workaround for:
dotnet/runtime#61857

Changes

In case of legacy activities (i.e. new Activity()), we run the sampler after the activity is started. When the sampler runs it updates the ActivityTraceFlags property based on the sampling result. However, updating the trace flags on activity does not get reflected on Id if the Id was accessed before sampler runs. This results is incorrect propagation when the propagator uses Id property to set the traceparent header. This PR prevents accessing Id before sampler runs by manually creating it using TraceId, SpanId and ActivityTraceFlags.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Design discussion issue #
  • Changes in public API reviewed

@codecov
Copy link

codecov bot commented Nov 22, 2021

Codecov Report

Merging #2659 (7f86ffe) into main (9e83587) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head 7f86ffe differs from pull request most recent head 10f79a7. Consider uploading reports for the commit 10f79a7 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2659      +/-   ##
==========================================
+ Coverage   82.89%   82.91%   +0.01%     
==========================================
  Files         249      249              
  Lines        8705     8707       +2     
==========================================
+ Hits         7216     7219       +3     
+ Misses       1489     1488       -1     
Impacted Files Coverage Δ
...nTelemetry/Internal/OpenTelemetrySdkEventSource.cs 74.76% <100.00%> (+3.33%) ⬆️
...ZPages/Implementation/ZPagesExporterEventSource.cs 56.25% <0.00%> (-6.25%) ⬇️
...Telemetry/Internal/SelfDiagnosticsEventListener.cs 96.87% <0.00%> (-0.79%) ⬇️

@vishweshbankwar vishweshbankwar marked this pull request as ready for review November 22, 2021 23:53
@vishweshbankwar vishweshbankwar requested a review from a team November 22, 2021 23:53
}
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.

vishweshbankwar and others added 2 commits November 22, 2021 15:59
Co-authored-by: Reiley Yang <reyang@microsoft.com>
Co-authored-by: Reiley Yang <reyang@microsoft.com>
Copy link
Member

@reyang reyang left a comment

Choose a reason for hiding this comment

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

LGTM.

@reyang
Copy link
Member

reyang commented Nov 23, 2021

Might worth a changelog update.

@cijothomas
Copy link
Member

@vishweshbankwar Please update the description to explain the impact of the associated issue in the HttpClient/GrpcClient instrumentation or any others using Propagators and leveraging Activity.Id.

We were living with this issue for a loooon time until you found it - so could you open an issue to track improving the unit/functional tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants