Exception middlewares introduce duplicates of error.type diagnostic tags in some situations #55159
Closed
1 task done
Labels
area-middleware
Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares
area-networking
Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Is there an existing issue for this?
Describe the bug
In some specific situations exceptions can cause the
Microsoft.AspNetCore.Hosting,http.server.request.duration
metric to duplicateerror.type
tags. One such situation is throwing an exception in a handler after a websocket connection is closed, but there might be others.This is devastating if downstream systems cannot handle tag duplication. E.g. with prometheus once tags are duplicated scraping fails completely, marking the service as down in prometheus. The only way to recover from this is restarting the service (until the issue reoccurs).
I found open-telemetry/opentelemetry-dotnet#5199 reported against OTEL which sounds like it might describe the same problem caused in other ways.
Expected Behavior
Regardless of in which situation an exception is thrown, the tags on the metrics should not be duplicated. Looking at the code this seems to be the intent of the implementation to begin with.
Steps To Reproduce
dotnet run
dotnet-counters collect -n DuplicateTagRepro Microsoft.AspNetCore.Hosting
(or any other tooling that allows seing theMicrosot.AspNetCore.Hosting
metrics.wscat --connect localhost:5221/
Microsoft.AspNetCore.Hosting,http.server.request.duration
metrics with duplicatederror.type=System.InvalidOperationException
tagsE.g.
Microsoft.AspNetCore.Hosting,http.server.request.duration (s)[error.type=System.InvalidOperationException;error.type=System.InvalidOperationException;error.type=System.InvalidOperationException;http.request.method=GET;http.response.status_code=101;http.route=/;network.protocol.version=1.1;url.scheme=http;Percentile=50]
You will see one duplication per exception middleware. E.g. when running in Development mode the automatically added developer exception middleware will introduce another duplication.
Exceptions (if any)
As expected (and correct), triggering the exception causes one or more exceptions related to unhandled exceptions:
.NET Version
8.0.200 or 9.0.100-preview.3.24204.13
Anything else?
I reproduced this in dotnet 8.0.200 and the current preview 9.0.100-preview.3.24204.13.
At least one part in this duplication seems to be the unexpected presence of the exception in
HostingMetrics.RequestEnd
https://github.com/dotnet/aspnetcore/blob/8486d31e24f30e3fa1809a95699a0adc16f448d7/src/Hosting/Hosting/src/Internal/HostingMetrics.cs#L72C1-L77C14
The comment there states, that if
ExceptionHandlerMiddleware
orDeveloperExceptionMiddleware
is used, theexception
variable should benull
. However this is not the case with the reproducer above.Other instances of the tag are introduced in the
ExceptionHandlerMiddlewareImpl
inaspnetcore/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs
Line 130 in 8486d31
and
DeveloperExceptionPageMiddlewareImpl
inaspnetcore/src/Middleware/Diagnostics/src/DeveloperExceptionPage/DeveloperExceptionPageMiddlewareImpl.cs
Line 128 in b83e1e1
Which call
DiagnosticsTelemetry.ReportUnhandledException
which adds theerror.type
tag into theIHttpMetricsTagsFeature
Tags
list. This is later given toHostingMetrics.RequestEnd
ascustomTag
entries.This way a single record can have three identical
error.type
entries. One for each middleware and one by theRequestEnd
function.The text was updated successfully, but these errors were encountered: