| 1 |
import gzip |
| 2 |
from unittest import TestCase |
| 3 |
from wsgiref.util import setup_testing_defaults |
| 4 |
|
| 5 |
from prometheus_client import CollectorRegistry, Counter, make_wsgi_app |
| 6 |
from prometheus_client.exposition import _bake_output, CONTENT_TYPE_LATEST |
| 7 |
|
| 8 |
|
| 9 |
class WSGITest(TestCase): |
| 10 |
def setUp(self): |
| 11 |
self.registry = CollectorRegistry() |
| 12 |
self.captured_status = None |
| 13 |
self.captured_headers = None |
| 14 |
|
| 15 |
self.environ = {} |
| 16 |
setup_testing_defaults(self.environ) |
| 17 |
|
| 18 |
def capture(self, status, header): |
| 19 |
self.captured_status = status |
| 20 |
self.captured_headers = header |
| 21 |
|
| 22 |
def increment_metrics(self, metric_name, help_text, increments): |
| 23 |
c = Counter(metric_name, help_text, registry=self.registry) |
| 24 |
for _ in range(increments): |
| 25 |
c.inc() |
| 26 |
|
| 27 |
def assert_outputs(self, outputs, metric_name, help_text, increments, compressed): |
| 28 |
self.assertEqual(len(outputs), 1) |
| 29 |
if compressed: |
| 30 |
output = gzip.decompress(outputs[0]).decode(encoding="utf-8") |
| 31 |
else: |
| 32 |
output = outputs[0].decode('utf8') |
| 33 |
|
| 34 |
self.assertEqual(self.captured_status, "200 OK") |
| 35 |
|
| 36 |
num_of_headers = 2 if compressed else 1 |
| 37 |
self.assertEqual(len(self.captured_headers), num_of_headers) |
| 38 |
self.assertIn(("Content-Type", CONTENT_TYPE_LATEST), self.captured_headers) |
| 39 |
if compressed: |
| 40 |
self.assertIn(("Content-Encoding", "gzip"), self.captured_headers) |
| 41 |
|
| 42 |
self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output) |
| 43 |
self.assertIn("# TYPE " + metric_name + "_total counter\n", output) |
| 44 |
self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output) |
| 45 |
|
| 46 |
def validate_metrics(self, metric_name, help_text, increments): |
| 47 |
""" |
| 48 |
WSGI app serves the metrics from the provided registry. |
| 49 |
""" |
| 50 |
self.increment_metrics(metric_name, help_text, increments) |
| 51 |
|
| 52 |
app = make_wsgi_app(self.registry) |
| 53 |
outputs = app(self.environ, self.capture) |
| 54 |
|
| 55 |
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=False) |
| 56 |
|
| 57 |
def test_report_metrics_1(self): |
| 58 |
self.validate_metrics("counter", "A counter", 2) |
| 59 |
|
| 60 |
def test_report_metrics_2(self): |
| 61 |
self.validate_metrics("counter", "Another counter", 3) |
| 62 |
|
| 63 |
def test_report_metrics_3(self): |
| 64 |
self.validate_metrics("requests", "Number of requests", 5) |
| 65 |
|
| 66 |
def test_report_metrics_4(self): |
| 67 |
self.validate_metrics("failed_requests", "Number of failed requests", 7) |
| 68 |
|
| 69 |
def test_favicon_path(self): |
| 70 |
from unittest.mock import patch |
| 71 |
|
| 72 |
|
| 73 |
with patch("prometheus_client.exposition._bake_output", side_effect=_bake_output) as mock: |
| 74 |
|
| 75 |
app = make_wsgi_app(self.registry) |
| 76 |
|
| 77 |
favicon_environ = dict(self.environ) |
| 78 |
favicon_environ['PATH_INFO'] = '/favicon.ico' |
| 79 |
outputs = app(favicon_environ, self.capture) |
| 80 |
|
| 81 |
self.assertEqual(outputs, [b'']) |
| 82 |
self.assertEqual(mock.call_count, 0) |
| 83 |
|
| 84 |
app(self.environ, self.capture) |
| 85 |
self.assertEqual(mock.call_count, 1) |
| 86 |
|
| 87 |
def test_gzip(self): |
| 88 |
|
| 89 |
metric_name = "counter" |
| 90 |
help_text = "A counter" |
| 91 |
increments = 2 |
| 92 |
self.increment_metrics(metric_name, help_text, increments) |
| 93 |
app = make_wsgi_app(self.registry) |
| 94 |
|
| 95 |
gzip_environ = dict(self.environ) |
| 96 |
gzip_environ['HTTP_ACCEPT_ENCODING'] = 'gzip' |
| 97 |
outputs = app(gzip_environ, self.capture) |
| 98 |
|
| 99 |
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=True) |
| 100 |
|
| 101 |
def test_gzip_disabled(self): |
| 102 |
|
| 103 |
metric_name = "counter" |
| 104 |
help_text = "A counter" |
| 105 |
increments = 2 |
| 106 |
self.increment_metrics(metric_name, help_text, increments) |
| 107 |
|
| 108 |
app = make_wsgi_app(self.registry, disable_compression=True) |
| 109 |
|
| 110 |
gzip_environ = dict(self.environ) |
| 111 |
gzip_environ['HTTP_ACCEPT_ENCODING'] = 'gzip' |
| 112 |
outputs = app(gzip_environ, self.capture) |
| 113 |
|
| 114 |
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=False) |