Skip to content

Commit 01f4fc1

Browse files
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
1 parent 89b3d5c commit 01f4fc1

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services:
1212
build: .
1313
ports:
1414
- "8000:8000"
15+
- "8001:8001"
1516
expose:
1617
- 8080
1718
networks:

src/exporter.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Main module that loads Prometheus registry and starts a web-server."""
2+
import threading
23
from wsgiref.simple_server import make_server
34
from prometheus_client import REGISTRY, make_wsgi_app
45
from metrics import PrometheusCustomCollector
56

6-
77
def return200(_, start_fn):
88
"""Wsgi http response function."""
99
start_fn('200 OK', [])
@@ -21,16 +21,26 @@ def exporter(environ, start_fn): # pylint: disable=inconsistent-return-statemen
2121
match environ['PATH_INFO']:
2222
case '/metrics':
2323
return metrics_app(environ, start_fn)
24+
case _:
25+
return return404(environ, start_fn)
26+
27+
def liveness(environ, start_fn):
28+
"""Liveness endpoint function"""
29+
match environ['PATH_INFO']:
2430
case '/readiness':
2531
return return200(environ, start_fn)
2632
case '/liveness':
2733
return return200(environ, start_fn)
28-
case _:
29-
return return404(environ, start_fn)
3034

35+
def start_liveness():
36+
"""Liveness thread function"""
37+
httpd_liveness = make_server('', 8001, liveness)
38+
httpd_liveness.serve_forever()
3139

3240
if __name__ == '__main__':
3341
REGISTRY.register(PrometheusCustomCollector())
3442
metrics_app = make_wsgi_app()
43+
liveness_thread = threading.Thread(target=start_liveness)
44+
liveness_thread.start()
3545
httpd = make_server('', 8000, exporter)
3646
httpd.serve_forever()

src/test_exporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests the exporter module"""
22
from unittest import TestCase, mock
33

4-
from exporter import return200, return404, exporter
4+
from exporter import return200, return404, exporter, liveness
55

66

77
class TestExporter(TestCase):
@@ -35,15 +35,15 @@ def test_exporter_readiness(self):
3535
with the environ and HTTP response callable"""
3636
environ = {'PATH_INFO': '/readiness'}
3737
with mock.patch('exporter.return200') as mocked:
38-
exporter(environ, self.start_fn_mock)
38+
liveness(environ, self.start_fn_mock)
3939
mocked.assert_called_once_with(environ, self.start_fn_mock)
4040

4141
def test_exporter_liveness(self):
4242
"""Tests that the liveness path invokes return200
4343
with the environ and HTTP response callable"""
4444
environ = {'PATH_INFO': '/liveness'}
4545
with mock.patch('exporter.return200') as mocked:
46-
exporter(environ, self.start_fn_mock)
46+
liveness(environ, self.start_fn_mock)
4747
mocked.assert_called_once_with(environ, self.start_fn_mock)
4848

4949
def test_exporter_404(self):

0 commit comments

Comments
 (0)