1
1
#[ cfg( test) ]
2
2
#[ path = "metrics_test.rs" ]
3
3
mod metrics_tests;
4
-
4
+ use std :: fmt :: Debug ;
5
5
use std:: str:: FromStr ;
6
6
7
7
use indexmap:: IndexMap ;
@@ -70,9 +70,24 @@ impl MetricCounter {
70
70
counter ! ( self . name) . increment ( value) ;
71
71
}
72
72
73
+ #[ cfg( any( feature = "testing" , test) ) ]
73
74
pub fn parse_numeric_metric < T : Num + FromStr > ( & self , metrics_as_string : & str ) -> Option < T > {
74
75
parse_numeric_metric :: < T > ( metrics_as_string, self . get_name ( ) , None )
75
76
}
77
+
78
+ #[ cfg( any( feature = "testing" , test) ) ]
79
+ pub fn assert_eq < T : Num + FromStr + Debug > ( & self , metrics_as_string : & str , expected_value : T ) {
80
+ let metric_value = self . parse_numeric_metric :: < T > ( metrics_as_string) . unwrap ( ) ;
81
+ assert_eq ! (
82
+ metric_value,
83
+ expected_value,
84
+ "Metric counter {} did not match the expected value. expected value: {:?}, metric \
85
+ value: {:?}",
86
+ self . get_name( ) ,
87
+ expected_value,
88
+ metric_value
89
+ ) ;
90
+ }
76
91
}
77
92
78
93
pub struct LabeledMetricCounter {
@@ -117,13 +132,34 @@ impl LabeledMetricCounter {
117
132
counter ! ( self . name, labels) . increment ( value) ;
118
133
}
119
134
135
+ #[ cfg( any( feature = "testing" , test) ) ]
120
136
pub fn parse_numeric_metric < T : Num + FromStr > (
121
137
& self ,
122
138
metrics_as_string : & str ,
123
139
labels : & [ ( & ' static str , & ' static str ) ] ,
124
140
) -> Option < T > {
125
141
parse_numeric_metric :: < T > ( metrics_as_string, self . get_name ( ) , Some ( labels) )
126
142
}
143
+
144
+ #[ cfg( any( feature = "testing" , test) ) ]
145
+ pub fn assert_eq < T : Num + FromStr + Debug > (
146
+ & self ,
147
+ metrics_as_string : & str ,
148
+ expected_value : T ,
149
+ label : & [ ( & ' static str , & ' static str ) ] ,
150
+ ) {
151
+ let metric_value = self . parse_numeric_metric :: < T > ( metrics_as_string, label) . unwrap ( ) ;
152
+ assert_eq ! (
153
+ metric_value,
154
+ expected_value,
155
+ "Metric counter {} {:?} did not match the expected value. expected value: {:?}, metric
156
+ value: {:?}" ,
157
+ self . get_name( ) ,
158
+ label,
159
+ expected_value,
160
+ metric_value
161
+ ) ;
162
+ }
127
163
}
128
164
129
165
pub struct MetricGauge {
@@ -162,6 +198,7 @@ impl MetricGauge {
162
198
gauge ! ( self . name) . decrement ( value. into_f64 ( ) ) ;
163
199
}
164
200
201
+ #[ cfg( any( feature = "testing" , test) ) ]
165
202
pub fn parse_numeric_metric < T : Num + FromStr > ( & self , metrics_as_string : & str ) -> Option < T > {
166
203
parse_numeric_metric :: < T > ( metrics_as_string, self . get_name ( ) , None )
167
204
}
@@ -173,6 +210,20 @@ impl MetricGauge {
173
210
pub fn set_lossy < T : LossyIntoF64 > ( & self , value : T ) {
174
211
gauge ! ( self . name) . set ( value. into_f64 ( ) ) ;
175
212
}
213
+
214
+ #[ cfg( any( feature = "testing" , test) ) ]
215
+ pub fn assert_eq < T : Num + FromStr + Debug > ( & self , metrics_as_string : & str , expected_value : T ) {
216
+ let metric_value = self . parse_numeric_metric :: < T > ( metrics_as_string) . unwrap ( ) ;
217
+ assert_eq ! (
218
+ metric_value,
219
+ expected_value,
220
+ "Metric gauge {} did not match the expected value. expected value: {:?}, metric
221
+ value: {:?}" ,
222
+ self . get_name( ) ,
223
+ expected_value,
224
+ metric_value
225
+ ) ;
226
+ }
176
227
}
177
228
178
229
/// An object which can be lossy converted into a `f64` representation.
@@ -244,6 +295,7 @@ impl LabeledMetricGauge {
244
295
gauge ! ( self . name, label) . decrement ( value. into_f64 ( ) ) ;
245
296
}
246
297
298
+ #[ cfg( any( feature = "testing" , test) ) ]
247
299
pub fn parse_numeric_metric < T : Num + FromStr > (
248
300
& self ,
249
301
metrics_as_string : & str ,
@@ -255,6 +307,26 @@ impl LabeledMetricGauge {
255
307
pub fn set < T : IntoF64 > ( & self , value : T , label : & [ ( & ' static str , & ' static str ) ] ) {
256
308
gauge ! ( self . name, label) . set ( value. into_f64 ( ) ) ;
257
309
}
310
+
311
+ #[ cfg( any( feature = "testing" , test) ) ]
312
+ pub fn assert_eq < T : Num + FromStr + Debug > (
313
+ & self ,
314
+ metrics_as_string : & str ,
315
+ expected_value : T ,
316
+ label : & [ ( & ' static str , & ' static str ) ] ,
317
+ ) {
318
+ let metric_value = self . parse_numeric_metric :: < T > ( metrics_as_string, label) . unwrap ( ) ;
319
+ assert_eq ! (
320
+ metric_value,
321
+ expected_value,
322
+ "Metric gauge {} {:?} did not match the expected value. expected value: {:?}, metric
323
+ value: {:?}" ,
324
+ self . get_name( ) ,
325
+ label,
326
+ expected_value,
327
+ metric_value
328
+ ) ;
329
+ }
258
330
}
259
331
260
332
pub struct MetricHistogram {
@@ -300,9 +372,24 @@ impl MetricHistogram {
300
372
histogram ! ( self . name) . record_many ( value. into_f64 ( ) , count) ;
301
373
}
302
374
375
+ #[ cfg( any( feature = "testing" , test) ) ]
303
376
pub fn parse_histogram_metric ( & self , metrics_as_string : & str ) -> Option < HistogramValue > {
304
377
parse_histogram_metric ( metrics_as_string, self . get_name ( ) , None )
305
378
}
379
+
380
+ // Only the sum and count values are compared, not the quantiles.
381
+ #[ cfg( any( feature = "testing" , test) ) ]
382
+ pub fn assert_eq ( & self , metrics_as_string : & str , expected_value : & HistogramValue ) {
383
+ let metric_value = self . parse_histogram_metric ( metrics_as_string) . unwrap ( ) ;
384
+ assert ! (
385
+ metric_value. sum == expected_value. sum && metric_value. count == expected_value. count,
386
+ "Metric histogram {} sum or count did not match the expected value. expected value: \
387
+ {:?}, metric value: {:?}",
388
+ self . get_name( ) ,
389
+ expected_value,
390
+ metric_value
391
+ ) ;
392
+ }
306
393
}
307
394
308
395
pub struct LabeledMetricHistogram {
@@ -354,13 +441,35 @@ impl LabeledMetricHistogram {
354
441
histogram ! ( self . name, labels) . record_many ( value. into_f64 ( ) , count) ;
355
442
}
356
443
444
+ #[ cfg( any( feature = "testing" , test) ) ]
357
445
pub fn parse_histogram_metric (
358
446
& self ,
359
447
metrics_as_string : & str ,
360
448
labels : & [ ( & ' static str , & ' static str ) ] ,
361
449
) -> Option < HistogramValue > {
362
450
parse_histogram_metric ( metrics_as_string, self . get_name ( ) , Some ( labels) )
363
451
}
452
+
453
+ // Only the sum and count values are compared, not the quantiles.
454
+ #[ cfg( any( feature = "testing" , test) ) ]
455
+ // TODO(tsabary): unite the labeled and unlabeld assert_eq functions.
456
+ pub fn assert_eq (
457
+ & self ,
458
+ metrics_as_string : & str ,
459
+ expected_value : & HistogramValue ,
460
+ label : & [ ( & ' static str , & ' static str ) ] ,
461
+ ) {
462
+ let metric_value = self . parse_histogram_metric ( metrics_as_string, label) . unwrap ( ) ;
463
+ assert ! (
464
+ metric_value. sum == expected_value. sum && metric_value. count == expected_value. count,
465
+ "Metric histogram {} {:?} sum or count did not match the expected value. expected \
466
+ value: {:?}, metric value: {:?}",
467
+ self . get_name( ) ,
468
+ label,
469
+ expected_value,
470
+ metric_value
471
+ ) ;
472
+ }
364
473
}
365
474
366
475
/// Parses a specific numeric metric value from a metrics string.
0 commit comments