Skip to content

Decide whether auto instrumented transactions in MAUI should set Scope.Transaction #5313

Closed

Description

@jamescrosswell

Description

See:

// Note that we may be creating a transaction here and if so we explicitly don't store it on
// Scope.Transaction, because Scope.Transaction is AsyncLocal<T> and MAUI Apps have a global scope. The
// results would be that we would store the transaction on the scope, but it would never be cleared again,
// since the next call to OnPropertyChanged for this RelayCommand will (likely) be from a different thread.
var span = hub.StartSpan(SpanName, SpanOp);
if (span is ITransactionTracer transaction)
{
hub.ConfigureScope(scope => scope.Transaction = transaction);
}

The code comment contradicts the code implementation. Either we change/remove the comment or we change the code.

If the comment is correct then the code itself needs to be changed (seems likely but need to verify).

Activity

  1. added
    .NET
    on Jun 25, 2026
  2. linear-code commented on Jun 25, 2026

    @linear-code
  3. Flash0ver commented on Jul 1, 2026

    @Flash0ver
    Contributor

    We do that for ASP.NET Core already ... but .NET MAUI uses (or should at least use) a Global Scope (user code could, but should not, overwrite the default we set in our MAUI init)

    Thoughts

    • clicking/tapping a button that does perform a navigation
    • clicking/tapping a button that does not navigate

    Edge Case
    .NET MAUI app on Windows, where SQL Server is used, and DiagnosticSource integration is used, which only instruments when Scope.Transaction is not null. So performance spans on MAUI would not capture DB calls.

    We could also make this behavior an opt-in.

    This issue becomes relevant with #5138.

    Also considering https://develop.sentry.dev/sdk/foundations/state-management/scopes/

    • Global Scope = Global Mode (e.g. automatically in .NET MAUI app, should be manually set in WPF app)
    • Isolation = AsyncLocal flow (e.g. ASP.NET Core Requests)
    • Current = with AsyncLocalScopeManager we can SentrySdk.PushScope onto the Stack when not in Global Mode
  4. Flash0ver commented on Jul 8, 2026

    @Flash0ver
    Contributor

    We do want to auto-set the transaction onto the current scope, but only when there is no Transaction assigned to the scope already (scope.Transaction ??= transaction).

    Considered this scenario:

    • our auto-instrumentation via a Button click starts a Transaction
      • and now also sets it
    • the user's event handler StartTransaction starting a new Root-Transaction (unlike StartSpan that would start a Child-Span)
    • then, Scope.Transaction would still have our auto-instrumented UI-Span, rather than the user's Transaction
    • users could still "force" setting "their" Transaction via ConfigureScope(scope => scope.Transaction = transaction);
  5. self-assigned this
    on Jul 8, 2026
  6. jamescrosswell commented on Jul 9, 2026

    @jamescrosswell
    CollaboratorAuthor

    Implemented in:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

.NETFramework: MAUIImprovementSpans

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions