Azure Monitor OpenTelemetry Exporter
The Azure Monitor OpenTelemetry Exporter for Python enables applications to send telemetry data (traces, metrics, and logs) from the OpenTelemetry SDK to Azure Monitor Application Insights. It is designed for users requiring advanced configuration and fine-grained control over their telemetry pipeline. This library is part of the Azure SDK for Python and is currently in a beta release phase, receiving frequent updates and improvements.
Warnings
- breaking The package name has undergone several changes. Ensure you are using the correct and current package name: `azure-monitor-opentelemetry-exporter`. Older documentation or samples might refer to `azure-opentelemetry-exporter-azuremonitor` or `microsoft-opentelemetry-exporter-azuremonitor`.
- deprecated The `AzureMonitorLogExporter` for logging is currently in an EXPERIMENTAL state. Microsoft warns that possible breaking changes may occur in future releases for this component.
- gotcha Users often confuse this exporter with the `Azure Monitor OpenTelemetry Distro`. This `azure-monitor-opentelemetry-exporter` library is for advanced scenarios requiring explicit OpenTelemetry SDK configuration. For simpler, one-line setup and auto-instrumentation, the `azure-monitor-opentelemetry` distro is recommended.
- gotcha Connection string is the primary configuration method. It's best practice to set the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable for authentication. Directly embedding the instrumentation key in code is not recommended for production.
- gotcha Failing to explicitly shut down or flush OpenTelemetry providers and exporters can lead to lost telemetry. It is critical to call `shutdown()` on `TracerProvider`, `MeterProvider`, and `LoggerProvider` before your application exits to ensure all buffered telemetry is sent.
- gotcha When logging in Azure Functions environments, avoid duplicate telemetry by not enabling both the native logging instrumentation in Azure Functions and the `azure-monitor-opentelemetry` logging instrumentation within the distribution. This can result in two entries for each log.
- gotcha The Azure Monitor Exporter currently has limitations in handling oversized log messages (exceeding 65536 bytes), which may prevent larger logs from being delivered to Application Insights. This could be a blocker for migration in environments with verbose logging.
Install
-
pip install azure-monitor-opentelemetry-exporter --pre
Imports
- AzureMonitorTraceExporter
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
- AzureMonitorMetricExporter
from azure.monitor.opentelemetry.exporter import AzureMonitorMetricExporter
- AzureMonitorLogExporter
from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter
Quickstart
import os
import logging
from opentelemetry import trace, metrics
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader, ConsoleMetricExporter
from opentelemetry._logs import set_logger_provider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from azure.monitor.opentelemetry.exporter import (
AzureMonitorTraceExporter,
AzureMonitorMetricExporter,
AzureMonitorLogExporter
)
CONNECTION_STRING = os.environ.get('APPLICATIONINSIGHTS_CONNECTION_STRING', 'InstrumentationKey=YOUR_INSTRUMENTATION_KEY')
# --- Tracing ---
print('Setting up tracing...')
exporter_trace = AzureMonitorTraceExporter(connection_string=CONNECTION_STRING)
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(exporter_trace))
trace.set_tracer_provider(tracer_provider)
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("my-trace-span"):
print("Hello from trace!")
# --- Metrics ---
print('Setting up metrics...')
exporter_metric = AzureMonitorMetricExporter(connection_string=CONNECTION_STRING)
metric_reader = PeriodicExportingMetricReader(exporter_metric, export_interval_millis=1000)
meter_provider = MeterProvider(metric_readers=[metric_reader])
metrics.set_meter_provider(meter_provider)
meter = metrics.get_meter(__name__)
counter = meter.create_counter("my_counter", description="A simple counter")
counter.add(1, {"key": "value"})
# --- Logging ---
print('Setting up logging...')
exporter_log = AzureMonitorLogExporter(connection_string=CONNECTION_STRING)
logger_provider = LoggerProvider()
set_logger_provider(logger_provider)
logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter_log))
handler = LoggingHandler(logger_provider=logger_provider)
# Attach OTel handler to root logger
root_logger = logging.getLogger()
root_logger.addHandler(handler)
root_logger.setLevel(logging.INFO)
# Log a message
root_logger.info("Hello from log!")
# --- Flush and Shutdown ---
print('Flushing and shutting down...')
tracer_provider.shutdown()
meter_provider.shutdown()
logger_provider.shutdown()
print('Telemetry sent to Azure Monitor (check your Application Insights resource).')