Skip to content

Add IRouteDiagnosticsMetadata #47492

Closed
Closed
@JamesNK

Description

@JamesNK

Background and Motivation

The Open Telemetry HTTP server semantic conventions records required and optional data to include with metrics measurements. A recommended tag is the route match by the HTTP request. For example, an HTTP request to app.MapGet("product/{id}", () => ...) would have the route product/{id}.

The route is available in ASP.NET Core on the matched endpoint in the RouteEndpoint.RoutePattern. However, HTTP metrics are run in Microsoft.AspNetCore.Hosting. That code doesn't have access to RouteEndpoint, which is in Microsoft.AspNetCore.Routing, because of layering.

Two possible solutions:

  • Move RouteEndpoint, RoutePattern, and all its dependencies to Microsoft.AspNetCore.Http.Abstractions and type forward. Then hosting can check if the matched endpoint is a RouteEndpoint and access the route. However, the route pattern is quite a lot of code to move.
  • Or, add new diagnostics metadata to Microsoft.AspNetCore.Http.Abstractions. The metadata has a property with a string version of the pattern. Hosting diagnostics can then get the metadata from the endpoint, and use whatever text value is present.

Proposed API

namespace Microsoft.AspNetCore.Http.Metadata;

+ public interface IRouteDiagnosticsMetadata
+ {
+     string Route { get; }
+ }

Usage Examples

if (context.MetricsEnabled)
{
    var route = httpContext.GetEndpoint()?.Metadata.GetMetadata<IRouteDiagnosticsMetadata>()?.Route;

    _metrics.RequestEnd(
        httpContext.Request.Protocol,
        httpContext.Request.Scheme,
        httpContext.Request.Method,
        httpContext.Request.Host,
        route,
        httpContext.Response.StatusCode,
        startTimestamp,
        currentTimestamp);
}

Alternative Designs

See description.

Risks

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions