Skip to content

Commit 3be819b

Browse files
authored
Use the same metrics endpoint label for 404 requests (#3455)
* Use the same metrics endpoint label for 404 requests * Leave comment on high cardinality labels
1 parent d48b15f commit 3be819b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/dstack/_internal/server/app.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,31 @@ def _extract_project_name(request: Request):
306306

307307
return project_name
308308

309+
def _extract_endpoint_label(request: Request, response: Response) -> str:
310+
route = request.scope.get("route")
311+
route_path = getattr(route, "path", None)
312+
if route_path:
313+
return route_path
314+
if not request.url.path.startswith("/api/"):
315+
return "__non_api__"
316+
if response.status_code == status.HTTP_404_NOT_FOUND:
317+
return "__not_found__"
318+
return "__unmatched__"
319+
309320
project_name = _extract_project_name(request)
310321
response: Response = await call_next(request)
322+
endpoint_label = _extract_endpoint_label(request, response)
311323

312324
REQUEST_DURATION.labels(
313325
method=request.method,
314-
endpoint=request.url.path,
326+
endpoint=endpoint_label,
315327
http_status=response.status_code,
316328
project_name=project_name,
317329
).observe(request.state.process_time)
318330

319331
REQUESTS_TOTAL.labels(
320332
method=request.method,
321-
endpoint=request.url.path,
333+
endpoint=endpoint_label,
322334
http_status=response.status_code,
323335
project_name=project_name,
324336
).inc()

src/dstack/_internal/server/routers/prometheus.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
async def get_prometheus_metrics(
2626
session: Annotated[AsyncSession, Depends(get_session)],
2727
) -> str:
28+
# Note: Prometheus warns against storing high cardinality values in labels,
29+
# yet both client and custom metrics have labels like project, run, fleet, etc.
30+
# This may require a very big Prometheus server with lots of storage.
2831
if not settings.ENABLE_PROMETHEUS_METRICS:
2932
raise error_not_found()
3033
custom_metrics_ = await custom_metrics.get_metrics(session=session)

0 commit comments

Comments
 (0)