Instrumenting Django with OpenTelemetry


  • Python 3.6+

Instrumenting Django app

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

def main():
        # Copy DSN here or use UPTRACE_DSN env var.
  • Step 4. Edit 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.

UPTRACE_DSN="https://<token><project_id>" ./ 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.