Send Python application data to Observe

This page describes how to install the Observe Agent, then instrument your Python applications to send data to Observe.

Instead of “zero-code” auto-instrumentation for Python, we recommend one-line programmatic auto-instrumentation for the following reasons:

  • This gives you more control over initializing it early in your code.
  • Easily pluggable into process fork calls in popular frameworks like Gunicorn / uvicorn.
  • Makes it compatible with OpenLLMetry instrumentation for LLM Explorer.

For more information about Gunicorn / uWSGI, see Working with Fork Process Models in the OpenTelemetry Python documentation.

To view the list of auto-instrumented libraries and compatible versions, see bootstrap_gen.py in the OpenTelemetry documentation on GitHub.

To manually instrument custom logic, see Instrumentation for Python in the OpenTelemetry documentation.

Instrument your Python application

Perform the following steps to instrument your Python application.

  1. Install the required library dependencies:
pip install opentelemetry-distro opentelemetry-exporter-otlp
  1. Import and initialize the auto-instrumentor in the app’s entrypoint. Note that OpenTelemetry should take precedence all other processes.
from opentelemetry.instrumentation import auto_instrumentation

auto_instrumentation.initialize()
  1. Set the following environment variables. Replace the placeholders such as ${YOUR_SERVICE_NAME} with the actual information from your environment.
OTEL_SERVICE_NAME=${YOUR_SERVICE_NAME}
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=${YOUR_APP_ENVIRONMENT}
OTEL_EXPORTER_OTLP_ENDPOINT=http://${YOUR_OBSERVE_AGENT_ENDPOINT}:4318 
OTEL_TRACES_EXPORTER=otlp
OTEL_METRICS_EXPORTER=otlp
OTEL_LOGS_EXPORTER=otlp 
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true

Run your instrumented app

opentelemetry-instrument \
    python myapp.py

Next steps

Navigate to Traces in your Observe tenant to view traces from your application.