11
11
using Microsoft . Extensions . DependencyInjection ;
12
12
using Microsoft . Extensions . Diagnostics . Metrics ;
13
13
using Microsoft . Extensions . Logging . Abstractions ;
14
+ using Microsoft . Extensions . Telemetry . Testing . Metering ;
14
15
15
16
namespace Microsoft . AspNetCore . Hosting . Tests ;
16
17
@@ -25,9 +26,9 @@ public void MultipleRequests()
25
26
var httpContext = new DefaultHttpContext ( ) ;
26
27
var meter = meterFactory . Meters . Single ( ) ;
27
28
28
- using var requestDurationRecorder = new InstrumentRecorder < double > ( meterFactory , HostingMetrics . MeterName , "http-server-request-duration" ) ;
29
- using var currentRequestsRecorder = new InstrumentRecorder < long > ( meterFactory , HostingMetrics . MeterName , "http-server-current-requests" ) ;
30
- using var unhandledRequestsRecorder = new InstrumentRecorder < long > ( meterFactory , HostingMetrics . MeterName , "http-server-unhandled-requests" ) ;
29
+ using var requestDurationCollector = new MetricCollector < double > ( meterFactory , HostingMetrics . MeterName , "http-server-request-duration" ) ;
30
+ using var currentRequestsCollector = new MetricCollector < long > ( meterFactory , HostingMetrics . MeterName , "http-server-current-requests" ) ;
31
+ using var unhandledRequestsCollector = new MetricCollector < long > ( meterFactory , HostingMetrics . MeterName , "http-server-unhandled-requests" ) ;
31
32
32
33
// Act/Assert
33
34
Assert . Equal ( HostingMetrics . MeterName , meter . Name ) ;
@@ -39,10 +40,10 @@ public void MultipleRequests()
39
40
context1 . HttpContext . Response . StatusCode = StatusCodes . Status200OK ;
40
41
hostingApplication . DisposeContext ( context1 , null ) ;
41
42
42
- Assert . Collection ( currentRequestsRecorder . GetMeasurements ( ) ,
43
+ Assert . Collection ( currentRequestsCollector . GetMeasurementSnapshot ( ) ,
43
44
m => Assert . Equal ( 1 , m . Value ) ,
44
45
m => Assert . Equal ( - 1 , m . Value ) ) ;
45
- Assert . Collection ( requestDurationRecorder . GetMeasurements ( ) ,
46
+ Assert . Collection ( requestDurationCollector . GetMeasurementSnapshot ( ) ,
46
47
m => AssertRequestDuration ( m , HttpProtocol . Http11 , StatusCodes . Status200OK ) ) ;
47
48
48
49
// Request 2 (after failure)
@@ -51,12 +52,12 @@ public void MultipleRequests()
51
52
context2 . HttpContext . Response . StatusCode = StatusCodes . Status500InternalServerError ;
52
53
hostingApplication . DisposeContext ( context2 , new InvalidOperationException ( "Test error" ) ) ;
53
54
54
- Assert . Collection ( currentRequestsRecorder . GetMeasurements ( ) ,
55
+ Assert . Collection ( currentRequestsCollector . GetMeasurementSnapshot ( ) ,
55
56
m => Assert . Equal ( 1 , m . Value ) ,
56
57
m => Assert . Equal ( - 1 , m . Value ) ,
57
58
m => Assert . Equal ( 1 , m . Value ) ,
58
59
m => Assert . Equal ( - 1 , m . Value ) ) ;
59
- Assert . Collection ( requestDurationRecorder . GetMeasurements ( ) ,
60
+ Assert . Collection ( requestDurationCollector . GetMeasurementSnapshot ( ) ,
60
61
m => AssertRequestDuration ( m , HttpProtocol . Http11 , StatusCodes . Status200OK ) ,
61
62
m => AssertRequestDuration ( m , HttpProtocol . Http2 , StatusCodes . Status500InternalServerError , exceptionName : "System.InvalidOperationException" ) ) ;
62
63
@@ -66,37 +67,37 @@ public void MultipleRequests()
66
67
context3 . HttpContext . Items [ "__RequestUnhandled" ] = true ;
67
68
context3 . HttpContext . Response . StatusCode = StatusCodes . Status404NotFound ;
68
69
69
- Assert . Collection ( currentRequestsRecorder . GetMeasurements ( ) ,
70
+ Assert . Collection ( currentRequestsCollector . GetMeasurementSnapshot ( ) ,
70
71
m => Assert . Equal ( 1 , m . Value ) ,
71
72
m => Assert . Equal ( - 1 , m . Value ) ,
72
73
m => Assert . Equal ( 1 , m . Value ) ,
73
74
m => Assert . Equal ( - 1 , m . Value ) ,
74
75
m => Assert . Equal ( 1 , m . Value ) ) ;
75
- Assert . Collection ( requestDurationRecorder . GetMeasurements ( ) ,
76
+ Assert . Collection ( requestDurationCollector . GetMeasurementSnapshot ( ) ,
76
77
m => AssertRequestDuration ( m , HttpProtocol . Http11 , StatusCodes . Status200OK ) ,
77
78
m => AssertRequestDuration ( m , HttpProtocol . Http2 , StatusCodes . Status500InternalServerError , exceptionName : "System.InvalidOperationException" ) ) ;
78
79
79
80
hostingApplication . DisposeContext ( context3 , null ) ;
80
81
81
- Assert . Collection ( currentRequestsRecorder . GetMeasurements ( ) ,
82
+ Assert . Collection ( currentRequestsCollector . GetMeasurementSnapshot ( ) ,
82
83
m => Assert . Equal ( 1 , m . Value ) ,
83
84
m => Assert . Equal ( - 1 , m . Value ) ,
84
85
m => Assert . Equal ( 1 , m . Value ) ,
85
86
m => Assert . Equal ( - 1 , m . Value ) ,
86
87
m => Assert . Equal ( 1 , m . Value ) ,
87
88
m => Assert . Equal ( - 1 , m . Value ) ) ;
88
- Assert . Collection ( requestDurationRecorder . GetMeasurements ( ) ,
89
+ Assert . Collection ( requestDurationCollector . GetMeasurementSnapshot ( ) ,
89
90
m => AssertRequestDuration ( m , HttpProtocol . Http11 , StatusCodes . Status200OK ) ,
90
91
m => AssertRequestDuration ( m , HttpProtocol . Http2 , StatusCodes . Status500InternalServerError , exceptionName : "System.InvalidOperationException" ) ,
91
92
m => AssertRequestDuration ( m , HttpProtocol . Http3 , StatusCodes . Status404NotFound ) ) ;
92
- Assert . Collection ( unhandledRequestsRecorder . GetMeasurements ( ) ,
93
+ Assert . Collection ( unhandledRequestsCollector . GetMeasurementSnapshot ( ) ,
93
94
m => Assert . Equal ( 1 , m . Value ) ) ;
94
95
95
- static void AssertRequestDuration ( Measurement < double > measurement , string protocol , int statusCode , string exceptionName = null )
96
+ static void AssertRequestDuration ( CollectedMeasurement < double > measurement , string protocol , int statusCode , string exceptionName = null )
96
97
{
97
98
Assert . True ( measurement . Value > 0 ) ;
98
- Assert . Equal ( protocol , ( string ) measurement . Tags . ToArray ( ) . Single ( t => t . Key == "protocol" ) . Value ) ;
99
- Assert . Equal ( statusCode , ( int ) measurement . Tags . ToArray ( ) . Single ( t => t . Key == "status-code" ) . Value ) ;
99
+ Assert . Equal ( protocol , ( string ) measurement . Tags [ "protocol" ] ) ;
100
+ Assert . Equal ( statusCode , ( int ) measurement . Tags [ "status-code" ] ) ;
100
101
if ( exceptionName == null )
101
102
{
102
103
Assert . DoesNotContain ( measurement . Tags . ToArray ( ) , t => t . Key == "exception-name" ) ;
@@ -132,17 +133,17 @@ public async Task StartListeningDuringRequest_NotMeasured()
132
133
133
134
await syncPoint . WaitForSyncPoint ( ) . DefaultTimeout ( ) ;
134
135
135
- using var requestDurationRecorder = new InstrumentRecorder < double > ( meterFactory , HostingMetrics . MeterName , "http-server-request-duration" ) ;
136
- using var currentRequestsRecorder = new InstrumentRecorder < long > ( meterFactory , HostingMetrics . MeterName , "http-server-current-requests" ) ;
136
+ using var requestDurationCollector = new MetricCollector < double > ( meterFactory , HostingMetrics . MeterName , "http-server-request-duration" ) ;
137
+ using var currentRequestsCollector = new MetricCollector < long > ( meterFactory , HostingMetrics . MeterName , "http-server-current-requests" ) ;
137
138
context1 . HttpContext . Response . StatusCode = StatusCodes . Status200OK ;
138
139
139
140
syncPoint . Continue ( ) ;
140
141
await processRequestTask . DefaultTimeout ( ) ;
141
142
142
143
hostingApplication . DisposeContext ( context1 , null ) ;
143
144
144
- Assert . Empty ( currentRequestsRecorder . GetMeasurements ( ) ) ;
145
- Assert . Empty ( requestDurationRecorder . GetMeasurements ( ) ) ;
145
+ Assert . Empty ( currentRequestsCollector . GetMeasurementSnapshot ( ) ) ;
146
+ Assert . Empty ( requestDurationCollector . GetMeasurementSnapshot ( ) ) ;
146
147
}
147
148
148
149
[ Fact ]
@@ -154,8 +155,8 @@ public void IHttpMetricsTagsFeatureNotUsedFromFeatureCollection()
154
155
var httpContext = new DefaultHttpContext ( ) ;
155
156
var meter = meterFactory . Meters . Single ( ) ;
156
157
157
- using var requestDurationRecorder = new InstrumentRecorder < double > ( meterFactory , HostingMetrics . MeterName , "http-server-request-duration" ) ;
158
- using var currentRequestsRecorder = new InstrumentRecorder < long > ( meterFactory , HostingMetrics . MeterName , "http-server-current-requests" ) ;
158
+ using var requestDurationCollector = new MetricCollector < double > ( meterFactory , HostingMetrics . MeterName , "http-server-request-duration" ) ;
159
+ using var currentRequestsCollector = new MetricCollector < long > ( meterFactory , HostingMetrics . MeterName , "http-server-current-requests" ) ;
159
160
160
161
// Act/Assert
161
162
Assert . Equal ( HostingMetrics . MeterName , meter . Name ) ;
0 commit comments