Skip to content
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

Allow setting the active span on the scope #2364

Merged
merged 7 commits into from
May 12, 2023
Merged

Conversation

mattjohnsonpint
Copy link
Contributor

With this change, the GetSpan method on the Scope class has been obsoleted, and replaced with a Span property that has both a getter and a setter.

Previous usage:

SentrySdk.ConfigureScope(scope =>
{
    var span = scope.GetSpan();
});

New usage:

SentrySdk.ConfigureScope(scope =>
{
    var span = scope.Span;
});

That's the boring part. The new interesting part is that you can now set the span if desired. This can be used to pin an active span as the root span before starting complex tasks such as doing work in parallel. This allows each subsequent child span to attach to the root, rather than to each other.

For example:

var span = SentrySdk.GetSpan();
Parallel.ForEach(items, item =>
{
    SentrySdk.WithScope(scope =>
    {
        scope.Span = span;

        // Do some work that might generate new child spans, etc.
        // They'll all be created from the span set on the scope.
    });
});

Or asynchronously:

var span = SentrySdk.GetSpan();
await Parallel.ForEachAsync(items, async (item, cancellationToken) =>
{
    await SentrySdk.WithScopeAsync(async scope =>
    {
        scope.Span = span;

        // Do some async work that might generate new child spans, etc.
        // They'll all be created from the span set on the scope.
    });
});

Or asynchronously with tasks:

var span = SentrySdk.GetSpan();
await Task.WhenAll(items.Select(async item =>
{
    await SentrySdk.WithScopeAsync(async scope =>
    {
        scope.Span = span;

        // Do some async work that might generate new child spans, etc.
        // They'll all be created from the span set on the scope.
    });
}));

Resolves #1845
Resolves #1679

@mattjohnsonpint
Copy link
Contributor Author

Using the example from #1679, without this feature the spans would sometimes link to each other (because more than one parallel request is active at a time).

image

With this feature, the request spans are all rooted correctly:

image

@mattjohnsonpint mattjohnsonpint merged commit a8c8ea5 into main May 12, 2023
@mattjohnsonpint mattjohnsonpint deleted the feat/scope-set-span branch May 12, 2023 17:49
@getsentry getsentry deleted a comment from github-actions bot May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow defining the "Active Span" Option to link Http Request Spans to the Root Transaction?
2 participants