Skip to content

Commit

Permalink
Fixed Missing Endpoint
Browse files Browse the repository at this point in the history
When an exception if processed by the ExceptionHandler middleware the original endpoint is deleted
  • Loading branch information
cliedeman committed Dec 7, 2023
1 parent a963ec8 commit edaf416
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#endif
using Microsoft.AspNetCore.Http;
#if !NETSTANDARD
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Routing;
#endif
using OpenTelemetry.Context.Propagation;
Expand Down Expand Up @@ -243,7 +244,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<IExceptionHandlerPathFeature>()?.Endpoint as RouteEndpoint ??
context.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText;
if (!string.IsNullOrEmpty(routePattern))
{
activity.DisplayName = this.GetDisplayName(context.Request.Method, routePattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Routing;
#endif
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -121,7 +122,9 @@ public void OnStopEventWritten(string name, object payload)
tags.Add(new KeyValuePair<string, object>(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<IExceptionHandlerPathFeature>()?.Endpoint as RouteEndpoint ??
context.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText;
if (!string.IsNullOrEmpty(route))
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRoute, route));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,15 @@
"expectedStatusCode": 200,
"currentHttpRoute": null,
"expectedHttpRoute": "/MinimalApiUsingMapGroup/{id}"
},
{
"name": "Exception Handled by Exception Handler Middleware",
"minimumDotnetVersion": 7,
"testApplicationScenario": "ExceptionMiddleware",
"httpMethod": "GET",
"path": "/Exception",
"expectedStatusCode": 500,
"currentHttpRoute": null,
"expectedHttpRoute": "/Exception"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public enum TestApplicationScenario
/// An Razor Pages application.
/// </summary>
RazorPages,

/// <summary>
/// Application with Exception Handling Middleware.
/// </summary>
ExceptionMiddleware,
}

internal class TestApplicationFactory
Expand All @@ -66,6 +71,8 @@ internal class TestApplicationFactory
return CreateMinimalApiApplication();
case TestApplicationScenario.RazorPages:
return CreateRazorPagesApplication();
case TestApplicationScenario.ExceptionMiddleware:
return CreateExceptionHandlerApplication();
default:
throw new ArgumentException($"Invalid {nameof(TestApplicationScenario)}");
}
Expand Down Expand Up @@ -167,4 +174,26 @@ private static WebApplication CreateRazorPagesApplication()

return app;
}

private static WebApplication CreateExceptionHandlerApplication()
{
var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();

#if NET7_0_OR_GREATER
builder.Services.AddProblemDetails();
#endif

var app = builder.Build();

#if NET7_0_OR_GREATER
app.UseExceptionHandler();
#endif
app.Urls.Clear();
app.Urls.Add("http://[::1]:0");

app.MapGet("/Exception", (ctx) => throw new ApplicationException());

return app;
}
}

0 comments on commit edaf416

Please sign in to comment.