Skip to content

Instantly share code, notes, and snippets.

@beniwohli
Created September 13, 2018 14:22
Show Gist options
  • Save beniwohli/8bc26c43396d3fb3b2c38e0dad680633 to your computer and use it in GitHub Desktop.
Save beniwohli/8bc26c43396d3fb3b2c38e0dad680633 to your computer and use it in GitHub Desktop.
How to keep Django Rest Framework viewset actions separate on Elastic APM

How to keep Django Rest Framework viewset actions separate on Elastic APM

Elastic APM 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
import elasticapm
class ViewSetTransactionNameMixin(object):
def initialize_request(self, request, *args, **kwargs):
"""
sets the correct name for Elastic APM, taking the action into account
"""
drf_request = super(ViewSetTransactionNameMixin, self).initialize_request(
request, *args, **kwargs
)
module = self.__module__
view_name = self.__class__.__name__
elasticapm.set_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