Skip to content

Commit 53c393c

Browse files
committed
Everywhere
1 parent da37b24 commit 53c393c

17 files changed

+202
-256
lines changed

src/Http/Routing/test/UnitTests/Microsoft.AspNetCore.Routing.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<Reference Include="Microsoft.Extensions.Diagnostics" />
1616
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" />
1717
<Reference Include="Microsoft.Extensions.Logging" />
18+
<Reference Include="Microsoft.Extensions.Telemetry.Testing" />
1819
<Reference Include="Microsoft.Extensions.WebEncoders" />
1920

2021
<Compile Include="$(SharedSourceRoot)Metrics\TestMeterFactory.cs" LinkBase="shared" />

src/Http/Routing/test/UnitTests/RoutingMetricsTests.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.Extensions.Logging;
1414
using Microsoft.Extensions.Logging.Abstractions;
1515
using Microsoft.Extensions.Options;
16+
using Microsoft.Extensions.Telemetry.Testing.Metering;
1617
using Moq;
1718

1819
namespace Microsoft.AspNetCore.Routing;
@@ -34,8 +35,8 @@ public async Task Match_Success()
3435
var httpContext = new DefaultHttpContext();
3536
var meter = meterFactory.Meters.Single();
3637

37-
using var routingMatchSuccessRecorder = new InstrumentRecorder<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-success");
38-
using var routingMatchFailureRecorder = new InstrumentRecorder<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-failure");
38+
using var routingMatchSuccessCollector = new MetricCollector<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-success");
39+
using var routingMatchFailureCollector = new MetricCollector<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-failure");
3940

4041
// Act
4142
await middleware.Invoke(httpContext);
@@ -44,9 +45,9 @@ public async Task Match_Success()
4445
Assert.Equal(RoutingMetrics.MeterName, meter.Name);
4546
Assert.Null(meter.Version);
4647

47-
Assert.Collection(routingMatchSuccessRecorder.GetMeasurements(),
48+
Assert.Collection(routingMatchSuccessCollector.GetMeasurementSnapshot(),
4849
m => AssertSuccess(m, "/{hi}", fallback: false));
49-
Assert.Empty(routingMatchFailureRecorder.GetMeasurements());
50+
Assert.Empty(routingMatchFailureCollector.GetMeasurementSnapshot());
5051
}
5152

5253
[Theory]
@@ -70,8 +71,8 @@ public async Task Match_SuccessFallback_SetTagIfPresent(bool hasFallbackMetadata
7071
var httpContext = new DefaultHttpContext();
7172
var meter = meterFactory.Meters.Single();
7273

73-
using var routingMatchSuccessRecorder = new InstrumentRecorder<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-success");
74-
using var routingMatchFailureRecorder = new InstrumentRecorder<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-failure");
74+
using var routingMatchSuccessCollector = new MetricCollector<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-success");
75+
using var routingMatchFailureCollector = new MetricCollector<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-failure");
7576

7677
// Act
7778
await middleware.Invoke(httpContext);
@@ -80,9 +81,9 @@ public async Task Match_SuccessFallback_SetTagIfPresent(bool hasFallbackMetadata
8081
Assert.Equal(RoutingMetrics.MeterName, meter.Name);
8182
Assert.Null(meter.Version);
8283

83-
Assert.Collection(routingMatchSuccessRecorder.GetMeasurements(),
84+
Assert.Collection(routingMatchSuccessCollector.GetMeasurementSnapshot(),
8485
m => AssertSuccess(m, "/{hi}", fallback: hasFallbackMetadata));
85-
Assert.Empty(routingMatchFailureRecorder.GetMeasurements());
86+
Assert.Empty(routingMatchFailureCollector.GetMeasurementSnapshot());
8687
}
8788

8889
[Fact]
@@ -99,8 +100,8 @@ public async Task Match_Success_MissingRoute()
99100
var httpContext = new DefaultHttpContext();
100101
var meter = meterFactory.Meters.Single();
101102

102-
using var routingMatchSuccessRecorder = new InstrumentRecorder<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-success");
103-
using var routingMatchFailureRecorder = new InstrumentRecorder<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-failure");
103+
using var routingMatchSuccessCollector = new MetricCollector<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-success");
104+
using var routingMatchFailureCollector = new MetricCollector<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-failure");
104105

105106
// Act
106107
await middleware.Invoke(httpContext);
@@ -109,9 +110,9 @@ public async Task Match_Success_MissingRoute()
109110
Assert.Equal(RoutingMetrics.MeterName, meter.Name);
110111
Assert.Null(meter.Version);
111112

112-
Assert.Collection(routingMatchSuccessRecorder.GetMeasurements(),
113+
Assert.Collection(routingMatchSuccessCollector.GetMeasurementSnapshot(),
113114
m => AssertSuccess(m, "(missing)", fallback: false));
114-
Assert.Empty(routingMatchFailureRecorder.GetMeasurements());
115+
Assert.Empty(routingMatchFailureCollector.GetMeasurementSnapshot());
115116
}
116117

117118
[Fact]
@@ -125,8 +126,8 @@ public async Task Match_Failure()
125126
var httpContext = new DefaultHttpContext();
126127
var meter = meterFactory.Meters.Single();
127128

128-
using var routingMatchSuccessRecorder = new InstrumentRecorder<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-success");
129-
using var routingMatchFailureRecorder = new InstrumentRecorder<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-failure");
129+
using var routingMatchSuccessCollector = new MetricCollector<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-success");
130+
using var routingMatchFailureCollector = new MetricCollector<long>(meterFactory, RoutingMetrics.MeterName, "routing-match-failure");
130131

131132
// Act
132133
await middleware.Invoke(httpContext);
@@ -135,16 +136,16 @@ public async Task Match_Failure()
135136
Assert.Equal(RoutingMetrics.MeterName, meter.Name);
136137
Assert.Null(meter.Version);
137138

138-
Assert.Empty(routingMatchSuccessRecorder.GetMeasurements());
139-
Assert.Collection(routingMatchFailureRecorder.GetMeasurements(),
139+
Assert.Empty(routingMatchSuccessCollector.GetMeasurementSnapshot());
140+
Assert.Collection(routingMatchFailureCollector.GetMeasurementSnapshot(),
140141
m => Assert.Equal(1, m.Value));
141142
}
142143

143-
private void AssertSuccess(Measurement<long> measurement, string route, bool fallback)
144+
private void AssertSuccess(CollectedMeasurement<long> measurement, string route, bool fallback)
144145
{
145146
Assert.Equal(1, measurement.Value);
146-
Assert.Equal(route, (string)measurement.Tags.ToArray().Single(t => t.Key == "route").Value);
147-
Assert.Equal(fallback, (bool)measurement.Tags.ToArray().Single(t => t.Key == "fallback").Value);
147+
Assert.Equal(route, (string)measurement.Tags["route"]);
148+
Assert.Equal(fallback, (bool)measurement.Tags["fallback"]);
148149
}
149150

150151
private EndpointRoutingMiddleware CreateMiddleware(

src/Middleware/Diagnostics/test/UnitTests/DeveloperExceptionPageMiddlewareTest.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Extensions.DependencyInjection;
1818
using Microsoft.Extensions.Diagnostics.Metrics;
1919
using Microsoft.Extensions.Hosting;
20+
using Microsoft.Extensions.Telemetry.Testing.Metering;
2021

2122
namespace Microsoft.AspNetCore.Diagnostics;
2223

@@ -538,16 +539,9 @@ public async Task NullInfoInCompilationException_ShouldNotThrowExceptionGenerati
538539
public async Task UnhandledError_ExceptionNameTagAdded()
539540
{
540541
// Arrange
541-
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
542-
543542
var meterFactory = new TestMeterFactory();
544-
using var requestDurationRecorder = new InstrumentRecorder<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http-server-request-duration");
545-
using var requestExceptionRecorder = new InstrumentRecorder<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
546-
using var measurementReporter = new MeasurementReporter<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http-server-request-duration");
547-
measurementReporter.Register(m =>
548-
{
549-
tcs.SetResult();
550-
});
543+
using var requestDurationCollector = new MetricCollector<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http-server-request-duration");
544+
using var requestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
551545

552546
using var host = new HostBuilder()
553547
.ConfigureServices(s =>
@@ -577,33 +571,33 @@ public async Task UnhandledError_ExceptionNameTagAdded()
577571
var response = await server.CreateClient().GetAsync("/path");
578572
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
579573

580-
await tcs.Task.DefaultTimeout();
574+
await requestDurationCollector.WaitForMeasurementsAsync(numMeasurements: 1).DefaultTimeout();
581575

582576
// Assert
583577
Assert.Collection(
584-
requestDurationRecorder.GetMeasurements(),
578+
requestDurationCollector.GetMeasurementSnapshot(),
585579
m =>
586580
{
587581
Assert.True(m.Value > 0);
588582
Assert.Equal(500, (int)m.Tags.ToArray().Single(t => t.Key == "status-code").Value);
589583
Assert.Equal("System.Exception", (string)m.Tags.ToArray().Single(t => t.Key == "exception-name").Value);
590584
});
591-
Assert.Collection(requestExceptionRecorder.GetMeasurements(),
585+
Assert.Collection(requestExceptionCollector.GetMeasurementSnapshot(),
592586
m => AssertRequestException(m, "System.Exception", "Unhandled"));
593587
}
594588

595-
private static void AssertRequestException(Measurement<long> measurement, string exceptionName, string result, string handler = null)
589+
private static void AssertRequestException(CollectedMeasurement<long> measurement, string exceptionName, string result, string handler = null)
596590
{
597591
Assert.Equal(1, measurement.Value);
598-
Assert.Equal(exceptionName, (string)measurement.Tags.ToArray().Single(t => t.Key == "exception-name").Value);
599-
Assert.Equal(result, measurement.Tags.ToArray().Single(t => t.Key == "result").Value.ToString());
592+
Assert.Equal(exceptionName, (string)measurement.Tags["exception-name"]);
593+
Assert.Equal(result, measurement.Tags["result"].ToString());
600594
if (handler == null)
601595
{
602-
Assert.DoesNotContain(measurement.Tags.ToArray(), t => t.Key == "handler");
596+
Assert.False(measurement.Tags.ContainsKey("handler"));
603597
}
604598
else
605599
{
606-
Assert.Equal(handler, (string)measurement.Tags.ToArray().Single(t => t.Key == "handler").Value);
600+
Assert.Equal(handler, (string)measurement.Tags["handler"]);
607601
}
608602
}
609603

src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerMiddlewareTest.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Microsoft.Extensions.Hosting;
2020
using Microsoft.Extensions.Logging.Abstractions;
2121
using Microsoft.Extensions.Options;
22+
using Microsoft.Extensions.Telemetry.Testing.Metering;
2223
using Moq;
2324

2425
namespace Microsoft.AspNetCore.Diagnostics;
@@ -210,7 +211,7 @@ public async Task Metrics_NoExceptionThrown()
210211
var middleware = CreateMiddleware(_ => Task.CompletedTask, optionsAccessor, exceptionHandlers, meterFactory);
211212
var meter = meterFactory.Meters.Single();
212213

213-
using var diagnosticsRequestExceptionRecorder = new InstrumentRecorder<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
214+
using var diagnosticsRequestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
214215

215216
// Act
216217
await middleware.Invoke(httpContext);
@@ -219,7 +220,7 @@ public async Task Metrics_NoExceptionThrown()
219220
Assert.Equal(DiagnosticsMetrics.MeterName, meter.Name);
220221
Assert.Null(meter.Version);
221222

222-
Assert.Empty(diagnosticsRequestExceptionRecorder.GetMeasurements());
223+
Assert.Empty(diagnosticsRequestExceptionCollector.GetMeasurementSnapshot());
223224
}
224225

225226
[Fact]
@@ -233,13 +234,13 @@ public async Task Metrics_ExceptionThrown_Handled_Reported()
233234
var middleware = CreateMiddleware(_ => throw new InvalidOperationException(), optionsAccessor, exceptionHandlers, meterFactory);
234235
var meter = meterFactory.Meters.Single();
235236

236-
using var diagnosticsRequestExceptionRecorder = new InstrumentRecorder<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
237+
using var diagnosticsRequestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
237238

238239
// Act
239240
await middleware.Invoke(httpContext);
240241

241242
// Assert
242-
Assert.Collection(diagnosticsRequestExceptionRecorder.GetMeasurements(),
243+
Assert.Collection(diagnosticsRequestExceptionCollector.GetMeasurementSnapshot(),
243244
m => AssertRequestException(m, "System.InvalidOperationException", "Handled", typeof(TestExceptionHandler).FullName));
244245
}
245246

@@ -255,13 +256,13 @@ public async Task Metrics_ExceptionThrown_ResponseStarted_Skipped()
255256
var middleware = CreateMiddleware(_ => throw new InvalidOperationException(), optionsAccessor, exceptionHandlers, meterFactory);
256257
var meter = meterFactory.Meters.Single();
257258

258-
using var diagnosticsRequestExceptionRecorder = new InstrumentRecorder<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
259+
using var diagnosticsRequestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
259260

260261
// Act
261262
await Assert.ThrowsAsync<InvalidOperationException>(() => middleware.Invoke(httpContext));
262263

263264
// Assert
264-
Assert.Collection(diagnosticsRequestExceptionRecorder.GetMeasurements(),
265+
Assert.Collection(diagnosticsRequestExceptionCollector.GetMeasurementSnapshot(),
265266
m => AssertRequestException(m, "System.InvalidOperationException", "Skipped"));
266267
}
267268

@@ -280,13 +281,13 @@ public async Task Metrics_ExceptionThrown_DefaultSettings_Handled_Reported()
280281
var middleware = CreateMiddleware(_ => throw new InvalidOperationException(), optionsAccessor, meterFactory: meterFactory);
281282
var meter = meterFactory.Meters.Single();
282283

283-
using var diagnosticsRequestExceptionRecorder = new InstrumentRecorder<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
284+
using var diagnosticsRequestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
284285

285286
// Act
286287
await middleware.Invoke(httpContext);
287288

288289
// Assert
289-
Assert.Collection(diagnosticsRequestExceptionRecorder.GetMeasurements(),
290+
Assert.Collection(diagnosticsRequestExceptionCollector.GetMeasurementSnapshot(),
290291
m => AssertRequestException(m, "System.InvalidOperationException", "Handled", null));
291292
}
292293

@@ -304,28 +305,28 @@ public async Task Metrics_ExceptionThrown_Unhandled_Reported()
304305
var middleware = CreateMiddleware(_ => throw new InvalidOperationException(), optionsAccessor, meterFactory: meterFactory);
305306
var meter = meterFactory.Meters.Single();
306307

307-
using var diagnosticsRequestExceptionRecorder = new InstrumentRecorder<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
308+
using var diagnosticsRequestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "diagnostics-handler-exception");
308309

309310
// Act
310311
await Assert.ThrowsAsync<InvalidOperationException>(() => middleware.Invoke(httpContext));
311312

312313
// Assert
313-
Assert.Collection(diagnosticsRequestExceptionRecorder.GetMeasurements(),
314+
Assert.Collection(diagnosticsRequestExceptionCollector.GetMeasurementSnapshot(),
314315
m => AssertRequestException(m, "System.InvalidOperationException", "Unhandled"));
315316
}
316317

317-
private static void AssertRequestException(Measurement<long> measurement, string exceptionName, string result, string handler = null)
318+
private static void AssertRequestException(CollectedMeasurement<long> measurement, string exceptionName, string result, string handler = null)
318319
{
319320
Assert.Equal(1, measurement.Value);
320-
Assert.Equal(exceptionName, (string)measurement.Tags.ToArray().Single(t => t.Key == "exception-name").Value);
321-
Assert.Equal(result, measurement.Tags.ToArray().Single(t => t.Key == "result").Value.ToString());
321+
Assert.Equal(exceptionName, (string)measurement.Tags["exception-name"]);
322+
Assert.Equal(result, measurement.Tags["result"].ToString());
322323
if (handler == null)
323324
{
324-
Assert.DoesNotContain(measurement.Tags.ToArray(), t => t.Key == "handler");
325+
Assert.False(measurement.Tags.ContainsKey("handler"));
325326
}
326327
else
327328
{
328-
Assert.Equal(handler, (string)measurement.Tags.ToArray().Single(t => t.Key == "handler").Value);
329+
Assert.Equal(handler, (string)measurement.Tags["handler"]);
329330
}
330331
}
331332

src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerTest.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.Extensions.Hosting;
1515
using Microsoft.Extensions.Logging;
1616
using Microsoft.Extensions.Logging.Testing;
17+
using Microsoft.Extensions.Telemetry.Testing.Metering;
1718

1819
namespace Microsoft.AspNetCore.Diagnostics;
1920

@@ -915,15 +916,8 @@ public async Task ExceptionHandlerWithExceptionHandlerNotReplacedWithGlobalRoute
915916
public async Task UnhandledError_ExceptionNameTagAdded()
916917
{
917918
// Arrange
918-
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
919-
920919
var meterFactory = new TestMeterFactory();
921-
using var instrumentRecorder = new InstrumentRecorder<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http-server-request-duration");
922-
using var measurementReporter = new MeasurementReporter<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http-server-request-duration");
923-
measurementReporter.Register(m =>
924-
{
925-
tcs.SetResult();
926-
});
920+
using var instrumentCollector = new MetricCollector<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http-server-request-duration");
927921

928922
using var host = new HostBuilder()
929923
.ConfigureServices(s =>
@@ -959,11 +953,11 @@ public async Task UnhandledError_ExceptionNameTagAdded()
959953
var response = await server.CreateClient().GetAsync("/path");
960954
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
961955

962-
await tcs.Task.DefaultTimeout();
956+
await instrumentCollector.WaitForMeasurementsAsync(numMeasurements: 1).DefaultTimeout();
963957

964958
// Assert
965959
Assert.Collection(
966-
instrumentRecorder.GetMeasurements(),
960+
instrumentCollector.GetMeasurementSnapshot(),
967961
m =>
968962
{
969963
Assert.True(m.Value > 0);

src/Middleware/Diagnostics/test/UnitTests/Microsoft.AspNetCore.Diagnostics.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Reference Include="Microsoft.Extensions.DependencyInjection" />
2222
<Reference Include="Microsoft.Extensions.DiagnosticAdapter" />
2323
<Reference Include="Microsoft.Extensions.FileProviders.Embedded" />
24+
<Reference Include="Microsoft.Extensions.Telemetry.Testing" />
2425
<Reference Include="Microsoft.AspNetCore" />
2526
<Reference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" />
2627
</ItemGroup>

src/Middleware/RateLimiting/test/Microsoft.AspNetCore.RateLimiting.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<Reference Include="Microsoft.AspNetCore.Http" />
99
<Reference Include="Microsoft.AspNetCore.RateLimiting" />
1010
<Reference Include="Microsoft.AspNetCore.Routing" />
11+
<Reference Include="Microsoft.Extensions.Telemetry.Testing" />
1112

1213
<Compile Include="$(SharedSourceRoot)Metrics\TestMeterFactory.cs" LinkBase="shared" />
1314
<Compile Include="$(SharedSourceRoot)SyncPoint\SyncPoint.cs" LinkBase="shared" />

0 commit comments

Comments
 (0)