Skip to content

Allow creation of a root Activity when Activity.Current is not null #65528

Open
@alanwest

Description

@alanwest

Regarding Span Creation the OpenTelemetry specification states that the API must accept:

The parent Context or an indication that the new Span should be a root Span.

The current ActivitySource.StartActivity API does not allow for this if Activity.Current != null. The following code creates a new activity as a child of Activity.Current.

var rootSpan = activitySource.StartActivity(
        "RootSpan",
        ActivityKind.Internal,
        parentContext: default)

One solution may be to set Activity.Current = null when StartActivity receives parentContext = default, but then it may be debatable whether Activity.Current should then be set to:

  • the newly created root activity, or
  • be restored to the previous value of Activity.Current prior to returning from StartActivity.

The OpenTelemetry specification also states:

In languages with implicit Context propagation, Span creation MUST NOT set the newly created Span as the active Span in the current Context by default, but this functionality MAY be offered additionally as a separate operation.

This implies that StartActivity should not affect the value of Activity.Current, but since it already does today it may be reasonable to set Activity.Current to the newly created root activity.

Another option could be to introduce a new API StartRootActivity that starts a new activity but does not affect Activity.Current. It would be up to the user to manage Activity.Current.


This need was originally outlined in open-telemetry/opentelemetry-dotnet#984.

@cijothomas

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions