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-otlpThen auto-detect and install instrumentation packages for your dependencies:
opentelemetry-bootstrap -a installFor a typical Django stack this installs:
opentelemetry-instrumentation-djangoopentelemetry-instrumentation-psycopg2 # PostgreSQLopentelemetry-instrumentation-redis # Redis cache / sessionsopentelemetry-instrumentation-celery # Celery tasksopentelemetry-instrumentation-requests # Outbound HTTPopentelemetry-instrumentation-loggingopentelemetry-instrumentation-urllib32. Set environment variables
export OTEL_SERVICE_NAME=django-appexport OTEL_EXPORTER_OTLP_ENDPOINT="$last9_otlp_endpoint"export OTEL_EXPORTER_OTLP_HEADERS="Authorization=$last9_otlp_auth_header"export OTEL_TRACES_EXPORTER=otlpexport OTEL_METRICS_EXPORTER=otlpexport OTEL_TRACES_SAMPLER="always_on"export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=production"export OTEL_LOG_LEVEL=error3. Run with opentelemetry-instrument
opentelemetry-instrument python manage.py runserverGunicorn forks worker processes after boot, which resets OTel’s global state. Use a post_fork hook to re-initialize in each worker.
Create gunicorn.conf.py:
from opentelemetry import metrics, tracefrom opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporterfrom opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporterfrom opentelemetry.sdk.metrics import MeterProviderfrom opentelemetry.sdk.metrics.export import PeriodicExportingMetricReaderfrom opentelemetry.sdk.resources import Resourcefrom opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.sdk.trace.export import BatchSpanProcessor
workers = 4bind = "0.0.0.0:8000"
def post_fork(server, worker): resource = Resource.create({"service.name": "django-app"})
tp = TracerProvider(resource=resource) tp.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) trace.set_tracer_provider(tp)
reader = PeriodicExportingMetricReader(OTLPMetricExporter()) mp = MeterProvider(resource=resource, metric_readers=[reader]) metrics.set_meter_provider(mp)opentelemetry-instrument gunicorn myproject.wsgi -c gunicorn.conf.pyopentelemetry-instrument uwsgi --http :8000 --module myproject.wsgi --master --processes 4For Django Channels or async Django:
opentelemetry-instrument uvicorn myproject.asgi:application --host 0.0.0.0 --port 8000What Gets Instrumented Automatically
| Component | Package | What’s traced |
|---|---|---|
| HTTP requests | opentelemetry-instrumentation-django | View method, URL pattern, status code, latency |
| Django ORM | opentelemetry-instrumentation-django | SQL queries, table, operation type |
| PostgreSQL | opentelemetry-instrumentation-psycopg2 | Query text, db name, server address/port |
| Redis | opentelemetry-instrumentation-redis | Command, key, latency |
| Celery | opentelemetry-instrumentation-celery | Task name, queue, retry count, result |
| Outbound HTTP | opentelemetry-instrumentation-requests | Method, URL, status code |
Docker
FROM python:3.12-slim
WORKDIR /appCOPY requirements.txt .RUN pip install -r requirements.txt && opentelemetry-bootstrap -a install
COPY . .
ENV OTEL_SERVICE_NAME=django-appENV OTEL_TRACES_SAMPLER=always_on
CMD ["opentelemetry-instrument", "gunicorn", "myproject.wsgi", "-c", "gunicorn.conf.py"]Kubernetes Deployment
apiVersion: apps/v1kind: Deploymentmetadata: name: django-appspec: 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.