<@ULVA73B9P> Does prefect 3 have notifications bas...
# ask-marvin
m
@Marvin Does prefect 3 have notifications based on tags? In Prefect 2 I was able to configure a notification so any job with a specific tag would alert the notification channel.
m
In Prefect 3.x, you can indeed configure notifications based on tags, but it involves using automations and triggers. The concept of triggers allows you to specify conditions under which certain actions, like notifications, should be executed. Here's a brief overview of how you might achieve this: 1. Tags and Flows: You can use tags within your flows to categorize or label them. For example, using
with tags("your_tag_name")
in your flow definition. 2. Triggers and Automations: Prefect 3.x allows you to set up automations with triggers. You can define these triggers in a YAML file or directly through the Prefect UI. These triggers can be based on flow run states, and you can configure them to match flows with specific tags. 3. Notification Configuration: You can set up notification blocks in the Prefect UI to determine where notifications should be sent (e.g., Slack, email). Then, create an automation that uses these notification blocks to alert you based on the flow's completion or failure state. For more detailed guidance, you can refer to the Prefect 3.x Automations and Triggers documentation. If you want to explore how tags are used in code, you can find an example here.
m
@Marvin Good. What is the Prefect CLI command to configure the automation to send a notification for failed flow run using a slack webhook Block.
m
To configure an automation in Prefect 3.x to send a notification for a failed flow run using a Slack webhook block, you would typically set this up through the Prefect UI rather than using a direct CLI command. However, here's a general outline of the steps you would follow: 1. Create a Slack Webhook Block: First, you need to create a Slack webhook block in the Prefect UI. This involves specifying the webhook URL and any additional configuration needed for your Slack channel. 2. Set Up an Automation: In the Prefect UI, navigate to the Automations section. Here, you can create a new automation. 3. Define the Trigger: Set the trigger for the automation to be based on the flow run state. Specifically, you want the trigger to activate when a flow run enters a "Failed" state. 4. Configure the Notification: Use the Slack webhook block you created to configure the notification action. This will ensure that when the trigger conditions are met, a notification is sent to the specified Slack channel. For more detailed instructions, you can refer to the Prefect Automations and Triggers documentation. Unfortunately, there isn't a specific CLI command to directly set up automations with notifications in Prefect 3.x; these configurations are primarily done through the UI.
m
@Marvin OK, please provide a python SDK script that configures an automation to send a notification with a slack webhook block (the block already exists), when a flow with the tag 'alarm' fails.
m
To configure an automation using the Prefect Python SDK to send a notification with a Slack webhook block when a flow with the tag 'alarm' fails, you can use the following script. This assumes you have already created the Slack webhook block:
Copy code
python
from prefect import get_client
from prefect.automations import AutomationCore, EventTrigger, SendNotification
from prefect.blocks.notifications import SlackWebhook

# Initialize client
client = get_client()

# Define the Slack webhook block name
slack_block_name = "your-slack-webhook-block-name"

# Define the automation
automation = AutomationCore(
    name="Notify on Alarm Tag Failure",
    triggers=[
        EventTrigger(
            event="prefect.flow-run.Failed",
            match_related=[{"prefect.resource.role": "tag", "prefect.resource.id": "prefect.tag.alarm"}]
        )
    ],
    actions=[
        SendNotification(
            block_name=slack_block_name,
            message="A flow with the 'alarm' tag has failed!"
        )
    ]
)

# Register the automation
client.create_automation(automation)
### Key Points: - Slack Webhook Block: Ensure the
slack_block_name
is set to the name of your existing Slack webhook block. - EventTrigger: It listens for the
prefect.flow-run.Failed
event and matches flows with the 'alarm' tag. - SendNotification: This action sends a notification using the specified Slack webhook block. This script uses the Prefect client to create and register the automation with the specified triggers and actions. Adjust the block name and message as needed for your setup.
m
@Marvin Cool! And how do I change this script to use any arbitrary tag?
m
To modify the script to use any arbitrary tag, you can parameterize the tag name. Here's how you can adjust the script to accept a tag name dynamically:
Copy code
python
from prefect import get_client
from prefect.automations import AutomationCore, EventTrigger, SendNotification
from prefect.blocks.notifications import SlackWebhook

# Initialize client
client = get_client()

# Define the Slack webhook block name
slack_block_name = "your-slack-webhook-block-name"

# Function to create automation for any tag
def create_automation_for_tag(tag_name):
    # Define the automation
    automation = AutomationCore(
        name=f"Notify on {tag_name} Tag Failure",
        triggers=[
            EventTrigger(
                event="prefect.flow-run.Failed",
                match_related=[{"prefect.resource.role": "tag", "prefect.resource.id": f"prefect.tag.{tag_name}"}]
            )
        ],
        actions=[
            SendNotification(
                block_name=slack_block_name,
                message=f"A flow with the '{tag_name}' tag has failed!"
            )
        ]
    )

    # Register the automation
    client.create_automation(automation)

# Example usage with an arbitrary tag
create_automation_for_tag("your-arbitrary-tag")
### Key Changes: - Function Definition: Wrapped the automation creation logic in a function
create_automation_for_tag
that accepts
tag_name
as a parameter. - Dynamic Tag Handling: The tag name is used dynamically in both the trigger's match condition and the notification message. You can call
create_automation_for_tag
with any tag name you want to monitor for failures. Adjust the
tag_name
in the function call as needed.
m
@Marvin how can I change the script to create an automation that triggers the slack webhook block if a flow with a tag enters state Failed, Timed out, or crashed.
m
To modify the script to trigger the Slack webhook block when a flow with a specific tag enters the "Failed," "Timed out," or "Crashed" state, you can adjust the event triggers to include multiple states. Here's how you can do it:
Copy code
python
from prefect import get_client
from prefect.automations import AutomationCore, EventTrigger, SendNotification
from prefect.blocks.notifications import SlackWebhook

# Initialize client
client = get_client()

# Define the Slack webhook block name
slack_block_name = "your-slack-webhook-block-name"

# Function to create automation for any tag and multiple states
def create_automation_for_tag(tag_name):
    # Define the automation
    automation = AutomationCore(
        name=f"Notify on {tag_name} Tag State Change",
        triggers=[
            EventTrigger(
                event={"prefect.flow-run.Failed", "prefect.flow-run.TimedOut", "prefect.flow-run.Crashed"},
                match_related=[{"prefect.resource.role": "tag", "prefect.resource.id": f"prefect.tag.{tag_name}"}]
            )
        ],
        actions=[
            SendNotification(
                block_name=slack_block_name,
                message=f"A flow with the '{tag_name}' tag has entered a critical state!"
            )
        ]
    )

    # Register the automation
    client.create_automation(automation)

# Example usage with an arbitrary tag
create_automation_for_tag("your-arbitrary-tag")
### Key Changes: - Multiple Event Triggers: The
event
parameter in
EventTrigger
is now a set that includes
"prefect.flow-run.Failed"
,
"prefect.flow-run.TimedOut"
, and
"prefect.flow-run.Crashed"
. - Dynamic Tag Handling: The function can still be used with any arbitrary tag. This setup ensures that the automation will trigger the Slack notification if the flow with the specified tag enters any of the specified states. Adjust the
tag_name
in the function call as needed.