Skip to content

Consider setting the transaction on the current scope automatically #2399

Closed as not planned

Description

@mattjohnsonpint

Problem Statement

When manually instrumenting a transaction, it's advised to add it to the current scope:

SentrySdk.ConfigureScope(scope => scope.Transaction = transaction);

This is in the docs: https://docs.sentry.io/platforms/dotnet/performance/instrumentation/custom-instrumentation/

And samples:

// This starts a new transaction and attaches it to the scope.
var transaction = SentrySdk.StartTransaction("Program Main", "function");
SentrySdk.ConfigureScope(scope => scope.Transaction = transaction);

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.

Activity

  1. mattjohnsonpint commented on Jun 5, 2023

    @mattjohnsonpint
    ContributorAuthor

    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.

  2. removed their assignment
    on Jun 20, 2023
  3. jamescrosswell commented on Feb 22, 2024

    @jamescrosswell
    Collaborator

    Also, with regard to #2066 - we should not be setting transactions bridged from otel spans on the scope at all.

    Would love to know the rational for this as I've possibly already gone and broken things:

  4. jamescrosswell commented on Mar 12, 2024

    @jamescrosswell
    Collaborator

    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.

    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.Transaction could be replaced by Activity.Current.

  5. jamescrosswell commented on Feb 17, 2025

    @jamescrosswell
    Collaborator

    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.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.

  6. removed theissue type on Aug 6, 2025
  7. added
    .NET
    and removed on Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETFeatureGood First Issue

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions