Skip to content

Use request.path_info to create Django transaction name #998

@OutOfFocus4

Description

@OutOfFocus4

Currently, the Django integration creates a transaction from the request URL by calling LEGACY_RESOLVER.resolve(request.path). This transforms URLS such as /test/1/ into /test/{pk}/.

However, if the Django application is served from a non-root path, such as /django/, request.path becomes /django/test/1/. Django only processes the /test/1/ part, which is accessible in request.path_info, but the transaction becomes /django/test/1/.

To demonstrate this, I created a new Django project and added the setting FORCE_SCRIPT_NAME = "/test" to settings.py.
I then created the following view:

from django.http.response import JsonResponse

from sentry_sdk.integrations.django.transactions import LEGACY_RESOLVER


def show_example(request, *args, **kwargs):
    return JsonResponse(
        {
            "current": LEGACY_RESOLVER.resolve(request.path),
            "correct": LEGACY_RESOLVER.resolve(request.path_info),
        }
    )

I added

path('<int:pk>/<var>/', views.show_example)

to the urlpatterns, then executed the runserver management command.

Here is the output of curl http://localhost:8000/1/@/:

{"current": "/test/1/@/", "correct": "/{pk}/{var}/"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions