MCPcopy Index your code
hub / github.com/prometheus/client_python / test_collect

Method test_collect

tests/test_multiprocess.py:244–299  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

242 self.assertEqual(files(), ['counter_0.db', 'counter_1.db'])
243
244 def test_collect(self):
245 pid = 0
246 values.ValueClass = MultiProcessValue(lambda: pid)
247 labels = {i: i for i in 'abcd'}
248
249 def add_label(key, value):
250 l = labels.copy()
251 l[key] = value
252 return l
253
254 c = Counter('c', 'help', labelnames=labels.keys(), registry=None)
255 g = Gauge('g', 'help', labelnames=labels.keys(), registry=None)
256 h = Histogram('h', 'help', labelnames=labels.keys(), registry=None)
257
258 c.labels(**labels).inc(1)
259 g.labels(**labels).set(1)
260 h.labels(**labels).observe(1)
261
262 pid = 1
263
264 c.labels(**labels).inc(1)
265 g.labels(**labels).set(1)
266 h.labels(**labels).observe(5)
267
268 metrics = {m.name: m for m in self.collector.collect()}
269
270 self.assertEqual(
271 metrics['c'].samples, [Sample('c_total', labels, 2.0)]
272 )
273 metrics['g'].samples.sort(key=lambda x: x[1]['pid'])
274 self.assertEqual(metrics['g'].samples, [
275 Sample('g', add_label('pid', '0'), 1.0),
276 Sample('g', add_label('pid', '1'), 1.0),
277 ])
278
279 expected_histogram = [
280 Sample('h_sum', labels, 6.0),
281 Sample('h_bucket', add_label('le', '0.005'), 0.0),
282 Sample('h_bucket', add_label('le', '0.01'), 0.0),
283 Sample('h_bucket', add_label('le', '0.025'), 0.0),
284 Sample('h_bucket', add_label('le', '0.05'), 0.0),
285 Sample('h_bucket', add_label('le', '0.075'), 0.0),
286 Sample('h_bucket', add_label('le', '0.1'), 0.0),
287 Sample('h_bucket', add_label('le', '0.25'), 0.0),
288 Sample('h_bucket', add_label('le', '0.5'), 0.0),
289 Sample('h_bucket', add_label('le', '0.75'), 0.0),
290 Sample('h_bucket', add_label('le', '1.0'), 1.0),
291 Sample('h_bucket', add_label('le', '2.5'), 1.0),
292 Sample('h_bucket', add_label('le', '5.0'), 2.0),
293 Sample('h_bucket', add_label('le', '7.5'), 2.0),
294 Sample('h_bucket', add_label('le', '10.0'), 2.0),
295 Sample('h_bucket', add_label('le', '+Inf'), 2.0),
296 Sample('h_count', labels, 2.0),
297 ]
298
299 self.assertEqual(metrics['h'].samples, expected_histogram)
300
301 def test_collect_histogram_ordering(self):

Callers

nothing calls this directly

Calls 11

MultiProcessValueFunction · 0.90
CounterClass · 0.90
GaugeClass · 0.85
HistogramClass · 0.85
SampleClass · 0.85
keysMethod · 0.80
incMethod · 0.45
labelsMethod · 0.45
setMethod · 0.45
observeMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected