<@ULVA73B9P> explain with example json and dialogu...
# ask-marvin
k
@Marvin explain with example json and dialogue, how I can setup a custom automation to alarm me when any flow with a certain tag fails three times in a row
m
Sure, let's walk through how you can set up a custom automation in Prefect to notify you when any flow with a certain tag fails three times in a row. We'll use a JSON configuration as an example, but please note that the exact steps may vary depending on your version of Prefect and the execution environment. Here's a rough idea of what we aim to achieve: 1. Define an event handler: Create a custom state handler or event handler that is triggered on flow run completion. This handler will need to keep track of the number of consecutive failures for flows with a certain tag. 2. Track consecutive failures: Within the handler, implement a logic that checks the flow's tag and increments a failure count if the flow has failed. You would need to reset this count once the flow succeeds or does not meet the criteria for a consecutive failure. 3. Configure notifications: Once the failure count reaches three, use your preferred method (such as sending a webhook to a chat service, triggering an email, etc.) to notify you about the triple failure. Since we are looking for articles or guides to help us set up automations and handle failures, let's search for documents that could provide us with detailed instructions and an example configuration in JSON format. I'll proceed with finding more detailed resources from the Prefect documentation and GitHub issues (if any relevant issues exist). Here's a step-by-step guide, including a JSON example, on how to set up a custom automation in Prefect to alarm you when any flow with a certain tag fails three times in a row: 1. Implement a State Handler: According to the Prefect documentation on state changes, you can create a state handler that fires upon state changes of flow or task runs. This custom script will need to keep a persistent count of the number of consecutive failures for flows with the specific tag you're interested in. 2. Track Consecutive Failures: Ensure your state handler logic increments a failure count when a flow with the desired tag fails and resets the count when the flow succeeds or the tag does not match. Persist this count somewhere, such as a database or a Prefect State. 3. Configure Notifications: Once the count reaches three, your handler should trigger a notification. Prefect Cloud has built-in notification mechanisms, as indicated in the documentation. You could send a message through a webhook to a chat service or email yourself. 4. Hook the State Handler to your Flows: In your flow definition, attach the custom state handler to your flow. Every time your flow changes state, Prefect will invoke your custom handler. Here's an example of what the JSON configuration for such an automation might look like:
Copy code
json
{
  "version": 1,
  "description": "Notify on three consecutive flow failures with a certain tag",
  "state_handlers": [
    {
      "type": "custom_consecutive_failure_handler",
      "config": {
        "tag_to_track": "critical-flow",
        "failure_threshold": 3,
        "notification_config": {
          "method": "webhook",
          "webhook_url": "<https://hooks.example.com/alarms>"
        }
      }
    }
  ]
}
In the example above, you would need to replace
"critical-flow"
with the actual tag you wish to monitor, and
"<https://hooks.example.com/alarms>"
with the URL of your notification service. Unfortunately, GitHub search did not result in any issues directly related to this topic, so it seems we're crafting a solution from documentation and first principles. Please note that the actual implementation of the
custom_consecutive_failure_handler
is not detailed here and would require programming knowledge to develop according to Prefect's state handler API. You would also have to adjust the JSON schema based on your specific version of Prefect and your execution environment.
👀 1
Remember to check the latest Prefect documentation on state change hooks and Prefect Cloud notifications for precise instructions and updates that may impact your custom automation setup.
n
yeesh not ideal
Copy code
from prefect import flow, tags
from prefect.events.schemas import DeploymentTrigger

TAG_NAME = "foobar"

@flow
def upstream():
    pass

@flow
def downstream():
    pass

if __name__ == "__main__":
    
    with tags(TAG_NAME):
        upstream()
        upstream()
        upstream()
    
    downstream.serve(
        name="downstream-of-tagged-upstream",
        triggers=[
            DeploymentTrigger(
                expect={"prefect.flow-run.Completed"},
                match_related=[
                    {
                        "prefect.resource.role": "tag",
                        "prefect.resource.id": f"prefect.tag.{TAG_NAME}",
                    },
                    {
                        "prefect.resource.role": "flow",
                        "prefect.resource.name": "upstream",
                    }
                ],
                threshold=3,
            )
        ]
    )
something like this perhaps?