import re import time import pytest import requests from pyinfra.config import get_config from pyinfra.payload_processing.monitor import get_monitor @pytest.fixture(scope="class") def monitor_config(): config = get_config() config.prometheus_metric_prefix = "monitor_test" config.prometheus_port = 8000 return config @pytest.fixture(scope="class") def prometheus_monitor(monitor_config): return get_monitor(monitor_config) @pytest.fixture def monitored_mock_function(prometheus_monitor): def process(data=None): time.sleep(2) return ["result1", "result2", "result3"] return prometheus_monitor(process) class TestPrometheusMonitor: def test_prometheus_endpoint_is_available(self, prometheus_monitor, monitor_config): resp = requests.get(f"http://{monitor_config.prometheus_host}:{monitor_config.prometheus_port}/prometheus") assert resp.status_code == 200 def test_processing_with_a_monitored_fn_increases_parameter_counter(self, monitored_mock_function, monitor_config): monitored_mock_function(data=None) resp = requests.get(f"http://{monitor_config.prometheus_host}:{monitor_config.prometheus_port}/prometheus") pattern = re.compile(r".*monitor_test_processing_time_count (\d\.\d).*") assert pattern.search(resp.text).group(1) == "1.0" monitored_mock_function(data=None) resp = requests.get(f"http://{monitor_config.prometheus_host}:{monitor_config.prometheus_port}/prometheus") assert pattern.search(resp.text).group(1) == "2.0"