Still using Jaeger/Sentry? Uptrace is a distributed tracing tool that monitors performance, errors, and logs using OpenTelemetry.

Django OpenTelemetry Instrumentation

Prerequisites

  • Python 3.8+

Installation

To instrument Django app, you need a correspoding Django instrumentation:

pip install opentelemetry-instrumentation-django

Usage

Django instrumentation uses DJANGO_SETTINGS_MODULE env variable to find settings file. Django defines that variable in manage.py file so you should instrument Django app from that file:

# manage.py

from opentelemetry.instrumentation.django import DjangoInstrumentor

def main():
    # DjangoInstrumentor uses DJANGO_SETTINGS_MODULE to instrument the project.
    # Make sure the var is available before you call the DjangoInstrumentor.
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

    DjangoInstrumentor().instrument()

See exampleopen in new window for details.

Instrumenting PostgreSQL engine

You can configure Django to use PostgreSQL by changing settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

Internally, postgresql' engine uses psycopg2 library. To instrument psycopg2, you need a corresponding instrumentation:

pip install opentelemetry-instrumentation-psycopg2

Then actually instrument the library from the main function:

from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor

Psycopg2Instrumentor().instrument()

Instrumenting MySQL engine

You can configure Django to use MySQL by changing settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

Internally, mysql engine uses mysqlclientopen in new window library that implements Python Database API. To instrument any library that uses Python DB API, you need the opentelemetry-instrumentation-dbapi instrumentation:

pip install opentelemetry-instrumentation-dbapi

Then actually instrument the mysqlclient library from the main function:

import MySQLdb
from opentelemetry.instrumentation.dbapi import trace_integration

trace_integration(MySQLdb, "connect", "mysql")

Instrumenting SQLite engine

You can configure Django to use SQLite by changing settings.py:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}

Internally, sqlite3 Django engine uses sqlite3open in new window Python library. To instrument sqlite3, you need a corresponding instrumentation:

pip install opentelemetry-instrumentation-sqlite3

Then actually instrument the sqlite3 library from the main function:

from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor

SQLite3Instrumentor().instrument()

What's next?

Next, instrument more operations, for example, database queries and network calls. You can also learn about OpenTelemetry Python Tracing API to create your own instrumentations.

See also:

Last Updated: