|
Add this skill
npx mdskills install sickn33/azure-monitor-opentelemetry-exporter-pyComprehensive low-level OpenTelemetry exporter with clear examples for traces, metrics, and logs
1---2name: azure-monitor-opentelemetry-exporter-py3description: |4 Azure Monitor OpenTelemetry Exporter for Python. Use for low-level OpenTelemetry export to Application Insights.5 Triggers: "azure-monitor-opentelemetry-exporter", "AzureMonitorTraceExporter", "AzureMonitorMetricExporter", "AzureMonitorLogExporter".6package: azure-monitor-opentelemetry-exporter7---89# Azure Monitor OpenTelemetry Exporter for Python1011Low-level exporter for sending OpenTelemetry traces, metrics, and logs to Application Insights.1213## Installation1415```bash16pip install azure-monitor-opentelemetry-exporter17```1819## Environment Variables2021```bash22APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=xxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.com/23```2425## When to Use2627| Scenario | Use |28|----------|-----|29| Quick setup, auto-instrumentation | `azure-monitor-opentelemetry` (distro) |30| Custom OpenTelemetry pipeline | `azure-monitor-opentelemetry-exporter` (this) |31| Fine-grained control over telemetry | `azure-monitor-opentelemetry-exporter` (this) |3233## Trace Exporter3435```python36from opentelemetry import trace37from opentelemetry.sdk.trace import TracerProvider38from opentelemetry.sdk.trace.export import BatchSpanProcessor39from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter4041# Create exporter42exporter = AzureMonitorTraceExporter(43 connection_string="InstrumentationKey=xxx;..."44)4546# Configure tracer provider47trace.set_tracer_provider(TracerProvider())48trace.get_tracer_provider().add_span_processor(49 BatchSpanProcessor(exporter)50)5152# Use tracer53tracer = trace.get_tracer(__name__)54with tracer.start_as_current_span("my-span"):55 print("Hello, World!")56```5758## Metric Exporter5960```python61from opentelemetry import metrics62from opentelemetry.sdk.metrics import MeterProvider63from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader64from azure.monitor.opentelemetry.exporter import AzureMonitorMetricExporter6566# Create exporter67exporter = AzureMonitorMetricExporter(68 connection_string="InstrumentationKey=xxx;..."69)7071# Configure meter provider72reader = PeriodicExportingMetricReader(exporter, export_interval_millis=60000)73metrics.set_meter_provider(MeterProvider(metric_readers=[reader]))7475# Use meter76meter = metrics.get_meter(__name__)77counter = meter.create_counter("requests_total")78counter.add(1, {"route": "/api/users"})79```8081## Log Exporter8283```python84import logging85from opentelemetry._logs import set_logger_provider86from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler87from opentelemetry.sdk._logs.export import BatchLogRecordProcessor88from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter8990# Create exporter91exporter = AzureMonitorLogExporter(92 connection_string="InstrumentationKey=xxx;..."93)9495# Configure logger provider96logger_provider = LoggerProvider()97logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))98set_logger_provider(logger_provider)99100# Add handler to Python logging101handler = LoggingHandler(level=logging.INFO, logger_provider=logger_provider)102logging.getLogger().addHandler(handler)103104# Use logging105logger = logging.getLogger(__name__)106logger.info("This will be sent to Application Insights")107```108109## From Environment Variable110111Exporters read `APPLICATIONINSIGHTS_CONNECTION_STRING` automatically:112113```python114from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter115116# Connection string from environment117exporter = AzureMonitorTraceExporter()118```119120## Azure AD Authentication121122```python123from azure.identity import DefaultAzureCredential124from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter125126exporter = AzureMonitorTraceExporter(127 credential=DefaultAzureCredential()128)129```130131## Sampling132133Use `ApplicationInsightsSampler` for consistent sampling:134135```python136from opentelemetry.sdk.trace import TracerProvider137from opentelemetry.sdk.trace.sampling import ParentBasedTraceIdRatio138from azure.monitor.opentelemetry.exporter import ApplicationInsightsSampler139140# Sample 10% of traces141sampler = ApplicationInsightsSampler(sampling_ratio=0.1)142143trace.set_tracer_provider(TracerProvider(sampler=sampler))144```145146## Offline Storage147148Configure offline storage for retry:149150```python151from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter152153exporter = AzureMonitorTraceExporter(154 connection_string="...",155 storage_directory="/path/to/storage", # Custom storage path156 disable_offline_storage=False # Enable retry (default)157)158```159160## Disable Offline Storage161162```python163exporter = AzureMonitorTraceExporter(164 connection_string="...",165 disable_offline_storage=True # No retry on failure166)167```168169## Sovereign Clouds170171```python172from azure.identity import AzureAuthorityHosts, DefaultAzureCredential173from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter174175# Azure Government176credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)177exporter = AzureMonitorTraceExporter(178 connection_string="InstrumentationKey=xxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.us/",179 credential=credential180)181```182183## Exporter Types184185| Exporter | Telemetry Type | Application Insights Table |186|----------|---------------|---------------------------|187| `AzureMonitorTraceExporter` | Traces/Spans | requests, dependencies, exceptions |188| `AzureMonitorMetricExporter` | Metrics | customMetrics, performanceCounters |189| `AzureMonitorLogExporter` | Logs | traces, customEvents |190191## Configuration Options192193| Parameter | Description | Default |194|-----------|-------------|---------|195| `connection_string` | Application Insights connection string | From env var |196| `credential` | Azure credential for AAD auth | None |197| `disable_offline_storage` | Disable retry storage | False |198| `storage_directory` | Custom storage path | Temp directory |199200## Best Practices2012021. **Use BatchSpanProcessor** for production (not SimpleSpanProcessor)2032. **Use ApplicationInsightsSampler** for consistent sampling across services2043. **Enable offline storage** for reliability in production2054. **Use AAD authentication** instead of instrumentation keys2065. **Set export intervals** appropriate for your workload2076. **Use the distro** (`azure-monitor-opentelemetry`) unless you need custom pipelines208
Full transparency — inspect the skill content before installing.