Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* The value of the `http.route` attribute is now aligned with ASP.NET Core itself.
([#3992](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3992))

## 1.15.1

Released 2026-Mar-11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,40 @@

#if NET

using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Http.Metadata;

namespace OpenTelemetry.Instrumentation.AspNetCore;

// Adapted from the following ASP.NET Core code:
// - https://github.com/dotnet/aspnetcore/blob/1855c53140e5254d54078ccea41e2b21bd264309/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L533-L536
// - https://github.com/dotnet/aspnetcore/blob/1855c53140e5254d54078ccea41e2b21bd264309/src/Shared/HttpExtensions.cs#L36-L47
// - https://github.com/dotnet/aspnetcore/blob/1855c53140e5254d54078ccea41e2b21bd264309/src/Shared/Diagnostics/RouteDiagnosticsHelpers.cs#L8-L16.

internal static class HttpContextExtensions
{
public static string? GetHttpRoute(this HttpContext context)
{
var endpoint = context.Features.Get<IExceptionHandlerPathFeature>()?.Endpoint as RouteEndpoint ??
context.GetEndpoint() as RouteEndpoint;
var endpoint = GetOriginalEndpoint(context);
var route = endpoint?.Metadata.GetMetadata<IRouteDiagnosticsMetadata>()?.Route;
return route is not null ? ResolveHttpRoute(route) : null;
}

private static Endpoint? GetOriginalEndpoint(HttpContext context)
{
var endpoint = context.GetEndpoint();

// Some middleware re-execute the middleware pipeline with the HttpContext. Before they do this, they clear state from context, such as the previously matched endpoint.
// The original endpoint is stashed with a known key in HttpContext.Items. Use it as a fallback.
if (endpoint is null && context.Items.TryGetValue("__OriginalEndpoint", out var e) && e is Endpoint originalEndpoint)
{
endpoint = originalEndpoint;
Comment thread
martincostello marked this conversation as resolved.
}

return endpoint?.RoutePattern.RawText;
return endpoint;
}

private static string ResolveHttpRoute(string route)
=> string.IsNullOrEmpty(route) ? "/" : route;
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@

```json
{
"ActivityDisplayName": "GET",
"ActivityHttpRoute": "",
"ActivityDisplayName": "GET /",
"ActivityHttpRoute": "/",
"IdealHttpRoute": "/Index",
"RouteInfo": {
"HttpMethod": "GET",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@

```json
{
"ActivityDisplayName": "GET",
"ActivityHttpRoute": "",
"ActivityDisplayName": "GET /",
"ActivityHttpRoute": "/",
"IdealHttpRoute": "/Index",
"RouteInfo": {
"HttpMethod": "GET",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@

```json
{
"ActivityDisplayName": "GET",
"ActivityHttpRoute": "",
"ActivityDisplayName": "GET /",
"ActivityHttpRoute": "/",
"IdealHttpRoute": "/Index",
"RouteInfo": {
"HttpMethod": "GET",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@
"httpMethod": "GET",
"path": "/",
"expectedStatusCode": 200,
"currentHttpRoute": "",
"expectedHttpRoute": "/Index"
"currentHttpRoute": "/",
"expectedHttpRoute": "/Index",
"expectedMetricRoute": ""
},
{
"name": "Root path",
Expand All @@ -142,9 +143,8 @@
"httpMethod": "GET",
"path": "/",
"expectedStatusCode": 200,
"currentHttpRoute": "",
"expectedHttpRoute": "/Index",
"expectedMetricRoute": "/"
"currentHttpRoute": "/",
"expectedHttpRoute": "/Index"
},
{
"name": "Index page",
Expand Down
Loading