Automatically instrument your Python application with OpenTelemetry

This walkthrough shows how to add observability to your Python application using automatic instrumentation for OpenTelemetry Python.

Enrichment with OneAgent

It is currently not possible to enrich automatically instrumented services with host-relevant information. To achieve this, you'd need to switch to manual instrumentation.

Step 1 Get the Dynatrace access details

Determine the API base URL

For details on how to assemble the base OTLP endpoint URL, see Export with OTLP. The URL should end in /api/v2/otlp.

Get API access token

The access token for ingesting traces, logs, and metrics can be generated under Access Tokens.

Export with OTLP has more details on the format and the necessary access scopes.

Step 2 Instrument your application

The following example uses opentelemetry-distro to configure all available and applicable instrumentation libraries automatically. Instead, you could also skip steps 1 and 2 and selectively add instrumentation libraries by installing opentelemetry-instrumentation and the applicable instrumentation libraries manually.

  1. Use pip to install the following packages.

    pip install opentelemetry-distro
    pip install opentelemetry-exporter-otlp
  2. Run the following command to initialize and bootstrap the automatic instrumentation.

    opentelemetry-bootstrap -a install
  3. Configure the following environment variables to set the service and export details, substituting [URL] and [TOKEN] with the values for the base URL and access token.

    OTEL_EXPORTER_OTLP_ENDPOINT=[URL]
    OTEL_EXPORTER_OTLP_HEADERS="Authorization=Api-Token%20[TOKEN]"
    OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=delta
    OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
    OTEL_SERVICE_NAME="python-quickstart"
    URL encoding in environment variables

    Please note that the space in OTEL_EXPORTER_OTLP_HEADERS is encoded as %20 as the variable follows the correlation context convention and its value needs to be percent-encoded.

  4. Run your application with the opentelemetry-instrument agent.

    opentelemetry-instrument python myapp.py

Step 3 optional Add telemetry signals manually optional

As automatic instrumentation on Python does not provide pre-configured tracer and meter providers, you'd need to follow the initialization and instrumentation steps at Manually instrument your Python application with OpenTelemetry, if you wish to manually add custom signals (such as traces and metrics) on top of automatic instrumentation.

Step 4 Ensure context propagation

Context propagation is particularly important when network calls (for example, REST) are involved.

With automatic instrumentation, this should be automatically taken care of by the instrumentation libraries. If the used network libraries are not be covered by that, you would need to switch to manual instrumentation instead and handle propagation manually.

Step 5 optional Configure data capture to meet privacy requirements optional

While Dynatrace automatically captures all OpenTelemetry attributes, only attribute values specified in the allowlist are stored and displayed in the Dynatrace web UI. This prevents accidental storage of personal data, so you can meet your privacy requirements and control the amount of monitoring data stored.

To view your custom attributes, you need to allow them in the Dynatrace web UI first. To learn how to configure attribute storage and masking, see Attribute redaction.

Step 6 Verify data ingestion into Dynatrace

Once you have finished the instrumentation of your application, perform a couple of test actions to create and send demo traces, metrics, and logs and verify that they were correctly ingested into Dynatrace.

To do that for traces, go to Distributed Traces or Distributed Traces Classic (latest Dynatrace) and select the Ingested traces tab. If you use OneAgent, select PurePaths instead.

For metrics and logs, go to Metrics or Logs or Logs & Events (latest Dynatrace).