Skip to content

RecordException doesn't work with ExceptionHandler middleware #1749

Open
@jcain

Description

@jcain

Bug Report

List of all OpenTelemetry NuGet
packages
and version that you are
using (e.g. OpenTelemetry 1.0.2):

OpenTelemetry 1.6.0-rc.1
OpenTelemetry.Api 1.6.0-rc.1
OpenTelemetry.Exporter.Console 1.6.0-rc.1
OpenTelemetry.Exporter.OpenTelemetryProtocol 1.6.0-rc.1
OpenTelemetry.Extensions.Hosting 1.6.0-rc.1
OpenTelemetry.Instrumentation.AspNetCore 1.5.1-beta.1

Runtime version (e.g. net462, net48, netcoreapp3.1, net6.0 etc. You can
find this information from the *.csproj file):

net7.0

Symptom

When using the ExceptionHandler middleware, e.g., .UseExceptionHandler(), the AddAspNetCoreInstrumentation auto instrumentation fails to record exceptions as activity events, even though RecordException = true.

What is the expected behavior?
AspNetCoreInstrumentation records exceptions even when the AspNetCore Exception Handler middleware is enabled.

What did you expect to see?

Exceptions recorded as activity events

What is the actual behavior?

Exceptions activity events are not recorded.

What did you see instead?

No events

Reproduce

Create a self-contained project using the template of your choice, apply the
minimum required code to result in the issue you're observing.

/*
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.1-beta.1" />
*/
using OpenTelemetry.Trace;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddProblemDetails();
builder.Services.AddOpenTelemetry()
   .WithTracing(tracerProviderBuilder =>
   {
      _ = tracerProviderBuilder.AddAspNetCoreInstrumentation(options =>
      {
         options.RecordException = true;
      });

      _ = tracerProviderBuilder.AddConsoleExporter();
   });

WebApplication app = builder.Build();

app.UseExceptionHandler(new ExceptionHandlerOptions()
{
   // Uncomment for a workaround
   //ExceptionHandler = (ctx) =>
   //{
   //   IExceptionHandlerFeature? feature = ctx.Features.Get<IExceptionHandlerFeature>();
   //   if (feature is not null)
   //   {
   //      Activity.Current?.RecordException(feature.Error);
   //   }
   //   return Task.CompletedTask;
   //}
});

app.MapGet("/", () =>
{
   throw new Exception("Error");
});

app.Run();

We will close this issue if:

  • The repro project you share with us is complex. We can't investigate custom
    projects, so don't point us to such, please.
  • If we can not reproduce the behavior you're reporting.

Additional Context

Implementing a custom ExceptionHandler via the ExceptionHandlerOptions provides a workaround, but I'm not sure if this is the preferred way to do this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp:instrumentation.aspnetcoreThings related to OpenTelemetry.Instrumentation.AspNetCorequestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions