Skip to content

Commit

Permalink
contrib/asgi: fix outcome for 500 responses without exception
Browse files Browse the repository at this point in the history
Set the outcome based on the status code of the http.response.start
ASGI message instead of relying only on the raise of an exception.
  • Loading branch information
xrmx committed Jun 7, 2024
1 parent d0ab4ea commit 3e33d8f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions elasticapm/contrib/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async def wrapped_send(message) -> None:
await set_context(lambda: middleware.get_data_from_response(message, constants.TRANSACTION), "response")
result = "HTTP {}xx".format(message["status"] // 100)
elasticapm.set_transaction_result(result, override=False)
elasticapm.set_transaction_outcome(http_status_code=message["status"], override=False)
await send(message)

return wrapped_send
Expand Down
5 changes: 5 additions & 0 deletions tests/contrib/asgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ async def boom() -> None:
@app.route("/body")
async def json():
return jsonify({"hello": "world"})


@app.route("/500", methods=["GET"])
async def error():
return "KO", 500
17 changes: 17 additions & 0 deletions tests/contrib/asgi/asgi_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ async def test_transaction_span(instrumented_app, elasticapm_client):
assert span["sync"] == False


@pytest.mark.asyncio
async def test_transaction_span(instrumented_app, elasticapm_client):
async with async_asgi_testclient.TestClient(instrumented_app) as client:
resp = await client.get("/500")
assert resp.status_code == 500
assert resp.text == "KO"

assert len(elasticapm_client.events[constants.TRANSACTION]) == 1
assert len(elasticapm_client.events[constants.SPAN]) == 0
transaction = elasticapm_client.events[constants.TRANSACTION][0]
assert transaction["name"] == "GET unknown route"
assert transaction["result"] == "HTTP 5xx"
assert transaction["outcome"] == "failure"
assert transaction["context"]["request"]["url"]["full"] == "/500"
assert transaction["context"]["response"]["status_code"] == 500


@pytest.mark.asyncio
async def test_transaction_ignore_url(instrumented_app, elasticapm_client):
elasticapm_client.config.update("1", transaction_ignore_urls="/foo*")
Expand Down

0 comments on commit 3e33d8f

Please sign in to comment.