diff --git a/logprep/connector/http/input.py b/logprep/connector/http/input.py index 1451a0b04..d0dafebbc 100644 --- a/logprep/connector/http/input.py +++ b/logprep/connector/http/input.py @@ -411,9 +411,6 @@ class Config(Input.Config): metafield_name: str = field(validator=validators.instance_of(str), default="@metadata") """Defines the name of the key for the collected metadata fields""" - health_timeout: int = field(validator=validators.instance_of(int), default=5) - """Timeout in seconds for health check""" - __slots__ = [] messages: mp.Queue = None diff --git a/logprep/metrics/exporter.py b/logprep/metrics/exporter.py index 969895354..6f43ca785 100644 --- a/logprep/metrics/exporter.py +++ b/logprep/metrics/exporter.py @@ -21,16 +21,17 @@ def make_patched_asgi_app(functions: Iterable[Callable] | None) -> Callable: functions = functions if functions else [lambda: DEFAULT_HEALTH_STATE] + response_start_ok = {"type": "http.response.start", "status": 200} + response_body_ok = {"type": "http.response.body", "body": b"OK"} + response_start_fail = {"type": "http.response.start", "status": 503} + response_body_fail = {"type": "http.response.body", "body": b"FAIL"} + async def asgi_app(scope, receive, send): """asgi app with health check and metrics handling""" if scope["type"] == "http" and scope["path"] == "/health": - await send( - { - "type": "http.response.start", - "status": 200 if all(f() for f in functions) else 503, - } - ) - await send({"type": "http.response.body", "body": b"OK"}) + success = all(f() for f in functions) + await send(response_start_ok if success else response_start_fail) + await send(response_body_ok if success else response_body_fail) else: await prometheus_app(scope, receive, send) diff --git a/tests/unit/metrics/test_exporter.py b/tests/unit/metrics/test_exporter.py index 35a91d817..740ac2253 100644 --- a/tests/unit/metrics/test_exporter.py +++ b/tests/unit/metrics/test_exporter.py @@ -127,3 +127,26 @@ def test_health_check_returns_status_code(self, functions, expected): resp = requests.get("http://localhost:8000/health", timeout=0.5) assert resp.status_code == expected exporter.server.shut_down() + + @pytest.mark.parametrize( + "functions, expected", + [ + ([lambda: True], "OK"), + ([lambda: True, lambda: True], "OK"), + ([lambda: False], "FAIL"), + ([lambda: False, lambda: False], "FAIL"), + ([lambda: False, lambda: True, lambda: True], "FAIL"), + ], + ) + def test_health_check_returns_body(self, functions, expected): + exporter = PrometheusExporter(self.metrics_config) + exporter.server = http.ThreadingHTTPServer( + exporter.configuration.uvicorn_config | {"port": 8000, "host": "0.0.0.0"}, + make_patched_asgi_app(functions), + daemon=False, + logger_name="Exporter", + ) + exporter.server.start() + resp = requests.get("http://localhost:8000/health", timeout=0.5) + assert resp.content.decode() == expected + exporter.server.shut_down()