Azure Application Insights Client (Legacy)
This is the legacy Python client library for sending telemetry to Microsoft Azure Application Insights. It was last updated in October 2018 (version 0.1.1) and has since been superseded by `opencensus-ext-azure` and more recently by `azure-monitor-opentelemetry-exporter` for modern Azure Monitor integration. This package is no longer actively maintained.
Warnings
- breaking The `azure-applicationinsights` library (version <= 0.1.1) is deprecated and no longer actively maintained. Modern Python applications should use `opencensus-ext-azure` or `azure-monitor-opentelemetry-exporter` for Application Insights integration. These new approaches require significant code changes.
- gotcha The PyPI package is `azure-applicationinsights`, but the top-level import module is `applicationinsights`. This can lead to `ModuleNotFoundError` if `from azure_applicationinsights import ...` or `from azure.applicationinsights import ...` is attempted.
- deprecated This library relies on an older approach for telemetry collection and might not support newer Application Insights features or conform to modern Azure SDK guidelines. Consider it end-of-life for new development.
Install
-
pip install azure-applicationinsights
Imports
- TelemetryClient
from applicationinsights import TelemetryClient
Quickstart
import os
from applicationinsights import TelemetryClient
# Get your Application Insights Instrumentation Key from environment variable
# For Azure Portal: Application Insights -> Overview -> Essentials -> Instrumentation Key
instrumentation_key = os.environ.get('APPINSIGHTS_INSTRUMENTATION_KEY', 'YOUR_INSTRUMENTATION_KEY')
if not instrumentation_key or instrumentation_key == 'YOUR_INSTRUMENTATION_KEY':
print("WARNING: APPINSIGHTS_INSTRUMENTATION_KEY environment variable not set or placeholder used.")
print("Please set it to run this example.")
else:
client = TelemetryClient(instrumentation_key)
# Track a simple event
client.track_event('TestEvent', properties={'key': 'value'})
# Track a metric
client.track_metric('TestMetric', 42.0)
# Track a trace message
client.track_trace('This is a test trace message.', properties={'level': 'INFO'})
# Flush the telemetry buffer (important for short-lived scripts)
client.flush()
print("Telemetry sent using legacy client.")