Skip to content

Commit

Permalink
optimize asgi health app
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Sep 11, 2024
1 parent c4b3e33 commit f50ef13
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
3 changes: 0 additions & 3 deletions logprep/connector/http/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions logprep/metrics/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/metrics/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit f50ef13

Please sign in to comment.