From 01f4fc1b524dc455130d0c07b3d3bdc78c5fe690 Mon Sep 17 00:00:00 2001 From: rodgerjohnson Date: Wed, 13 Mar 2024 00:00:23 +0900 Subject: [PATCH] NODE-2836 Separate Liveness check proc to not be blocked by requests to /metrics (#58) * Separate Liveness check proc to not block requests * rm test changes * Test fixes * Test fixes * Linting changes --- docker-compose.yml | 1 + src/exporter.py | 16 +++++++++++++--- src/test_exporter.py | 6 +++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 6e510c1..5d1caa3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,7 @@ services: build: . ports: - "8000:8000" + - "8001:8001" expose: - 8080 networks: diff --git a/src/exporter.py b/src/exporter.py index caa50a2..cb99228 100644 --- a/src/exporter.py +++ b/src/exporter.py @@ -1,9 +1,9 @@ """Main module that loads Prometheus registry and starts a web-server.""" +import threading from wsgiref.simple_server import make_server from prometheus_client import REGISTRY, make_wsgi_app from metrics import PrometheusCustomCollector - def return200(_, start_fn): """Wsgi http response function.""" start_fn('200 OK', []) @@ -21,16 +21,26 @@ def exporter(environ, start_fn): # pylint: disable=inconsistent-return-statemen match environ['PATH_INFO']: case '/metrics': return metrics_app(environ, start_fn) + case _: + return return404(environ, start_fn) + +def liveness(environ, start_fn): + """Liveness endpoint function""" + match environ['PATH_INFO']: case '/readiness': return return200(environ, start_fn) case '/liveness': return return200(environ, start_fn) - case _: - return return404(environ, start_fn) +def start_liveness(): + """Liveness thread function""" + httpd_liveness = make_server('', 8001, liveness) + httpd_liveness.serve_forever() if __name__ == '__main__': REGISTRY.register(PrometheusCustomCollector()) metrics_app = make_wsgi_app() + liveness_thread = threading.Thread(target=start_liveness) + liveness_thread.start() httpd = make_server('', 8000, exporter) httpd.serve_forever() diff --git a/src/test_exporter.py b/src/test_exporter.py index 93c17e5..1324696 100644 --- a/src/test_exporter.py +++ b/src/test_exporter.py @@ -1,7 +1,7 @@ """Tests the exporter module""" from unittest import TestCase, mock -from exporter import return200, return404, exporter +from exporter import return200, return404, exporter, liveness class TestExporter(TestCase): @@ -35,7 +35,7 @@ def test_exporter_readiness(self): with the environ and HTTP response callable""" environ = {'PATH_INFO': '/readiness'} with mock.patch('exporter.return200') as mocked: - exporter(environ, self.start_fn_mock) + liveness(environ, self.start_fn_mock) mocked.assert_called_once_with(environ, self.start_fn_mock) def test_exporter_liveness(self): @@ -43,7 +43,7 @@ def test_exporter_liveness(self): with the environ and HTTP response callable""" environ = {'PATH_INFO': '/liveness'} with mock.patch('exporter.return200') as mocked: - exporter(environ, self.start_fn_mock) + liveness(environ, self.start_fn_mock) mocked.assert_called_once_with(environ, self.start_fn_mock) def test_exporter_404(self):