Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add a span around Django rest framework authentication #3006

Open
angusholder opened this issue Apr 23, 2024 · 1 comment
Open

Add a span around Django rest framework authentication #3006

angusholder opened this issue Apr 23, 2024 · 1 comment
Labels

Comments

@angusholder
Copy link

Problem Statement

In Sentry Performance, when I look at the queries list in a performance event of an API view, the view.render event always begins with a few of the same queries, for looking up the auth token, and are rarely relevant. It would be better if those auth queries were in a separate span, to reduce visual noise.

Solution Brainstorm

I currently subclass the authentication class and add a span myself, like this:

class TracedTokenAuthentication(rest_framework.authentication.TokenAuthentication):
    def authenticate(self, request):
        with sentry_sdk.start_span(op='authenticate'):
            return super().authenticate(request)

The disadvantage of this is you can use multiple authentications, and each would need modifying. A better place for the span is probably rest_framework.request.Request._authenticate, which loops over all configured authentications.

@angusholder angusholder added the enhancement New feature or request label Apr 23, 2024
@sl0thentr0py
Copy link
Member

sl0thentr0py commented Apr 24, 2024

@angusholder makes sense, feel free to PR and add a simliar patch here

try:
from rest_framework.views import APIView # type: ignore
except ImportError:
pass
else:
old_drf_initial = APIView.initial
def sentry_patched_drf_initial(self, request, *args, **kwargs):
# type: (APIView, Any, *Any, **Any) -> Any
with capture_internal_exceptions():
request._request._sentry_drf_request_backref = weakref.ref(
request
)
pass
return old_drf_initial(self, request, *args, **kwargs)
APIView.initial = sentry_patched_drf_initial

if you want it quicker, otherwise I'll put it on our backlog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

2 participants