-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
Fix: Scope not being applied to OpenTelemetry spans in ASP.NET Core #2690
Conversation
…ed during OpenTelemetry span processing
Nice findings! Thanks for digging through that! This seems relevant: #972 |
Kind of yeah. currently our middleware includes this line... note the
That makes sense, for the most part, since the ScopeManager we use in ASP.NET Core is using an All good, until we want to start tracing stuff to do with the request via Diagnostic events that probably get emmited in the context of that AsyncLocal middleware pipeline but don't get received/consumed in that same AsyncLocal context. By the time the OpenTelemetry .NET SDK (and therefore our Looking at what happens in the Dispose method, it looks like the Scope itself doesn't get disposed - rather the scope/client call stack gets reset to it's previous state (which doesn't include the new scope that was cloned/pushed to the top of the stack). So if we retained a reference to that scope, we could prevent it from being garbage collected. Maybe we could avoid having to clone it then - I'll play around and see if I can get that working. |
It was never the whole stack (just the top scope on the stack) but actually the Scope itself doesn't get disposed explicitly, so we can avoid even that (see latest commit). No cloning required then! |
@bitsandfoxes I'm not sure how easy/practical that would be. We'd essentially have to replicate all of the logic here somehow and execute it in our SpanProcessor... then we probably don't want to run that logic again in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Instructions and example for changelogPlease add an entry to Example: ## Unreleased
- Scope not being applied to OpenTelemetry spans in ASP.NET Core ([#2690](https://github.com/getsentry/sentry-dotnet/pull/2690)) If none of the above apply, you can opt out of this check by adding |
Co-authored-by: Ivan Dlugos <dlugos.ivan@gmail.com> Co-authored-by: James Crosswell <jamescrosswell@users.noreply.github.com> Co-authored-by: Ivan Dlugos <6349682+vaind@users.noreply.github.com> Co-authored-by: Stefan Jandl <reg@bitfox.at> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: getsentry-bot <bot@sentry.io> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> fix build on mac m1 (#2702) Fix: Scope not being applied to OpenTelemetry spans in ASP.NET Core (#2690)
Problem
Fixes #2644... however
Transaction.Request.Method
was just one of a number of attributes missing that would ordinarily be applied to Transactions from the Scope:sentry-dotnet/src/Sentry/Internal/Hub.cs
Lines 484 to 488 in 0ae99b1
SentryMiddleware and SentrySpanProcessor don't share a ScopeAndClientStack
Part of the problem is that OpenTelemetry spans get created as a result of System.Diagnostic events that Otel subscribes to... and those events are received/processed in a different AsyncLocal context to the rest of our middleware pipeline. As such, our
SentryMiddleware
and ourSentrySpanProcessor
don't share the same ScopeAndClientStack:sentry-dotnet/src/Sentry/Internal/SentryScopeManager.cs
Lines 24 to 27 in efb5dc8
Scope popped before SentrySpanProcessor.OnEnd is called
Initially I tried to work around the problem above by using a
ScopeAndClientStack
that was bound toActivity.Current.RootId
rather than to the currentAsyncLocal
context... and that was possible. However it revealed another problem, which is that by the timeSentrySpanProcessor.OnEnd
is called, the Scope that was pushed by our middleware had already been popped from theScopeAndClientStack
... The fact that Otel is using a System.Diagnostics activity event subscription means that processing of these events happens async (kind of like events on a queue).Solution
The second of those two problems forced me to come up with a fairly simple solution in the end.. which is to:
sentry-dotnet/src/Sentry.AspNetCore/SentryMiddleware.cs
Lines 136 to 141 in d189a78
sentry-dotnet/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs
Lines 161 to 165 in 2e4e4b5