8000 Add no-op asgi-lifespan handling · sheenobu/client_python@28f4ca4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28f4ca4

Browse files
authored
Add no-op asgi-lifespan handling
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 <sheena.artrip@gmail.com>
1 parent db391db commit 28f4ca4

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

prometheus_client/asgi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def make_asgi_app(registry: CollectorRegistry = REGISTRY, disable_compression: b
99
"""Create a ASGI app which serves the metrics from a registry."""
1010

1111
async def prometheus_app(scope, receive, send):
12+
if scope.get("type") == "lifespan":
13+
payload = await receive()
14+
await send({'type': payload['type'] + ".complete"})
15+
return
1216
assert scope.get("type") == "http"
1317
# Prepare parameters
1418
params = parse_qs(scope.get('query_string', b''))

0 commit comments

Comments
 (0)
0