Skip to content

Commit de87f25

Browse files
committed
Merge branch '7635-add-exemplars-to-prometheus-metrics' of github.com:bisgaard-itis/osparc-simcore into 7635-add-exemplars-to-prometheus-metrics
2 parents 6481c9a + 46f1ace commit de87f25

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

packages/service-library/src/servicelib/aiohttp/monitoring.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
generate_latest,
1313
)
1414
from 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

2316
from ..common_headers import (
2417
UNDEFINED_DEFAULT_SIMCORE_USER_AGENT_VALUE,
2518
X_SIMCORE_USER_AGENT,
2619
)
2720
from ..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

2929
log = logging.getLogger(__name__)
3030

packages/service-library/src/servicelib/fastapi/monitoring.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ async def dispatch(
6767

6868

6969
def 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)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

0 commit comments

Comments
 (0)