Skip to content

Commit e9b92c4

Browse files
committed
HostingMetrics: empty "http.route" tags shouldn't be present
1 parent 61984fd commit e9b92c4

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void RequestEnd(string protocol, string scheme, string method, string? ro
6666

6767
// Add information gathered during request.
6868
tags.Add("http.response.status_code", GetBoxedStatusCode(statusCode));
69-
if (route != null)
69+
if (!string.IsNullOrEmpty(route))
7070
{
7171
tags.Add("http.route", route);
7272
}

src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,71 @@ public void Metrics_Route_RouteTagReported()
352352
});
353353
}
354354

355+
private sealed class EmptyRouteDiagnosticsMetadata : IRouteDiagnosticsMetadata
356+
{
357+
public string Route { get; } = "";
358+
}
359+
360+
[Fact]
361+
public void Metrics_Route_RouteTagMissingWhenEmpty()
362+
{
363+
// Arrange
364+
var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString());
365+
366+
var testMeterFactory = new TestMeterFactory();
367+
using var activeRequestsCollector = new MetricCollector<long>(testMeterFactory, HostingMetrics.MeterName, "http.server.active_requests");
368+
using var requestDurationCollector = new MetricCollector<double>(testMeterFactory, HostingMetrics.MeterName, "http.server.request.duration");
369+
370+
// Act
371+
var hostingApplication = CreateApplication(out var features, eventSource: hostingEventSource, meterFactory: testMeterFactory, configure: c =>
372+
{
373+
c.Request.Protocol = "1.1";
374+
c.Request.Scheme = "http";
375+
c.Request.Method = "POST";
376+
c.Request.Host = new HostString("localhost");
377+
c.Request.Path = "";
378+
c.Request.ContentType = "text/plain";
379+
c.Request.ContentLength = 1024;
380+
});
381+
var context = hostingApplication.CreateContext(features);
382+
383+
Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(),
384+
m =>
385+
{
386+
Assert.Equal(1, m.Value);
387+
Assert.Equal("http", m.Tags["url.scheme"]);
388+
Assert.Equal("POST", m.Tags["http.request.method"]);
389+
});
390+
391+
context.HttpContext.SetEndpoint(new Endpoint(
392+
c => Task.CompletedTask,
393+
new EndpointMetadataCollection(new EmptyRouteDiagnosticsMetadata()),
394+
"Test empty endpoint"));
395+
396+
hostingApplication.DisposeContext(context, null);
397+
398+
// Assert
399+
Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(),
400+
m =>
401+
{
402+
Assert.Equal(1, m.Value);
403+
Assert.Equal("http", m.Tags["url.scheme"]);
404+
Assert.Equal("POST", m.Tags["http.request.method"]);
405+
},
406+
m =>
407+
{
408+
Assert.Equal(-1, m.Value);
409+
Assert.Equal("http", m.Tags["url.scheme"]);
410+
Assert.Equal("POST", m.Tags["http.request.method"]);
411+
});
412+
Assert.Collection(requestDurationCollector.GetMeasurementSnapshot(),
413+
m =>
414+
{
415+
Assert.True(m.Value > 0);
416+
Assert.False(m.Tags.ContainsKey("http.route"));
417+
});
418+
}
419+
355420
[Fact]
356421
public void Metrics_DisableHttpMetricsWithMetadata_NoMetrics()
357422
{

0 commit comments

Comments
 (0)