|
5 | 5 | using System.Diagnostics.Metrics;
|
6 | 6 | using System.Diagnostics.Tracing;
|
7 | 7 | using System.Reflection;
|
| 8 | +using Microsoft.AspNetCore.Builder; |
8 | 9 | using Microsoft.AspNetCore.Http;
|
9 | 10 | using Microsoft.AspNetCore.Http.Features;
|
| 11 | +using Microsoft.AspNetCore.Http.Metadata; |
10 | 12 | using Microsoft.AspNetCore.Internal;
|
11 | 13 | using Microsoft.AspNetCore.InternalTesting;
|
12 | 14 | using Microsoft.Extensions.Diagnostics.Metrics;
|
@@ -182,7 +184,6 @@ public void Metrics_RequestChanges_OriginalValuesUsed()
|
182 | 184 |
|
183 | 185 | var testMeterFactory = new TestMeterFactory();
|
184 | 186 | using var activeRequestsCollector = new MetricCollector<long>(testMeterFactory, HostingMetrics.MeterName, "http.server.active_requests");
|
185 |
| - using var requestDurationCollector = new MetricCollector<double>(testMeterFactory, HostingMetrics.MeterName, "http.server.request.duration"); |
186 | 187 |
|
187 | 188 | // Act
|
188 | 189 | var hostingApplication = CreateApplication(out var features, eventSource: hostingEventSource, meterFactory: testMeterFactory, configure: c =>
|
@@ -233,6 +234,126 @@ public void Metrics_RequestChanges_OriginalValuesUsed()
|
233 | 234 | Assert.Null(context.MetricsTagsFeature.Protocol);
|
234 | 235 | }
|
235 | 236 |
|
| 237 | + [Fact] |
| 238 | + public void Metrics_Route_RouteTagReported() |
| 239 | + { |
| 240 | + // Arrange |
| 241 | + var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString()); |
| 242 | + |
| 243 | + var testMeterFactory = new TestMeterFactory(); |
| 244 | + using var activeRequestsCollector = new MetricCollector<long>(testMeterFactory, HostingMetrics.MeterName, "http.server.active_requests"); |
| 245 | + using var requestDurationCollector = new MetricCollector<double>(testMeterFactory, HostingMetrics.MeterName, "http.server.request.duration"); |
| 246 | + |
| 247 | + // Act |
| 248 | + var hostingApplication = CreateApplication(out var features, eventSource: hostingEventSource, meterFactory: testMeterFactory, configure: c => |
| 249 | + { |
| 250 | + c.Request.Protocol = "1.1"; |
| 251 | + c.Request.Scheme = "http"; |
| 252 | + c.Request.Method = "POST"; |
| 253 | + c.Request.Host = new HostString("localhost"); |
| 254 | + c.Request.Path = "/hello"; |
| 255 | + c.Request.ContentType = "text/plain"; |
| 256 | + c.Request.ContentLength = 1024; |
| 257 | + }); |
| 258 | + var context = hostingApplication.CreateContext(features); |
| 259 | + |
| 260 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 261 | + m => |
| 262 | + { |
| 263 | + Assert.Equal(1, m.Value); |
| 264 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 265 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 266 | + }); |
| 267 | + |
| 268 | + context.HttpContext.SetEndpoint(new Endpoint( |
| 269 | + c => Task.CompletedTask, |
| 270 | + new EndpointMetadataCollection(new TestRouteDiagnosticsMetadata()), |
| 271 | + "Test endpoint")); |
| 272 | + |
| 273 | + hostingApplication.DisposeContext(context, null); |
| 274 | + |
| 275 | + // Assert |
| 276 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 277 | + m => |
| 278 | + { |
| 279 | + Assert.Equal(1, m.Value); |
| 280 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 281 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 282 | + }, |
| 283 | + m => |
| 284 | + { |
| 285 | + Assert.Equal(-1, m.Value); |
| 286 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 287 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 288 | + }); |
| 289 | + Assert.Collection(requestDurationCollector.GetMeasurementSnapshot(), |
| 290 | + m => |
| 291 | + { |
| 292 | + Assert.True(m.Value > 0); |
| 293 | + Assert.Equal("hello/{name}", m.Tags["http.route"]); |
| 294 | + }); |
| 295 | + } |
| 296 | + |
| 297 | + [Fact] |
| 298 | + public void Metrics_DisableHttpMetrics_NoMetrics() |
| 299 | + { |
| 300 | + // Arrange |
| 301 | + var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString()); |
| 302 | + |
| 303 | + var testMeterFactory = new TestMeterFactory(); |
| 304 | + using var activeRequestsCollector = new MetricCollector<long>(testMeterFactory, HostingMetrics.MeterName, "http.server.active_requests"); |
| 305 | + using var requestDurationCollector = new MetricCollector<double>(testMeterFactory, HostingMetrics.MeterName, "http.server.request.duration"); |
| 306 | + |
| 307 | + // Act |
| 308 | + var hostingApplication = CreateApplication(out var features, eventSource: hostingEventSource, meterFactory: testMeterFactory, configure: c => |
| 309 | + { |
| 310 | + c.Request.Protocol = "1.1"; |
| 311 | + c.Request.Scheme = "http"; |
| 312 | + c.Request.Method = "POST"; |
| 313 | + c.Request.Host = new HostString("localhost"); |
| 314 | + c.Request.Path = "/hello"; |
| 315 | + c.Request.ContentType = "text/plain"; |
| 316 | + c.Request.ContentLength = 1024; |
| 317 | + }); |
| 318 | + var context = hostingApplication.CreateContext(features); |
| 319 | + |
| 320 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 321 | + m => |
| 322 | + { |
| 323 | + Assert.Equal(1, m.Value); |
| 324 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 325 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 326 | + }); |
| 327 | + |
| 328 | + context.HttpContext.SetEndpoint(new Endpoint( |
| 329 | + c => Task.CompletedTask, |
| 330 | + new EndpointMetadataCollection(new TestRouteDiagnosticsMetadata(), new DisableHttpMetricsAttribute()), |
| 331 | + "Test endpoint")); |
| 332 | + |
| 333 | + hostingApplication.DisposeContext(context, null); |
| 334 | + |
| 335 | + // Assert |
| 336 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 337 | + m => |
| 338 | + { |
| 339 | + Assert.Equal(1, m.Value); |
| 340 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 341 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 342 | + }, |
| 343 | + m => |
| 344 | + { |
| 345 | + Assert.Equal(-1, m.Value); |
| 346 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 347 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 348 | + }); |
| 349 | + Assert.Empty(requestDurationCollector.GetMeasurementSnapshot()); |
| 350 | + } |
| 351 | + |
| 352 | + private sealed class TestRouteDiagnosticsMetadata : IRouteDiagnosticsMetadata |
| 353 | + { |
| 354 | + public string Route { get; } = "hello/{name}"; |
| 355 | + } |
| 356 | + |
236 | 357 | [Fact]
|
237 | 358 | public void DisposeContextDoesNotThrowWhenContextScopeIsNull()
|
238 | 359 | {
|
|
0 commit comments