From c2fd145d541cbf3dce8138d9a7e9bdf3d43b5efb Mon Sep 17 00:00:00 2001 From: Ciaran Liedeman Date: Wed, 6 Dec 2023 13:07:46 +0200 Subject: [PATCH] Fixed Missing Endpoint When an exception if processed by the ExceptionHandler middleware the original endpoint is deleted --- .../Implementation/HttpInListener.cs | 5 ++++- .../Implementation/HttpInMetricsListener.cs | 6 +++++- .../RouteTests/RoutingTestCases.json | 10 ++++++++++ .../TestApplication/TestApplicationFactory.cs | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index b10c8a6c4ca..34830438543 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -35,6 +35,8 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation; +using Microsoft.AspNetCore.Diagnostics; + internal class HttpInListener : ListenerHandler { internal const string ActivityOperationName = "Microsoft.AspNetCore.Hosting.HttpRequestIn"; @@ -243,7 +245,8 @@ public void OnStopActivity(Activity activity, object payload) var response = context.Response; #if !NETSTANDARD - var routePattern = (context.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText; + var routePattern = (context.Features.Get().Endpoint as RouteEndpoint)?.RoutePattern.RawText ?? + (context.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText; if (!string.IsNullOrEmpty(routePattern)) { activity.DisplayName = this.GetDisplayName(context.Request.Method, routePattern); diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs index b3631d0ef21..06db3e56103 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs @@ -28,6 +28,8 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation; +using Microsoft.AspNetCore.Diagnostics; + internal sealed class HttpInMetricsListener : ListenerHandler { internal const string HttpServerRequestDurationMetricName = "http.server.request.duration"; @@ -121,7 +123,9 @@ public void OnStopEventWritten(string name, object payload) tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpRequestMethod, httpMethod)); #if NET6_0_OR_GREATER - var route = (context.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText; + // Check the exception handler feature first in case the endpoint was overwritten + var route = (context.Features.Get().Endpoint as RouteEndpoint)?.RoutePattern.RawText ?? + (context.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText; if (!string.IsNullOrEmpty(route)) { tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpRoute, route)); diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/RoutingTestCases.json b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/RoutingTestCases.json index 4d871d986a1..fec6c194459 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/RoutingTestCases.json +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/RoutingTestCases.json @@ -198,5 +198,15 @@ "expectedStatusCode": 200, "currentHttpRoute": null, "expectedHttpRoute": "/MinimalApiUsingMapGroup/{id}" + }, + { + "name": "Exception Handled by Exception HAndler", + "minimumDotnetVersion": 7, + "testApplicationScenario": "MinimalApi", + "httpMethod": "GET", + "path": "/MinimalApiException", + "expectedStatusCode": 200, + "currentHttpRoute": null, + "expectedHttpRoute": "/MinimalApiException" } ] diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/TestApplication/TestApplicationFactory.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/TestApplication/TestApplicationFactory.cs index b2fc1cdfba3..b277d60187d 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/TestApplication/TestApplicationFactory.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/TestApplication/TestApplicationFactory.cs @@ -130,11 +130,13 @@ private static WebApplication CreateMinimalApiApplication() builder.Logging.ClearProviders(); var app = builder.Build(); + app.UseExceptionHandler(); app.Urls.Clear(); app.Urls.Add("http://[::1]:0"); app.MapGet("/MinimalApi", () => Results.Ok()); app.MapGet("/MinimalApi/{id}", (int id) => Results.Ok()); + app.MapGet("/MinimalApiException", (ctx) => throw new ApplicationException()); #if NET7_0_OR_GREATER var api = app.MapGroup("/MinimalApiUsingMapGroup");