klen / klen/asgi-prometheus

Requests with scope type "websocket" raise a KeyError because there are no method for this kind of asgi request

Open
#2 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
9
Forks
0
PR merge metrics
No merged PRs in 30d

Description

The asgi scope definition for "websocket" requests does not contain a "method" entry (cf https://asgi.readthedocs.io/en/latest/specs/www.html#websocket-connection-scope). The BaseMiddleware from asgi-tools calls `__process__` if `scope in ("http", "websocket")` (default, overridable at class level).

Thus this causes an exception for all websocket requests:
https://github.com/klen/asgi-prometheus/blob/630e0f7c1fe973905a882a34b73a0935abb392ef/asgi_prometheus/__init__.py#L70

I'm not sure on how this should behave for websocket requests, I fix it for now be removing the "websocket" scope from the middleware scopes but this is a local workaround.

```
asgi_app = PrometheusMiddleware(asgi_app, metrics_url=_metrics_url, group_paths=["/"])
asgi_app.scopes = ("http",)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Read the ASGI websocket connection scope definition and inspect asgi_prometheus/__init__.py around line 70, where PrometheusMiddleware processes HTTP and websocket scopes. Check how BaseMiddleware invokes __process__ for those scopes and how the workaround changes scopes to only HTTP. Done means websocket requests no longer raise a KeyError and the intended metric behavior is established.

Written by the indexing model from the issue text.

Assessment

Tech stack
prometheus, python
Domain
backend-api-design, observability
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.