From ad7ec0649cc12156c1d21c459adb725acda3b5c8 Mon Sep 17 00:00:00 2001 From: maguowei Date: Fri, 8 Mar 2019 17:36:47 +0800 Subject: [PATCH] add Django REST framework support --- README.rst | 2 ++ django_opentracing/tracing.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) 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