diff --git a/README.rst b/README.rst index e0deaff..3feecb2 100644 --- a/README.rst +++ b/README.rst @@ -54,6 +54,8 @@ If you want to directly override the ``DjangoTracing`` used, you can use the fol # some_opentracing_tracer can be any valid OpenTracing tracer implementation OPENTRACING_TRACING = django_opentracing.DjangoTracing(some_opentracing_tracer) + # for Django REST framework users + OPENTRACING_TRACING = django_opentracing.DjangoRestFrameworkTracing(some_opentracing_tracer) **Note:** Valid request attributes to trace are listed [here](https://docs.djangoproject.com/en/1.9/ref/request-response/#django.http.HttpRequest). When you trace an attribute, this means that created spans will have tags with the attribute name and the request's value. diff --git a/django_opentracing/tracing.py b/django_opentracing/tracing.py index c2c746a..1b54b10 100644 --- a/django_opentracing/tracing.py +++ b/django_opentracing/tracing.py @@ -145,6 +145,18 @@ def _call_start_span_cb(self, span, request): pass +class DjangoRestFrameworkTracing(DjangoTracing): + '''tracer for Django REST framework + ''' + def get_span(self, request): + ''' + @param request + Returns the span tracing this request + ''' + scope = self._current_spans.get(request._request, None) + return None if scope is None else scope.span + + def initialize_global_tracer(tracing): ''' Initialisation as per https://github.com/opentracing/opentracing-python/blob/9f9ef02d4ef7863fb26d3534a38ccdccf245494c/opentracing/__init__.py#L36 # noqa