Skip to main content

OpenTracing support for Django applications

Project description

https://travis-ci.org/opentracing-contrib/python-django.svg?branch=master https://img.shields.io/pypi/v/django_opentracing.svg https://img.shields.io/pypi/pyversions/django_opentracing.svg https://img.shields.io/pypi/dm/django_opentracing.svg

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.

If you are migrating from the 0.x series, you may want to read the list of breaking changes.

Installation

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 settings.py file:

import django_opentracing

# OpenTracing settings

# if not included, defaults to True.
# has to come before OPENTRACING_TRACING setting because python...
OPENTRACING_TRACE_ALL = True

# defaults to []
# only valid if OPENTRACING_TRACE_ALL == True
OPENTRACING_TRACED_ATTRIBUTES = ['arg1', 'arg2']

# Callable that returns an `opentracing.Tracer` implementation.
OPENTRACING_TRACER_CALLABLE = 'opentracing.Tracer'

# Parameters for the callable (Depending on the tracer implementation chosen)
OPENTRACING_TRACER_PARAMETERS = {
    'example-parameter-host': 'collector',
}

If you want to directly override the DjangoTracing used, you can use the following. This may cause import loops (See #10)

# some_opentracing_tracer can be any valid OpenTracing tracer implementation
OPENTRACING_TRACING = django_opentracing.DjangoTracing(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, OPENTRACING_TRACE_ALL needs to be set to True (the default). 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 settings.py file’s MIDDLEWARE_CLASSES at the top of the stack.

MIDDLEWARE_CLASSES = [
    'django_opentracing.OpenTracingMiddleware',
    ... # other middleware classes
]

Tracing Individual Requests

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

from django.conf import settings

tracing = settings.OPENTRACING_TRACING

@tracing.trace(optional_args)
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 settings.py file.

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

Note: If OPENTRACING_TRACE_ALL is set to True, 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 DjangoTracing.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:

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

Example

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

Breaking changes from 0.x

Starting with the 1.0 version, a few changes have taken place from previous versions:

  • DjangoTracer has been renamed to DjangoTracing, although DjangoTracer can be used still as a deprecated name. Likewise for OPENTRACING_TRACER being renamed to OPENTRACING_TRACING.

  • When using the middleware layer, OPENTRACING_TRACE_ALL defaults to True.

  • When no opentracing.Tracer is provided, DjangoTracing will rely on the global tracer.

Further Information

If you’re interested in learning more about the OpenTracing standard, please visit opentracing.io 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 community@opentracing.io.

Project details


Download files

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

Source Distribution

django_opentracing-1.1.0.tar.gz (24.5 kB view details)

Uploaded Source

Built Distributions

django_opentracing-1.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

django_opentracing-1.1.0-py2-none-any.whl (9.8 kB view details)

Uploaded Python 2

File details

Details for the file django_opentracing-1.1.0.tar.gz.

File metadata

  • Download URL: django_opentracing-1.1.0.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for django_opentracing-1.1.0.tar.gz
Algorithm Hash digest
SHA256 73559bf00facdcfdfb398a8ad76154ae902e0fd678db5b70bf1f74bc8f8df1a1
MD5 9c6a3e35a78901095227c99952f91380
BLAKE2b-256 1dcffdf089df0636604190501b585ba800beb0b3c355f0bc1ea4286210e7d447

See more details on using hashes here.

File details

Details for the file django_opentracing-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_opentracing-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for django_opentracing-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7be785401f7c608bf502c798d56cade256af134be7908e8e320ca12c1694fc48
MD5 85ff7fe50511dcc9795ff033799de848
BLAKE2b-256 d2944405a6e1ee8d383e4374dfae9a5103cd4ef94af3f9ed1200fbc625bef6a5

See more details on using hashes here.

File details

Details for the file django_opentracing-1.1.0-py2-none-any.whl.

File metadata

  • Download URL: django_opentracing-1.1.0-py2-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.15

File hashes

Hashes for django_opentracing-1.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 103a1501081fd4b2f897ec60ecab4d8f6ce044da096d407fe25999eb5e8933f2
MD5 e5dc9fa13a73266213b0a369c7544883
BLAKE2b-256 e9d5f84f0b6ccc1a403dd0dadbaa5c1f02b09e7bea166b6f5714327bd0b5165c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page