Skip to content

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

Open
@Lukazoid

Description

@Lukazoid

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions