Azure Application Insights Management Client Library
The `azure-mgmt-applicationinsights` library is the Microsoft Azure Application Insights Management Client for Python, providing programmatic access to manage Application Insights resources. It allows you to create, update, delete, and list Application Insights components and related entities. As part of the Azure SDK for Python, it follows a regular release cadence, with the current stable version being 4.1.0.
Warnings
- breaking Major breaking changes occurred during the transition from 'Track 1' (older `msrestazure`-based) to 'Track 2' (modern `azure-core`-based) versions of Azure SDK libraries. `azure-mgmt-applicationinsights` v4.x.x is a Track 2 library.
- deprecated Older authentication methods (e.g., `ServicePrincipalCredentials` from `msrestazure`) are deprecated in favor of the `azure-identity` library.
- gotcha List operations in modern Azure SDK (Track 2) libraries often return async iterators directly, differing from older SDKs that might have returned a `Paged` object or required explicit calls to `by_page()`.
Install
-
pip install azure-mgmt-applicationinsights azure-identity
Imports
- ApplicationInsightsManagementClient
from azure.mgmt.applicationinsights import ApplicationInsightsManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.applicationinsights import ApplicationInsightsManagementClient
# Ensure you have your Azure Subscription ID set as an environment variable or hardcoded.
# For a production environment, use environment variables or Azure Key Vault.
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
if not subscription_id or subscription_id == "YOUR_SUBSCRIPTION_ID":
raise ValueError("AZURE_SUBSCRIPTION_ID environment variable is required for this example.")
# Authenticate with Azure using DefaultAzureCredential.
# This credential type attempts to authenticate through multiple methods,
# including environment variables (AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID),
# managed identity, Visual Studio Code, Azure CLI, etc.
credential = DefaultAzureCredential()
# Create an Application Insights Management Client
client = ApplicationInsightsManagementClient(credential, subscription_id)
# Example: List all Application Insights components in the subscription
print("Listing all Application Insights components in the subscription...")
try:
for component in client.components.list():
print(f" - Name: {component.name}, Resource Group: {component.resource_group_name}, Location: {component.location}, Kind: {component.kind}")
print("\nSuccessfully listed Application Insights components.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your AZURE_SUBSCRIPTION_ID is correct and your credentials have 'Reader' role on the subscription.")