From 17c5eebdf6f53b084bb0af23c6151125e82c4cb4 Mon Sep 17 00:00:00 2001 From: Julius Unverfehrt Date: Thu, 18 Jan 2024 08:19:46 +0100 Subject: [PATCH] finnish prometheus --- .../docker-compose.yml | 0 .../prometheus_monitoring_test.py | 26 ++++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) rename tests/{tests_with_docker_compose => }/docker-compose.yml (100%) diff --git a/tests/tests_with_docker_compose/docker-compose.yml b/tests/docker-compose.yml similarity index 100% rename from tests/tests_with_docker_compose/docker-compose.yml rename to tests/docker-compose.yml diff --git a/tests/tests_with_docker_compose/prometheus_monitoring_test.py b/tests/tests_with_docker_compose/prometheus_monitoring_test.py index d6b7c8a..a6d0df1 100644 --- a/tests/tests_with_docker_compose/prometheus_monitoring_test.py +++ b/tests/tests_with_docker_compose/prometheus_monitoring_test.py @@ -1,3 +1,4 @@ +import re from time import sleep import pytest @@ -8,13 +9,15 @@ from pyinfra.monitor.prometheus import add_prometheus_endpoint, make_prometheus_ from pyinfra.webserver import create_webserver_thread -@pytest.fixture(scope="function") +@pytest.fixture(scope="class") def app_with_prometheus_endpoint(settings): app = FastAPI() app = add_prometheus_endpoint(app) thread = create_webserver_thread(app, settings) thread.daemon = True thread.start() + sleep(1) + yield thread.join(timeout=1) @@ -28,23 +31,22 @@ def monitored_function(settings): class TestPrometheusMonitor: - # def test_prometheus_endpoint_is_available(self, app_with_prometheus_endpoint, settings): - # resp = requests.get(f"http://{settings.webserver.host}:{settings.webserver.port}/prometheus") - # assert resp.status_code == 200 + def test_prometheus_endpoint_is_available(self, app_with_prometheus_endpoint, settings): + resp = requests.get(f"http://{settings.webserver.host}:{settings.webserver.port}/prometheus") + assert resp.status_code == 200 def test_processing_with_a_monitored_fn_increases_parameter_counter( self, app_with_prometheus_endpoint, monitored_function, settings ): + pattern = re.compile(rf".*{settings.metrics.prometheus.prefix}_processing_time_count (\d\.\d).*") + resp = requests.get(f"http://{settings.webserver.host}:{settings.webserver.port}/prometheus") - print(resp.text) - monitored_function() - resp = requests.get(f"http://{settings.webserver.host}:{settings.webserver.port}/prometheus") - print(resp.text) - # assert resp.text.count(f"{settings.metrics.prometheus.prefix}_processing_time_count") == 1 + assert pattern.search(resp.text).group(1) == "0.0" monitored_function() resp = requests.get(f"http://{settings.webserver.host}:{settings.webserver.port}/prometheus") - print(resp.text) + assert pattern.search(resp.text).group(1) == "1.0" + monitored_function() - # print(resp.text.count(f"{settings.metrics.prometheus.prefix}_processing_time_count")) - # assert resp.text.count(f"{settings.metrics.prometheus.prefix}_processing_time_count") == 2 + resp = requests.get(f"http://{settings.webserver.host}:{settings.webserver.port}/prometheus") + assert pattern.search(resp.text).group(1) == "2.0"