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