|
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;
|
@@ -228,7 +230,6 @@ public void Metrics_RequestChanges_OriginalValuesUsed()
|
228 | 230 |
|
229 | 231 | var testMeterFactory = new TestMeterFactory();
|
230 | 232 | using var activeRequestsCollector = new MetricCollector<long>(testMeterFactory, HostingMetrics.MeterName, "http.server.active_requests");
|
231 |
| - using var requestDurationCollector = new MetricCollector<double>(testMeterFactory, HostingMetrics.MeterName, "http.server.request.duration"); |
232 | 233 |
|
233 | 234 | // Act
|
234 | 235 | var hostingApplication = CreateApplication(out var features, eventSource: hostingEventSource, meterFactory: testMeterFactory, configure: c =>
|
@@ -279,6 +280,182 @@ public void Metrics_RequestChanges_OriginalValuesUsed()
|
279 | 280 | Assert.Null(context.MetricsTagsFeature.Protocol);
|
280 | 281 | }
|
281 | 282 |
|
| 283 | + [Fact] |
| 284 | + public void Metrics_Route_RouteTagReported() |
| 285 | + { |
| 286 | + // Arrange |
| 287 | + var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString()); |
| 288 | + |
| 289 | + var testMeterFactory = new TestMeterFactory(); |
| 290 | + using var activeRequestsCollector = new MetricCollector<long>(testMeterFactory, HostingMetrics.MeterName, "http.server.active_requests"); |
| 291 | + using var requestDurationCollector = new MetricCollector<double>(testMeterFactory, HostingMetrics.MeterName, "http.server.request.duration"); |
| 292 | + |
| 293 | + // Act |
| 294 | + var hostingApplication = CreateApplication(out var features, eventSource: hostingEventSource, meterFactory: testMeterFactory, configure: c => |
| 295 | + { |
| 296 | + c.Request.Protocol = "1.1"; |
| 297 | + c.Request.Scheme = "http"; |
| 298 | + c.Request.Method = "POST"; |
| 299 | + c.Request.Host = new HostString("localhost"); |
| 300 | + c.Request.Path = "/hello"; |
| 301 | + c.Request.ContentType = "text/plain"; |
| 302 | + c.Request.ContentLength = 1024; |
| 303 | + }); |
| 304 | + var context = hostingApplication.CreateContext(features); |
| 305 | + |
| 306 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 307 | + m => |
| 308 | + { |
| 309 | + Assert.Equal(1, m.Value); |
| 310 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 311 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 312 | + }); |
| 313 | + |
| 314 | + context.HttpContext.SetEndpoint(new Endpoint( |
| 315 | + c => Task.CompletedTask, |
| 316 | + new EndpointMetadataCollection(new TestRouteDiagnosticsMetadata()), |
| 317 | + "Test endpoint")); |
| 318 | + |
| 319 | + hostingApplication.DisposeContext(context, null); |
| 320 | + |
| 321 | + // Assert |
| 322 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 323 | + m => |
| 324 | + { |
| 325 | + Assert.Equal(1, m.Value); |
| 326 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 327 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 328 | + }, |
| 329 | + m => |
| 330 | + { |
| 331 | + Assert.Equal(-1, m.Value); |
| 332 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 333 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 334 | + }); |
| 335 | + Assert.Collection(requestDurationCollector.GetMeasurementSnapshot(), |
| 336 | + m => |
| 337 | + { |
| 338 | + Assert.True(m.Value > 0); |
| 339 | + Assert.Equal("hello/{name}", m.Tags["http.route"]); |
| 340 | + }); |
| 341 | + } |
| 342 | + |
| 343 | + [Fact] |
| 344 | + public void Metrics_DisableHttpMetricsWithMetadata_NoMetrics() |
| 345 | + { |
| 346 | + // Arrange |
| 347 | + var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString()); |
| 348 | + |
| 349 | + var testMeterFactory = new TestMeterFactory(); |
| 350 | + using var activeRequestsCollector = new MetricCollector<long>(testMeterFactory, HostingMetrics.MeterName, "http.server.active_requests"); |
| 351 | + using var requestDurationCollector = new MetricCollector<double>(testMeterFactory, HostingMetrics.MeterName, "http.server.request.duration"); |
| 352 | + |
| 353 | + // Act |
| 354 | + var hostingApplication = CreateApplication(out var features, eventSource: hostingEventSource, meterFactory: testMeterFactory, configure: c => |
| 355 | + { |
| 356 | + c.Request.Protocol = "1.1"; |
| 357 | + c.Request.Scheme = "http"; |
| 358 | + c.Request.Method = "POST"; |
| 359 | + c.Request.Host = new HostString("localhost"); |
| 360 | + c.Request.Path = "/hello"; |
| 361 | + c.Request.ContentType = "text/plain"; |
| 362 | + c.Request.ContentLength = 1024; |
| 363 | + }); |
| 364 | + var context = hostingApplication.CreateContext(features); |
| 365 | + |
| 366 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 367 | + m => |
| 368 | + { |
| 369 | + Assert.Equal(1, m.Value); |
| 370 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 371 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 372 | + }); |
| 373 | + |
| 374 | + context.HttpContext.SetEndpoint(new Endpoint( |
| 375 | + c => Task.CompletedTask, |
| 376 | + new EndpointMetadataCollection(new TestRouteDiagnosticsMetadata(), new DisableHttpMetricsAttribute()), |
| 377 | + "Test endpoint")); |
| 378 | + |
| 379 | + hostingApplication.DisposeContext(context, null); |
| 380 | + |
| 381 | + // Assert |
| 382 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 383 | + m => |
| 384 | + { |
| 385 | + Assert.Equal(1, m.Value); |
| 386 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 387 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 388 | + }, |
| 389 | + m => |
| 390 | + { |
| 391 | + Assert.Equal(-1, m.Value); |
| 392 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 393 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 394 | + }); |
| 395 | + Assert.Empty(requestDurationCollector.GetMeasurementSnapshot()); |
| 396 | + } |
| 397 | + |
| 398 | + [Fact] |
| 399 | + public void Metrics_DisableHttpMetricsWithFeature_NoMetrics() |
| 400 | + { |
| 401 | + // Arrange |
| 402 | + var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString()); |
| 403 | + |
| 404 | + var testMeterFactory = new TestMeterFactory(); |
| 405 | + using var activeRequestsCollector = new MetricCollector<long>(testMeterFactory, HostingMetrics.MeterName, "http.server.active_requests"); |
| 406 | + using var requestDurationCollector = new MetricCollector<double>(testMeterFactory, HostingMetrics.MeterName, "http.server.request.duration"); |
| 407 | + |
| 408 | + // Act |
| 409 | + var hostingApplication = CreateApplication(out var features, eventSource: hostingEventSource, meterFactory: testMeterFactory, configure: c => |
| 410 | + { |
| 411 | + c.Request.Protocol = "1.1"; |
| 412 | + c.Request.Scheme = "http"; |
| 413 | + c.Request.Method = "POST"; |
| 414 | + c.Request.Host = new HostString("localhost"); |
| 415 | + c.Request.Path = "/hello"; |
| 416 | + c.Request.ContentType = "text/plain"; |
| 417 | + c.Request.ContentLength = 1024; |
| 418 | + }); |
| 419 | + var context = hostingApplication.CreateContext(features); |
| 420 | + |
| 421 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 422 | + m => |
| 423 | + { |
| 424 | + Assert.Equal(1, m.Value); |
| 425 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 426 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 427 | + }); |
| 428 | + |
| 429 | + context.HttpContext.Features.Get<IHttpMetricsTagsFeature>().MetricsDisabled = true; |
| 430 | + |
| 431 | + // Assert 1 |
| 432 | + Assert.True(context.MetricsTagsFeature.MetricsDisabled); |
| 433 | + |
| 434 | + hostingApplication.DisposeContext(context, null); |
| 435 | + |
| 436 | + // Assert 2 |
| 437 | + Assert.Collection(activeRequestsCollector.GetMeasurementSnapshot(), |
| 438 | + m => |
| 439 | + { |
| 440 | + Assert.Equal(1, m.Value); |
| 441 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 442 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 443 | + }, |
| 444 | + m => |
| 445 | + { |
| 446 | + Assert.Equal(-1, m.Value); |
| 447 | + Assert.Equal("http", m.Tags["url.scheme"]); |
| 448 | + Assert.Equal("POST", m.Tags["http.request.method"]); |
| 449 | + }); |
| 450 | + Assert.Empty(requestDurationCollector.GetMeasurementSnapshot()); |
| 451 | + Assert.False(context.MetricsTagsFeature.MetricsDisabled); |
| 452 | + } |
| 453 | + |
| 454 | + private sealed class TestRouteDiagnosticsMetadata : IRouteDiagnosticsMetadata |
| 455 | + { |
| 456 | + public string Route { get; } = "hello/{name}"; |
| 457 | + } |
| 458 | + |
282 | 459 | [Fact]
|
283 | 460 | public void DisposeContextDoesNotThrowWhenContextScopeIsNull()
|
284 | 461 | {
|
|
0 commit comments