OpenTelemetry Django Instrumentation

Instrument django to trace Django applications.

SQLCOMMENTER

You can optionally configure Django instrumentation to enable sqlcommenter which enriches the query with contextual information.

Usage

from opentelemetry.instrumentation.django import DjangoInstrumentor

DjangoInstrumentor().instrument(is_sql_commentor_enabled=True)

For example,

Invoking Users().objects.all() will lead to sql query "select * from auth_users" but when SQLCommenter is enabled
the query will get appended with some configurable tags like "select * from auth_users /*metrics=value*/;"

SQLCommenter Configurations

We can configure the tags to be appended to the sqlquery log by adding below variables to the settings.py

SQLCOMMENTER_WITH_FRAMEWORK = True(Default) or False

For example, :: Enabling this flag will add django framework and it’s version which is /framework=’django%3A2.2.3/

SQLCOMMENTER_WITH_CONTROLLER = True(Default) or False

For example, :: Enabling this flag will add controller name that handles the request /controller=’index’/

SQLCOMMENTER_WITH_ROUTE = True(Default) or False

For example, :: Enabling this flag will add url path that handles the request /route=’polls/’/

SQLCOMMENTER_WITH_APP_NAME = True(Default) or False

For example, :: Enabling this flag will add app name that handles the request /app_name=’polls’/

SQLCOMMENTER_WITH_OPENTELEMETRY = True(Default) or False

For example, :: Enabling this flag will add opentelemetry traceparent /traceparent=’00-fd720cffceba94bbf75940ff3caaf3cc-4fd1a2bdacf56388-01’/

SQLCOMMENTER_WITH_DB_DRIVER = True(Default) or False

For example, :: Enabling this flag will add name of the db driver /db_driver=’django.db.backends.postgresql’/

Usage

from opentelemetry.instrumentation.django import DjangoInstrumentor

DjangoInstrumentor().instrument()

Configuration

Exclude lists

To exclude certain URLs from being tracked, set the environment variable OTEL_PYTHON_DJANGO_EXCLUDED_URLS (or OTEL_PYTHON_EXCLUDED_URLS as fallback) with comma delimited regexes representing which URLs to exclude.

For example,

export OTEL_PYTHON_DJANGO_EXCLUDED_URLS="client/.*/info,healthcheck"

will exclude requests such as https://site/client/123/info and https://site/xyz/healthcheck.

Request attributes

To extract certain attributes from Django’s request object and use them as span attributes, set the environment variable OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS to a comma delimited list of request attribute names.

For example,

export OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS='path_info,content_type'

will extract path_info and content_type attributes from every traced request and add them as span attritbues.

Django Request object reference: https://docs.djangoproject.com/en/3.1/ref/request-response/#attributes

Request and Response hooks

The instrumentation supports specifying request and response hooks. These are functions that get called back by the instrumentation right after a Span is created for a request and right before the span is finished while processing a response. The hooks can be configured as follows:

def request_hook(span, request):
    pass

def response_hook(span, request, response):
    pass

DjangoInstrumentation().instrument(request_hook=request_hook, response_hook=response_hook)

Django Request object: https://docs.djangoproject.com/en/3.1/ref/request-response/#httprequest-objects Django Response object: https://docs.djangoproject.com/en/3.1/ref/request-response/#httpresponse-objects

Capture HTTP request and response headers

You can configure the agent to capture predefined HTTP headers as span attributes, according to the semantic convention.

Request headers

To capture predefined HTTP request headers as span attributes, set the environment variable OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST to a comma-separated list of HTTP header names.

For example,

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="content_type,custom_request_header"

will extract content_type and custom_request_header from request headers and add them as span attributes.

It is recommended that you should give the correct names of the headers to be captured in the environment variable. Request header names in django are case insensitive. So, giving header name as CUStom_Header in environment variable will be able capture header with name custom-header.

The name of the added span attribute will follow the format http.request.header.<header_name> where <header_name> being the normalized HTTP header name (lowercase, with - characters replaced by _ ). The value of the attribute will be single item list containing all the header values.

Example of the added span attribute, http.request.header.custom_request_header = ["<value1>,<value2>"]

Response headers

To capture predefined HTTP response headers as span attributes, set the environment variable OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE to a comma-separated list of HTTP header names.

For example,

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="content_type,custom_response_header"

will extract content_type and custom_response_header from response headers and add them as span attributes.

It is recommended that you should give the correct names of the headers to be captured in the environment variable. Response header names captured in django are case insensitive. So, giving header name as CUStomHeader in environment variable will be able capture header with name customheader.

The name of the added span attribute will follow the format http.response.header.<header_name> where <header_name> being the normalized HTTP header name (lowercase, with - characters replaced by _ ). The value of the attribute will be single item list containing all the header values.

Example of the added span attribute, http.response.header.custom_response_header = ["<value1>,<value2>"]

API

class opentelemetry.instrumentation.django.DjangoInstrumentor(*args, **kwargs)[source]

Bases: opentelemetry.instrumentation.instrumentor.BaseInstrumentor

An instrumentor for Django

See BaseInstrumentor

instrumentation_dependencies()[source]

Return a list of python packages with versions that the will be instrumented.

The format should be the same as used in requirements.txt or setup.py.

For example, if an instrumentation instruments requests 1.x, this method should look like:

def instrumentation_dependencies(self) -> Collection[str]:

return [‘requests ~= 1.0’]

This will ensure that the instrumentation will only be used when the specified library is present in the environment.

Return type

Collection[str]