prometheus / client_python

Prometheus instrumentation library for Python applications
Apache License 2.0
3.94k stars 795 forks source link

ASGI lifespan crash #855

Open sheenobu opened 1 year ago

sheenobu commented 1 year ago

client_python version: testing on 0.15.0 but the offending code exists in the master branch as well.

I'm trying to use the prometheus ASGI handler with Hypercorns DispatcherMiddleware. It sends lifecycle events which are not properly handled:

def make_asgi_app(registry: CollectorRegistry = REGISTRY, disable_compression: bool = False) -> Callable:
    """Create a ASGI app which serves the metrics from a registry."""

    async def prometheus_app(scope, receive, send):
        assert scope.get("type") == "http"
        # Prepare parameters

assert scope.get("type") == "http" ensures any ASGI middleware sending lifecycle events will fail to start the prometheus asgi app.

sheenobu commented 1 year ago

middleware solution if you are eager for a fix:

https://gist.github.com/sheenobu/064b501b76415c6f6dacb6afa1ac6305