Skip to content

Commit c70dbcb

Browse files
committed
HostingMetrics: empty "http.route" tags should be set to "/"
These changes have been decided in #62431
1 parent bbbfebe commit c70dbcb

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/Hosting/Hosting/src/Internal/HostingMetrics.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Diagnostics.Metrics;
8+
89
using Microsoft.AspNetCore.Http;
910

1011
namespace Microsoft.AspNetCore.Hosting;
@@ -66,10 +67,12 @@ public void RequestEnd(string protocol, string scheme, string method, string? ro
6667

6768
// Add information gathered during request.
6869
tags.Add("http.response.status_code", GetBoxedStatusCode(statusCode));
69-
if (!string.IsNullOrEmpty(route))
70+
var httpRoute = route;
71+
if (string.IsNullOrEmpty(httpRoute))
7072
{
71-
tags.Add("http.route", route);
73+
httpRoute = "/";
7274
}
75+
tags.Add("http.route", httpRoute);
7376

7477
// Add before some built in tags so custom tags are prioritized when dealing with duplicates.
7578
if (customTags != null)

src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics.Metrics;
66
using System.Diagnostics.Tracing;
77
using System.Reflection;
8+
89
using Microsoft.AspNetCore.Builder;
910
using Microsoft.AspNetCore.Http;
1011
using Microsoft.AspNetCore.Http.Features;
@@ -15,6 +16,7 @@
1516
using Microsoft.Extensions.Diagnostics.Metrics.Testing;
1617
using Microsoft.Extensions.Logging;
1718
using Microsoft.Extensions.Logging.Testing;
19+
1820
using Moq;
1921

2022
namespace Microsoft.AspNetCore.Hosting.Tests;
@@ -358,7 +360,7 @@ private sealed class EmptyRouteDiagnosticsMetadata : IRouteDiagnosticsMetadata
358360
}
359361

360362
[Fact]
361-
public void Metrics_Route_RouteTagMissingWhenEmpty()
363+
public void Metrics_Route_RouteTagIsRootWhenEmpty()
362364
{
363365
// Arrange
364366
var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString());
@@ -413,7 +415,7 @@ public void Metrics_Route_RouteTagMissingWhenEmpty()
413415
m =>
414416
{
415417
Assert.True(m.Value > 0);
416-
Assert.False(m.Tags.ContainsKey("http.route"));
418+
Assert.Equal("/", m.Tags["http.route"]);
417419
});
418420
}
419421

0 commit comments

Comments
 (0)