Azure Application Insights Legacy Python SDK

0.11.10 · maintenance · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

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.")

view raw JSON →