@@ -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_RouteTagIsRootWhenEmpty ( )
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 . Equal ( "/" , m . Tags [ "http.route" ] ) ;
417+ } ) ;
418+ }
419+
355420 [ Fact ]
356421 public void Metrics_DisableHttpMetricsWithMetadata_NoMetrics ( )
357422 {
0 commit comments