@@ -11,77 +11,67 @@ namespace Prometheus.Client.MetricServer.Tests;
11
11
12
12
public class MetricServerTests
13
13
{
14
+ private IMetricServer _metricServer ;
14
15
private readonly ITestOutputHelper _testOutputHelper ;
15
16
private const int _port = 9091 ;
16
17
17
18
public MetricServerTests ( ITestOutputHelper testOutputHelper )
18
19
{
19
20
_testOutputHelper = testOutputHelper ;
21
+ _metricServer = new MetricServer ( new MetricServerOptions
22
+ {
23
+ Port = _port ,
24
+ CollectorRegistryInstance = new CollectorRegistry ( )
25
+ } ) ;
20
26
}
21
27
22
28
[ Fact ]
23
29
public void Start_Stop_IsRunning ( )
24
30
{
25
- var metricServer = new MetricServer ( new MetricServerOptions
26
- {
27
- Port = _port ,
28
- CollectorRegistryInstance = new CollectorRegistry ( )
29
- } ) ;
30
- metricServer . Start ( ) ;
31
- Assert . True ( metricServer . IsRunning ) ;
32
- metricServer . Stop ( ) ;
33
- Assert . False ( metricServer . IsRunning ) ;
31
+ _metricServer . Start ( ) ;
32
+ Assert . True ( _metricServer . IsRunning ) ;
33
+ _metricServer . Stop ( ) ;
34
+ Assert . False ( _metricServer . IsRunning ) ;
34
35
}
35
36
36
37
[ Fact ]
37
38
public void Start_DoubleStop_IsRunning ( )
38
39
{
39
- var metricServer = new MetricServer ( new MetricServerOptions
40
- {
41
- Port = _port ,
42
- CollectorRegistryInstance = new CollectorRegistry ( )
43
- } ) ;
44
- metricServer . Start ( ) ;
45
- Assert . True ( metricServer . IsRunning ) ;
46
- metricServer . Stop ( ) ;
47
- Assert . False ( metricServer . IsRunning ) ;
48
- metricServer . Stop ( ) ;
49
- Assert . False ( metricServer . IsRunning ) ;
40
+ _metricServer . Start ( ) ;
41
+ Assert . True ( _metricServer . IsRunning ) ;
42
+ _metricServer . Stop ( ) ;
43
+ Assert . False ( _metricServer . IsRunning ) ;
44
+ _metricServer . Stop ( ) ;
45
+ Assert . False ( _metricServer . IsRunning ) ;
50
46
}
51
47
52
48
[ Fact ]
53
49
public void DoubleStart_Stop_IsRunning ( )
54
50
{
55
- var metricServer = new MetricServer ( new MetricServerOptions
56
- {
57
- Port = _port ,
58
- CollectorRegistryInstance = new CollectorRegistry ( )
59
- } ) ;
60
- metricServer . Start ( ) ;
61
- Assert . True ( metricServer . IsRunning ) ;
62
- metricServer . Start ( ) ;
63
- Assert . True ( metricServer . IsRunning ) ;
64
- metricServer . Stop ( ) ;
65
- Assert . False ( metricServer . IsRunning ) ;
51
+ _metricServer . Start ( ) ;
52
+ Assert . True ( _metricServer . IsRunning ) ;
53
+ _metricServer . Start ( ) ;
54
+ Assert . True ( _metricServer . IsRunning ) ;
55
+ _metricServer . Stop ( ) ;
56
+ Assert . False ( _metricServer . IsRunning ) ;
66
57
}
67
58
68
59
[ Fact ]
69
60
public void Start_Stop_DefaultPort_IsRunning ( )
70
61
{
71
- var metricServer = new MetricServer ( ) ;
72
- metricServer . Start ( ) ;
73
- Assert . True ( metricServer . IsRunning ) ;
74
- metricServer . Stop ( ) ;
75
- Assert . False ( metricServer . IsRunning ) ;
62
+ _metricServer = new MetricServer ( new MetricServerOptions { CollectorRegistryInstance = new CollectorRegistry ( ) } ) ;
63
+ _metricServer . Start ( ) ;
64
+ Assert . True ( _metricServer . IsRunning ) ;
65
+ _metricServer . Stop ( ) ;
66
+ Assert . False ( _metricServer . IsRunning ) ;
76
67
}
77
68
78
69
[ Fact ]
79
70
public async Task Base_MapPath ( )
80
71
{
81
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port } ) ;
82
72
try
83
73
{
84
- metricServer . Start ( ) ;
74
+ _metricServer . Start ( ) ;
85
75
var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
86
76
counter . Inc ( ) ;
87
77
using var httpClient = new HttpClient ( ) ;
@@ -95,17 +85,17 @@ public async Task Base_MapPath()
95
85
}
96
86
finally
97
87
{
98
- metricServer . Stop ( ) ;
88
+ _metricServer . Stop ( ) ;
99
89
}
100
90
}
101
91
102
92
[ Fact ]
103
93
public async Task MapPath_WithEndSlash ( )
104
94
{
105
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port , MapPath = "/test" } ) ;
95
+ _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , MapPath = "/test" } ) ;
106
96
try
107
97
{
108
- metricServer . Start ( ) ;
98
+ _metricServer . Start ( ) ;
109
99
var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
110
100
counter . Inc ( ) ;
111
101
using var httpClient = new HttpClient ( ) ;
@@ -119,7 +109,7 @@ public async Task MapPath_WithEndSlash()
119
109
}
120
110
finally
121
111
{
122
- metricServer . Stop ( ) ;
112
+ _metricServer . Stop ( ) ;
123
113
}
124
114
}
125
115
@@ -136,10 +126,10 @@ public void Wrong_MapPath()
136
126
[ InlineData ( "/metrics965" ) ]
137
127
public async Task MapPath ( string mapPath )
138
128
{
139
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port , MapPath = mapPath } ) ;
129
+ _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , MapPath = mapPath } ) ;
140
130
try
141
131
{
142
- metricServer . Start ( ) ;
132
+ _metricServer . Start ( ) ;
143
133
var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
144
134
counter . Inc ( ) ;
145
135
using var httpClient = new HttpClient ( ) ;
@@ -153,7 +143,7 @@ public async Task MapPath(string mapPath)
153
143
}
154
144
finally
155
145
{
156
- metricServer . Stop ( ) ;
146
+ _metricServer . Stop ( ) ;
157
147
}
158
148
}
159
149
@@ -162,11 +152,11 @@ public async Task Custom_Find_Metric()
162
152
{
163
153
var registry = new CollectorRegistry ( ) ;
164
154
var factory = new MetricFactory ( registry ) ;
165
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = registry } ) ;
155
+ _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = registry } ) ;
166
156
167
157
try
168
158
{
169
- metricServer . Start ( ) ;
159
+ _metricServer . Start ( ) ;
170
160
171
161
const string metricName = "myCounter" ;
172
162
var counter = factory . CreateCounter ( metricName , "helptext" ) ;
@@ -183,23 +173,21 @@ public async Task Custom_Find_Metric()
183
173
}
184
174
finally
185
175
{
186
- metricServer . Stop ( ) ;
176
+ _metricServer . Stop ( ) ;
187
177
}
188
178
}
189
179
190
180
[ Fact ]
191
181
public async Task AddLegacyMetrics_False_CheckMetrics ( )
192
182
{
193
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) } ) ;
194
-
195
183
try
196
184
{
197
- metricServer . Start ( ) ;
185
+ _metricServer . Start ( ) ;
198
186
using var httpClient = new HttpClient ( ) ;
199
187
string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /metrics") ;
200
- Assert . Contains ( "process_working_set_bytes " , response ) ;
188
+ Assert . Contains ( "process_private_memory_bytes " , response ) ;
201
189
Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
202
- Assert . DoesNotContain ( "process_working_set " , response ) ;
190
+ Assert . DoesNotContain ( "process_private_bytes " , response ) ;
203
191
Assert . DoesNotContain ( "dotnet_totalmemory" , response ) ;
204
192
}
205
193
catch ( Exception ex )
@@ -209,23 +197,23 @@ public async Task AddLegacyMetrics_False_CheckMetrics()
209
197
}
210
198
finally
211
199
{
212
- metricServer . Stop ( ) ;
200
+ _metricServer . Stop ( ) ;
213
201
}
214
202
}
215
203
216
204
[ Fact ]
217
205
public async Task AddLegacyMetrics_True_CheckMetrics ( )
218
206
{
219
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , AddLegacyMetrics = true } ) ;
207
+ _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , AddLegacyMetrics = true } ) ;
220
208
221
209
try
222
210
{
223
- metricServer . Start ( ) ;
211
+ _metricServer . Start ( ) ;
224
212
using var httpClient = new HttpClient ( ) ;
225
213
string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /metrics") ;
226
- Assert . Contains ( "process_working_set_bytes " , response ) ;
214
+ Assert . Contains ( "process_private_memory_bytes " , response ) ;
227
215
Assert . Contains ( "dotnet_total_memory_bytes" , response ) ;
228
- Assert . Contains ( "process_working_set " , response ) ;
216
+ Assert . Contains ( "process_private_bytes " , response ) ;
229
217
Assert . Contains ( "dotnet_totalmemory" , response ) ;
230
218
}
231
219
catch ( Exception ex )
@@ -235,17 +223,16 @@ public async Task AddLegacyMetrics_True_CheckMetrics()
235
223
}
236
224
finally
237
225
{
238
- metricServer . Stop ( ) ;
226
+ _metricServer . Stop ( ) ;
239
227
}
240
228
}
241
229
242
230
[ Fact ]
243
231
public async Task Url_NotFound ( )
244
232
{
245
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port } ) ;
246
233
try
247
234
{
248
- metricServer . Start ( ) ;
235
+ _metricServer . Start ( ) ;
249
236
var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter" , "help" ) ;
250
237
counter . Inc ( ) ;
251
238
using var httpClient = new HttpClient ( ) ;
@@ -260,19 +247,18 @@ public async Task Url_NotFound()
260
247
}
261
248
finally
262
249
{
263
- metricServer . Stop ( ) ;
250
+ _metricServer . Stop ( ) ;
264
251
}
265
252
}
266
253
267
254
[ Fact ]
268
255
public async Task Find_Default_Metric ( )
269
256
{
270
- var registry = new CollectorRegistry ( ) ;
271
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = registry , UseDefaultCollectors = true } ) ;
257
+ _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = new CollectorRegistry ( ) , UseDefaultCollectors = true } ) ;
272
258
273
259
try
274
260
{
275
- metricServer . Start ( ) ;
261
+ _metricServer . Start ( ) ;
276
262
277
263
using var httpClient = new HttpClient ( ) ;
278
264
string response = await httpClient . GetStringAsync ( $ "http://localhost:{ _port } /metrics") ;
@@ -286,21 +272,23 @@ public async Task Find_Default_Metric()
286
272
}
287
273
finally
288
274
{
289
- metricServer . Stop ( ) ;
275
+ _metricServer . Stop ( ) ;
290
276
}
291
277
}
292
278
293
279
[ Fact ]
294
280
public async Task Add_Encoding ( )
295
281
{
296
- var metricServer = new MetricServer ( new MetricServerOptions { Port = _port , ResponseEncoding = Encoding . UTF8 } ) ;
282
+ var registry = new CollectorRegistry ( ) ;
283
+ var factory = new MetricFactory ( registry ) ;
284
+ _metricServer = new MetricServer ( new MetricServerOptions { Port = _port , CollectorRegistryInstance = registry , ResponseEncoding = Encoding . UTF8 } ) ;
297
285
298
286
try
299
287
{
300
- metricServer . Start ( ) ;
288
+ _metricServer . Start ( ) ;
301
289
302
290
const string help = "русский хелп" ;
303
- var counter = Metrics . DefaultFactory . CreateCounter ( "test_counter_rus" , help ) ;
291
+ var counter = factory . CreateCounter ( "test_counter_rus" , help ) ;
304
292
counter . Inc ( ) ;
305
293
306
294
using var httpClient = new HttpClient ( ) ;
@@ -314,7 +302,7 @@ public async Task Add_Encoding()
314
302
}
315
303
finally
316
304
{
317
- metricServer . Stop ( ) ;
305
+ _metricServer . Stop ( ) ;
318
306
}
319
307
}
320
308
0 commit comments