Skip to content

HostingMetrics: empty "http.route" tags shouldn't be present #62431

@Elanis

Description

@Elanis
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Microsoft.AspNetCore.Hosting create a metric called http.server.request.duration documented here: https://learn.microsoft.com/en-us/aspnet/core/log-mon/metrics/built-in

One the available labels is http.route which is optional.

Issue:

http.route can be populated with various string such as:

  • path/to/my/route/{id}
  • index
  • api/controller/route
  • <empty string>
  • ...

And it can also be missing.

However, based on the OpenMetrics spec used by Prometheus, empty label values should be treated as if the label was not present.
That means when exporting data to prometheus, we currently create two different metrics (one for http_route= <empty string>, one for the missing http_route), which create a "duplicate sample for timestamp" error when importing data.

Fix:

To fix this issue, I changed the metric labelling so an empty route doesn't create the label, as if the variable was null.

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0

Anything else?

No response

Activity

added
needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
on Jun 21, 2025
added and removed
needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
on Jun 21, 2025
MackinnonBuck

MackinnonBuck commented on Jun 23, 2025

@MackinnonBuck
Member

@JamesNK, should we not be adding the http.route attribute when it's empty?

JamesNK

JamesNK commented on Jun 23, 2025

@JamesNK
Member

Is it valid to have an empty string here? I think app.MapGet("", () => "Hello world") would match the site route.

Need to test. If empty string is a valid route in ASP.NET Core then maybe we turn empty string into http.route=/.

JamesNK

JamesNK commented on Jun 23, 2025

@JamesNK
Member

I confirmed that an empty string for MapGet does work and matches the root of the web app.

Also the OpenTelemetry spec says that attribute values with an empty string are meaningful: https://opentelemetry.io/docs/specs/otel/common/

Attribute values expressing a numerical value of zero, an empty string, or an empty array are considered meaningful and MUST be stored and passed on to processors / exporters.

Elanis

Elanis commented on Jun 24, 2025

@Elanis
ContributorAuthor

Hm, so OTEL and Prometheus don't agree on this...

I didn't find this before, thanks for digging!

This still poses an issue, but a more complex than I thought 😅

halter73

halter73 commented on Jun 26, 2025

@halter73
Member

Need to test. If empty string is a valid route in ASP.NET Core then maybe we turn empty string into http.route=/.

I'm pretty sure an empty route is valid and exactly equivalent to "/", so I agree this is what we should do. The safest thing is probably to only do this for metrics, but I think this could be normalized even during RouteEndpoint creation without much impact.

Elanis

Elanis commented on Jun 27, 2025

@Elanis
ContributorAuthor

I believe this a good way to solve this issue yes!

It looks like I get this empty HTTP route only on my razor-based projects, and not on webapi projects, so that's probably the root route indeed.

Elanis

Elanis commented on Jul 11, 2025

@Elanis
ContributorAuthor

Hi!
I'm up to update the PR ( #62432 ) when you agree on a solution, I guess it still needs some further discussions, but feel free to @ me once you agreed on a solution, I'll be happy to write it and make this bug disappear :)

sebastienros

sebastienros commented on Jul 29, 2025

@sebastienros
Member

@Elanis I believe @JamesNK and @halter73 agree with using / when the route is empty. Do you want to update your PR with that behavior, it would still be able to make it for the next release.

added this to the 10.0-rc1 milestone on Jul 29, 2025
Elanis

Elanis commented on Jul 29, 2025

@Elanis
ContributorAuthor

Done :)

self-assigned this
on Aug 19, 2025
modified the milestones: 10.0-rc1, 10.0-rc2 on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @halter73@JamesNK@sebastienros@gfoidl@danmoseley

    Issue actions

      HostingMetrics: empty "http.route" tags shouldn't be present · Issue #62431 · dotnet/aspnetcore