Instrument Python applications with OpenTelemetry Metrics
This guide shows how to instrument your Python application with OpenTelemetry and export the metrics to Dynatrace.
- To learn more about how Dynatrace works with OpenTelemetry, see Send data to Dynatrace with OpenTelemetry.
- To learn how to export traces to Dynatrace with OpenTelemetry, see Instrument Python applications with OpenTelemetry.
Overview
To monitor your Python application with OpenTelemetry
Instrument your application
Send the data to Dynatrace
Verify that the traces are ingested into Dynatrace
Instrument your application
To instrument manually, install the packages below and add the code snippet below to any Python method you want to monitor.
- Set names for the meter and the instrument.
- Add a description and attributes to the instrument as you see fit.
pip install opentelemetry-exporter-otlp-proto-http
pip install opentelemetry-instrumentation
from opentelemetry import metrics as metrics
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
PeriodicExportingMetricReader,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.metrics import set_meter_provider, get_meter_provider
import random
merged = dict()
for name in ["dt_metadata_e617c525669e072eebe3d0f08212e8f2.json", "/var/lib/dynatrace/enrichment/dt_metadata.json"]:
try:
data = ''
with open(name) as f:
data = json.load(f if name.startswith("/var") else open(f.read()))
merged.update(data)
except:
pass
merged.update({
"service.name": "python-quickstart",
"service.version": "1.0.1",
})
resource = Resource.create(merged)
reader = PeriodicExportingMetricReader(exporter) #The exporter is set in the next step
provider = MeterProvider(metric_readers=[reader], resource=resource) #Register the reader with a provider
set_meter_provider(provider)
meter = get_meter_provider().get_meter("my-meter", "0.1.2") #TODO Replace with the name of your meter
counter = meter.create_counter(
name="my-counter", #TODO Replace with the name of your instrument
description="This is my cool Counter." #TODO Replace with the description of your instrument
)
Add the code below to any call you want to monitor. You can also add attributes to each measurement. The data is generated randomly and needs to be replaced by your data. In this case, we used a Counter instrument and an UpDownCounter instrument. To find the instrument that fits your needs, see the official OpenTelemetry documentation.
attributes = {"my-key-1": "my-value-1", "my-key-2": "my-value-2"} #TODO Replace with your own attributes
counter.add(random.randint(0,1000), attributes)
Please note that attributes can take a couple of minutes to be shown in the Dynatrace UI.
Send data to Dynatrace
To send data to Dynatrace, you need to add the packages and then add and configure the code snippet below in your Python application code.
If the following code snippet isn't registered before creating the instruments, metrics will not be exported.
Dynatrace version 1.254+
pip install opentelemetry-exporter-otlp-proto-http
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
exporter = OTLPMetricExporter(
endpoint="<URL>", #TODO Replace <URL> to your SaaS/Managed-URL as mentioned in the next step
headers={"Authorization": "<TOKEN>"}, #TODO Replace <TOKEN> with your API Token as mentioned in the next step
preferred_temporality={Counter: AggregationTemporality.DELTA})
To make sure your data arrives where it should be, you need to define the correct endpoint and token.
- To set the endpoint
- Use your Environment ID to set the endpoint to which your app will send traces as follows:
- Dynatrace SaaS
https://{your-environment-id}.live.dynatrace.com/api/v2/otlp/v1/metrics
- Dynatrace Managed
https://{your-domain}/e/{your-environment-id}/api/v2/otlp/v1/metrics
- Dynatrace ActiveGate
https://{your-activegate-endpoint}/e/{your-environment-id}/api/v2/otlp/v1/metrics
- You may need to include the port to your ActiveGate endpoint. For example:
https://{your-activegate-endpoint}:9999/e/{your-environment-id}/api/v2/otlp/v1/metrics
- If you are running a containerized ActiveGate, you need to use the FQDN of it. For example:
https://{your-activegate-service-name}.dynatrace.svc.cluster.local/e/{your-environment-id}/api/v2/otlp/v1/metrics
- You may need to include the port to your ActiveGate endpoint. For example:
- Dynatrace SaaS
- Replace
<URL>
in the code snippet above with your endpoint.
- Use your Environment ID to set the endpoint to which your app will send traces as follows:
- To create an authentication token
- In the Dynatrace menu, go to Access tokens and select Generate new token.
- Provide a Token name.
- In the Search scopes box, search for
Ingest metrics
(underAPI v2 scopes
), and select the checkbox. - Select Generate token.
- Select Copy to copy the token to your clipboard.
- Save the token in a safe place; you can't display it again.
- Replace
<TOKEN>
in the code snippet above with your token.
Dynatrace version 1.222+
pip install opentelemetry-exporter-dynatrace-metrics
from dynatrace.opentelemetry.metrics.export import (configure_dynatrace_metrics_export)
metrics.set_meter_provider(MeterProvider(
metric_readers=[configure_dynatrace_metrics_export(
export_interval_millis=5000,
endpoint_url="<URL>", #TODO Replace with your URL
api_token="<TOKEN>") #TODO Replace with your Api-Token
]))
If no OneAgent is available locally, you need to define the correct endpoint and token to make sure your data arrives where it should be.
- To set the endpoint
- Use your Environment ID to set the endpoint to which your app will send traces as follows:
- Dynatrace SaaS
https://{your-environment-id}.live.dynatrace.com/api/v2/metrics/ingest
- Dynatrace Managed
https://{your-domain}/e/{your-environment-id}/api/v2/metrics/ingest
- Dynatrace ActiveGate
https://{your-activegate-endpoint}/e/{your-environment-id}/api/v2/metrics/ingest
- You may need to include the port to your ActiveGate endpoint. For example:
https://{your-activegate-endpoint}:9999/e/{your-environment-id}/api/v2/metrics/ingest
- If you are running a containerized ActiveGate, you need to use the FQDN of it. For example:
https://{your-activegate-service-name}.dynatrace.svc.cluster.local/e/{your-environment-id}/api/v2/metrics/ingest
- You may need to include the port to your ActiveGate endpoint. For example:
- Dynatrace SaaS
- Replace
<URL>
in the code snippet above with your endpoint.
- Use your Environment ID to set the endpoint to which your app will send traces as follows:
- To create an authentication token
- In the Dynatrace menu, go to Access tokens and select Generate new token.
- Provide a Token name.
- In the Search scopes box, search for
Ingest metrics
(underAPI v2 scopes
), and select the checkbox. - Select Generate token.
- Select Copy to copy the token to your clipboard.
- Save the token in a safe place; you can't display it again.
- Replace
<TOKEN>
in the code snippet above with your token.
Verify that the metrics are ingested into Dynatrace
A few minutes after restarting your app, look for your metrics:
- In the Dynatrace menu, go to Metrics and filter for your metrics (by the names you have given them).
- To get more detailed information, or to filter for different criteria, expand Details for a metric and select Create chart.
If your application does not receive any traffic, there will be no metrics.