Skip to content

Commit

Permalink
Merge branch 'main' into task/BCY-651
Browse files Browse the repository at this point in the history
  • Loading branch information
mchinaloy authored Apr 29, 2024
2 parents c2f6034 + 01f4fc1 commit 9cc965d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
build: .
ports:
- "8000:8000"
- "8001:8001"
expose:
- 8080
networks:
Expand Down
16 changes: 13 additions & 3 deletions src/exporter.py
Original file line number Diff line number Diff line change
@@ -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', [])
Expand All @@ -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()
6 changes: 3 additions & 3 deletions src/test_exporter.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -35,15 +35,15 @@ 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):
"""Tests that the liveness path invokes return200
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):
Expand Down

0 comments on commit 9cc965d

Please sign in to comment.