Skip to content

Consider setting the transaction on the current scope automatically #2399

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.

Metadata

Metadata

Labels

.NETPull requests that update .net codeFeatureNew feature or requestGood First IssueGood for newcomers

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions