Skip to content

Instantly share code, notes, and snippets.

@beniwohli
Last active February 5, 2018 19:41
Show Gist options
  • Save beniwohli/b494bd4b368ea4095dad3b629333ae58 to your computer and use it in GitHub Desktop.
Save beniwohli/b494bd4b368ea4095dad3b629333ae58 to your computer and use it in GitHub Desktop.

How to keep Django Rest Framework viewset actions separate on Opbeat

Opbeat by default uses the name of the Django view as the identifier of a transaction, combined with the request method. While this works well in general, it leads to confusing results with view sets from Django Rest Framework. It cobbles list and detail views together, as well as any custom actions you might have defined in your viewset.

You can work around this by adding the action of the viewset to the transaction name. Simply add the provided mixin to any view set:

class BooksViewSet(ViewSetTransactionNameMixin, viewsets.ModelViewSet):
    # your code here
class ViewSetTransactionNameMixin(object):
def initialize_request(self, request, *args, **kwargs):
"""
sets the correct name for Opbeat, taking the action into account
"""
drf_request = super(ViewSetTransactionNameMixin, self).initialize_request(
request, *args, **kwargs
)
module = self.__module__
view_name = self.__class__.__name__
request._opbeat_transaction_name = '{}.{}.{}'.format(
module, view_name, self.action
)
return drf_request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment