Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

OpenTracing support for Django applications

Project Description

This package enables distributed tracing in Django projects via The OpenTracing Project. Once a production system contends with real concurrency or splits into many services, crucial (and formerly easy) tasks become difficult: user-facing latency optimization, root-cause analysis of backend errors, communication about distinct pieces of a now-distributed system, etc. Distributed tracing follows a request on its journey from inception to completion from mobile/browser all the way to the microservices.

As core services and libraries adopt OpenTracing, the application builder is no longer burdened with the task of adding basic tracing instrumentation to their own code. In this way, developers can build their applications with the tools they prefer and benefit from built-in tracing instrumentation. OpenTracing implementations exist for major distributed tracing systems and can be bound or swapped with a one-line configuration change.

If you want to learn more about the underlying python API, visit the python source code.


Run the following command:

$ pip install django_opentracing

Setting up Tracing

In order to implement tracing in your system, add the following lines of code to your site’s file:

import django_opentracing

# OpenTracing settings

# if not included, defaults to False
# has to come before OPENTRACING_TRACER setting because python...

# defaults to []
# only valid if OPENTRACING_TRACE_ALL == True

# some_opentracing_tracer can be any valid OpenTracing tracer implementation
OPENTRACING_TRACER = django_opentracing.DjangoTracer(some_opentracing_tracer),

Note: Valid request attributes to trace are listed [here]( When you trace an attribute, this means that created spans will have tags with the attribute name and the request’s value.

Tracing All Requests

In order to trace all requests, set OPENTRACING_TRACE_ALL = True. If you want to trace any attributes for all requests, then add them to OPENTRACING_TRACED_ATTRIBUTES. For example, if you wanted to trace the path and method, then set OPENTRACING_TRACED_ATTRIBUTES = [‘path’, ‘method’].

Tracing all requests uses the middleware django_opentracing.OpenTracingMiddleware, so add this to your file’s MIDDLEWARE_CLASSES at the top of the stack.

    ... # other middleware classes

Tracing Individual Requests

If you don’t want to trace all requests to your site, then you can use function decorators to trace individual view functions. This can be done by adding the following lines of code to (or any other file that has url handler functions):

from django.conf import settings

tracer = settings.OPENTRACING_TRACER

def some_view_func(request):
    ... #do some stuff

This tracing method doesn’t use middleware, so there’s no need to add it to your file.

The optional arguments allow for tracing of request attributes. For example, if you want to trace metadata, you could pass in @tracer.trace(‘META’) and request.META would be set as a tag on all spans for this view function.

Note: If you turn on OPENTRACING_TRACE_ALL, this decorator will be ignored, including any traced request attributes.

Accessing Spans Manually

In order to access the span for a request, we’ve provided an method DjangoTracer.get_span(request) that returns the span for the request, if it is exists and is not finished. This can be used to log important events to the span, set tags, or create child spans to trace non-RPC events.

Tracing an RPC

If you want to make an RPC and continue an existing trace, you can inject the current span into the RPC. For example, if making an http request, the following code will continue your trace across the wire:

def some_view_func(request):
    new_request = some_http_request
    current_span = tracer.get_span(request)
    text_carrier = {}
    opentracing_tracer.inject(span, opentracing.Format.TEXT_MAP, text_carrier)
    for k, v in text_carrier.iteritems():
    ... # make request


Here is an example of a Django application that acts as both a client and server, with integrated OpenTracing tracers.

Further Information

If you’re interested in learning more about the OpenTracing standard, please visit or join the mailing list. If you would like to implement OpenTracing in your project and need help, feel free to send us a note at

Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django_opentracing-0.1.16.tar.gz (6.5 kB) Copy SHA256 Checksum SHA256 Source Dec 11, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting