diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index b10c8a6c4c..3483043854 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 b3631d0ef2..06db3e5610 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 4d871d986a..fec6c19445 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 b2fc1cdfba..b277d60187 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");