Skip to content
Last9
Book demo

Django

Instrument Django applications with OpenTelemetry for automatic request tracing, ORM query monitoring, and full-stack observability — zero code changes required

Use OpenTelemetry to instrument your Django application and send telemetry data to Last9. opentelemetry-instrument handles instrumentation at startup — no code changes required.

Prerequisites

  • Python 3.8 or higher
  • Django 3.2 or higher
  • Last9 account with OTLP credentials

Installation

1. Install packages

pip install opentelemetry-distro opentelemetry-exporter-otlp

Then auto-detect and install instrumentation packages for your dependencies:

opentelemetry-bootstrap -a install

For a typical Django stack this installs:

opentelemetry-instrumentation-django
opentelemetry-instrumentation-psycopg2 # PostgreSQL
opentelemetry-instrumentation-redis # Redis cache / sessions
opentelemetry-instrumentation-celery # Celery tasks
opentelemetry-instrumentation-requests # Outbound HTTP
opentelemetry-instrumentation-logging
opentelemetry-instrumentation-urllib3

2. Set environment variables

export OTEL_SERVICE_NAME=django-app
export OTEL_EXPORTER_OTLP_ENDPOINT="$last9_otlp_endpoint"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=$last9_otlp_auth_header"
export OTEL_TRACES_EXPORTER=otlp
export OTEL_METRICS_EXPORTER=otlp
export OTEL_TRACES_SAMPLER="always_on"
export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=production"
export OTEL_LOG_LEVEL=error

3. Run with opentelemetry-instrument

opentelemetry-instrument python manage.py runserver

What Gets Instrumented Automatically

ComponentPackageWhat’s traced
HTTP requestsopentelemetry-instrumentation-djangoView method, URL pattern, status code, latency
Django ORMopentelemetry-instrumentation-djangoSQL queries, table, operation type
PostgreSQLopentelemetry-instrumentation-psycopg2Query text, db name, server address/port
Redisopentelemetry-instrumentation-redisCommand, key, latency
Celeryopentelemetry-instrumentation-celeryTask name, queue, retry count, result
Outbound HTTPopentelemetry-instrumentation-requestsMethod, URL, status code

Docker

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt && opentelemetry-bootstrap -a install
COPY . .
ENV OTEL_SERVICE_NAME=django-app
ENV OTEL_TRACES_SAMPLER=always_on
CMD ["opentelemetry-instrument", "gunicorn", "myproject.wsgi", "-c", "gunicorn.conf.py"]

Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
name: django-app
spec:
template:
spec:
containers:
- name: django-app
image: your-registry/django-app:latest
command: ["opentelemetry-instrument", "gunicorn", "myproject.wsgi", "-c", "gunicorn.conf.py"]
env:
- name: OTEL_SERVICE_NAME
value: "django-app"
- name: OTEL_EXPORTER_OTLP_ENDPOINT
valueFrom:
secretKeyRef:
name: last9-credentials
key: endpoint
- name: OTEL_EXPORTER_OTLP_HEADERS
valueFrom:
secretKeyRef:
name: last9-credentials
key: auth-header
- name: OTEL_RESOURCE_ATTRIBUTES
value: "deployment.environment=production"

View Traces and Metrics

After running your application, navigate to Trace Explorer and Metrics Explorer in Last9.