Automatically instrument your Python application with OpenTelemetry
This walkthrough shows how to add observability to your Python application using automatic instrumentation for OpenTelemetry Python.
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.
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.
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.
-
Use pip to install the following packages.
pip install opentelemetry-distropip install opentelemetry-exporter-otlp -
Run the following command to initialize and bootstrap the automatic instrumentation.
opentelemetry-bootstrap -a install -
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=deltaOTEL_EXPORTER_OTLP_PROTOCOL=http/protobufOTEL_SERVICE_NAME="python-quickstart"URL encoding in environment variablesPlease 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. -
Run your application with the
opentelemetry-instrument
agent.opentelemetry-instrument python myapp.py
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.
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.
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.
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).