From 0da11f068bd34bc534baf34027b2a925475f6666 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Thu, 11 Nov 2021 16:22:30 -0800 Subject: [PATCH] fix(fastapi): check for prometheus info.response When this is unchecked, exceptions cause the resulting stack trace to be oblivious to the original exception thrown. This commit changes that behavior so that metrics are created only when info.response exists. Signed-off-by: Kevin Morris --- aurweb/prometheus.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/aurweb/prometheus.py b/aurweb/prometheus.py index a64f6b27..dae56320 100644 --- a/aurweb/prometheus.py +++ b/aurweb/prometheus.py @@ -79,9 +79,10 @@ def http_requests_total() -> Callable[[Info], None]: method = scope.get("method") path = get_matching_route_path(base_scope, scope.get("router").routes) - status = str(int(info.response.status_code))[:1] + "xx" - metric.labels(method=method, path=path, status=status).inc() + if info.response: + status = str(int(info.response.status_code))[:1] + "xx" + metric.labels(method=method, path=path, status=status).inc() return instrumentation @@ -95,7 +96,8 @@ def http_api_requests_total() -> Callable[[Info], None]: def instrumentation(info: Info) -> None: if info.request.url.path.rstrip("/") == "/rpc": type = info.request.query_params.get("type", "None") - status = str(info.response.status_code)[:1] + "xx" - metric.labels(type=type, status=status).inc() + if info.response: + status = str(info.response.status_code)[:1] + "xx" + metric.labels(type=type, status=status).inc() return instrumentation