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.

Compatibility

See Supported Python libraries and frameworks for the Python compatibility matrix.

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

Get the Observe Agent

Use the Add Data portal in the product to get the Observe Agent installed in your environment.

  1. From the left navigation rail, select Data & integrations > Add data.
  2. In the Observe Agent section, pick your environment, then follow the instructions to create a data ingest token and install the Observe Agent.

Depending on the environment you select, you are asked to select some options for installing and configuring the Observe Agent.

Customize your Observe Agent installation in Kubernetes with the following options:

  • Decide what data you want to collect: logs and/or metrics.
  • Whether or not to enable fleet monitoring.
  • Specify the environment value (deployment.environment.name) to enable usage and cost breakdowns.
  1. Follow the remainder of the instructions to install the Observe Agent and verify that your data is being received.

After the Observe Agent is installed, you can Instrument your Python application.

Configure zero-code auto-instrumentation for your Python apps

Perform the following steps to instrument your Python application.

Install the required dependencies

Run the following commands to install the required library dependencies:

pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install

Set environment variables

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

Instrument your Python app

Pick from one of the following options.

(Option 1) Instrument using zero-code auto-instrumentation

Run the following command to instrument your app:

opentelemetry-instrument \
    python myapp.py

(Option 2) Instrument using programmatic auto-instrumentation

Perform the following steps:

  1. Initialize OpenTelemetry:

    
    from opentelemetry.instrumentation.auto_instrumentation import initialize
    initialize()
  2. Run the application:

    python myapp.py

Instrument FastAPI apps

For FastAPI, initialize() must be called before importing FastAPI because instrumentation patches are applied at import time:

from opentelemetry.instrumentation.auto_instrumentation import initialize
initialize()

from fastapi import FastAPI

app = FastAPI()

Next steps

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