Instrumenting Django with OpenTelemetry

Prerequisites

  • Python 3.6+

Instrumenting Django app

git clone https://github.com/uptrace/uptrace-python.git
cd example/django
  • Step 2. Install the required dependencies:
pip install -r requirements.txt
  • Step 3. Edit manage.py to configure OpenTelemetry to export data to Uptrace:
import uptrace

def main():
    uptrace.configure_opentelemetry(
        # Copy DSN here or use UPTRACE_DSN env var.
        dsn="",
        service_name="myservice",
        service_version="1.0.0",
    )
  • Step 4. Edit manage.py to instrument the Django app:
from opentelemetry.instrumentation.django import DjangoInstrumentor

def main():
    # DjangoInstrumentor uses DJANGO_SETTINGS_MODULE to instrument the app. Make sure to define it.
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app_name.settings")

    # Instrument the app by adding middleware etc.
    DjangoInstrumentor().instrument()

    ...
UPTRACE_DSN="https://<token>@api.uptrace.dev/<project_id>" ./manage.py runserver

What is next?

By now, you should have a Django app with instrumented views. Next, instrumentopen in new window more operations (for example, database queries) or learn about OpenTelemetry API to create your own instrumentations.