Instrument Python applications with OpenTelemetry

This guide shows how to instrument your Python application with OpenTelemetry and export the traces to Dynatrace. To learn more about how Dynatrace works with OpenTelemetry, see Send data to Dynatrace with OpenTelemetry.

Prerequisites

The following prerequisites and limitations apply:

  • Dynatrace version 1.222+
  • W3C Trace Context is enabled
    1. From the Dynatrace menu, go to Settings and select Server-side service monitoring > Deep monitoring > Distributed tracing.
    2. Turn on Send W3C Trace Context HTTP headers.

Instrument the application

Dynatrace uses OpenTelemetry Trace Ingest to provide end-to-end visibility into your Python applications. To instrument the application:

  1. Install the required packages
  2. Get the endpoint and authentication token
  3. Configure the OpenTelemetry Exporter
  4. Run the auto-instrumentation

1. Install the required packages

pip install opentelemetry-sdk
pip install opentelemetry-instrumentation
pip install opentelemetry-exporter-otlp-proto-http

Go to the OpenTelemetry repository, find the framework that your app is using, and install the corresponding package using the following command:

pip install opentelemetry-instrumentation-<USED_FRAMEWORK>

2. Get the endpoint and authentication token

To determine the endpoint to which your app will send traces

  1. Open Dynatrace.
  2. Check the address line of your browser. The URL will match one of the following patterns:
    • Dynatrace SaaS: https://{your-environment-id}.live.dynatrace.com/...
    • Dynatrace Managed: https://{your-domain}/e/{your-environment-id}/...
  3. Replace the ... part with api/v2/otlp/v1/traces to get the URL you will need in section 3 below.
    • Dynatrace SaaS: https://{your-environment-id}.live.dynatrace.com/api/v2/otlp/v1/traces
    • Dynatrace Managed: https://{your-domain}/e/{your-environment-id}/api/v2/otlp/v1/traces

To create an authentication token

  1. In the Dynatrace menu, go to Access tokens and select Generate new token.
  2. Provide a Token name.
  3. In the Search scopes box, search for Ingest OpenTelemetry traces and select the checkbox.
  4. Select Generate token.
  5. Select Copy to copy the token to your clipboard.
  6. Save the token in a safe place; you can't display it again, and you will need it in section 3 below.

3. Add the OpenTelemetry tracer to your code

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
    OTLPSpanExporter,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    BatchSpanProcessor,
)

span_exporter = OTLPSpanExporter(
    endpoint="<URL>", #TODO replace <URL> with the URL as determined in section 2 above
    headers={
        "Authorization": "Api-Token <TOKEN>" #TODO replace <TOKEN> with the authentication token created in section 2 above
    },
)

resource = Resource.create({
    #customizable resource attributes
    "service.name": "python-quickstart",
    "service.version": "1.0.1"
})

trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer_provider().get_tracer(__name__)

trace.get_tracer_provider().add_span_processor(
    BatchSpanProcessor(span_exporter)
)

4. Run your app

Use opentelemetry-instrument to run your app:

opentelemetry-instrument python app.py
  • Note that, depending on your system, you might need to run python3 instead.

Verify that the traces are ingested into Dynatrace

A few minutes after restarting your app, look for your spans:

  • In the Dynatrace menu, go to Distributed traces and select the Ingested traces tab.
  • Your spans will be part of an existing PurePath, if the root of your call is already being monitored by the OneAgent.

If your app is not getting any traffic, there will be no traces.

(Optional) Configure data capture to meet privacy requirements

While Dynatrace automatically captures all OpenTelemetry resource and span 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 that's stored.

To view your custom span attributes, you need to allow them in the Dynatrace web UI first:

  • Span attributes In the Dynatrace menu, go to Settings and select Server-side service monitoring > Span attributes.
  • Resource attributes In the Dynatrace menu, go to Settings and select Server-side service monitoring > Resource attributes.