Skip to content

Commit 424a8b9

Browse files
authored
fix(django): Send correct "url" transaction source if Django resolver fails to resolve (#1525)
1 parent 1ab5d08 commit 424a8b9

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sentry_sdk.hub import Hub, _should_send_default_pii
1010
from sentry_sdk.scope import add_global_event_processor
1111
from sentry_sdk.serializer import add_global_repr_processor
12-
from sentry_sdk.tracing import SOURCE_FOR_STYLE
12+
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_URL
1313
from sentry_sdk.tracing_utils import record_sql_queries
1414
from sentry_sdk.utils import (
1515
HAS_REAL_CONTEXTVARS,
@@ -323,12 +323,10 @@ def _patch_django_asgi_handler():
323323
def _set_transaction_name_and_source(scope, transaction_style, request):
324324
# type: (Scope, str, WSGIRequest) -> None
325325
try:
326-
transaction_name = ""
326+
transaction_name = None
327327
if transaction_style == "function_name":
328328
fn = resolve(request.path).func
329-
transaction_name = (
330-
transaction_from_function(getattr(fn, "view_class", fn)) or ""
331-
)
329+
transaction_name = transaction_from_function(getattr(fn, "view_class", fn))
332330

333331
elif transaction_style == "url":
334332
if hasattr(request, "urlconf"):
@@ -338,9 +336,15 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
338336
else:
339337
transaction_name = LEGACY_RESOLVER.resolve(request.path_info)
340338

339+
if transaction_name is None:
340+
transaction_name = request.path_info
341+
source = TRANSACTION_SOURCE_URL
342+
else:
343+
source = SOURCE_FOR_STYLE[transaction_style]
344+
341345
scope.set_transaction_name(
342346
transaction_name,
343-
source=SOURCE_FOR_STYLE[transaction_style],
347+
source=source,
344348
)
345349
except Exception:
346350
pass

sentry_sdk/integrations/django/transactions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ def resolve(
127127
path, # type: str
128128
urlconf=None, # type: Union[None, Tuple[URLPattern, URLPattern, URLResolver], Tuple[URLPattern]]
129129
):
130-
# type: (...) -> str
130+
# type: (...) -> Optional[str]
131131
resolver = get_resolver(urlconf)
132132
match = self._resolve(resolver, path)
133-
return match or path
133+
return match
134134

135135

136136
LEGACY_RESOLVER = RavenResolver()

tests/integrations/django/test_basic.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,27 +469,36 @@ def test_django_connect_breadcrumbs(
469469

470470

471471
@pytest.mark.parametrize(
472-
"transaction_style,expected_transaction,expected_source",
472+
"transaction_style,client_url,expected_transaction,expected_source,expected_response",
473473
[
474-
("function_name", "tests.integrations.django.myapp.views.message", "component"),
475-
("url", "/message", "route"),
474+
(
475+
"function_name",
476+
"/message",
477+
"tests.integrations.django.myapp.views.message",
478+
"component",
479+
b"ok",
480+
),
481+
("url", "/message", "/message", "route", b"ok"),
482+
("url", "/404", "/404", "url", b"404"),
476483
],
477484
)
478485
def test_transaction_style(
479486
sentry_init,
480487
client,
481488
capture_events,
482489
transaction_style,
490+
client_url,
483491
expected_transaction,
484492
expected_source,
493+
expected_response,
485494
):
486495
sentry_init(
487496
integrations=[DjangoIntegration(transaction_style=transaction_style)],
488497
send_default_pii=True,
489498
)
490499
events = capture_events()
491-
content, status, headers = client.get(reverse("message"))
492-
assert b"".join(content) == b"ok"
500+
content, status, headers = client.get(client_url)
501+
assert b"".join(content) == expected_response
493502

494503
(event,) = events
495504
assert event["transaction"] == expected_transaction

tests/integrations/django/test_transactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
def test_legacy_resolver_no_match():
3131
resolver = RavenResolver()
3232
result = resolver.resolve("/foo/bar", example_url_conf)
33-
assert result == "/foo/bar"
33+
assert result is None
3434

3535

3636
def test_legacy_resolver_complex_match():

0 commit comments

Comments
 (0)