Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASGI lifespan crash #855

Open
sheenobu opened this issue Nov 15, 2022 · 1 comment · Fixed by sheenobu/client_python#1 · May be fixed by #856
Open

ASGI lifespan crash #855

sheenobu opened this issue Nov 15, 2022 · 1 comment · Fixed by sheenobu/client_python#1 · May be fixed by #856

Comments

@sheenobu
Copy link

sheenobu commented Nov 15, 2022

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 added a commit to sheenobu/client_python that referenced this issue Nov 16, 2022
Super simple hook that checks for lifespan and immediately responds type.complete, essentially making startup/shutdown lifespan events no-ops for the prometheus ASGI app.

Fixes prometheus#855

```
from hypercorn.middleware import DispatcherMiddleware
from prometheus_client import make_asgi_app
import asyncio

async def main():

    from hypercorn.asyncio import serve
    from hypercorn.config import Config

    app = DispatcherMiddleware({
        "/metrics": make_asgi_app(),
    })


    config = Config()
    config.bind = ["localhost:8080"]

    await serve(app, config)
```

Before:

```
    assert scope.get("type") == "http"
AssertionError
```

After:

```
[2022-11-15 16:05:20 -0800] [59048] [INFO] Running on http://127.0.0.1:8080 (CTRL + C to quit)
INFO:hypercorn.error:Running on http://127.0.0.1:8080 (CTRL + C to quit)
```

Signed-off-by: Sheena Artrip <[email protected]>
@sheenobu sheenobu linked a pull request Nov 16, 2022 that will close this issue
@sheenobu sheenobu reopened this Nov 16, 2022
@sheenobu
Copy link
Author

middleware solution if you are eager for a fix:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant