-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Emil Madsen <sovende@gmail.com>
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from urllib.parse import parse_qs | ||
|
||
from .exposition import choose_encoder | ||
from .registry import REGISTRY | ||
|
||
|
||
def make_asgi_app(registry=REGISTRY): | ||
"""Create a ASGI app which serves the metrics from a registry.""" | ||
|
||
async def prometheus_app(scope, receive, send): | ||
assert scope.get("type") == "http" | ||
params = parse_qs(scope.get('query_string', b'')) | ||
r = registry | ||
accept_header = "Accept: " + ",".join([ | ||
value.decode("utf8") for (name, value) in scope.get('headers') | ||
if name.decode("utf8") == 'accept' | ||
]) | ||
encoder, content_type = choose_encoder(accept_header) | ||
if 'name[]' in params: | ||
r = r.restricted_registry(params['name[]']) | ||
output = encoder(r) | ||
|
||
payload = await receive() | ||
if payload.get("type") == "http.request": | ||
await send( | ||
{ | ||
"type": "http.response.start", | ||
"status": 200, | ||
"headers": [[b"Content-Type", content_type.encode('utf8')]], | ||
} | ||
) | ||
await send({"type": "http.response.body", "body": output}) | ||
|
||
return prometheus_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters