Azure Application Insights Legacy Python SDK
raw JSON → 0.11.10 verified Thu Apr 09 auth: no python maintenance
This is the *legacy* Python SDK for sending telemetry to Azure Application Insights. It allows applications to send custom events, metrics, and traces directly to Application Insights. For new applications, it is strongly recommended to use the OpenTelemetry-based `azure-monitor-opentelemetry-distro` package. The `applicationinsights` package is currently at version 0.11.10 and receives minimal updates, primarily maintenance.
pip install applicationinsights Common errors
error ModuleNotFoundError: No module named 'applicationinsights' ↓
cause The 'applicationinsights' package is not installed in the Python environment.
fix
Install the package using pip: 'pip install applicationinsights'.
error ImportError: cannot import name 'ApplicationInsightsDataClient' from 'azure.monitor.query' ↓
cause The 'ApplicationInsightsDataClient' class is not available in the 'azure.monitor.query' module.
fix
Use 'LogsQueryClient' from 'azure.monitor.query' instead: 'from azure.monitor.query import LogsQueryClient'.
error TypeError: 'NoneType' object is not callable ↓
cause Attempting to call a function or method that is None, possibly due to incorrect initialization of the Application Insights client.
fix
Ensure the Application Insights client is properly initialized with a valid instrumentation key before use.
error ValueError: Invalid instrumentation key ↓
cause The provided instrumentation key is invalid or incorrectly formatted.
fix
Verify that the instrumentation key is correct and properly formatted as a GUID.
error AttributeError: 'TelemetryClient' object has no attribute 'track_event' ↓
cause The 'TelemetryClient' object does not have a 'track_event' method, possibly due to using an outdated version of the 'applicationinsights' package.
fix
Update the 'applicationinsights' package to the latest version using pip: 'pip install --upgrade applicationinsights'.
Warnings
breaking This `applicationinsights` package is the *legacy* Python SDK for Azure Application Insights. For all new development and recommended usage, developers should use the `azure-monitor-opentelemetry-distro` package, which is based on OpenTelemetry and offers broader instrumentation and future compatibility. ↓
fix For new projects, install `azure-monitor-opentelemetry-distro` (e.g., `pip install azure-monitor-opentelemetry-distro`) and follow its documentation for OpenTelemetry-based setup. Migrating existing `applicationinsights` code requires significant changes.
gotcha The legacy `applicationinsights` SDK uses an 'Instrumentation Key' for authentication. The modern OpenTelemetry-based SDK (`azure-monitor-opentelemetry-distro`) uses a 'Connection String'. These are distinct values and not interchangeable. ↓
fix Ensure you are using the correct identifier for your chosen SDK. For this legacy SDK, provide the Instrumentation Key. For the modern SDK, provide the Connection String.
gotcha Telemetry sent via `TelemetryClient` is buffered in memory. If your application exits abruptly or runs for a short period, you must explicitly call `client.flush()` to ensure all buffered telemetry is sent to Application Insights before termination. ↓
fix Always include `client.flush()` at appropriate points, such as before exiting a script or at the end of a request handler in a web application. For long-running applications, consider using a periodic flush or a background thread.
deprecated Support for Python 3.6 was dropped in the related OpenTelemetry-based SDK (v1.0.0b6). While this specific legacy package (0.11.10) might still work with Python 3.6, its maintenance status means future compatibility with older Python versions is not guaranteed, and it's unlikely to receive updates for new Python releases. ↓
fix It is recommended to use Python 3.7 or newer. For new applications, use the `azure-monitor-opentelemetry-distro` with a supported Python version.
Imports
- TelemetryClient
from applicationinsights import TelemetryClient - logging
import logging from applicationinsights.logging import enable
Quickstart
import os
from applicationinsights import TelemetryClient
# For legacy SDK, use Instrumentation Key
# Find your Instrumentation Key in your Application Insights resource in Azure Portal.
INSTRUMENTATION_KEY = os.environ.get('APPINSIGHTS_INSTRUMENTATIONKEY', 'YOUR_LEGACY_INSTRUMENTATION_KEY')
if not INSTRUMENTATION_KEY or INSTRUMENTATION_KEY == 'YOUR_LEGACY_INSTRUMENTATION_KEY':
print("Warning: APPINSIGHTS_INSTRUMENTATIONKEY not set. Telemetry will not be sent.")
exit()
client = TelemetryClient(INSTRUMENTATION_KEY)
# Track an event
client.track_event('MyLegacyPythonEvent', {'custom_property': 'value1'})
# Track a metric
client.track_metric('MyLegacyMetric', 123.45)
# Track a trace message
client.track_trace('This is a legacy trace message', properties={'level': 'INFO'})
# Ensure telemetry is sent before application exits
client.flush()
print("Legacy telemetry sent.")