fix prometheus tests WIP

This commit is contained in:
Julius Unverfehrt 2024-01-17 17:39:53 +01:00
parent f31693d36a
commit 358e227251
6 changed files with 113 additions and 12 deletions

32
poetry.lock generated
View File

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

View File

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

View File

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

18
pyinfra/webserver.py Normal file
View File

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

View File

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

View File

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