Skip to content

Latest commit

 

History

History

Module_4

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Automations, Events, and Incidents Quickstart

1. Create a notification automation that fires on flow run completion.

  1. Find the Automations page in the UI and click the + button.

  2. Define your trigger:

    Add a tag that will filter out other people's flow runs, ideally you've tagged you deployment with your name, like in this example, I put taylor Alt text

  3. Define your action:

    Use the block you created during the blocks quickstart. Alt text

  4. Name your automation: Alt text

  5. Run a deployment (ensure that it meets the criteria set in your automation's trigger, e.g. the tags pacc and taylor)

2. Browse the events dashboard.

  1. See if you can spot the events related to the automation you just created: Alt text
  2. What about events related to your deployment? Alt text
  3. How about events relating to the blocks you created today? Alt text
  4. Click on the Flow run of a Block event to see when the method was called during the flow's execution: Alt text Alt text
  5. Now simply click on the ✨ icon at the top right of the events line to return to the event feed. This action filters the feed to show only events related to the flow run from the page you were just viewing.

3. Create a metrics trigger.

  1. Navigate to the automations page and create a new automation. Alt text
  2. Create a metrics trigger that alerts you after 10 minutes of a sustained success rate below 90% (referencing the average of the past day). Alt text
  3. Make the action a notification.

4. Declare and resolve a Prefect Incident

Incidents are formal declarations of disruptions to a workspace. With automations, activity in that workspace can be paused when an incident is created and resumed when it is resolved.

  1. Find the Incidents page and click the + button.

  2. Define a test incident, call it something like your-name-pacc-test and click Declare: Alt text

  3. Add a test comment.

  4. Navigate to the Flow Runs page and find a failed flow run. (Notice the AI Error Summaries) Alt text

  5. Click into a failed flow run, click on the three dots, and then click Add to incident Alt text

  6. Change the severity of the incident.

  7. Mark the incident as resolved. ✅ Alt text

  8. Note: Incidents are a more powerful feature when combined with automations:

    Automations can be used for triggering an incident and for selecting actions to take when an incident is triggered. For example, a work pool status change could trigger the declaration of an incident, or a critical level incident could trigger a notification action.

    To automatically take action when an incident is declared, set up a custom trigger that listens for declaration events.

    {
    "match": {
        "prefect.resource.id": "prefect-cloud.incident.*"
    },
    "expect": [
        "prefect-cloud.incident.declared"
    ],
    "posture": "Reactive",
    "threshold": 1,
    "within": 0
    }

5. Advanced Automations: Create a custom trigger and compound actions

In this example, you'll create a custom trigger that results in an action of a toy webhook being called.

  1. Find an block-based event in the event feed and click into the event details.

    Filter by Resource: Alt text

  2. Click on the Raw tab to review the event's JSON values then click on the three dots at the top right and click Automate. Alt text

    You'll see we can describe a custom triggering event using JSON: Alt text

    Note: There are many things we can do to fine tune this custom trigger. For example, we could change the threshold from 1 to 2 if we want this event to occur twice before triggering any action. For more information on trigger grammar, check out these docs.

  3. For the action select Call a webhook and define a webhook block.

    1. First get a toy webhook endpoint at Webhook.site
    2. Copy your unique URL
    3. Back in Prefect's UI, select the action Call a webhook
    4. Add + a webhook block, paste in your unique URL from Webhook.site, and click Create. Alt text Alternatively, exit the automation screen, go to the Blocks page, and create the Web Hook block from there. Once created go back to the events feed to re-create the custom trigger.
  4. Add another action to notify yourself about this.

    Your Actions page should look something like: Alt text

  5. Call your Automation something like taylor-pacc-json-block-automation and save.

  6. Run your flow that loads the block, something like:

    from prefect import flow, task
    from prefect.blocks.system import JSON
    
    @task
    def load_block():
        jb = JSON.load("taylor-pacc-json-block")
        my_dict = jb.value
    
        print(my_dict)
    
    @flow(log_prints=True)
    def load_block_flow():
        load_block()
    
    if __name__ == "__main__":
        load_block_flow()
  7. Verify that you got a notification email and verify that the webhook was called.