Skip to content
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

Automatically detect exceptions and set Activity status #1853

Closed
reyang opened this issue Feb 26, 2021 · 1 comment · Fixed by #1858
Closed

Automatically detect exceptions and set Activity status #1853

reyang opened this issue Feb 26, 2021 · 1 comment · Fixed by #1858
Labels
enhancement New feature or request pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Comments

@reyang
Copy link
Member

reyang commented Feb 26, 2021

Feature Request

Is your feature request related to a problem?

Capturing exception and setting Activity status to ERROR could be a common need. It is a bit annoying to do this all over the places.

Describe the solution you'd like:

I wonder if it would be cool to have a processor that automatically detects exception when an Activity ended, and set the status to ERROR accordingly.

Describe alternatives you've considered.

using System;
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using OpenTelemetry;
using OpenTelemetry.Trace;

class SehDetectionProcessor : BaseProcessor<Activity>
{
    public override void OnEnd(Activity activity)
    {
        if (Marshal.GetExceptionPointers() != IntPtr.Zero)
        {
            activity.SetStatus(Status.Error);
        }
    }
}

class Program
{
    static readonly ActivitySource DemoSource = new ActivitySource("DemoSource");

    static void Main()
    {
        AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;

        using var tracerProvider = Sdk.CreateTracerProviderBuilder()
            .AddSource("DemoSource")
            .AddProcessor(new SehDetectionProcessor())
            .AddConsoleExporter()
            .Build();

        using (DemoSource.StartActivity("Foo"))
        {
            using (DemoSource.StartActivity("Bar"))
            {
                throw new Exception("Oops!");
            }
        }
    }

    static void UnhandledExceptionHandler(object source, UnhandledExceptionEventArgs args)
    {
        var activity = Activity.Current;

        while (activity != null)
        {
            activity.Dispose(); // TODO: probably check if the activity is already disposed/finished by looking at the end time?
            activity = activity.Parent;
        }
    }
}
@reyang reyang added enhancement New feature or request pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package labels Feb 26, 2021
@reyang
Copy link
Member Author

reyang commented Feb 26, 2021

@CodeBlanch do you see this useful or crazy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant