Skip to content

vllm.entrypoints.serve.instrumentator.metrics

PrometheusResponse

Bases: Response

Source code in vllm/entrypoints/serve/instrumentator/metrics.py
class PrometheusResponse(Response):
    media_type = prometheus_client.CONTENT_TYPE_LATEST

media_type class-attribute instance-attribute

media_type = CONTENT_TYPE_LATEST

attach_router

attach_router(app: FastAPI)

Mount prometheus metrics to a FastAPI app.

Source code in vllm/entrypoints/serve/instrumentator/metrics.py
def attach_router(app: FastAPI):
    """Mount prometheus metrics to a FastAPI app."""

    registry = get_prometheus_registry()

    # `response_class=PrometheusResponse` is needed to return an HTTP response
    # with header "Content-Type: text/plain; version=0.0.4; charset=utf-8"
    # instead of the default "application/json" which is incorrect.
    # See https://github.com/trallnag/prometheus-fastapi-instrumentator/issues/163#issue-1296092364
    Instrumentator(
        excluded_handlers=[
            "/metrics",
            "/health",
            "/load",
            "/ping",
            "/version",
            "/server_info",
        ],
        registry=registry,
    ).add().instrument(app).expose(app, response_class=PrometheusResponse)

    # Add prometheus asgi middleware to route /metrics requests
    metrics_route = Mount("/metrics", make_asgi_app(registry=registry))

    # Workaround for 307 Redirect for /metrics
    metrics_route.path_regex = re.compile("^/metrics(?P<path>.*)$")
    app.routes.append(metrics_route)