From 358e227251f6ef9a6b7b61eb5c1f2a3d21ee8696 Mon Sep 17 00:00:00 2001 From: Julius Unverfehrt Date: Wed, 17 Jan 2024 17:39:53 +0100 Subject: [PATCH] fix prometheus tests WIP --- poetry.lock | 32 +++++++++++- pyinfra/monitor/prometheus.py | 19 +++---- pyinfra/utils/config_validation.py | 5 ++ pyinfra/webserver.py | 18 +++++++ pyproject.toml | 1 + .../prometheus_monitoring_test.py | 50 +++++++++++++++++++ 6 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 pyinfra/webserver.py create mode 100644 tests/tests_with_docker_compose/prometheus_monitoring_test.py diff --git a/poetry.lock b/poetry.lock index ff4b69b..d820b51 100644 --- a/poetry.lock +++ b/poetry.lock @@ -670,6 +670,17 @@ files = [ {file = "funcy-2.0.tar.gz", hash = "sha256:3963315d59d41c6f30c04bc910e10ab50a3ac4a225868bfa96feed133df075cb"}, ] +[[package]] +name = "h11" +version = "0.14.0" +description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1" +optional = false +python-versions = ">=3.7" +files = [ + {file = "h11-0.14.0-py3-none-any.whl", hash = "sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761"}, + {file = "h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d"}, +] + [[package]] name = "idna" version = "3.6" @@ -1842,6 +1853,25 @@ brotli = ["brotli (>=1.0.9)", "brotlicffi (>=0.8.0)"] socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"] zstd = ["zstandard (>=0.18.0)"] +[[package]] +name = "uvicorn" +version = "0.26.0" +description = "The lightning-fast ASGI server." +optional = false +python-versions = ">=3.8" +files = [ + {file = "uvicorn-0.26.0-py3-none-any.whl", hash = "sha256:cdb58ef6b8188c6c174994b2b1ba2150a9a8ae7ea5fb2f1b856b94a815d6071d"}, + {file = "uvicorn-0.26.0.tar.gz", hash = "sha256:48bfd350fce3c5c57af5fb4995fded8fb50da3b4feb543eb18ad7e0d54589602"}, +] + +[package.dependencies] +click = ">=7.0" +h11 = ">=0.8" +typing-extensions = {version = ">=4.0", markers = "python_version < \"3.11\""} + +[package.extras] +standard = ["colorama (>=0.4)", "httptools (>=0.5.0)", "python-dotenv (>=0.13)", "pyyaml (>=5.1)", "uvloop (>=0.14.0,!=0.15.0,!=0.15.1)", "watchfiles (>=0.13)", "websockets (>=10.4)"] + [[package]] name = "wcwidth" version = "0.2.12" @@ -1870,4 +1900,4 @@ dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<3.11" -content-hash = "7ade3c9e8e0b7897da004073c8827e18d6e2d3c575a0c4f48ce46dcc58af1e1f" +content-hash = "947961b5c6b624da6ff0644fa320e8854255dc893e0ffb697b67576c4da86eb8" diff --git a/pyinfra/monitor/prometheus.py b/pyinfra/monitor/prometheus.py index debf2da..8dc8b01 100644 --- a/pyinfra/monitor/prometheus.py +++ b/pyinfra/monitor/prometheus.py @@ -27,7 +27,12 @@ def add_prometheus_endpoint(app: FastAPI, registry: CollectorRegistry = REGISTRY return app -def make_prometheus_processing_time_decorator_from_settings(settings: Dynaconf, registry: CollectorRegistry = REGISTRY): +Decorator = TypeVar("Decorator", bound=Callable[[Callable], Callable]) + + +def make_prometheus_processing_time_decorator_from_settings( + settings: Dynaconf, registry: CollectorRegistry = REGISTRY +) -> Decorator: """Make a decorator for monitoring the processing time of a function. The decorator is only applied if the prometheus metrics are enabled in the settings. """ @@ -42,9 +47,6 @@ def make_prometheus_processing_time_decorator_from_settings(settings: Dynaconf, ) -Decorator = TypeVar("Decorator", bound=Callable[[Callable], Callable]) - - def make_prometheus_processing_time_decorator( prefix: str = "readactmanager_research_service", registry: CollectorRegistry = REGISTRY, @@ -57,16 +59,11 @@ def make_prometheus_processing_time_decorator( def inner(*args, **kwargs): start = time() - result: Sized = process_fn(*args, **kwargs) + result = process_fn(*args, **kwargs) runtime = time() - start - if not result: - return result - - processing_time_per_entity = runtime / len(result) - - processing_time_sum.observe(processing_time_per_entity) + processing_time_sum.observe(runtime) return result diff --git a/pyinfra/utils/config_validation.py b/pyinfra/utils/config_validation.py index 056b4a3..028f5d4 100644 --- a/pyinfra/utils/config_validation.py +++ b/pyinfra/utils/config_validation.py @@ -33,6 +33,11 @@ prometheus_validators = [ Validator("metrics.prometheus.enabled", must_exist=True), ] +webserver_validators = [ + Validator("webserver.host", must_exist=True), + Validator("webserver.port", must_exist=True), +] + def validate_settings(settings: Dynaconf, validators): settings_valid = True diff --git a/pyinfra/webserver.py b/pyinfra/webserver.py new file mode 100644 index 0000000..acecf24 --- /dev/null +++ b/pyinfra/webserver.py @@ -0,0 +1,18 @@ +import logging +import threading + +import uvicorn +from dynaconf import Dynaconf +from fastapi import FastAPI + +from pyinfra.utils.config_validation import validate_settings, webserver_validators + + +def create_webserver_thread(app: FastAPI, settings: Dynaconf) -> threading.Thread: + validate_settings(settings, validators=webserver_validators) + + return threading.Thread( + target=lambda: uvicorn.run( + app, port=settings.webserver.port, host=settings.webserver.host, log_level=logging.WARNING + ) + ) diff --git a/pyproject.toml b/pyproject.toml index e6a7126..3f3b41f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ pycryptodome = "^3.19" # research shared packages kn-utils = { version = "^0.2.4.dev112", source = "gitlab-research" } fastapi = "^0.109.0" +uvicorn = "^0.26.0" [tool.poetry.group.dev.dependencies] pytest = "^7" diff --git a/tests/tests_with_docker_compose/prometheus_monitoring_test.py b/tests/tests_with_docker_compose/prometheus_monitoring_test.py new file mode 100644 index 0000000..d6b7c8a --- /dev/null +++ b/tests/tests_with_docker_compose/prometheus_monitoring_test.py @@ -0,0 +1,50 @@ +from time import sleep + +import pytest +import requests +from fastapi import FastAPI + +from pyinfra.monitor.prometheus import add_prometheus_endpoint, make_prometheus_processing_time_decorator_from_settings +from pyinfra.webserver import create_webserver_thread + + +@pytest.fixture(scope="function") +def app_with_prometheus_endpoint(settings): + app = FastAPI() + app = add_prometheus_endpoint(app) + thread = create_webserver_thread(app, settings) + thread.daemon = True + thread.start() + thread.join(timeout=1) + + +@pytest.fixture +def monitored_function(settings): + @make_prometheus_processing_time_decorator_from_settings(settings) + def process(*args, **kwargs): + sleep(0.5) + + return process + + +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_processing_with_a_monitored_fn_increases_parameter_counter( + self, app_with_prometheus_endpoint, monitored_function, settings + ): + 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 + + monitored_function() + resp = requests.get(f"http://{settings.webserver.host}:{settings.webserver.port}/prometheus") + print(resp.text) + 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