Skip to content

How to Start a Root Span When Using Activity Compatibility #6741

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

Open
Lukazoid opened this issue Mar 4, 2025 · 2 comments
Open

How to Start a Root Span When Using Activity Compatibility #6741

Lukazoid opened this issue Mar 4, 2025 · 2 comments

Comments

@Lukazoid
Copy link

Lukazoid commented Mar 4, 2025

We currently make use of auto-instrumentation via the activity compatibility (our code all uses ActivitySource everywhere) and we have a situation where we'd like to create a new root span (we start a subscription to some service and want each item to be a root).

From the dotnet/runtime github this should be possible by setting Activity.Current = null before starting the activity (see dotnet/runtime#65528 (comment)), however in reality this seems to be having little effect and the spans are still being associated to the original parent.

Here is an example:

using var subscriber = activitySource.StartActivity("Subscription");

await Task.Run(async () =>
{
    var previous = Activity.Current;
    try
    {
        Activity.Current = null;
        using var callback = activitySource.StartActivity("Callback");
        await Task.Delay(1000);
    }
    finally
    {
        Activity.Current = previous;
    }
});

From browsing PRs I stumbled across the following comment:

One part of the design that I think is worth mentioning in the PR description is how we handle a new "root" Activity object when there iss already an active Datadog Span: In this case, activity.Parent == null (there's no in-process parent Activity object) but we set the ParentId property to the parent Span's SpanId

Originally posted by @zacharycmontoya in #2446 (review)

Could this automatic setting of the parent id to the implicitly created DataDog parent span be the reason setting Activity.Current to null is having no effect?

If so, how are we meant to create new root spans via ActivitySource?

Many thanks!

@Lukazoid
Copy link
Author

Lukazoid commented Mar 4, 2025

Here is a full example as a minimal API

app.MapGet("/span_test", async (ActivitySource activitySource) =>
{
    await Task.Run(async () =>
    {
        var previous = Activity.Current;
        try
        {
            Activity.Current = null;
            using var callback = activitySource.StartActivity("Callback");
            await Task.Delay(1000);
        }
        finally
        {
            Activity.Current = previous;
        }
    });
});

And what appears in datadog is the following:

Image

Image

However I would have expected to not see the Callback parented

@Lukazoid
Copy link
Author

For now I have worked around this by capturing ExecutionContext on application start and restoring it before starting the child activity I want detached which appears to be working but isn't too desirable.

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

No branches or pull requests

1 participant