Skip to content

Add Meter/Counters to track hits for each route/endpoint #46361

@Tratcher

Description

@Tratcher

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Customers have asked to know how much traffic is going to specific routes/endpoints, especially routes they've added to catch misdirected traffic (e.g. favicon.ico when the site has none). These are used to guide investigations and development efforts.

Describe the solution you'd like

We can use System.Diagnostics.Metrics.Meter to make counters per route or endpoint. and then monitor those using dotnet-counters.

Here's a simple example I tried in the EndpointRoutingMiddleware.

    private readonly Meter _meter = new Meter("Microsoft.AspNetCore.Routing");
    private readonly Counter<int> _routeMatchCounter;
...
        _routeMatchCounter = _meter.CreateCounter<int>("matched");
...
            _routeMatchCounter.Add(1, new KeyValuePair<string, object?>("endpoint", endpoint.DisplayName));

Then
dotnet-counters monitor --name MinimalSample Microsoft.AspNetCore.Routing
Produces:

Press p to pause, r to resume, q to quit.
    Status: Running

[Microsoft.AspNetCore.Routing]
    matched (Count / 1 sec)
        endpoint=405 HTTP Method Not Supported                             0
        endpoint=HTTP: GET /                                               0
        endpoint=HTTP: GET /hello/{name} => SayHello                       0
        endpoint=HTTP: GET /json => Json                                   0
        endpoint=HTTP: GET /null-result                                    0
        endpoint=HTTP: GET /plaintext => Plaintext                         0
        endpoint=HTTP: GET /problem/{problemType}                          0
        endpoint=HTTP: GET /todo/{id}                                      0

Which kind of counter?

  • I'm told ObservableCounter is more performant than Counter, but it requires more of your own bookkeeping.

What kind of data?

  • Not the full path or query, it may contain sensitive data or vary too much by parameters
  • Maybe not even individual endpoints? That might be too granular.
  • How about one per route pattern (including constraints?)? That avoids things like 405 HTTP Method Not Supported. Is it granular enough? Probably, people can be more specific with their route definitions if they want more granular counters.

Where does the counter go?

  • EndpointRoutingMiddleware can see endpoints, but lacks visibility into routes.
  • Where has access to the path and constraints? In the DFA builder/matcher somewhere?

Additional context

No response

Metadata

Metadata

Assignees

Labels

area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions