Install & Compatibility
Where this runs
tested against v1.1.15 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 54.7MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 5.5s · import 0.000s · 56MB
55MB installed
● package 55MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
AzureMonitorTraceExporter
✓ from opencensus.ext.azure.trace_exporter import AzureMonitorTraceExporter
✗ from opencensus.ext.azure.trace_exporter import AzureMonitorTraceExporter
This quickstart demonstrates how to set up and use the `AzureMonitorTraceExporter` for distributed tracing and `AzureMonitorLogHandler` for sending application logs to Azure Application Insights. Ensure the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable is set for telemetry to be sent successfully.
import os
import logging
from opencensus.ext.azure.log_exporter import AzureMonitorLogHandler
from opencensus.ext.azure.trace_exporter import AzureMonitorTraceExporter
from opencensus.trace.tracer import Tracer
from opencensus.trace.samplers import ProbabilitySampler
# Set your Azure Application Insights Connection String as an environment variable
# APPLICATIONINSIGHTS_CONNECTION_STRING='InstrumentationKey=YOUR_INSTRUMENTATION_KEY;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/'
CONNECTION_STRING = os.environ.get('APPLICATIONINSIGHTS_CONNECTION_STRING', '')
if not CONNECTION_STRING:
print("WARNING: APPLICATIONINSIGHTS_CONNECTION_STRING environment variable not set. Telemetry will not be sent.")
# --- Tracing Example ---
print("\n--- Tracing Example ---")
# Initialize a trace exporter
trace_exporter = AzureMonitorTraceExporter(
connection_string=CONNECTION_STRING
)
tracer = Tracer(exporter=trace_exporter, sampler=ProbabilitySampler(1.0))
with tracer.span(name='my_span'):
print(" Inside a traced span.")
with tracer.span(name='child_span'):
print(" Inside a child span.")
print(" Trace exported. Check Azure Portal after a short delay.")
# --- Logging Example ---
print("\n--- Logging Example ---")
# Initialize a log handler
handler = AzureMonitorLogHandler(
connection_string=CONNECTION_STRING
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.info('This is an info log via Azure Monitor.')
logger.warning('This is a warning log.')
try:
1 / 0
except ZeroDivisionError:
logger.exception('An error occurred during division.')
print(" Logs exported. Check Azure Portal after a short delay.")
# In a real application, ensure exporters are properly shut down on exit
# trace_exporter.shutdown()
# handler.close()
Debug
Known issues
breakingThe OpenCensus project, including `opencensus-ext-azure`, has officially transitioned to OpenTelemetry. While existing OpenCensus applications will continue to function, the project is in maintenance mode, meaning no new features will be added, and future development efforts are focused on OpenTelemetry. New applications should consider starting with OpenTelemetry.fixFor new projects, consider adopting OpenTelemetry. For existing projects, plan for eventual migration to OpenTelemetry. Refer to OpenTelemetry's official documentation for migration guides.
affects: All versions
deprecatedActive feature development for `opencensus-ext-azure` has ceased. The library will only receive critical bug fixes. This impacts long-term support and access to new Azure Monitor features.fixBe aware that new features or integrations for Azure Monitor may only be available through the OpenTelemetry Azure Monitor Exporter. Consider migrating to OpenTelemetry for future-proof development.
affects: All versions >= 1.0.0
gotchaIt is recommended to use the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable instead of `APPINSIGHTS_INSTRUMENTATIONKEY` for configuration. Connection strings offer more flexibility and security, supporting endpoint customization and other settings.fixPrefer setting the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable with your Application Insights connection string. If specifying in code, pass `connection_string` argument to the exporter.
affects: All versions
gotchaWhen integrating logging, ensure you don't add duplicate `AzureMonitorLogHandler` instances to the same logger or its ancestors, as this can lead to logs being sent multiple times.fixCheck `logger.handlers` before adding a new `AzureMonitorLogHandler`. For example: `if not any(isinstance(h, AzureMonitorLogHandler) for h in logger.handlers): logger.addHandler(handler)`
affects: All versions
gotchaCompatibility issues can arise if the `opencensus` core library version does not meet the requirements specified by `opencensus-ext-azure`. Always ensure `opencensus` is installed within the required range.fixVerify that your installed `opencensus` package satisfies the version constraint (`opencensus>=0.11.0,<1.0.0` for current `opencensus-ext-azure` versions). Use `pip install -U opencensus` to update, or `pip install 'opencensus>=0.11.0,<1.0.0'`.
affects: All versions
Upgrade
Version history
1.1.15latest on PyPI · released Jun 3, 2025
Audit
Dependencies
opencensusrequiredCore OpenCensus library for common API surfaces and context management.
azure-corerequiredMicrosoft Azure SDK for Python (Core) for shared Azure functionalities.
azure-functions-workeroptionalRequired for specific Azure Functions integration features.