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 <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-11 16:22:30 -08:00
parent 8788f99005
commit 0da11f068b
No known key found for this signature in database
GPG key ID: F7E46DED420788F3

View file

@ -79,9 +79,10 @@ def http_requests_total() -> Callable[[Info], None]:
method = scope.get("method") method = scope.get("method")
path = get_matching_route_path(base_scope, scope.get("router").routes) 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 return instrumentation
@ -95,7 +96,8 @@ def http_api_requests_total() -> Callable[[Info], None]:
def instrumentation(info: Info) -> None: def instrumentation(info: Info) -> None:
if info.request.url.path.rstrip("/") == "/rpc": if info.request.url.path.rstrip("/") == "/rpc":
type = info.request.query_params.get("type", "None") type = info.request.query_params.get("type", "None")
status = str(info.response.status_code)[:1] + "xx" if info.response:
metric.labels(type=type, status=status).inc() status = str(info.response.status_code)[:1] + "xx"
metric.labels(type=type, status=status).inc()
return instrumentation return instrumentation