You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider setting the transaction on the current scope automatically #2399
Adding the transaction to the scope is required for methods like SentrySDK.GetSpan() to work - which is useful for then calling methods like StartChild() to instrument a specific portion of the app. If the transaction is not set on the scope, then GetSpan() returns null.
It's very easy to forget to do this. It would be better if it happened automatically when starting a transaction, instead of having to call a separate API.
Solution Brainstorm
When calling StartTransaction, if there's an active scope that doesn't already have a transaction set, that we could set it on the scope at that time.
We should be careful that any existing code that is setting it manually doesn't break, by only setting it when null, and only clearing it if we were the one to set it.
Possible breaking change
Since this is a possible breaking change, we could phase it in via some kind of SentryOptions.AutoSetScopeTransactions property or something, which defaults to false initially but which we could change to default to true in some future major release.
Would love to know the rational for this as I've possibly already gone and broken things:
I think I might have had an Aha moment. Folks can set Scope.Transaction manualy and the Sentry SDK often sets this automatically via our various integrations (e.g. our ASP.NET and ASP.NET Core middleware).
When we set this in the SpanProcessor we are potentially overwriting a value that might have been set by either our SDK or the user.
In the case of our SDK Integrations, I don't think this is a problem (yet) since we wouldn't generally mix any of the Integrations that set the transaction automatically (Sentry.AspNet, Sentry.AspNetCore, Sentry.Azure.Functions.Worker) and Otel (you either instrument with one or the other... but not both).
So I think the situation in which this would be a problem would be if someone had configured Sentry to use OpenTelemetry and had also manually instrumented part of their code using Sentry's instrumentation API... If an OpenTelemetry Span hit the span processor while they were in the middle of a SentryTransaction (e.g. if they were using OpenTelemetry to instrument GraphQL or something but using Sentry for the rest of their application) their Scope.Transaction could potentially be overwritten by the SpanProcessor logic.
If OTEL is being used everywhere under the hood, there would no longer be a difference between the Sentry instrumentation and the OTEL instrumentation and Scope.Transaction could be replaced by Activity.Current.
If OTEL is being used everywhere under the hood, there would no longer be a difference between the Sentry instrumentation and the OTEL instrumentation and Scope.Transaction could be replaced by Activity.Current.
It turns out we can't do this. It would mean taking a hard dependency on the OpenTelemetry NuGet packages... and we have fought (with good reason) for a long time against introducing any dependencies to the core Sentry package.
Activity
mattjohnsonpint commentedon Jun 5, 2023
With regard to potentially more than one concurrent transaction, only the first would be set on the scope.
Also, with regard to #2066 - we should not be setting transactions bridged from otel spans on the scope at all.
jamescrosswell commentedon Feb 22, 2024
Would love to know the rational for this as I've possibly already gone and broken things:
jamescrosswell commentedon Mar 12, 2024
I think I might have had an Aha moment. Folks can set
Scope.Transactionmanualy and the Sentry SDK often sets this automatically via our various integrations (e.g. our ASP.NET and ASP.NET Core middleware).When we set this in the SpanProcessor we are potentially overwriting a value that might have been set by either our SDK or the user.
In the case of our SDK Integrations, I don't think this is a problem (yet) since we wouldn't generally mix any of the Integrations that set the transaction automatically (
Sentry.AspNet,Sentry.AspNetCore,Sentry.Azure.Functions.Worker) and Otel (you either instrument with one or the other... but not both).So I think the situation in which this would be a problem would be if someone had configured Sentry to use OpenTelemetry and had also manually instrumented part of their code using Sentry's instrumentation API... If an OpenTelemetry Span hit the span processor while they were in the middle of a SentryTransaction (e.g. if they were using OpenTelemetry to instrument GraphQL or something but using Sentry for the rest of their application) their Scope.Transaction could potentially be overwritten by the SpanProcessor logic.
All of this should hopefully be resolved by:
If OTEL is being used everywhere under the hood, there would no longer be a difference between the Sentry instrumentation and the OTEL instrumentation and
Scope.Transactioncould be replaced byActivity.Current.jamescrosswell commentedon Feb 17, 2025
It turns out we can't do this. It would mean taking a hard dependency on the OpenTelemetry NuGet packages... and we have fought (with good reason) for a long time against introducing any dependencies to the core Sentry package.
jamescrosswell commentedon Jul 19, 2026
See :