Microsoft Azure Monitor Client Library for Python
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Azure Monitor Workspaces for Prometheus metrics collection and storage, providing managed Prometheus-compatible endpoints for container and Kubernetes monitoring with long-term retention and integration with Azure Monitor.
Create, update, and manage Azure Monitor Workspaces for Prometheus metrics storage and querying.
def create(resource_group_name: str, azure_monitor_workspace_name: str, azure_monitor_workspace_properties: AzureMonitorWorkspaceResource, **kwargs: Any) -> AzureMonitorWorkspaceResource:
"""
Create an Azure Monitor Workspace.
Parameters:
- resource_group_name: str - Name of the resource group
- azure_monitor_workspace_name: str - Name of the Azure Monitor workspace
- azure_monitor_workspace_properties: AzureMonitorWorkspaceResource - Workspace configuration
Returns:
AzureMonitorWorkspaceResource - The created workspace
"""
def get(resource_group_name: str, azure_monitor_workspace_name: str, **kwargs: Any) -> AzureMonitorWorkspaceResource:
"""
Returns an Azure Monitor Workspace.
Parameters:
- resource_group_name: str - Name of the resource group
- azure_monitor_workspace_name: str - Name of the Azure Monitor workspace
Returns:
AzureMonitorWorkspaceResource - The workspace details
"""
def update(resource_group_name: str, azure_monitor_workspace_name: str, azure_monitor_workspace_properties: AzureMonitorWorkspaceResourceForUpdate, **kwargs: Any) -> AzureMonitorWorkspaceResource:
"""
Updates part of an Azure Monitor Workspace.
Parameters:
- resource_group_name: str - Name of the resource group
- azure_monitor_workspace_name: str - Name of the Azure Monitor workspace
- azure_monitor_workspace_properties: AzureMonitorWorkspaceResourceForUpdate - Properties to update
Returns:
AzureMonitorWorkspaceResource - The updated workspace
"""
def begin_delete(resource_group_name: str, azure_monitor_workspace_name: str, **kwargs: Any) -> LROPoller[None]:
"""
Delete an Azure Monitor Workspace.
Parameters:
- resource_group_name: str - Name of the resource group
- azure_monitor_workspace_name: str - Name of the Azure Monitor workspace
Returns:
LROPoller[None] - Long-running operation poller for deletion
"""List Azure Monitor Workspaces within subscriptions and resource groups.
def list_by_resource_group(resource_group_name: str, **kwargs: Any) -> ItemPaged[AzureMonitorWorkspaceResource]:
"""
Lists all workspaces in the specified resource group.
Parameters:
- resource_group_name: str - Name of the resource group
Returns:
ItemPaged[AzureMonitorWorkspaceResource] - List of workspaces
"""
def list_by_subscription(**kwargs: Any) -> ItemPaged[AzureMonitorWorkspaceResource]:
"""
Lists all workspaces in the specified subscription.
Returns:
ItemPaged[AzureMonitorWorkspaceResource] - List of workspaces
"""from azure.mgmt.monitor.models import (
AzureMonitorWorkspaceResource, AzureMonitorWorkspaceResourceProperties,
AzureMonitorWorkspaceDefaultIngestionSettings, AzureMonitorWorkspaceMetrics
)
# Define workspace properties
workspace_properties = AzureMonitorWorkspaceResourceProperties(
account_id="workspace-account-id",
default_ingestion_settings=AzureMonitorWorkspaceDefaultIngestionSettings(
data_collection_endpoint_resource_id=f"/subscriptions/{subscription_id}/resourceGroups/monitoring-rg/providers/Microsoft.Insights/dataCollectionEndpoints/prometheus-dce",
data_collection_rule_resource_id=f"/subscriptions/{subscription_id}/resourceGroups/monitoring-rg/providers/Microsoft.Insights/dataCollectionRules/prometheus-dcr"
),
metrics=AzureMonitorWorkspaceMetrics(
prometheus_query_endpoint="https://my-workspace.eastus.prometheus.monitor.azure.com",
internal_id="internal-workspace-id"
)
)
# Create Azure Monitor Workspace
workspace = AzureMonitorWorkspaceResource(
location="East US",
properties=workspace_properties
)
result = client.azure_monitor_workspaces.create(
resource_group_name="monitoring-rg",
azure_monitor_workspace_name="prometheus-workspace",
azure_monitor_workspace_properties=workspace
)
print(f"Workspace created: {result.id}")
print(f"Query endpoint: {result.properties.metrics.prometheus_query_endpoint}")# Create workspace specifically for AKS Prometheus metrics
aks_workspace = AzureMonitorWorkspaceResource(
location="East US",
properties=AzureMonitorWorkspaceResourceProperties(
account_id=f"aks-prometheus-{subscription_id}",
default_ingestion_settings=AzureMonitorWorkspaceDefaultIngestionSettings(
data_collection_endpoint_resource_id=f"/subscriptions/{subscription_id}/resourceGroups/aks-rg/providers/Microsoft.Insights/dataCollectionEndpoints/aks-prometheus-dce",
data_collection_rule_resource_id=f"/subscriptions/{subscription_id}/resourceGroups/aks-rg/providers/Microsoft.Insights/dataCollectionRules/aks-prometheus-dcr"
)
)
)
aks_result = client.azure_monitor_workspaces.create(
resource_group_name="aks-rg",
azure_monitor_workspace_name="aks-prometheus-workspace",
azure_monitor_workspace_properties=aks_workspace
)
# The workspace can now be referenced in AKS addon configuration
print(f"AKS Workspace ID: {aks_result.id}")
print(f"Use this ID in AKS monitoring addon configuration")from azure.mgmt.monitor.models import AzureMonitorWorkspaceResourceForUpdate
# Update workspace tags and properties
workspace_update = AzureMonitorWorkspaceResourceForUpdate(
tags={
"Environment": "Production",
"Team": "Platform",
"CostCenter": "Engineering"
}
)
updated_workspace = client.azure_monitor_workspaces.update(
resource_group_name="monitoring-rg",
azure_monitor_workspace_name="prometheus-workspace",
azure_monitor_workspace_properties=workspace_update
)
print(f"Workspace updated: {updated_workspace.tags}")# List all workspaces in subscription
all_workspaces = client.azure_monitor_workspaces.list_by_subscription()
print("Azure Monitor Workspaces:")
for workspace in all_workspaces:
print(f" Name: {workspace.name}")
print(f" Location: {workspace.location}")
print(f" Resource Group: {workspace.id.split('/')[4]}")
if workspace.properties and workspace.properties.metrics:
print(f" Query Endpoint: {workspace.properties.metrics.prometheus_query_endpoint}")
print(f" Provisioning State: {workspace.properties.provisioning_state}")
print()
# List workspaces in specific resource group
rg_workspaces = client.azure_monitor_workspaces.list_by_resource_group("monitoring-rg")
print(f"Workspaces in monitoring-rg: {len(list(rg_workspaces))}")# Delete workspace (long-running operation)
delete_operation = client.azure_monitor_workspaces.begin_delete(
resource_group_name="monitoring-rg",
azure_monitor_workspace_name="old-prometheus-workspace"
)
print("Deleting workspace...")
delete_operation.wait() # Wait for completion
print("Workspace deleted successfully")# Example of how the workspace would be used in AKS configuration
# (This would typically be done through ARM templates or Azure CLI)
workspace_resource_id = result.id
aks_addon_config = {
"monitoring": {
"enabled": True,
"config": {
"omsagent": {
"enabled": True,
"config": {
"logAnalyticsWorkspaceResourceID": f"/subscriptions/{subscription_id}/resourceGroups/monitoring-rg/providers/Microsoft.OperationalInsights/workspaces/aks-logs"
}
},
"azureMonitorWorkspaceResourceId": workspace_resource_id
}
}
}
print(f"AKS addon configuration:")
print(f"Workspace ID: {workspace_resource_id}")# The workspace query endpoint can be used with Grafana
query_endpoint = result.properties.metrics.prometheus_query_endpoint
grafana_datasource_config = {
"name": "Azure Monitor Workspace",
"type": "prometheus",
"url": query_endpoint,
"access": "proxy",
"basicAuth": False,
"jsonData": {
"httpMethod": "POST",
"manageAlerts": True,
"prometheusType": "Prometheus",
"prometheusVersion": "2.40.0"
}
}
print("Grafana data source configuration:")
print(f"URL: {query_endpoint}")class AzureMonitorWorkspaceResource:
"""Azure Monitor Workspace resource."""
location: str # Resource location
properties: Optional[AzureMonitorWorkspaceResourceProperties] # Workspace properties
etag: Optional[str] # Resource etag
id: Optional[str] # Resource ID
name: Optional[str] # Resource name
type: Optional[str] # Resource type
system_data: Optional[SystemData] # System metadata
tags: Optional[Dict[str, str]] # Resource tags
class AzureMonitorWorkspaceResourceProperties:
"""Azure Monitor Workspace properties."""
account_id: Optional[str] # Workspace account ID
metrics: Optional[AzureMonitorWorkspaceMetrics] # Metrics configuration
provisioning_state: Optional[str] # Provisioning state
public_network_access: Optional[PublicNetworkAccess] # Public network access
default_ingestion_settings: Optional[AzureMonitorWorkspaceDefaultIngestionSettings] # Default ingestion settings
class AzureMonitorWorkspaceMetrics:
"""Workspace metrics configuration."""
internal_id: Optional[str] # Internal workspace ID
prometheus_query_endpoint: Optional[str] # Prometheus query endpoint URL
class AzureMonitorWorkspaceDefaultIngestionSettings:
"""Default ingestion settings."""
data_collection_endpoint_resource_id: Optional[str] # Data collection endpoint ID
data_collection_rule_resource_id: Optional[str] # Data collection rule ID
class AzureMonitorWorkspaceResourceForUpdate:
"""Workspace resource for updates."""
tags: Optional[Dict[str, str]] # Resource tags to update
class SystemData:
"""System metadata."""
created_by: Optional[str] # Created by user/service
created_by_type: Optional[CreatedByType] # Creator type
created_at: Optional[datetime] # Creation timestamp
last_modified_by: Optional[str] # Last modified by user/service
last_modified_by_type: Optional[CreatedByType] # Last modifier type
last_modified_at: Optional[datetime] # Last modification timestamp
PublicNetworkAccess = Union["Enabled", "Disabled"]
CreatedByType = Union["User", "Application", "ManagedIdentity", "Key"]Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-monitor