| 1 |
import gzip |
| 2 |
from unittest import skipUnless, TestCase |
| 3 |
|
| 4 |
from prometheus_client import CollectorRegistry, Counter |
| 5 |
from prometheus_client.exposition import CONTENT_TYPE_LATEST |
| 6 |
|
| 7 |
try: |
| 8 |
|
| 9 |
import asyncio |
| 10 |
|
| 11 |
from asgiref.testing import ApplicationCommunicator |
| 12 |
|
| 13 |
from prometheus_client import make_asgi_app |
| 14 |
HAVE_ASYNCIO_AND_ASGI = True |
| 15 |
except ImportError: |
| 16 |
HAVE_ASYNCIO_AND_ASGI = False |
| 17 |
|
| 18 |
|
| 19 |
def setup_testing_defaults(scope): |
| 20 |
scope.update( |
| 21 |
{ |
| 22 |
"client": ("127.0.0.1", 32767), |
| 23 |
"headers": [], |
| 24 |
"http_version": "1.0", |
| 25 |
"method": "GET", |
| 26 |
"path": "/", |
| 27 |
"query_string": b"", |
| 28 |
"scheme": "http", |
| 29 |
"server": ("127.0.0.1", 80), |
| 30 |
"type": "http", |
| 31 |
} |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
class ASGITest(TestCase): |
| 36 |
@skipUnless(HAVE_ASYNCIO_AND_ASGI, "Don't have asyncio/asgi installed.") |
| 37 |
def setUp(self): |
| 38 |
self.registry = CollectorRegistry() |
| 39 |
self.captured_status = None |
| 40 |
self.captured_headers = None |
| 41 |
|
| 42 |
self.scope = {} |
| 43 |
setup_testing_defaults(self.scope) |
| 44 |
self.communicator = None |
| 45 |
|
| 46 |
def tearDown(self): |
| 47 |
if self.communicator: |
| 48 |
asyncio.get_event_loop().run_until_complete( |
| 49 |
self.communicator.wait() |
| 50 |
) |
| 51 |
|
| 52 |
def seed_app(self, app): |
| 53 |
self.communicator = ApplicationCommunicator(app, self.scope) |
| 54 |
|
| 55 |
def send_input(self, payload): |
| 56 |
asyncio.get_event_loop().run_until_complete( |
| 57 |
self.communicator.send_input(payload) |
| 58 |
) |
| 59 |
|
| 60 |
def send_default_request(self): |
| 61 |
self.send_input({"type": "http.request", "body": b""}) |
| 62 |
|
| 63 |
def get_output(self): |
| 64 |
output = asyncio.get_event_loop().run_until_complete( |
| 65 |
self.communicator.receive_output(0) |
| 66 |
) |
| 67 |
return output |
| 68 |
|
| 69 |
def get_all_output(self): |
| 70 |
outputs = [] |
| 71 |
while True: |
| 72 |
try: |
| 73 |
outputs.append(self.get_output()) |
| 74 |
except asyncio.TimeoutError: |
| 75 |
break |
| 76 |
return outputs |
| 77 |
|
| 78 |
def get_all_response_headers(self): |
| 79 |
outputs = self.get_all_output() |
| 80 |
response_start = next(o for o in outputs if o["type"] == "http.response.start") |
| 81 |
return response_start["headers"] |
| 82 |
|
| 83 |
def get_response_header_value(self, header_name): |
| 84 |
response_headers = self.get_all_response_headers() |
| 85 |
return next( |
| 86 |
value.decode("utf-8") |
| 87 |
for name, value in response_headers |
| 88 |
if name.decode("utf-8") == header_name |
| 89 |
) |
| 90 |
|
| 91 |
def increment_metrics(self, metric_name, help_text, increments): |
| 92 |
c = Counter(metric_name, help_text, registry=self.registry) |
| 93 |
for _ in range(increments): |
| 94 |
c.inc() |
| 95 |
|
| 96 |
def assert_outputs(self, outputs, metric_name, help_text, increments, compressed): |
| 97 |
self.assertEqual(len(outputs), 2) |
| 98 |
response_start = outputs[0] |
| 99 |
self.assertEqual(response_start['type'], 'http.response.start') |
| 100 |
response_body = outputs[1] |
| 101 |
self.assertEqual(response_body['type'], 'http.response.body') |
| 102 |
|
| 103 |
self.assertEqual(response_start['status'], 200) |
| 104 |
|
| 105 |
num_of_headers = 2 if compressed else 1 |
| 106 |
self.assertEqual(len(response_start['headers']), num_of_headers) |
| 107 |
self.assertIn((b"Content-Type", CONTENT_TYPE_LATEST.encode('utf8')), response_start['headers']) |
| 108 |
if compressed: |
| 109 |
self.assertIn((b"Content-Encoding", b"gzip"), response_start['headers']) |
| 110 |
|
| 111 |
if compressed: |
| 112 |
output = gzip.decompress(response_body['body']).decode('utf8') |
| 113 |
else: |
| 114 |
output = response_body['body'].decode('utf8') |
| 115 |
self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output) |
| 116 |
self.assertIn("# TYPE " + metric_name + "_total counter\n", output) |
| 117 |
self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output) |
| 118 |
|
| 119 |
def validate_metrics(self, metric_name, help_text, increments): |
| 120 |
""" |
| 121 |
ASGI app serves the metrics from the provided registry. |
| 122 |
""" |
| 123 |
self.increment_metrics(metric_name, help_text, increments) |
| 124 |
|
| 125 |
app = make_asgi_app(self.registry) |
| 126 |
self.seed_app(app) |
| 127 |
self.send_default_request() |
| 128 |
|
| 129 |
outputs = self.get_all_output() |
| 130 |
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=False) |
| 131 |
|
| 132 |
def test_report_metrics_1(self): |
| 133 |
self.validate_metrics("counter", "A counter", 2) |
| 134 |
|
| 135 |
def test_report_metrics_2(self): |
| 136 |
self.validate_metrics("counter", "Another counter", 3) |
| 137 |
|
| 138 |
def test_report_metrics_3(self): |
| 139 |
self.validate_metrics("requests", "Number of requests", 5) |
| 140 |
|
| 141 |
def test_report_metrics_4(self): |
| 142 |
self.validate_metrics("failed_requests", "Number of failed requests", 7) |
| 143 |
|
| 144 |
def test_gzip(self): |
| 145 |
|
| 146 |
metric_name = "counter" |
| 147 |
help_text = "A counter" |
| 148 |
increments = 2 |
| 149 |
self.increment_metrics(metric_name, help_text, increments) |
| 150 |
app = make_asgi_app(self.registry) |
| 151 |
self.seed_app(app) |
| 152 |
|
| 153 |
self.scope["headers"] = [(b"accept-encoding", b"gzip")] |
| 154 |
self.send_input({"type": "http.request", "body": b""}) |
| 155 |
|
| 156 |
outputs = self.get_all_output() |
| 157 |
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=True) |
| 158 |
|
| 159 |
def test_gzip_disabled(self): |
| 160 |
|
| 161 |
metric_name = "counter" |
| 162 |
help_text = "A counter" |
| 163 |
increments = 2 |
| 164 |
self.increment_metrics(metric_name, help_text, increments) |
| 165 |
|
| 166 |
app = make_asgi_app(self.registry, disable_compression=True) |
| 167 |
self.seed_app(app) |
| 168 |
|
| 169 |
self.scope["headers"] = [(b"accept-encoding", b"gzip")] |
| 170 |
self.send_input({"type": "http.request", "body": b""}) |
| 171 |
|
| 172 |
outputs = self.get_all_output() |
| 173 |
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=False) |
| 174 |
|
| 175 |
def test_openmetrics_encoding(self): |
| 176 |
"""Response content type is application/openmetrics-text when appropriate Accept header is in request""" |
| 177 |
app = make_asgi_app(self.registry) |
| 178 |
self.seed_app(app) |
| 179 |
self.scope["headers"] = [(b"Accept", b"application/openmetrics-text")] |
| 180 |
self.send_input({"type": "http.request", "body": b""}) |
| 181 |
|
| 182 |
content_type = self.get_response_header_value('Content-Type').split(";")[0] |
| 183 |
assert content_type == "application/openmetrics-text" |
| 184 |
|
| 185 |
def test_plaintext_encoding(self): |
| 186 |
"""Response content type is text/plain when Accept header is missing in request""" |
| 187 |
app = make_asgi_app(self.registry) |
| 188 |
self.seed_app(app) |
| 189 |
self.send_input({"type": "http.request", "body": b""}) |
| 190 |
|
| 191 |
content_type = self.get_response_header_value('Content-Type').split(";")[0] |
| 192 |
assert content_type == "text/plain" |