{"id":1601,"library":"opencensus-ext-azure","title":"OpenCensus Azure Monitor Exporter","description":"OpenCensus Azure Monitor Exporter is a Python library that enables applications to export telemetry data (traces, metrics, and logs) to Azure Monitor and Application Insights. It's part of the OpenCensus project, which is currently in maintenance mode, with OpenTelemetry being the recommended successor for new development. The library receives critical bug fixes but no new feature development. The current version is 1.1.15.","status":"maintenance","version":"1.1.15","language":"en","source_language":"en","source_url":"https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure","tags":["observability","metrics","tracing","logging","azure","application-insights","opencensus","cloud"],"install":[{"cmd":"pip install opencensus-ext-azure","lang":"bash","label":"Install core library"},{"cmd":"pip install opencensus-ext-azure[azure_functions]","lang":"bash","label":"Install with Azure Functions support"}],"dependencies":[{"reason":"Core OpenCensus library for common API surfaces and context management.","package":"opencensus","optional":false},{"reason":"Microsoft Azure SDK for Python (Core) for shared Azure functionalities.","package":"azure-core","optional":false},{"reason":"Required for specific Azure Functions integration features.","package":"azure-functions-worker","optional":true}],"imports":[{"symbol":"AzureMonitorTraceExporter","correct":"from opencensus.ext.azure.trace_exporter import AzureMonitorTraceExporter"},{"symbol":"AzureMonitorLogHandler","correct":"from opencensus.ext.azure.log_exporter import AzureMonitorLogHandler"},{"symbol":"AzureMonitorMetricsExporter","correct":"from opencensus.ext.azure.metrics_exporter import AzureMonitorMetricsExporter"},{"note":"Used with trace exporter to create spans.","symbol":"Tracer","correct":"from opencensus.trace.tracer import Tracer"}],"quickstart":{"code":"import os\nimport logging\nfrom opencensus.ext.azure.log_exporter import AzureMonitorLogHandler\nfrom opencensus.ext.azure.trace_exporter import AzureMonitorTraceExporter\nfrom opencensus.trace.tracer import Tracer\nfrom opencensus.trace.samplers import ProbabilitySampler\n\n# Set your Azure Application Insights Connection String as an environment variable\n# APPLICATIONINSIGHTS_CONNECTION_STRING='InstrumentationKey=YOUR_INSTRUMENTATION_KEY;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/'\nCONNECTION_STRING = os.environ.get('APPLICATIONINSIGHTS_CONNECTION_STRING', '')\n\nif not CONNECTION_STRING:\n    print(\"WARNING: APPLICATIONINSIGHTS_CONNECTION_STRING environment variable not set. Telemetry will not be sent.\")\n\n# --- Tracing Example ---\nprint(\"\\n--- Tracing Example ---\")\n# Initialize a trace exporter\ntrace_exporter = AzureMonitorTraceExporter(\n    connection_string=CONNECTION_STRING\n)\ntracer = Tracer(exporter=trace_exporter, sampler=ProbabilitySampler(1.0))\n\nwith tracer.span(name='my_span'):\n    print(\"  Inside a traced span.\")\n    with tracer.span(name='child_span'):\n        print(\"    Inside a child span.\")\n\nprint(\"  Trace exported. Check Azure Portal after a short delay.\")\n\n# --- Logging Example ---\nprint(\"\\n--- Logging Example ---\")\n# Initialize a log handler\nhandler = AzureMonitorLogHandler(\n    connection_string=CONNECTION_STRING\n)\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(__name__)\nlogger.addHandler(handler)\n\nlogger.info('This is an info log via Azure Monitor.')\nlogger.warning('This is a warning log.')\ntry:\n    1 / 0\nexcept ZeroDivisionError:\n    logger.exception('An error occurred during division.')\n\nprint(\"  Logs exported. Check Azure Portal after a short delay.\")\n\n# In a real application, ensure exporters are properly shut down on exit\n# trace_exporter.shutdown()\n# handler.close()\n","lang":"python","description":"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."},"warnings":[{"fix":"For new projects, consider adopting OpenTelemetry. For existing projects, plan for eventual migration to OpenTelemetry. Refer to OpenTelemetry's official documentation for migration guides.","message":"The 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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Be 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.","message":"Active 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.","severity":"deprecated","affected_versions":"All versions >= 1.0.0"},{"fix":"Prefer setting the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable with your Application Insights connection string. If specifying in code, pass `connection_string` argument to the exporter.","message":"It 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Check `logger.handlers` before adding a new `AzureMonitorLogHandler`. For example: `if not any(isinstance(h, AzureMonitorLogHandler) for h in logger.handlers): logger.addHandler(handler)`","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Verify 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'`.","message":"Compatibility 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}