Skip to content

Fix transaction naming for django-ninja endpoints with transaction_style="function_name" #5474

Open
avilaton wants to merge 1 commit intogetsentry:masterfrom
avilaton:transaction-naming-django-ninja
Open

Fix transaction naming for django-ninja endpoints with transaction_style="function_name" #5474
avilaton wants to merge 1 commit intogetsentry:masterfrom
avilaton:transaction-naming-django-ninja

Conversation

@avilaton
Copy link

@avilaton avilaton commented Feb 17, 2026

Description

DISCLAIMER: We patched django using a middleware to fix this and wanted to contribute it back to sentry, delegated this to copilot and it looks ok, no intention to be ai-slop, just wanted to raise the issue here.

Django-ninja endpoints were all grouped under ninja.operation.PathView._sync_view when using transaction_style="function_name", making it impossible to distinguish between different API endpoints in Sentry.

Django's resolve() returns the PathView wrapper function instead of the actual endpoint. The wrapper is shared across all endpoints on the same path, causing the grouping issue.

Changes

  • Added _unwrap_django_ninja_view() - Detects PathView wrappers by checking __qualname__, extracts the PathView instance from the closure, matches the HTTP method to the correct Operation, and returns the actual endpoint function
  • Modified _set_transaction_name_and_source() - Calls unwrapper when transaction_style="function_name" before extracting the transaction name
  • Added integration tests - Validates both function_name and url transaction styles work correctly with ninja endpoints

Example

Before this change, all these endpoints would be grouped as ninja.operation.PathView._sync_view:

@api.get("/users")
def list_users(request):
    ...

@api.post("/users")
def create_user(request):
    ...

After this change, they get distinct names: myapp.views.list_users and myapp.views.create_user.

…yle="function_name" (#1)

* Initial plan

* Add django-ninja support for transaction_style=function_name

Co-authored-by: avilaton <2209022+avilaton@users.noreply.github.com>

* Address code review feedback: fix exception handling and improve test

Co-authored-by: avilaton <2209022+avilaton@users.noreply.github.com>

* Fix trailing whitespace in docstring

Co-authored-by: avilaton <2209022+avilaton@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: avilaton <2209022+avilaton@users.noreply.github.com>
@github-actions
Copy link
Contributor

github-actions bot commented Feb 17, 2026

Semver Impact of This PR

None (no version bump detected)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Internal Changes 🔧

  • (openai-agents) Expect new tool fields by alexander-alderman-webb in #5471
  • (repo) Add .serena to .gitignore by ericapisani in #5464

Other

  • Fix transaction naming for django-ninja endpoints with transaction_style="function_name" by avilaton in #5474

🤖 This preview updates automatically when you update the PR.

@avilaton avilaton marked this pull request as ready for review February 17, 2026 17:58
@avilaton avilaton requested a review from a team as a code owner February 17, 2026 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments