|
3 | 3 |
|
4 | 4 | using System.Diagnostics;
|
5 | 5 | using System.Diagnostics.Metrics;
|
| 6 | +using System.Net; |
6 | 7 | using System.Net.Http;
|
7 | 8 | using System.Net.Http.Headers;
|
8 | 9 | using System.Net.Http.Json;
|
|
12 | 13 | using Microsoft.AspNetCore.Http;
|
13 | 14 | using Microsoft.AspNetCore.Http.Features;
|
14 | 15 | using Microsoft.AspNetCore.Mvc;
|
| 16 | +using Microsoft.AspNetCore.Routing; |
| 17 | +using Microsoft.AspNetCore.Routing.Patterns; |
15 | 18 | using Microsoft.AspNetCore.TestHost;
|
16 | 19 | using Microsoft.AspNetCore.Testing;
|
17 | 20 | using Microsoft.Extensions.DependencyInjection;
|
18 |
| -using Microsoft.Extensions.Diagnostics.Metrics; |
| 21 | +using Microsoft.Extensions.Diagnostics.Metrics.Testing; |
19 | 22 | using Microsoft.Extensions.Hosting;
|
| 23 | +using Microsoft.Extensions.Logging; |
20 | 24 | using Microsoft.Extensions.Logging.Abstractions;
|
21 | 25 | using Microsoft.Extensions.Options;
|
22 |
| -using Microsoft.Extensions.Diagnostics.Metrics.Testing; |
23 | 26 | using Moq;
|
24 | 27 |
|
25 | 28 | namespace Microsoft.AspNetCore.Diagnostics;
|
26 | 29 |
|
27 |
| -public class ExceptionHandlerMiddlewareTest |
| 30 | +public class ExceptionHandlerMiddlewareTest : LoggedTest |
28 | 31 | {
|
29 | 32 | [Fact]
|
30 | 33 | public async Task ExceptionIsSetOnProblemDetailsContext()
|
@@ -291,6 +294,133 @@ public async Task Metrics_ExceptionThrown_DefaultSettings_Handled_Reported()
|
291 | 294 | m => AssertRequestException(m, "System.InvalidOperationException", "handled", null));
|
292 | 295 | }
|
293 | 296 |
|
| 297 | + [Fact] |
| 298 | + public async Task Metrics_ExceptionThrown_Handled_UseOriginalRoute() |
| 299 | + { |
| 300 | + // Arrange |
| 301 | + var originalEndpointBuilder = new RouteEndpointBuilder(c => Task.CompletedTask, RoutePatternFactory.Parse("/path"), 0); |
| 302 | + var originalEndpoint = originalEndpointBuilder.Build(); |
| 303 | + |
| 304 | + var meterFactory = new TestMeterFactory(); |
| 305 | + using var requestDurationCollector = new MetricCollector<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http.server.request.duration"); |
| 306 | + using var requestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "aspnetcore.diagnostics.exceptions"); |
| 307 | + |
| 308 | + using var host = new HostBuilder() |
| 309 | + .ConfigureServices(s => |
| 310 | + { |
| 311 | + s.AddSingleton<IMeterFactory>(meterFactory); |
| 312 | + s.AddSingleton(LoggerFactory); |
| 313 | + }) |
| 314 | + .ConfigureWebHost(webHostBuilder => |
| 315 | + { |
| 316 | + webHostBuilder |
| 317 | + .UseTestServer() |
| 318 | + .Configure(app => |
| 319 | + { |
| 320 | + app.UseExceptionHandler(new ExceptionHandlerOptions |
| 321 | + { |
| 322 | + ExceptionHandler = (c) => Task.CompletedTask |
| 323 | + }); |
| 324 | + app.Run(context => |
| 325 | + { |
| 326 | + context.SetEndpoint(originalEndpoint); |
| 327 | + throw new Exception("Test exception"); |
| 328 | + }); |
| 329 | + |
| 330 | + }); |
| 331 | + }).Build(); |
| 332 | + |
| 333 | + await host.StartAsync(); |
| 334 | + |
| 335 | + var server = host.GetTestServer(); |
| 336 | + |
| 337 | + // Act |
| 338 | + var response = await server.CreateClient().GetAsync("/path"); |
| 339 | + Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); |
| 340 | + |
| 341 | + await requestDurationCollector.WaitForMeasurementsAsync(minCount: 1).DefaultTimeout(); |
| 342 | + |
| 343 | + // Assert |
| 344 | + Assert.Collection( |
| 345 | + requestDurationCollector.GetMeasurementSnapshot(), |
| 346 | + m => |
| 347 | + { |
| 348 | + Assert.True(m.Value > 0); |
| 349 | + Assert.Equal(500, (int)m.Tags["http.response.status_code"]); |
| 350 | + Assert.Equal("System.Exception", (string)m.Tags["error.type"]); |
| 351 | + Assert.Equal("/path", (string)m.Tags["http.route"]); |
| 352 | + }); |
| 353 | + Assert.Collection(requestExceptionCollector.GetMeasurementSnapshot(), |
| 354 | + m => AssertRequestException(m, "System.Exception", "handled")); |
| 355 | + } |
| 356 | + |
| 357 | + [Fact] |
| 358 | + public async Task Metrics_ExceptionThrown_Handled_UseNewRoute() |
| 359 | + { |
| 360 | + // Arrange |
| 361 | + var originalEndpointBuilder = new RouteEndpointBuilder(c => Task.CompletedTask, RoutePatternFactory.Parse("/path"), 0); |
| 362 | + var originalEndpoint = originalEndpointBuilder.Build(); |
| 363 | + |
| 364 | + var newEndpointBuilder = new RouteEndpointBuilder(c => Task.CompletedTask, RoutePatternFactory.Parse("/new"), 0); |
| 365 | + var newEndpoint = newEndpointBuilder.Build(); |
| 366 | + |
| 367 | + var meterFactory = new TestMeterFactory(); |
| 368 | + using var requestDurationCollector = new MetricCollector<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http.server.request.duration"); |
| 369 | + using var requestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "aspnetcore.diagnostics.exceptions"); |
| 370 | + |
| 371 | + using var host = new HostBuilder() |
| 372 | + .ConfigureServices(s => |
| 373 | + { |
| 374 | + s.AddSingleton<IMeterFactory>(meterFactory); |
| 375 | + s.AddSingleton(LoggerFactory); |
| 376 | + }) |
| 377 | + .ConfigureWebHost(webHostBuilder => |
| 378 | + { |
| 379 | + webHostBuilder |
| 380 | + .UseTestServer() |
| 381 | + .Configure(app => |
| 382 | + { |
| 383 | + app.UseExceptionHandler(new ExceptionHandlerOptions |
| 384 | + { |
| 385 | + ExceptionHandler = (c) => |
| 386 | + { |
| 387 | + c.SetEndpoint(newEndpoint); |
| 388 | + return Task.CompletedTask; |
| 389 | + } |
| 390 | + }); |
| 391 | + app.Run(context => |
| 392 | + { |
| 393 | + context.SetEndpoint(originalEndpoint); |
| 394 | + throw new Exception("Test exception"); |
| 395 | + }); |
| 396 | + |
| 397 | + }); |
| 398 | + }).Build(); |
| 399 | + |
| 400 | + await host.StartAsync(); |
| 401 | + |
| 402 | + var server = host.GetTestServer(); |
| 403 | + |
| 404 | + // Act |
| 405 | + var response = await server.CreateClient().GetAsync("/path"); |
| 406 | + Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); |
| 407 | + |
| 408 | + await requestDurationCollector.WaitForMeasurementsAsync(minCount: 1).DefaultTimeout(); |
| 409 | + |
| 410 | + // Assert |
| 411 | + Assert.Collection( |
| 412 | + requestDurationCollector.GetMeasurementSnapshot(), |
| 413 | + m => |
| 414 | + { |
| 415 | + Assert.True(m.Value > 0); |
| 416 | + Assert.Equal(500, (int)m.Tags["http.response.status_code"]); |
| 417 | + Assert.Equal("System.Exception", (string)m.Tags["error.type"]); |
| 418 | + Assert.Equal("/new", (string)m.Tags["http.route"]); |
| 419 | + }); |
| 420 | + Assert.Collection(requestExceptionCollector.GetMeasurementSnapshot(), |
| 421 | + m => AssertRequestException(m, "System.Exception", "handled")); |
| 422 | + } |
| 423 | + |
294 | 424 | [Fact]
|
295 | 425 | public async Task Metrics_ExceptionThrown_Unhandled_Reported()
|
296 | 426 | {
|
|
0 commit comments