Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't suppress exception chain #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion starlette_prometheus/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -
except BaseException as e:
status_code = HTTP_500_INTERNAL_SERVER_ERROR
EXCEPTIONS.labels(method=method, path_template=path_template, exception_type=type(e).__name__).inc()
raise e from None
raise e
else:
status_code = response.status_code
after_time = time.perf_counter()
Expand Down
38 changes: 38 additions & 0 deletions tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def foo(request):
def bar(request):
raise ValueError("bar")

@app_.route("/exception-with-chain/")
def exception_with_chain(request):
try:
raise ValueError("Original Exception")
except ValueError as exc:
raise TypeError("New Exception") from exc

@app_.route("/foo/{bar}/")
def foobar(request):
return PlainTextResponse(f"Foo: {request.path_params['bar']}")
Expand Down Expand Up @@ -75,6 +82,37 @@ def test_view_exception(self, client):
assert 'starlette_requests_in_progress{method="GET",path_template="/bar/"} 0.0' in metrics_text
assert 'starlette_requests_in_progress{method="GET",path_template="/metrics/"} 1.0' in metrics_text

def test_view_exception_with_chain(self, client):
# Do a request
with pytest.raises(TypeError) as exc:
client.get("/exception-with-chain/")

# Get metrics
response = client.get("/metrics/")
metrics_text = response.content.decode()

# Asserts: Exception chain gets propagated
assert exc.value.args == ("New Exception",)
assert isinstance(exc.value.__cause__, ValueError)
assert exc.value.__cause__.args == ("Original Exception",)

# Asserts: Requests
assert 'starlette_requests_total{method="GET",path_template="/exception-with-chain/"} 1.0' in metrics_text

# Asserts: Responses
assert (
"starlette_exceptions_total{"
'exception_type="TypeError",method="GET",path_template="/exception-with-chain/"'
"} 1.0" in metrics_text
)
assert (
"starlette_responses_total{" 'method="GET",path_template="/exception-with-chain/",status_code="500"' "} 1.0" in metrics_text
)

# Asserts: Requests in progress
assert 'starlette_requests_in_progress{method="GET",path_template="/bar/"} 0.0' in metrics_text
assert 'starlette_requests_in_progress{method="GET",path_template="/metrics/"} 1.0' in metrics_text

def test_path_substitution(self, client):
# Do a request
client.get("/foo/baz/")
Expand Down