|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import gzip |
| 4 | + |
| 5 | +from aiohttp import ClientResponse, hdrs, web |
| 6 | +from aiohttp.test_utils import AioHTTPTestCase |
| 7 | + |
| 8 | +from prometheus_client import CollectorRegistry, Counter, make_aiohttp_handler |
| 9 | +from prometheus_client.exposition import CONTENT_TYPE_PLAIN_0_0_4 |
| 10 | + |
| 11 | + |
| 12 | +class AioHTTPTest(AioHTTPTestCase): |
| 13 | + def setUp(self) -> None: |
| 14 | + self.registry = CollectorRegistry() |
| 15 | + |
| 16 | + async def get_application(self) -> web.Application: |
| 17 | + app = web.Application() |
| 18 | + # The AioHTTPTestCase requires that applications be static, so we need |
| 19 | + # both versions to be available so the test can choose between them |
| 20 | + app.router.add_get("/metrics", make_aiohttp_handler(self.registry)) |
| 21 | + app.router.add_get( |
| 22 | + "/metrics_uncompressed", |
| 23 | + make_aiohttp_handler(self.registry, disable_compression=True), |
| 24 | + ) |
| 25 | + return app |
| 26 | + |
| 27 | + def increment_metrics( |
| 28 | + self, metric_name: str, help_text: str, increments: int |
| 29 | + ) -> None: |
| 30 | + c = Counter(metric_name, help_text, registry=self.registry) |
| 31 | + for _ in range(increments): |
| 32 | + c.inc() |
| 33 | + |
| 34 | + def assert_metrics( |
| 35 | + self, |
| 36 | + output: str, |
| 37 | + metric_name: str, |
| 38 | + help_text: str, |
| 39 | + increments: int, |
| 40 | + ) -> None: |
| 41 | + self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output) |
| 42 | + self.assertIn("# TYPE " + metric_name + "_total counter\n", output) |
| 43 | + self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output) |
| 44 | + |
| 45 | + def assert_not_metrics( |
| 46 | + self, |
| 47 | + output: str, |
| 48 | + metric_name: str, |
| 49 | + help_text: str, |
| 50 | + increments: int, |
| 51 | + ) -> None: |
| 52 | + self.assertNotIn("# HELP " + metric_name + "_total " + help_text + "\n", output) |
| 53 | + self.assertNotIn("# TYPE " + metric_name + "_total counter\n", output) |
| 54 | + self.assertNotIn(metric_name + "_total " + str(increments) + ".0\n", output) |
| 55 | + |
| 56 | + async def assert_outputs( |
| 57 | + self, |
| 58 | + response: ClientResponse, |
| 59 | + metric_name: str, |
| 60 | + help_text: str, |
| 61 | + increments: int, |
| 62 | + ) -> None: |
| 63 | + self.assertIn( |
| 64 | + CONTENT_TYPE_PLAIN_0_0_4, |
| 65 | + response.headers.getall(hdrs.CONTENT_TYPE), |
| 66 | + ) |
| 67 | + output = await response.text() |
| 68 | + self.assert_metrics(output, metric_name, help_text, increments) |
| 69 | + |
| 70 | + async def validate_metrics( |
| 71 | + self, metric_name: str, help_text: str, increments: int |
| 72 | + ) -> None: |
| 73 | + """ |
| 74 | + AIOHTTP handler serves the metrics from the provided registry. |
| 75 | + """ |
| 76 | + self.increment_metrics(metric_name, help_text, increments) |
| 77 | + async with self.client.get("/metrics") as response: |
| 78 | + response.raise_for_status() |
| 79 | + await self.assert_outputs(response, metric_name, help_text, increments) |
| 80 | + |
| 81 | + async def test_report_metrics_1(self): |
| 82 | + await self.validate_metrics("counter", "A counter", 2) |
| 83 | + |
| 84 | + async def test_report_metrics_2(self): |
| 85 | + await self.validate_metrics("counter", "Another counter", 3) |
| 86 | + |
| 87 | + async def test_report_metrics_3(self): |
| 88 | + await self.validate_metrics("requests", "Number of requests", 5) |
| 89 | + |
| 90 | + async def test_report_metrics_4(self): |
| 91 | + await self.validate_metrics("failed_requests", "Number of failed requests", 7) |
| 92 | + |
| 93 | + async def test_gzip(self): |
| 94 | + # Increment a metric. |
| 95 | + metric_name = "counter" |
| 96 | + help_text = "A counter" |
| 97 | + increments = 2 |
| 98 | + self.increment_metrics(metric_name, help_text, increments) |
| 99 | + |
| 100 | + async with self.client.get( |
| 101 | + "/metrics", |
| 102 | + auto_decompress=False, |
| 103 | + headers={hdrs.ACCEPT_ENCODING: "gzip"}, |
| 104 | + ) as response: |
| 105 | + response.raise_for_status() |
| 106 | + self.assertIn(hdrs.CONTENT_ENCODING, response.headers) |
| 107 | + self.assertIn("gzip", response.headers.getall(hdrs.CONTENT_ENCODING)) |
| 108 | + body = await response.read() |
| 109 | + output = gzip.decompress(body).decode("utf8") |
| 110 | + self.assert_metrics(output, metric_name, help_text, increments) |
| 111 | + |
| 112 | + async def test_gzip_disabled(self): |
| 113 | + # Increment a metric. |
| 114 | + metric_name = "counter" |
| 115 | + help_text = "A counter" |
| 116 | + increments = 2 |
| 117 | + self.increment_metrics(metric_name, help_text, increments) |
| 118 | + |
| 119 | + async with self.client.get( |
| 120 | + "/metrics_uncompressed", |
| 121 | + auto_decompress=False, |
| 122 | + headers={hdrs.ACCEPT_ENCODING: "gzip"}, |
| 123 | + ) as response: |
| 124 | + response.raise_for_status() |
| 125 | + self.assertNotIn(hdrs.CONTENT_ENCODING, response.headers) |
| 126 | + output = await response.text() |
| 127 | + self.assert_metrics(output, metric_name, help_text, increments) |
| 128 | + |
| 129 | + async def test_openmetrics_encoding(self): |
| 130 | + """Response content type is application/openmetrics-text when appropriate Accept header is in request""" |
| 131 | + async with self.client.get( |
| 132 | + "/metrics", |
| 133 | + auto_decompress=False, |
| 134 | + headers={hdrs.ACCEPT: "application/openmetrics-text; version=1.0.0"}, |
| 135 | + ) as response: |
| 136 | + response.raise_for_status() |
| 137 | + self.assertEqual( |
| 138 | + response.headers.getone(hdrs.CONTENT_TYPE).split(";", maxsplit=1)[0], |
| 139 | + "application/openmetrics-text", |
| 140 | + ) |
| 141 | + |
| 142 | + async def test_plaintext_encoding(self): |
| 143 | + """Response content type is text/plain when Accept header is missing in request""" |
| 144 | + async with self.client.get("/metrics") as response: |
| 145 | + response.raise_for_status() |
| 146 | + self.assertEqual( |
| 147 | + response.headers.getone(hdrs.CONTENT_TYPE).split(";", maxsplit=1)[0], |
| 148 | + "text/plain", |
| 149 | + ) |
| 150 | + |
| 151 | + async def test_qs_parsing(self): |
| 152 | + """Only metrics that match the 'name[]' query string param appear""" |
| 153 | + |
| 154 | + metrics = [("asdf", "first test metric", 1), ("bsdf", "second test metric", 2)] |
| 155 | + |
| 156 | + for m in metrics: |
| 157 | + self.increment_metrics(*m) |
| 158 | + |
| 159 | + for i_1 in range(len(metrics)): |
| 160 | + async with self.client.get( |
| 161 | + "/metrics", |
| 162 | + params={"name[]": f"{metrics[i_1][0]}_total"}, |
| 163 | + ) as response: |
| 164 | + output = await response.text() |
| 165 | + self.assert_metrics(output, *metrics[i_1]) |
| 166 | + |
| 167 | + for i_2 in range(len(metrics)): |
| 168 | + if i_1 == i_2: |
| 169 | + continue |
| 170 | + |
| 171 | + self.assert_not_metrics(output, *metrics[i_2]) |
0 commit comments