open-telemetry/opentelemetry-dotnet

SetErrorStatusOnException with async Exceptions works incorrectly

Open

#2,753 opened on Dec 17, 2021

 (11 comments) (5 reactions) (0 assignees)C# (889 forks)auto 404
bughelp wantedkeep-open

Repository metrics

Stars
 (3,725 stars)
PR merge metrics
 (PR metrics pending)

Description

Bug Report

List of nugets

  • OpenTelemetry 1.1.0
  • OpenTelemetry.Exporter.Console 1.1.0

Runtime version:

  • net6.0

Symptom

When SetErrorStatusOnException is enabled and exception occurred and was caught in nested async methed, the outer activities are incorrectly tagged ar Error.

What is the expected behavior?

When all exceptions were caught in async methods, outer activities should not be tagged ar Error.

What is the actual behavior?

Currently when exception was thrown and caught in async method, outer activities are tagged as Error.

Reproduce

Code to reproduce the problem:

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

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

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

        try
        {
            using (var activity1 = MyActivitySource.StartActivity("Foo"))
            {
                using (var activity2 = MyActivitySource.StartActivity("Bar"))
                {
                    await Bar();
                    // no exception here
                }
            }
        }
        catch (Exception)
        {
            // swallow the exception
        }
    }

    private static async Task Bar()
    {
        await Task.Delay(100);
        try
        {
            await Bar2();
        }
        catch
        {
            // do nothing
        }
    }

    private static async Task Bar2()
    {
        await Task.Delay(100);
        throw new Exception("Oops!");
    }
}

Current result:

Activity.Id:          00-a74738702a69ac229573668ac075e01c-456b2d43151b21aa-01
Activity.ParentId:    00-a74738702a69ac229573668ac075e01c-bb28a8612b992338-01
Activity.ActivitySourceName: MyCompany.MyProduct.MyLibrary
Activity.DisplayName: Bar
Activity.Kind:        Internal
Activity.StartTime:   2021-12-17T10:18:34.8695981Z
Activity.Duration:    00:00:00.2853080
Activity.TagObjects:
    otel.status_code: ERROR
Resource associated with Activity:
    service.name: unknown_service:OpenTelemetryPoligon

Activity.Id:          00-a74738702a69ac229573668ac075e01c-bb28a8612b992338-01
Activity.ActivitySourceName: MyCompany.MyProduct.MyLibrary
Activity.DisplayName: Foo
Activity.Kind:        Internal
Activity.StartTime:   2021-12-17T10:18:34.8683046Z
Activity.Duration:    00:00:00.3186132
Activity.TagObjects:
    otel.status_code: ERROR
Resource associated with Activity:
    service.name: unknown_service:OpenTelemetryPoligon

Both activites are tagged with otel.status_code: ERROR

Contributor guide