OpenCensus Azure Monitor Exporter for telemetry data (logs, metrics, and traces) to Azure Monitor.
npx @tessl/cli install tessl/pypi-opencensus-ext-azure@1.1.0OpenCensus Azure Monitor Extension provides comprehensive telemetry data export capabilities from OpenCensus instrumentation directly to Azure Monitor. The package enables Python applications to send logs, distributed traces, and metrics to Microsoft Azure Application Insights for monitoring, diagnostics, and performance analysis.
pip install opencensus-ext-azurePrimary importers for the three main exporters:
from opencensus.ext.azure.log_exporter import AzureLogHandler, AzureEventHandler
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.ext.azure.metrics_exporter import MetricsExporter, new_metrics_exporterConfiguration and common utilities:
from opencensus.ext.azure.common import Options
from opencensus.ext.azure.common.storage import LocalFileStorage, LocalFileBlob
from opencensus.ext.azure.common.protocol import (
BaseObject, Envelope, Data, DataPoint, Event, ExceptionData,
Message, MetricData, Request, RemoteDependency
)
from opencensus.ext.azure.common.transport import TransportMixin, TransportStatusCode
from opencensus.ext.azure.common.processor import ProcessorMixin
from opencensus.ext.azure.common.utils import (
validate_instrumentation_key, parse_connection_string,
azure_monitor_context, timestamp_to_duration, timestamp_to_iso_str
)import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
# Configure the handler
handler = AzureLogHandler(connection_string="InstrumentationKey=your-key-here")
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.INFO)
# Use standard Python logging
logging.info("This message will be sent to Azure Monitor")
logging.error("This error will be tracked as an exception", exc_info=True)from opencensus.trace.tracer import Tracer
from opencensus.ext.azure.trace_exporter import AzureExporter
# Configure the exporter
exporter = AzureExporter(connection_string="InstrumentationKey=your-key-here")
tracer = Tracer(exporter=exporter)
# Create and use spans
with tracer.span(name="my_operation") as span:
span.add_attribute("key", "value")
# Your application logic herefrom opencensus.ext.azure.metrics_exporter import new_metrics_exporter
# Create a configured exporter with standard metrics
exporter = new_metrics_exporter(
connection_string="InstrumentationKey=your-key-here",
enable_standard_metrics=True
)
# Standard metrics (CPU, memory, requests) are automatically collectedThe OpenCensus Azure Monitor Extension follows a modular architecture:
Each exporter inherits common functionality from mixins while providing specialized telemetry type handling.
Python logging integration that sends log messages and exceptions to Azure Monitor as trace telemetry and custom events.
class AzureLogHandler(BaseLogHandler, TransportMixin, ProcessorMixin):
def __init__(self, **options): ...
def emit(self, record): ...
def close(self, timeout=None): ...
def add_telemetry_processor(self, processor): ...
class AzureEventHandler(BaseLogHandler, TransportMixin, ProcessorMixin):
def __init__(self, **options): ...
def emit(self, record): ...
def close(self, timeout=None): ...
def add_telemetry_processor(self, processor): ...Distributed tracing export that sends OpenCensus span data to Azure Monitor for request tracking, dependency mapping, and performance analysis.
class AzureExporter(BaseExporter, TransportMixin, ProcessorMixin):
def __init__(self, **options): ...
def export(self, span_datas): ...
def span_data_to_envelope(self, span_data): ...
def add_telemetry_processor(self, processor): ...Performance counters and custom metrics collection with automatic standard metrics for system monitoring.
class MetricsExporter(TransportMixin, ProcessorMixin):
def __init__(self, is_stats=False, **options): ...
def export_metrics(self, metrics): ...
def metric_to_envelopes(self, metric): ...
def shutdown(self): ...
def add_telemetry_processor(self, processor): ...
def new_metrics_exporter(**options):
"""
Factory function to create a fully configured metrics exporter.
Args:
**options: Configuration options for the exporter
Returns:
MetricsExporter: Configured exporter with background thread and standard metrics
"""Shared configuration options, utilities, protocol objects, and transport functionality used across all exporters.
class Options(BaseObject):
def __init__(self, **options): ...
def process_options(options): ...
def parse_connection_string(connection_string): ...
def validate_instrumentation_key(instrumentation_key): ...
class LocalFileStorage:
def __init__(self, path, **options): ...
def put(self, data, lease_period=0): ...
def get(self): ...
def close(self): ...
class LocalFileBlob:
def __init__(self, fullpath): ...
def get(self): ...
def put(self, data, lease_period=0): ...
def delete(self): ...
class TransportMixin:
def _transmit(self, envelopes): ...
def _transmit_from_storage(self): ...
class ProcessorMixin:
def add_telemetry_processor(self, processor): ...
def apply_telemetry_processors(self, envelopes): ...All exporters accept these configuration parameters:
connection_string (str): Azure Monitor connection string (recommended)instrumentation_key (str): Instrumentation key (deprecated, use connection_string)endpoint (str): Custom endpoint URL for Azure Monitorexport_interval (float): Export frequency in seconds (default: 15.0)max_batch_size (int): Maximum batch size for telemetry (default: 100)enable_local_storage (bool): Enable local file storage for reliability (default: True)storage_path (str): Custom storage path for local filestimeout (float): Network timeout in seconds (default: 10.0)proxies (dict): Proxy configuration for HTTP requestscredential: Azure credential object for authenticationWhen enabled, the metrics exporter automatically collects:
All exporters may raise:
ValueError: Invalid configuration options or parametersopencensus >= 0.11.4, < 1.0.0azure-core >= 1.12.0, < 2.0.0azure-identity >= 1.5.0, < 2.0.0psutil >= 5.6.3requests >= 2.19.0