{"id":5568,"library":"applicationinsights","title":"Azure Application Insights Legacy Python SDK","description":"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.","status":"maintenance","version":"0.11.10","language":"en","source_language":"en","source_url":"https://github.com/Microsoft/ApplicationInsights-Python","tags":["azure","application-insights","telemetry","monitoring","legacy"],"install":[{"cmd":"pip install applicationinsights","lang":"bash","label":"Install legacy SDK"}],"dependencies":[{"reason":"Used for HTTP communication with Application Insights ingestion endpoints.","package":"requests","optional":false},{"reason":"Used for structured logging integration.","package":"python-json-logger","optional":false}],"imports":[{"symbol":"TelemetryClient","correct":"from applicationinsights import TelemetryClient"},{"symbol":"logging","correct":"import logging\nfrom applicationinsights.logging import enable"}],"quickstart":{"code":"import os\nfrom applicationinsights import TelemetryClient\n\n# For legacy SDK, use Instrumentation Key\n# Find your Instrumentation Key in your Application Insights resource in Azure Portal.\nINSTRUMENTATION_KEY = os.environ.get('APPINSIGHTS_INSTRUMENTATIONKEY', 'YOUR_LEGACY_INSTRUMENTATION_KEY')\n\nif not INSTRUMENTATION_KEY or INSTRUMENTATION_KEY == 'YOUR_LEGACY_INSTRUMENTATION_KEY':\n    print(\"Warning: APPINSIGHTS_INSTRUMENTATIONKEY not set. Telemetry will not be sent.\")\n    exit()\n\nclient = TelemetryClient(INSTRUMENTATION_KEY)\n\n# Track an event\nclient.track_event('MyLegacyPythonEvent', {'custom_property': 'value1'})\n\n# Track a metric\nclient.track_metric('MyLegacyMetric', 123.45)\n\n# Track a trace message\nclient.track_trace('This is a legacy trace message', properties={'level': 'INFO'})\n\n# Ensure telemetry is sent before application exits\nclient.flush()\nprint(\"Legacy telemetry sent.\")","lang":"python","description":"Initializes the legacy `TelemetryClient` with an Instrumentation Key and sends a basic event, metric, and trace. Remember to set the `APPINSIGHTS_INSTRUMENTATIONKEY` environment variable or replace the placeholder."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":"All versions of `applicationinsights` (legacy SDK) vs. `azure-monitor-opentelemetry-distro` (modern SDK)"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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.","severity":"deprecated","affected_versions":"< 0.11.10 for Python 3.6"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}