getsentry / sentry-python

The official Python SDK for Sentry.io
https://sentry.io/for/python/
MIT License
1.76k stars 459 forks source link

Add a span around Django rest framework authentication #3006

Open angusholder opened 3 weeks ago

angusholder commented 3 weeks ago

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.

sl0thentr0py commented 3 weeks ago

@angusholder makes sense, feel free to PR and add a simliar patch here https://github.com/getsentry/sentry-python/blob/411c9f31be419aa04a6fc5643716802453770bbb/sentry_sdk/integrations/django/__init__.py#L301-L317 if you want it quicker, otherwise I'll put it on our backlog