Monitor Django with OpenTelemetry

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites.

In this article you will learn how to monitor and optimize Django performance using OpenTelemetry observability framework.

What is OpenTelemetry?

OpenTelemetryopen in new window is a vendor-neutral standard that allows you to collect and export tracesopen in new window, logsopen in new window, and metricsopen in new window.

OpenTelemetry is a community-driven open source project that offers several components:

OpenTelemetry API and SDK are available for most programming languages and allows to send telemetry data to any tracing toolopen in new window of your choice.


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

pip install opentelemetry-instrumentation-django


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


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")


See exampleopen in new window for details.

Instrumenting PostgreSQL engine

You can configure Django to use PostgreSQL by changing

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '',
        '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


Instrumenting MySQL engine

You can configure Django to use MySQL by changing

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '',
        '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

    "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


What is Uptrace?

Need a backend to store telemetry data collected by OpenTelemetry instrumentations?

Uptrace is an open source APM for OpenTelemetry with an intuitive query builder, rich dashboards, automatic alerts, and integrations for most languages and frameworks.

You can get startedopen in new window with Uptrace by downloading a DEB/RPM package or a pre-compiled Go binary.

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: