finnish prometheus

This commit is contained in:
Julius Unverfehrt 2024-01-18 08:19:46 +01:00
parent 358e227251
commit 17c5eebdf6
2 changed files with 14 additions and 12 deletions

View File

@ -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"