Skip to content

Commit

Permalink
fix(django): Proper transaction names for i18n routes (#3104)
Browse files Browse the repository at this point in the history
`pattern.pattern._route` for i18n'd Django routes is a proxy object rather than a string. This causes an exception in the resolver, leading to the transaction not getting a proper name but rather falling back to the default `Generic WSGI request`.

The string representation of the proxy object is the actual desired endpoint route, so let's use that.
  • Loading branch information
sentrivana authored May 27, 2024
1 parent 651f84d commit b496a71
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/django/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _simplify(self, pattern):
and isinstance(pattern.pattern, RoutePattern)
):
return self._new_style_group_matcher.sub(
lambda m: "{%s}" % m.group(2), pattern.pattern._route
lambda m: "{%s}" % m.group(2), str(pattern.pattern._route)
)

result = get_regex(pattern).pattern
Expand Down
12 changes: 12 additions & 0 deletions tests/integrations/django/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
import django
from django.utils.translation import pgettext_lazy


# django<2.0 has only `url` with regex based patterns.
Expand Down Expand Up @@ -116,3 +117,14 @@ def test_resolver_path_no_converter():
resolver = RavenResolver()
result = resolver.resolve("/api/v4/myproject", url_conf)
assert result == "/api/v4/{project_id}"


@pytest.mark.skipif(
django.VERSION < (2, 0),
reason="Django>=2.0 required for path patterns",
)
def test_resolver_path_with_i18n():
url_conf = (path(pgettext_lazy("url", "pgettext"), lambda x: ""),)
resolver = RavenResolver()
result = resolver.resolve("/pgettext", url_conf)
assert result == "/pgettext"

0 comments on commit b496a71

Please sign in to comment.