Hello, I think I know the answer to this but I am ...
# prefect-cloud
b
Hello, I think I know the answer to this but I am wondering if there are options. We have a situation where some flow runs purposely throw warnings which is mainly to tell us that a certain unfavorable condition exists and is not bad enough to fail the run. We would like to create an Automation notification when this occurs. Since there doesn't appear to be a flow-run state of warn or warning, it doesn't seem like we can create a notification around it using the Cloud UI. Is that true? My other thought is that we would have to handle this within the code and manually generate the notification within the code, which means we will then have to ensure the message format follows what we have setup for our other Automation notifications. Is this our only other option?
1
k
Yeah, since warnings are just a log level and not a state, there's nothing preexisting you can use in Automations for them. You can, however, call a function in your code that sends a message to whatever destination you're using for notifications.
b
Thanks Kevin. That's what I was assuming we were going to need to do.
Just wanted to make sure I wasn't missing something before we went down the path to create something via code.
k
It's likely that all the things you want to include in your notification are available in your flow run via runtime.
👍 1
b
Agreed
k
You could also emit a custom event, and set up an automation to react to that event.
b
Tell me more about the custom event. I started reading a little bit about them but was confused as to how to actually emit them. Is there a code example out there that I could take a look at?
k
that event will end up in the event feed for whatever workspace the flow run is affiliated with
b
Ok. That might be an easy option as well. Thanks for that
k
here's a quick example I emit a custom event from my flow:
Copy code
from prefect import flow, get_run_logger
from prefect.events import emit_event

@flow
def warning_event():
    # something acceptably bad happened
    get_run_logger().warning("This is a warning")
    emit_event(event="custom_events.logging.Warning", resource={"prefect.resource.id": "custom_events.event"})
I see it in my event feed, click on it, then click the three dots and click "Automate"
❤️ 1
upvote 1
that'll get you started building an automation that fires when that custom event is seen
🙏 1
b
Awesome!
@Kevin Grismore is it possible to include payload information in the "match" part of the custom trigger? We are putting our environment name in the payload part of the event and we want to only fire the custom trigger when the environment is "dev". This is sort of what I am thinking:
Copy code
{
  "match": {
    "prefect.resource.id": "test.dbt.SOME_DBT_TEST",
    "payload.environment": "dev"
  },
  "match_related": {},
  "after": [],
  "expect": [
    "custom_events.logging.dbt_warning"
  ],
  "for_each": [],
  "posture": "Reactive",
  "threshold": 1,
  "within": 0
}