Skip to content

Missing route pattern in requests that do not reach the MVC middleware #3461

@aelij

Description

@aelij

Bug Report

OpenTelemetry NuGet packages:

  • OpenTelemetry.Exporter.Console 1.3.0
  • OpenTelemetry.Extensions.Hosting 1.0.0-rc9.4
  • OpenTelemetry.Instrumentation.AspNetCore 1.0.0-rc9.4

Runtime version:

  • net6.0

Symptom

When MVC requests get stopped by previous middlewares (e.g. authentication), the Microsoft.AspNetCore.Mvc.BeforeAction event isn't sent and so the Activity isn't properly renamed with the route pattern and the http.route tag isn't added.

This is a problem since we can't easily identify requests that belong to the same operation due to various failures in middlewares.
In addition, requests that do not use MVC don't have the route set either.

What is the expected behavior?

The activity name and http.route tag should be equal to the route pattern.

Suggestion for a solution - Instead of relying on Mvc.BeforeAction use Microsoft.AspNetCore.Routing.EndpointMatched, which would also work outside of MVC. Then get the route pattern using:

(httpContext.GetEndpoint() as RouteEndpoint)?.RoutePattern?.RawText

What is the actual behavior?

The activity name equals the full URI path and http.route is empty.

Reproduce

otel-missing-route.zip

public class MyController : Controller
{
    [HttpGet("hasroute/{value}")]
    public int HasRoute(int value) => value;

    [HttpGet("noroute/{value}"), Authorize]
    public int NoRoute(int value) => value;
}

Calling those 2 endpoints (while unauthenticated) produces:

Activity.DisplayName: hasroute/{value}
http.route: hasroute/{value}

and

Activity.DisplayName: /noroute/1
Details
Activity.TraceId:          0ddb494d4312a1033e8650c55b83b894
Activity.SpanId:           5f78fc1e69504bc6
Activity.TraceFlags:           Recorded
Activity.ActivitySourceName: OpenTelemetry.Instrumentation.AspNetCore
Activity.DisplayName: hasroute/{value}
Activity.Kind:        Server
Activity.StartTime:   2022-07-20T14:58:10.5872211Z
Activity.Duration:    00:00:00.1429528
Activity.Tags:
    http.host: localhost:7196
    http.method: GET
    http.target: /hasroute/1
    http.url: https://localhost:7196/hasroute/1
    http.route: hasroute/{value}
    http.status_code: 200
   StatusCode : UNSET
Resource associated with Activity:
    service.name: unknown_service:WebApplication4

Activity.TraceId:          3d8431517010c153dcb6f681bf50a0a4
Activity.SpanId:           7a30556bc3aa45c3
Activity.TraceFlags:           Recorded
Activity.ActivitySourceName: OpenTelemetry.Instrumentation.AspNetCore
Activity.DisplayName: /noroute/1
Activity.Kind:        Server
Activity.StartTime:   2022-07-20T14:58:15.9094598Z
Activity.Duration:    00:00:00.0347058
Activity.Tags:
    http.host: localhost:7196
    http.method: GET
    http.target: /noroute/1
    http.url: https://localhost:7196/noroute/1
    http.status_code: 401
   StatusCode : UNSET
Resource associated with Activity:
    service.name: unknown_service:WebApplication4

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions