19
19
import unittest
20
20
21
21
22
+ def assert_not_observable (fn , * args , ** kwargs ):
23
+ """
24
+ Assert that a function call falls with a ValueError exception containing
25
+ 'missing label values'
26
+ """
27
+
28
+ try :
29
+ fn (* args , ** kwargs )
30
+ except ValueError as e :
31
+ assert 'missing label values' in str (e )
32
+ return
33
+
34
+ assert False , "Did not raise a 'missing label values' exception"
35
+
36
+
22
37
class TestCounter (unittest .TestCase ):
23
38
def setUp (self ):
24
39
self .registry = CollectorRegistry ()
@@ -36,7 +51,6 @@ def test_repr(self):
36
51
37
52
def test_negative_increment_raises (self ):
38
53
self .assertRaises (ValueError , self .counter .inc , - 1 )
39
-
40
54
41
55
def test_function_decorator (self ):
42
56
@self .counter .count_exceptions (ValueError )
@@ -76,18 +90,21 @@ def test_block_decorator(self):
76
90
77
91
def test_count_exceptions_not_observable (self ):
78
92
counter = Counter ('counter' , 'help' , labelnames = ('label' ,), registry = self .registry )
93
+ assert_not_observable (counter .count_exceptions )
79
94
80
- try :
81
- counter .count_exceptions ()
82
- except ValueError as e :
83
- self .assertIn ('missing label values' , str (e ))
95
+ def test_inc_not_observable (self ):
96
+ """.inc() must fail if the counter is not observable."""
97
+
98
+ counter = Counter ('counter' , 'help' , labelnames = ('label' ,), registry = self .registry )
99
+ assert_not_observable (counter .inc )
84
100
85
101
86
102
class TestGauge (unittest .TestCase ):
87
103
def setUp (self ):
88
104
self .registry = CollectorRegistry ()
89
105
self .gauge = Gauge ('g' , 'help' , registry = self .registry )
90
-
106
+ self .gauge_with_label <
9E88
span class=pl-c1>= Gauge ('g2' , 'help' , labelnames = ("label1" ,), registry = self .registry )
107
+
91
108
def test_repr (self ):
92
109
self .assertEqual (repr (self .gauge ), "prometheus_client.metrics.Gauge(g)" )
93
110
@@ -100,6 +117,21 @@ def test_gauge(self):
100
117
self .gauge .set (9 )
101
118
self .assertEqual (9 , self .registry .get_sample_value ('g' ))
102
119
120
+ def test_inc_not_observable (self ):
121
+ """.inc() must fail if the gauge is not observable."""
122
+
123
+ assert_not_observable (self .gauge_with_label .inc )
124
+
125
+ def test_dec_not_observable (self ):
126
+ """.dec() must fail if the gauge is not observable."""
127
+
128
+ assert_not_observable (self .gauge_with_label .dec )
129
+
130
+ def test_set_not_observable (self ):
131
+ """.set() must fail if the gauge is not observable."""
132
+
133
+ assert_not_observable (self .gauge_with_label .set , 1 )
134
+
103
135
def test_inprogress_function_decorator (self ):
104
136
self .assertEqual (0 , self .registry .get_sample_value ('g' ))
105
137
@@ -127,6 +159,11 @@ def test_gauge_function(self):
127
159
x ['a' ] = None
128
160
self .assertEqual (1 , self .registry .get_sample_value ('g' ))
129
161
162
+ def test_set_function_not_observable (self ):
163
+ """.set_function() must fail if the gauge is not observable."""
164
+
165
+ assert_not_observable (self .gauge_with_label .set_function , lambda : 1 )
166
+
130
167
def test_time_function_decorator (self ):
131
168
self .assertEqual (0 , self .registry .get_sample_value ('g' ))
132
169
@@ -167,25 +204,18 @@ def test_time_block_decorator(self):
167
204
168
205
def test_track_in_progress_not_observable (self ):
169
206
g = Gauge ('test' , 'help' , labelnames = ('label' ,), registry = self .registry )
170
-
171
- try :
172
- g .track_inprogress ()
173
- except ValueError as e :
174
- self .assertIn ('missing label values' , str (e ))
207
+ assert_not_observable (g .track_inprogress )
175
208
176
209
def test_timer_not_observable (self ):
177
210
g = Gauge ('test' , 'help' , labelnames = ('label' ,), registry = self .registry )
178
-
179
- try :
180
- g .time ()
181
- except ValueError as e :
182
- self .assertIn ('missing label values' , str (e ))
211
+ assert_not_observable (g .time )
183
212
184
213
185
214
class TestSummary (unittest .TestCase ):
186
215
def setUp (self ):
187
216
self .registry = CollectorRegistry ()
188
217
self .summary = Summary ('s' , 'help' , registry = self .registry )
218
+ self .summary_with_labels = Summary ('s_with_labels' , 'help' , labelnames = ("label1" ,), registry = self .registry )
189
219
190
220
def test_repr (self ):
191
221
self .assertEqual (repr (self .summary ), "prometheus_client.metrics.Summary(s)" )
@@ -197,6 +227,10 @@ def test_summary(self):
197
227
self .assertEqual (1 , self .registry .get_sample_value ('s_count' ))
198
228
self .assertEqual (10 , self .registry .get_sample_value ('s_sum' ))
199
229
230
+ def test_summary_not_observable (self ):
231
+ """.observe() must fail if the Summary is not observable."""
232
+ assert_not_observable (self .summary_with_labels .observe , 1 )
233
+
200
234
def test_function_decorator (self ):
201
235
self .assertEqual (0 , self .registry .get_sample_value ('s_count' ))
202
236
@@ -267,10 +301,7 @@ def test_block_decorator(self):
267
301
def test_timer_not_observable (self ):
268
302
s = Summary ('test' , 'help' , labelnames = ('label' ,), registry = self .registry )
269
303
270
- try :
271
- s .time ()
272
- except ValueError as e :
273
- self .assertIn ('missing label values' , str (e ))
304
+ assert_not_observable (s .time )
274
305
275
306
276
307
class TestHistogram (unittest .TestCase ):
@@ -315,6 +346,10 @@ def test_histogram(self):
315
346
self .assertEqual (3 , self .registry .get_sample_value ('h_count' ))
316
347
self .assertEqual (float ("inf" ), self .registry .get_sample_value ('h_sum' ))
317
348
349
+ def test_histogram_not_observable (self ):
350
+ """.observe() must fail if the Summary is not observable."""
351
+ assert_not_observable (self .labels .observe , 1 )
352
+
318
353
def test_setting_buckets (self ):
319
354
h = Histogram ('h' , 'help' , registry = None , buckets = [0 , 1 , 2 ])
320
355
self .assertEqual ([0.0 , 1.0 , 2.0 , float ("inf" )], h ._upper_bounds )
0 commit comments