Skip to content

Commit

Permalink
add health endpoint to metrics asgi app
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Aug 29, 2024
1 parent 1f390a8 commit 68f4695
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
30 changes: 23 additions & 7 deletions logprep/metrics/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,39 @@
from logprep.util import http
from logprep.util.configuration import MetricsConfig

prometheus_app = make_asgi_app(REGISTRY)

logger = getLogger("Exporter")


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,
}
)
await send({"type": "http.response.body", "body": b"OK"})
else:
await prometheus_app(scope, receive, send)


class PrometheusExporter:
"""Used to control the prometheus exporter and to manage the metrics"""

def __init__(self, configuration: MetricsConfig):
self.is_running = False
logger_name = "Exporter"
self._logger = getLogger(logger_name)
self._logger.debug("Initializing Prometheus Exporter")
logger.debug("Initializing Prometheus Exporter")
self.configuration = configuration
self._port = configuration.port
self._app = make_asgi_app(REGISTRY)
self._app = asgi_app
self._server = http.ThreadingHTTPServer(
configuration.uvicorn_config | {"port": self._port, "host": "0.0.0.0"},
self._app,
daemon=True,
logger_name=logger_name,
logger_name="Exporter",
)

def _prepare_multiprocessing(self):
Expand All @@ -45,7 +61,7 @@ def cleanup_prometheus_multiprocess_dir(self):
os.remove(os.path.join(root, file))
for directory in dirs:
shutil.rmtree(os.path.join(root, directory), ignore_errors=True)
self._logger.info("Cleaned up %s", multiprocess_dir)
logger.info("Cleaned up %s", multiprocess_dir)

def mark_process_dead(self, pid):
"""
Expand All @@ -63,5 +79,5 @@ def run(self):
"""Starts the default prometheus http endpoint"""
self._prepare_multiprocessing()
self._server.start()
self._logger.info("Prometheus Exporter started on port %s", self._port)
logger.info("Prometheus Exporter started on port %s", self._port)
self.is_running = True
26 changes: 24 additions & 2 deletions tests/unit/metrics/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@
# pylint: disable=protected-access
# pylint: disable=attribute-defined-outside-init
# pylint: disable=line-too-long
import asyncio
import os.path
from logging.config import dictConfig
from unittest import mock

import pytest
import requests
from prometheus_client import REGISTRY

from logprep.metrics.exporter import PrometheusExporter
from logprep.util import configuration, http
from logprep.util.configuration import MetricsConfig
from logprep.util.defaults import DEFAULT_LOG_CONFIG


@pytest.fixture
def loop():
loop = asyncio.new_event_loop()
yield loop
loop.close()


@mock.patch(
Expand Down Expand Up @@ -79,3 +88,16 @@ def test_mark_process_dead_calls_multiprocess_mark_dead(self, mock_multiprocess)
def test_exporter_spawns_server_on_all_interfaces(self):
exporter = PrometheusExporter(self.metrics_config)
assert exporter._server.server.config.host == "0.0.0.0"

def test_health_endpoint_returns_200(self):
exporter = PrometheusExporter(self.metrics_config)
exporter._server = http.ThreadingHTTPServer(
self.metrics_config.uvicorn_config | {"port": "8000", "host": "0.0.0.0"},
exporter._app,
daemon=False,
logger_name="Exporter",
)
exporter.run()
resp = requests.get("http://localhost:8000/health")
assert resp.status_code == 200
exporter._server.shut_down()

0 comments on commit 68f4695

Please sign in to comment.