Azure Monitor OpenTelemetry Distro for Python
Microsoft Azure Monitor Opentelemetry Distro Client Library for Python (version 1.8.7) offers a "one-stop-shop" solution for instrumenting Python applications. It captures traces, metrics, and logs via OpenTelemetry instrumentations and reports them to Azure Monitor Application Insights. The library is actively maintained with frequent updates, though many underlying OpenTelemetry instrumentations are still in beta and may introduce breaking changes.
Warnings
- breaking Migration from older Application Insights SDKs (2.x) to `azure-monitor-opentelemetry` requires significant code changes, as most 2.x APIs are not supported and need to be updated to OpenTelemetry-based APIs.
- breaking Many OpenTelemetry instrumentations bundled with this distro are currently in a beta state, meaning they are not stable and may introduce breaking changes in future releases.
- gotcha The `configure_azure_monitor()` function must be called early in your application's lifecycle, preferably before importing other instrumented libraries, to ensure all telemetry is captured correctly and avoid potential data loss.
- gotcha Enabling both native logging instrumentation in Azure Functions and `azure-monitor-opentelemetry` logging instrumentation within the distribution can lead to duplicate trace logs in Application Insights.
- gotcha Dependency telemetry might occasionally be missing the 'operation name' or 'device model', which can adversely affect performance analysis, filtering, and device cohort analysis in Application Insights.
Install
-
pip install azure-monitor-opentelemetry
Imports
- configure_azure_monitor
from azure.monitor.opentelemetry import configure_azure_monitor
Quickstart
import os
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
# Set your Application Insights Connection String as an environment variable:
# APPLICATIONINSIGHTS_CONNECTION_STRING="InstrumentationKey=YOUR_INSTRUMENTATION_KEY;IngestionEndpoint=https://YOUR_REGION.in.applicationinsights.azure.com/"
# One-line setup - reads connection string from the environment variable
# APPLICATIONINSIGHTS_CONNECTION_STRING. Call this early in your application.
configure_azure_monitor()
# Get a tracer from OpenTelemetry
tracer = trace.get_tracer(__name__)
# Create a sample span
with tracer.start_as_current_span("my-example-span"):
print("Hello from an OpenTelemetry-instrumented application!")
# Simulate some work
import time
time.sleep(0.1)
print("Telemetry sent to Azure Monitor (if connection string is configured).")