Skip to content

Ending an activity will not restore the previously active one for explicit parents #1677

@Oberon00

Description

@Oberon00

(This concerns code in the core Framework Activity.cs, so I'm not sure if this is the right bugtracker to use)

Symptom

Activity.Current becomes null whenever any Activity with explicit parent is ended. I would instead have expected that the previously active activity would be restored, independent of whether it was the parent or not.

Reproduce

using System;
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Trace;

public class Program
{
    private static readonly ActivitySource MyActivitySource = new ActivitySource(
        "MyCompany.MyProduct.MyLibrary");

    public static void Main()
    {
        using var tracerProvider = Sdk.CreateTracerProviderBuilder()
            .SetSampler(new AlwaysOnSampler())
            .AddSource("MyCompany.MyProduct.MyLibrary")
            .Build();

        using (var activity = MyActivitySource.StartActivity("SayHello"))
        {
            Console.WriteLine("Current before: {0}", Activity.Current?.DisplayName);
            using (var child = MyActivitySource.StartActivity("SayWorld", ActivityKind.Internal, activity.Context))
            {
                Console.WriteLine("Current child: {0}", Activity.Current?.DisplayName);
            }

            Console.WriteLine("Current after: {0}", Activity.Current?.DisplayName ?? "(null)");
        }
    }
}

Actual Output:

Current before: SayHello
Current child: SayWorld
Current after: (null)

Expected output would be that the last line is Current after: SayHello.

Additional context

Responsible line in Activity.Stop:

https://github.com/dotnet/runtime/blob/v5.0.1/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs#L629

I think the Activity should capture a "previous" Activity independently of the Parent Activity and then always set that as active when stopping it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions