Skip to content
New issue

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

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

Already on GitHub? # to your account

Allow custom transaction names in asgi #3664

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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: 2 additions & 0 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
TRANSACTION_SOURCE_ROUTE,
TRANSACTION_SOURCE_URL,
TRANSACTION_SOURCE_COMPONENT,
TRANSACTION_SOURCE_CUSTOM,
)
from sentry_sdk.utils import (
ContextVar,
Expand Down Expand Up @@ -274,6 +275,7 @@ def event_processor(self, event, hint, asgi_scope):
].get("source") in [
TRANSACTION_SOURCE_COMPONENT,
TRANSACTION_SOURCE_ROUTE,
TRANSACTION_SOURCE_CUSTOM,
]
if not already_set:
name, source = self._get_transaction_name_and_source(
Expand Down
42 changes: 42 additions & 0 deletions tests/integrations/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ async def app(scope, receive, send):
return app


@pytest.fixture
def asgi3_custom_transaction_app():

async def app(scope, receive, send):
sentry_sdk.get_current_scope().set_transaction_name("foobar", source="custom")
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [
[b"content-type", b"text/plain"],
],
}
)

await send(
{
"type": "http.response.body",
"body": b"Hello, world!",
}
)

return app


def test_invalid_transaction_style(asgi3_app):
with pytest.raises(ValueError) as exp:
SentryAsgiMiddleware(asgi3_app, transaction_style="URL")
Expand Down Expand Up @@ -679,3 +704,20 @@ def dummy_traces_sampler(sampling_context):

async with TestClient(app) as client:
await client.get(request_url)


@pytest.mark.asyncio
async def test_custom_transaction_name(
sentry_init, asgi3_custom_transaction_app, capture_events
):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
app = SentryAsgiMiddleware(asgi3_custom_transaction_app)

async with TestClient(app) as client:
await client.get("/test")

(transaction_event,) = events
assert transaction_event["type"] == "transaction"
assert transaction_event["transaction"] == "foobar"
assert transaction_event["transaction_info"] == {"source": "custom"}
Loading