Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP.NET Core performance middleware doesn't handle Attribute routing #761

Closed
kanadaj opened this issue Jan 23, 2021 · 3 comments · Fixed by #766
Closed

ASP.NET Core performance middleware doesn't handle Attribute routing #761

kanadaj opened this issue Jan 23, 2021 · 3 comments · Fixed by #766

Comments

@kanadaj
Copy link
Contributor

kanadaj commented Jan 23, 2021

With the current middleware implementation, attribute routing (eg [Route("/route/{param}")] ) is still handled as paths. There are 2 possible ways to work around this:

  1. On platforms where available, use httpContext.GetRouteData() (probably .NET Core 3 and newer), which extracts httpContext.Request.RouteValues if it's available.
  2. If the above is insufficient, move transaction name retrieval to after the middleware pipeline:
var transaction = hub.CreateTransaction(
    context.Request.Path, // Placeholder
    "http.server"
);

try
{
    await _next(context).ConfigureAwait(false);
}
finally
{
    transaction.Name = context.GetTransactionName();
    transaction.Finish(
        GetSpanStatusFromCode(context.Response.StatusCode)
    );
}

which will allow any logic that modifies routing to be applied before the transaction is sent.

@kanadaj
Copy link
Contributor Author

kanadaj commented Jan 23, 2021

cc @Tyrrrz

@Tyrrrz
Copy link
Contributor

Tyrrrz commented Jan 25, 2021

Afaik httpContext.GetRouteData() returns httpContext.Features.Get<IRoutingFeature?>()?.RouteData under the hood.

public static string? TryGetRouteTemplate(this HttpContext context)
{
#if !NETSTANDARD2_0
// Requires .UseRouting()/.UseEndpoints()
var endpoint = context.Features.Get<IEndpointFeature?>()?.Endpoint as RouteEndpoint;
var routePattern = endpoint?.RoutePattern.RawText;
if (!string.IsNullOrWhiteSpace(routePattern))
{
return routePattern;
}
#endif
// Requires legacy .UseMvc()
var routeData = context.Features.Get<IRoutingFeature?>()?.RouteData;
var controller = routeData?.Values["controller"]?.ToString();
var action = routeData?.Values["action"]?.ToString();
var area = routeData?.Values["area"]?.ToString();
if (!string.IsNullOrWhiteSpace(action))
{
return !string.IsNullOrWhiteSpace(area)
? $"{area}.{controller}.{action}"
: $"{controller}.{action}";
}
// If the handler doesn't use routing (i.e. it checks `context.Request.Path` directly),
// then there is no way for us to extract anything that resembles a route template.
return null;
}
public static string GetTransactionName(this HttpContext context)
{
// Try to get the route template or fallback to the request path
var method = context.Request.Method.ToUpperInvariant();
var route = context.TryGetRouteTemplate() ?? context.Request.Path;
return $"{method} {route}";
}

I'm guessing the logic short-circuits when it finds the route name on the endpoint feature and doesn't get to the routing feature. I wonder if switching them around would fix the issue.

@kanadaj
Copy link
Contributor Author

kanadaj commented Jan 27, 2021

Update on this one, this seems to only happen when a route 404s.

Also, GetRouteData() actually does more:

https://github.com/dotnet/aspnetcore/blob/c925f99cddac0df90ed0bc4a07ecda6b054a0b02/src/Http/Routing.Abstractions/src/RoutingHttpContextExtensions.cs#L30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants