File tree Expand file tree Collapse file tree 3 files changed +34
-10
lines changed Expand file tree Collapse file tree 3 files changed +34
-10
lines changed Original file line number Diff line number Diff line change 1212 generate_latest ,
1313)
1414from prometheus_client .registry import CollectorRegistry
15- from servicelib .aiohttp .typing_extension import Handler
16- from servicelib .prometheus_metrics import (
17- PrometheusMetrics ,
18- get_prometheus_metrics ,
19- record_request_metrics ,
20- record_response_metrics ,
21- )
2215
2316from ..common_headers import (
2417 UNDEFINED_DEFAULT_SIMCORE_USER_AGENT_VALUE ,
2518 X_SIMCORE_USER_AGENT ,
2619)
2720from ..logging_utils import log_catch
21+ from ..prometheus_metrics import (
22+ PrometheusMetrics ,
23+ get_prometheus_metrics ,
24+ record_request_metrics ,
25+ record_response_metrics ,
26+ )
27+ from .typing_extension import Handler
2828
2929log = logging .getLogger (__name__ )
3030
Original file line number Diff line number Diff line change @@ -67,9 +67,6 @@ async def dispatch(
6767
6868
6969def initialize_prometheus_instrumentation (app : FastAPI ) -> None :
70- # NOTE: this cannot be ran once the application is started
71-
72- # NOTE: use that registry to prevent having a global one
7370 metrics = get_prometheus_metrics ()
7471 app .state .prometheus_metrics = metrics
7572 app .add_middleware (PrometheusMiddleware , metrics = metrics )
Original file line number Diff line number Diff line change 1+ from collections .abc import AsyncIterable
2+
3+ import pytest
4+ from asgi_lifespan import LifespanManager
5+ from fastapi import FastAPI
6+ from httpx import AsyncClient
7+ from servicelib .fastapi .monitoring import setup_prometheus_instrumentation
8+
9+
10+ @pytest .fixture
11+ async def app (app : FastAPI ) -> AsyncIterable [FastAPI ]:
12+ """
13+ Fixture that sets up the Prometheus middleware in the FastAPI app.
14+ """
15+ setup_prometheus_instrumentation (app )
16+ async with LifespanManager (app ):
17+ yield app
18+
19+
20+ async def test_metrics_endpoint (client : AsyncClient , app : FastAPI ):
21+ """
22+ Test that the /metrics endpoint is available and returns Prometheus metrics.
23+ """
24+ response = await client .get ("/metrics" )
25+ assert response .status_code == 200
26+ assert "# HELP" in response .text
27+ assert "# TYPE" in response .text
You can’t perform that action at this time.
0 commit comments