How to create Azure Alert rule using Python SDK?

Sirivella Zephaniah 60 Reputation points
2024-03-20T04:42:14.67+00:00

I want to create an alert rule which will send an email notification when a specific pipeline fails.

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,914 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AshokPeddakotla-MSFT 33,851 Reputation points
    2024-03-20T06:21:38.8266667+00:00

    Sirivella Zephaniah Greetings!

    I want to create an alert rule which will send an email notification when a specific pipeline fails.

    To create an alert rule that sends an email notification when a specific pipeline fails, you can follow these steps:

    Azure Portal:

    1. Go to the Azure portal and navigate to your Machine Learning workspace.
    2. Click on the "Alerts" tab in the left-hand menu.
    3. Click on the "New alert rule" button.
    4. In the "Create alert rule" pane, select the "Metric alert" option. select your Machine Learning workspace and the "Pipelines" resource type.
    5. In the "Actions" section, select the "Email" option and enter the email address(es) you want to receive the notification.
    6. Click on the "Create alert rule" button to create the alert rule.

    Using Python SDK :

    To create an alert rule that sends an email notification when a specific pipeline fails using Python SDK, you can use the azure.mgmt.monitor package. Here is an example code snippet that creates an alert rule to send an email notification when a specific pipeline fails:

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.monitor import MonitorManagementClient
    from azure.mgmt.monitor.models import *
    
    # Replace with your own subscription ID, resource group name, and pipeline name
    subscription_id = '<subscription_id>'
    resource_group_name = '<resource_group_name>'
    workspace_name = '<workspace_name>'
    pipeline_name = '<pipeline_name>'
    
    # Replace with your own tenant ID, client ID, and client secret
    tenant_id = '<tenant_id>'
    client_id = '<client_id>'
    client_secret = '<client_secret>'
    
    # Authenticate with Azure using a service principal
    credentials = ServicePrincipalCredentials(
        client_id=client_id,
        secret=client_secret,
        tenant=tenant_id
    )
    
    # Create a MonitorManagementClient object
    monitor_client = MonitorManagementClient(
        credentials=credentials,
        subscription_id=subscription_id
    )
    
    # Define the alert rule criteria
    criteria = MetricAlertCriteria(
        name='PipelineRunStatus',
        metric_namespace='Microsoft.MachineLearningServices/workspaces/pipelines',
        dimensions=[
            MetricDimension(
                name='PipelineName',
                value=pipeline_name
            ),
            MetricDimension(
                name='Status',
                value='Failed'
            )
        ],
        metric_name='PipelineRunStatus',
        operator='GreaterThan',
        threshold='0',
        time_aggregation='Count',
        time_grain='PT5M'
    )
    
    # Define the alert rule action
    action = AlertRuleAction(
        send_email_to_subscription_administrator=True,
        custom_emails=['<email_address>']
    )
    
    # Create the alert rule object
    alert_rule = AlertRule(
        location='global',
        alert_rule_resource_id=f'/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.MachineLearningServices/workspaces/{workspace_name}',
        description='Alert rule for failed pipeline runs',
        criteria=criteria,
        actions=[action]
    )
    
    # Create the alert rule
    monitor_client.alert_rules.create_or_update(
        resource_group_name=resource_group_name,
        rule_name='FailedPipelineRuns',
        parameters=alert_rule
    )
    
    

    I would suggest you, please check the below documentation as well for more details.

    Monitor Azure Machine Learning

    Troubleshooting machine learning pipelines

    How to use pipeline UI to debug Azure Machine Learning pipeline failures

    Microsoft Azure SDK for Python

    Do let me know if that helps or have any other queries.

    If the response helped, please do click Accept Answer and Yes for was this answer helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.