Azure Monitor OpenTelemetry Distro for Python

1.8.7 · active · verified Mon Apr 06

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Azure Monitor OpenTelemetry Distro. Ensure the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable is set with your Azure Application Insights connection string. The `configure_azure_monitor()` function automatically sets up the necessary OpenTelemetry components. A simple custom span is created to illustrate basic tracing.

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).")

view raw JSON →