elastic / apm-agent-dotnet

https://www.elastic.co/guide/en/apm/agent/dotnet/current/index.html
Apache License 2.0
583 stars 209 forks source link

[BUG] Exceptions handled by IExceptionHandler<T> implementations are shown as Unhandled in APM UI #2312

Open ben-wallis opened 7 months ago

ben-wallis commented 7 months ago

APM Agent version

1.26.0

Environment

Operating system and version: N/A

.NET Framework/Core name and version .NET 8.0.2

Application Target Framework(s) (e.g. net462, netcoreapp3.1): net80

Describe the bug

In .NET 8 ASP.NET Core applications, the new IExceptionHandler<T> exception handlers result in exceptions showing as Unhandled in the APM UI even when they return true to indicate the exception was handled.

To Reproduce

A reproduction is available here: https://github.com/ben-wallis/apm-agent-dotnet/commit/be1ac9d63d9fda0055e87284e6b0572e6e2d0b2c

Even though ArithmericExceptionHandler in the above reproduction correclty handles the exception and returns true the exception is still shown on the Errors tab as Unhandled in APM:

image

Exceptions handled by IExceptionHandler<T> implementations emit the Microsoft.AspNetCore.Diagnostics.HandledException diagnostic event, whereas those that are truly unhandled emit the Microsoft.AspNetCore.Diagnostics.UnhandledException diagnostic event. Despite this difference, both diagnostic events result in the exception being captured as unhandled in AspNetCoreDiagnosticListener.cs due to both of these diagnostic events being handled in the same code path: https://github.com/elastic/apm-agent-dotnet/blame/a82dba1e52b20f303c16a772c3fdfdbffcdbfb76/src/integrations/Elastic.Apm.AspNetCore/DiagnosticListener/AspNetCoreDiagnosticListener.cs#L77

Expected behavior

Handled exceptions are not shown as Unhandled in the APM UI

Actual behavior

Handled exceptions are shown as Unhandled in the APM UI

stevejgordon commented 7 months ago

Thanks for raising this and including the repro @ben-wallis. We'll take a look at amending the logic to account for this.