forked from prometheus/client_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exposition.py
More file actions
779 lines (666 loc) · 31.9 KB
/
test_exposition.py
File metadata and controls
779 lines (666 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
import gzip
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
import threading
import time
import unittest
import pytest
from prometheus_client import (
CollectorRegistry, CONTENT_TYPE_LATEST, CONTENT_TYPE_PLAIN_0_0_4,
CONTENT_TYPE_PLAIN_1_0_0, core, Counter, delete_from_gateway, Enum, Gauge,
generate_latest, Histogram, Info, instance_ip_grouping_key, Metric,
push_to_gateway, pushadd_to_gateway, Summary,
)
from prometheus_client.core import GaugeHistogramMetricFamily, Timestamp
from prometheus_client.exposition import (
basic_auth_handler, choose_encoder, default_handler, MetricsHandler,
passthrough_redirect_handler, tls_auth_handler,
)
import prometheus_client.openmetrics.exposition as openmetrics
class TestGenerateText(unittest.TestCase):
def setUp(self):
self.registry = CollectorRegistry()
# Mock time so _created values are fixed.
self.old_time = time.time
time.time = lambda: 123.456
def tearDown(self):
time.time = self.old_time
def custom_collector(self, metric_family):
class CustomCollector:
def collect(self):
return [metric_family]
self.registry.register(CustomCollector())
def test_counter(self):
c = Counter('cc', 'A counter', registry=self.registry)
c.inc()
self.assertEqual(b"""# HELP cc_total A counter
# TYPE cc_total counter
cc_total 1.0
# HELP cc_created A counter
# TYPE cc_created gauge
cc_created 123.456
""", generate_latest(self.registry, openmetrics.ALLOWUTF8))
def test_counter_utf8(self):
c = Counter('utf8.cc', 'A counter', registry=self.registry)
c.inc()
self.assertEqual(b"""# HELP "utf8.cc_total" A counter
# TYPE "utf8.cc_total" counter
{"utf8.cc_total"} 1.0
# HELP "utf8.cc_created" A counter
# TYPE "utf8.cc_created" gauge
{"utf8.cc_created"} 123.456
""", generate_latest(self.registry, openmetrics.ALLOWUTF8))
def test_counter_utf8_escaped_underscores(self):
c = Counter('utf8.cc', 'A counter', registry=self.registry)
c.inc()
assert b"""# HELP utf8_cc_total A counter
# TYPE utf8_cc_total counter
utf8_cc_total 1.0
# HELP utf8_cc_created A counter
# TYPE utf8_cc_created gauge
utf8_cc_created 123.456
""" == generate_latest(self.registry, openmetrics.UNDERSCORES)
def test_counter_name_unit_append(self):
c = Counter('requests', 'Request counter', unit="total", registry=self.registry)
c.inc()
self.assertEqual(b"""# HELP requests_total_total Request counter
# TYPE requests_total_total counter
requests_total_total 1.0
# HELP requests_total_created Request counter
# TYPE requests_total_created gauge
requests_total_created 123.456
""", generate_latest(self.registry))
def test_counter_total(self):
c = Counter('cc_total', 'A counter', registry=self.registry)
c.inc()
self.assertEqual(b"""# HELP cc_total A counter
# TYPE cc_total counter
cc_total 1.0
# HELP cc_created A counter
# TYPE cc_created gauge
cc_created 123.456
""", generate_latest(self.registry))
def test_gauge(self):
g = Gauge('gg', 'A gauge', registry=self.registry)
g.set(17)
self.assertEqual(b'# HELP gg A gauge\n# TYPE gg gauge\ngg 17.0\n', generate_latest(self.registry))
def test_summary(self):
s = Summary('ss', 'A summary', ['a', 'b'], registry=self.registry)
s.labels('c', 'd').observe(17)
self.assertEqual(b"""# HELP ss A summary
# TYPE ss summary
ss_count{a="c",b="d"} 1.0
ss_sum{a="c",b="d"} 17.0
# HELP ss_created A summary
# TYPE ss_created gauge
ss_created{a="c",b="d"} 123.456
""", generate_latest(self.registry))
def test_histogram(self):
s = Histogram('hh', 'A histogram', registry=self.registry)
s.observe(0.05)
self.assertEqual(b"""# HELP hh A histogram
# TYPE hh histogram
hh_bucket{le="0.005"} 0.0
hh_bucket{le="0.01"} 0.0
hh_bucket{le="0.025"} 0.0
hh_bucket{le="0.05"} 1.0
hh_bucket{le="0.075"} 1.0
hh_bucket{le="0.1"} 1.0
hh_bucket{le="0.25"} 1.0
hh_bucket{le="0.5"} 1.0
hh_bucket{le="0.75"} 1.0
hh_bucket{le="1.0"} 1.0
hh_bucket{le="2.5"} 1.0
hh_bucket{le="5.0"} 1.0
hh_bucket{le="7.5"} 1.0
hh_bucket{le="10.0"} 1.0
hh_bucket{le="+Inf"} 1.0
hh_count 1.0
hh_sum 0.05
# HELP hh_created A histogram
# TYPE hh_created gauge
hh_created 123.456
""", generate_latest(self.registry))
def test_gaugehistogram(self):
self.custom_collector(GaugeHistogramMetricFamily('gh', 'help', buckets=[('1.0', 4), ('+Inf', 5)], gsum_value=7))
self.assertEqual(b"""# HELP gh help
# TYPE gh histogram
gh_bucket{le="1.0"} 4.0
gh_bucket{le="+Inf"} 5.0
# HELP gh_gcount help
# TYPE gh_gcount gauge
gh_gcount 5.0
# HELP gh_gsum help
# TYPE gh_gsum gauge
gh_gsum 7.0
""", generate_latest(self.registry))
def test_info(self):
i = Info('ii', 'A info', ['a', 'b'], registry=self.registry)
i.labels('c', 'd').info({'foo': 'bar'})
self.assertEqual(b'# HELP ii_info A info\n# TYPE ii_info gauge\nii_info{a="c",b="d",foo="bar"} 1.0\n',
generate_latest(self.registry))
def test_enum(self):
i = Enum('ee', 'An enum', ['a', 'b'], registry=self.registry, states=['foo', 'bar'])
i.labels('c', 'd').state('bar')
self.assertEqual(
b'# HELP ee An enum\n# TYPE ee gauge\nee{a="c",b="d",ee="foo"} 0.0\nee{a="c",b="d",ee="bar"} 1.0\n',
generate_latest(self.registry))
def test_unicode(self):
c = Gauge('cc', '\u4500', ['l'], registry=self.registry)
c.labels('\u4500').inc()
self.assertEqual(b'# HELP cc \xe4\x94\x80\n# TYPE cc gauge\ncc{l="\xe4\x94\x80"} 1.0\n',
generate_latest(self.registry))
def test_escaping(self):
g = Gauge('cc', 'A\ngaug\\e', ['a'], registry=self.registry)
g.labels('\\x\n"').inc(1)
self.assertEqual(b'# HELP cc A\\ngaug\\\\e\n# TYPE cc gauge\ncc{a="\\\\x\\n\\""} 1.0\n',
generate_latest(self.registry))
def test_nonnumber(self):
class MyNumber:
def __repr__(self):
return "MyNumber(123)"
def __float__(self):
return 123.0
class MyCollector:
def collect(self):
metric = Metric("nonnumber", "Non number", 'untyped')
metric.add_sample("nonnumber", {}, MyNumber())
yield metric
self.registry.register(MyCollector())
self.assertEqual(b'# HELP nonnumber Non number\n# TYPE nonnumber untyped\nnonnumber 123.0\n',
generate_latest(self.registry))
def test_timestamp(self):
class MyCollector:
def collect(self):
metric = Metric("ts", "help", 'untyped')
metric.add_sample("ts", {"foo": "a"}, 0, 123.456)
metric.add_sample("ts", {"foo": "b"}, 0, -123.456)
metric.add_sample("ts", {"foo": "c"}, 0, 123)
metric.add_sample("ts", {"foo": "d"}, 0, Timestamp(123, 456000000))
metric.add_sample("ts", {"foo": "e"}, 0, Timestamp(123, 456000))
metric.add_sample("ts", {"foo": "f"}, 0, Timestamp(123, 456))
yield metric
self.registry.register(MyCollector())
self.assertEqual(b"""# HELP ts help
# TYPE ts untyped
ts{foo="a"} 0.0 123456
ts{foo="b"} 0.0 -123456
ts{foo="c"} 0.0 123000
ts{foo="d"} 0.0 123456
ts{foo="e"} 0.0 123000
ts{foo="f"} 0.0 123000
""", generate_latest(self.registry))
class TestPushGateway(unittest.TestCase):
def setUp(self):
redirect_flag = 'testFlag'
self.redirect_flag = redirect_flag # preserve a copy for downstream test assertions
self.registry = CollectorRegistry()
self.counter = Gauge('g', 'help', registry=self.registry)
self.requests = requests = []
class TestHandler(BaseHTTPRequestHandler):
def do_PUT(self):
if 'with_basic_auth' in self.requestline and self.headers['authorization'] != 'Basic Zm9vOmJhcg==':
self.send_response(401)
elif 'redirect' in self.requestline and redirect_flag not in self.requestline:
# checks for an initial test request with 'redirect' but without the redirect_flag,
# and simulates a redirect to a url with the redirect_flag (which will produce a 201)
self.send_response(301)
self.send_header('Location', getattr(self, 'redirect_address', None))
else:
self.send_response(201)
length = int(self.headers['content-length'])
requests.append((self, self.rfile.read(length)))
self.end_headers()
do_POST = do_PUT
do_DELETE = do_PUT
# set up a separate server to serve a fake redirected request.
# the redirected URL will have `redirect_flag` added to it,
# which will cause the request handler to return 201.
httpd_redirect = HTTPServer(('localhost', 0), TestHandler)
self.redirect_address = TestHandler.redirect_address = \
f'http://localhost:{httpd_redirect.server_address[1]}/{redirect_flag}'
class TestRedirectServer(threading.Thread):
def run(self):
httpd_redirect.handle_request()
self.redirect_server = TestRedirectServer()
self.redirect_server.daemon = True
self.redirect_server.start()
# set up the normal server to serve the example requests across test cases.
httpd = HTTPServer(('localhost', 0), TestHandler)
self.address = f'http://localhost:{httpd.server_address[1]}'
class TestServer(threading.Thread):
def run(self):
httpd.handle_request()
self.server = TestServer()
self.server.daemon = True
self.server.start()
def test_push(self):
push_to_gateway(self.address, "my_job", self.registry)
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_schemeless_url(self):
push_to_gateway(self.address.replace('http://', ''), "my_job", self.registry)
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_groupingkey(self):
push_to_gateway(self.address, "my_job", self.registry, {'a': 9})
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/a/9')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_groupingkey_empty_label(self):
push_to_gateway(self.address, "my_job", self.registry, {'a': ''})
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/a@base64/=')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_groupingkey_with_spaces(self):
push_to_gateway(self.address, "my_job", self.registry, {'label': 'value with spaces'})
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/label@base64/dmFsdWUgd2l0aCBzcGFjZXM=')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_complex_groupingkey(self):
push_to_gateway(self.address, "my_job", self.registry, {'a': 9, 'b': 'a/ z'})
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/a/9/b@base64/YS8geg==')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_complex_job(self):
push_to_gateway(self.address, "my/job", self.registry)
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job@base64/bXkvam9i')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_pushadd(self):
pushadd_to_gateway(self.address, "my_job", self.registry)
self.assertEqual(self.requests[0][0].command, 'POST')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_pushadd_with_groupingkey(self):
pushadd_to_gateway(self.address, "my_job", self.registry, {'a': 9})
self.assertEqual(self.requests[0][0].command, 'POST')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/a/9')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_delete(self):
delete_from_gateway(self.address, "my_job")
self.assertEqual(self.requests[0][0].command, 'DELETE')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'')
def test_delete_with_groupingkey(self):
delete_from_gateway(self.address, "my_job", {'a': 9})
self.assertEqual(self.requests[0][0].command, 'DELETE')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/a/9')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'')
def test_push_with_handler(self):
def my_test_handler(url, method, timeout, headers, data):
headers.append(['X-Test-Header', 'foobar'])
# Handler should be passed sane default timeout
self.assertEqual(timeout, 30)
return default_handler(url, method, timeout, headers, data)
push_to_gateway(self.address, "my_job", self.registry, handler=my_test_handler)
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][0].headers.get('x-test-header'), 'foobar')
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_basic_auth_handler(self):
def my_auth_handler(url, method, timeout, headers, data):
return basic_auth_handler(url, method, timeout, headers, data, "foo", "bar")
push_to_gateway(self.address, "my_job_with_basic_auth", self.registry, handler=my_auth_handler)
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job_with_basic_auth')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_tls_auth_handler(self):
def my_auth_handler(url, method, timeout, headers, data):
certs_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'certs')
return tls_auth_handler(url, method, timeout, headers, data, os.path.join(certs_dir, "cert.pem"), os.path.join(certs_dir, "key.pem"))
push_to_gateway(self.address, "my_job_with_tls_auth", self.registry, handler=my_auth_handler)
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job_with_tls_auth')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_redirect_handler(self):
def my_redirect_handler(url, method, timeout, headers, data):
return passthrough_redirect_handler(url, method, timeout, headers, data)
push_to_gateway(self.address, "my_job_with_redirect", self.registry, handler=my_redirect_handler)
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job_with_redirect')
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
# ensure the redirect preserved request settings from the initial request.
self.assertEqual(self.requests[0][0].command, self.requests[1][0].command)
self.assertEqual(
self.requests[0][0].headers.get('content-type'),
self.requests[1][0].headers.get('content-type')
)
self.assertEqual(self.requests[0][1], self.requests[1][1])
# ensure the redirect took place at the expected redirect location.
self.assertEqual(self.requests[1][0].path, "/" + self.redirect_flag)
def test_push_with_trailing_slash(self):
address = self.address + '/'
push_to_gateway(address, "my_job_with_trailing_slash", self.registry)
self.assertNotIn('//', self.requests[0][0].path)
def test_push_with_gzip_compression(self):
push_to_gateway(self.address, "my_job", self.registry, compression='gzip')
request, body = self.requests[0]
self.assertEqual(request.headers.get('content-encoding'), 'gzip')
decompressed = gzip.decompress(body)
self.assertEqual(decompressed, b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_snappy_compression(self):
snappy = pytest.importorskip('snappy')
push_to_gateway(self.address, "my_job", self.registry, compression='snappy')
request, body = self.requests[0]
self.assertEqual(request.headers.get('content-encoding'), 'snappy')
decompressor = snappy.StreamDecompressor()
decompressed = decompressor.decompress(body)
flush = getattr(decompressor, 'flush', None)
if callable(flush):
decompressed += flush()
self.assertEqual(decompressed, b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
def test_push_with_invalid_compression(self):
with self.assertRaisesRegex(ValueError, 'Unsupported compression type'):
push_to_gateway(self.address, "my_job", self.registry, compression='brotli')
self.assertEqual(self.requests, [])
def test_instance_ip_grouping_key(self):
self.assertTrue('' != instance_ip_grouping_key()['instance'])
def test_metrics_handler(self):
handler = MetricsHandler.factory(self.registry)
self.assertEqual(handler.registry, self.registry)
def test_metrics_handler_subclassing(self):
subclass = type('MetricsHandlerSubclass', (MetricsHandler, object), {})
handler = subclass.factory(self.registry)
self.assertTrue(issubclass(handler, (MetricsHandler, subclass)))
@pytest.fixture
def registry():
return core.CollectorRegistry()
class Collector:
def __init__(self, metric_family, *values):
self.metric_family = metric_family
self.values = values
def collect(self):
self.metric_family.add_metric([], *self.values)
return [self.metric_family]
def _expect_metric_exception(registry, expected_error):
try:
generate_latest(registry, openmetrics.ALLOWUTF8)
except expected_error as exception:
assert isinstance(exception.args[-1], core.Metric)
# Got a valid error as expected, return quietly
return
raise RuntimeError('Expected exception not raised')
@pytest.mark.parametrize('MetricFamily', [
core.CounterMetricFamily,
core.GaugeMetricFamily,
])
@pytest.mark.parametrize('value,error', [
(None, TypeError),
('', ValueError),
('x', ValueError),
([], TypeError),
({}, TypeError),
])
def test_basic_metric_families(registry, MetricFamily, value, error):
metric_family = MetricFamily(MetricFamily.__name__, 'help')
registry.register(Collector(metric_family, value))
_expect_metric_exception(registry, error)
@pytest.mark.parametrize('count_value,sum_value,error', [
(None, 0, TypeError),
(0, None, TypeError),
('', 0, ValueError),
(0, '', ValueError),
([], 0, TypeError),
(0, [], TypeError),
({}, 0, TypeError),
(0, {}, TypeError),
])
def test_summary_metric_family(registry, count_value, sum_value, error):
metric_family = core.SummaryMetricFamily('summary', 'help')
registry.register(Collector(metric_family, count_value, sum_value))
_expect_metric_exception(registry, error)
@pytest.mark.parametrize('MetricFamily', [
core.GaugeHistogramMetricFamily,
])
@pytest.mark.parametrize('buckets,sum_value,error', [
([('spam', 0), ('eggs', 0)], None, TypeError),
([('spam', 0), ('eggs', None)], 0, TypeError),
([('spam', 0), (None, 0)], 0, AttributeError),
([('spam', None), ('eggs', 0)], 0, TypeError),
([(None, 0), ('eggs', 0)], 0, AttributeError),
([('spam', 0), ('eggs', 0)], '', ValueError),
([('spam', 0), ('eggs', '')], 0, ValueError),
([('spam', ''), ('eggs', 0)], 0, ValueError),
])
def test_histogram_metric_families(MetricFamily, registry, buckets, sum_value, error):
metric_family = MetricFamily(MetricFamily.__name__, 'help')
registry.register(Collector(metric_family, buckets, sum_value))
_expect_metric_exception(registry, error)
class TestChooseEncoder(unittest.TestCase):
def setUp(self):
self.registry = CollectorRegistry()
c = Counter('dotted.counter', 'A counter', registry=self.registry)
c.inc()
def custom_collector(self, metric_family):
class CustomCollector:
def collect(self):
return [metric_family]
self.registry.register(CustomCollector())
def assert_is_escaped(self, exp):
self.assertRegex(exp, r'.*\ndotted_counter_total 1.0\n.*')
def assert_is_utf8(self, exp):
self.assertRegex(exp, r'.*\n{"dotted.counter_total"} 1.0\n.*')
def assert_is_prom(self, exp):
self.assertNotRegex(exp, r'# EOF')
def assert_is_openmetrics(self, exp):
self.assertRegex(exp, r'# EOF')
def test_default_encoder(self):
generator, content_type = choose_encoder(None)
assert content_type == CONTENT_TYPE_PLAIN_0_0_4
exp = generator(self.registry).decode('utf-8')
self.assert_is_escaped(exp)
self.assert_is_prom(exp)
def test_plain_encoder(self):
generator, content_type = choose_encoder(CONTENT_TYPE_PLAIN_0_0_4)
assert content_type == CONTENT_TYPE_PLAIN_0_0_4
exp = generator(self.registry).decode('utf-8')
self.assert_is_escaped(exp)
self.assert_is_prom(exp)
def test_openmetrics_latest(self):
generator, content_type = choose_encoder(openmetrics.CONTENT_TYPE_LATEST)
assert content_type == 'application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=underscores'
exp = generator(self.registry).decode('utf-8')
self.assert_is_escaped(exp)
self.assert_is_openmetrics(exp)
def test_openmetrics_utf8(self):
generator, content_type = choose_encoder(openmetrics.CONTENT_TYPE_LATEST + '; escaping=allow-utf-8')
assert content_type == openmetrics.CONTENT_TYPE_LATEST + '; escaping=allow-utf-8'
exp = generator(self.registry).decode('utf-8')
self.assert_is_utf8(exp)
self.assert_is_openmetrics(exp)
def test_openmetrics_dots_escaping(self):
generator, content_type = choose_encoder(openmetrics.CONTENT_TYPE_LATEST + '; escaping=dots')
assert content_type == openmetrics.CONTENT_TYPE_LATEST + '; escaping=dots'
exp = generator(self.registry).decode('utf-8')
self.assertRegex(exp, r'.*\ndotted_dot_counter__total 1.0\n.*')
self.assert_is_openmetrics(exp)
def test_prom_latest(self):
generator, content_type = choose_encoder(CONTENT_TYPE_LATEST)
assert content_type == CONTENT_TYPE_PLAIN_1_0_0 + '; escaping=underscores'
exp = generator(self.registry).decode('utf-8')
self.assert_is_escaped(exp)
self.assert_is_prom(exp)
def test_prom_plain_1_0_0(self):
generator, content_type = choose_encoder(CONTENT_TYPE_PLAIN_1_0_0)
assert content_type == CONTENT_TYPE_PLAIN_1_0_0 + '; escaping=underscores'
exp = generator(self.registry).decode('utf-8')
self.assert_is_escaped(exp)
self.assert_is_prom(exp)
def test_prom_utf8(self):
generator, content_type = choose_encoder(CONTENT_TYPE_PLAIN_1_0_0 + '; escaping=allow-utf-8')
assert content_type == CONTENT_TYPE_PLAIN_1_0_0 + '; escaping=allow-utf-8'
exp = generator(self.registry).decode('utf-8')
self.assert_is_utf8(exp)
self.assert_is_prom(exp)
def test_prom_dots_escaping(self):
generator, content_type = choose_encoder(CONTENT_TYPE_PLAIN_1_0_0 + '; escaping=dots')
assert content_type == CONTENT_TYPE_PLAIN_1_0_0 + '; escaping=dots'
exp = generator(self.registry).decode('utf-8')
self.assertRegex(exp, r'.*\ndotted_dot_counter__total 1.0\n.*')
self.assert_is_prom(exp)
def test_openmetrics_no_version(self):
generator, content_type = choose_encoder('application/openmetrics-text; charset=utf-8; escaping=allow-utf-8')
assert content_type == 'application/openmetrics-text; version=1.0.0; charset=utf-8'
exp = generator(self.registry).decode('utf-8')
# No version -- allow-utf-8 rejected.
self.assert_is_escaped(exp)
self.assert_is_openmetrics(exp)
def test_prom_no_version(self):
generator, content_type = choose_encoder('text/plain; charset=utf-8; escaping=allow-utf-8')
assert content_type == 'text/plain; version=0.0.4; charset=utf-8'
exp = generator(self.registry).decode('utf-8')
# No version -- allow-utf-8 rejected.
self.assert_is_escaped(exp)
self.assert_is_prom(exp)
@pytest.mark.parametrize("scenario", [
{
"name": "empty string",
"input": "",
"expectedUnderscores": "",
"expectedDots": "",
"expectedValue": "",
},
{
"name": "legacy valid metric name",
"input": "no:escaping_required",
"expectedUnderscores": "no:escaping_required",
"expectedDots": "no:escaping__required",
"expectedValue": "no:escaping_required",
},
{
"name": "metric name with dots",
"input": "mysystem.prod.west.cpu.load",
"expectedUnderscores": "mysystem_prod_west_cpu_load",
"expectedDots": "mysystem_dot_prod_dot_west_dot_cpu_dot_load",
"expectedValue": "U__mysystem_2e_prod_2e_west_2e_cpu_2e_load",
},
{
"name": "metric name with dots and underscore",
"input": "mysystem.prod.west.cpu.load_total",
"expectedUnderscores": "mysystem_prod_west_cpu_load_total",
"expectedDots": "mysystem_dot_prod_dot_west_dot_cpu_dot_load__total",
"expectedValue": "U__mysystem_2e_prod_2e_west_2e_cpu_2e_load__total",
},
{
"name": "metric name with dots and colon",
"input": "http.status:sum",
"expectedUnderscores": "http_status:sum",
"expectedDots": "http_dot_status:sum",
"expectedValue": "U__http_2e_status:sum",
},
{
"name": "metric name with spaces and emoji",
"input": "label with 😱",
"expectedUnderscores": "label_with__",
"expectedDots": "label__with____",
"expectedValue": "U__label_20_with_20__1f631_",
},
{
"name": "metric name with unicode characters > 0x100",
"input": "花火",
"expectedUnderscores": "__",
"expectedDots": "____",
"expectedValue": "U___82b1__706b_",
},
{
"name": "metric name with spaces and edge-case value",
"input": "label with \u0100",
"expectedUnderscores": "label_with__",
"expectedDots": "label__with____",
"expectedValue": "U__label_20_with_20__100_",
},
])
def test_escape_metric_name(scenario):
input = scenario["input"]
got = openmetrics.escape_metric_name(input, openmetrics.UNDERSCORES)
assert got == scenario["expectedUnderscores"], f"[{scenario['name']}] Underscore escaping failed"
got = openmetrics.escape_metric_name(input, openmetrics.DOTS)
assert got == scenario["expectedDots"], f"[{scenario['name']}] Dots escaping failed"
got = openmetrics.escape_metric_name(input, openmetrics.VALUES)
assert got == scenario["expectedValue"], f"[{scenario['name']}] Value encoding failed"
@pytest.mark.parametrize("scenario", [
{
"name": "empty string",
"input": "",
"expectedUnderscores": "",
"expectedDots": "",
"expectedValue": "",
},
{
"name": "legacy valid label name",
"input": "no_escaping_required",
"expectedUnderscores": "no_escaping_required",
"expectedDots": "no__escaping__required",
"expectedValue": "no_escaping_required",
},
{
"name": "label name with dots",
"input": "mysystem.prod.west.cpu.load",
"expectedUnderscores": "mysystem_prod_west_cpu_load",
"expectedDots": "mysystem_dot_prod_dot_west_dot_cpu_dot_load",
"expectedValue": "U__mysystem_2e_prod_2e_west_2e_cpu_2e_load",
},
{
"name": "label name with dots and underscore",
"input": "mysystem.prod.west.cpu.load_total",
"expectedUnderscores": "mysystem_prod_west_cpu_load_total",
"expectedDots": "mysystem_dot_prod_dot_west_dot_cpu_dot_load__total",
"expectedValue": "U__mysystem_2e_prod_2e_west_2e_cpu_2e_load__total",
},
{
"name": "label name with dots and colon",
"input": "http.status:sum",
"expectedUnderscores": "http_status_sum",
"expectedDots": "http_dot_status__sum",
"expectedValue": "U__http_2e_status_3a_sum",
},
{
"name": "label name with spaces and emoji",
"input": "label with 😱",
"expectedUnderscores": "label_with__",
"expectedDots": "label__with____",
"expectedValue": "U__label_20_with_20__1f631_",
},
{
"name": "label name with unicode characters > 0x100",
"input": "花火",
"expectedUnderscores": "__",
"expectedDots": "____",
"expectedValue": "U___82b1__706b_",
},
{
"name": "label name with spaces and edge-case value",
"input": "label with \u0100",
"expectedUnderscores": "label_with__",
"expectedDots": "label__with____",
"expectedValue": "U__label_20_with_20__100_",
},
])
def test_escape_label_name(scenario):
input = scenario["input"]
got = openmetrics.escape_label_name(input, openmetrics.UNDERSCORES)
assert got == scenario["expectedUnderscores"], f"[{scenario['name']}] Underscore escaping failed"
got = openmetrics.escape_label_name(input, openmetrics.DOTS)
assert got == scenario["expectedDots"], f"[{scenario['name']}] Dots escaping failed"
got = openmetrics.escape_label_name(input, openmetrics.VALUES)
assert got == scenario["expectedValue"], f"[{scenario['name']}] Value encoding failed"
if __name__ == '__main__':
unittest.main()