Customize notifications in prefect orion notifications tab

Is there a way to change the function that creates the notification message created automatically when notification is set up on the Orion server?

Is there a way to use custom NotificationBlock to notify in the prefect Orion server? Do we simply need to create and register such NotificationBlock to make it available in the notification tab of the server?

2 Likes

You can implement custom notification blocks and register those. Alternatively, you can send customized alerts from your code, e.g.:

from prefect import flow, task
from prefect.blocks.notifications import SlackWebhook


@task
def send_alert():
    slack_webhook_block = SlackWebhook.load("slacknotificationblocktest") 
    slack_webhook_block.notify("Hello from Prefect!") 

@flow(name="Important-2.0-Flow")
def notification_flow():
    send_alert()

I was able to register a custom notification block class, and I can create instances on the UI.
It is working fine when I create a test block on the UI and load it to notify by hand.

However, when adding a notification via the UI with this new custom type (policy all tags and all states), it fails to notify with the following error:

Error sending notification for policy 85df5553-6aab-47eb-800a-40dd407c522b on flow run 9703078b-257c-4455-a187-90adf0b3412a
Traceback (most recent call last):
  File "/home/nduminy/.pyenv/versions/3.9.7/envs/youtube-v2-test/lib/python3.9/site-packages/prefect/orion/services/flow_run_notifications.py", line 88, in send_flow_run_notification
    block = Block._from_block_document(
  File "/home/nduminy/.pyenv/versions/3.9.7/envs/youtube-v2-test/lib/python3.9/site-packages/prefect/blocks/core.py", line 401, in _from_block_document
    else cls.get_block_class_from_schema(block_document.block_schema)
  File "/home/nduminy/.pyenv/versions/3.9.7/envs/youtube-v2-test/lib/python3.9/site-packages/prefect/blocks/core.py", line 432, in get_block_class_from_schema
    return lookup_type(cls, block_schema_to_key(schema))
  File "/home/nduminy/.pyenv/versions/3.9.7/envs/youtube-v2-test/lib/python3.9/site-packages/prefect/utilities/dispatch.py", line 186, in lookup_type
    raise KeyError(
KeyError: "No class found for dispatch key 'mattermost-webhook' in registry for type 'Block'."

The class for the custom block is exactly the same as the SlackWebhook one.

2 Likes

Let me ask the team, someone was recently working on the same to add Teams webhook

I have the same problem. Is there any news on this issue?

We now have apprise block which supports a variety of notification options. What’s your notification system? Can you check if apprise supports it?

Unfortunately, the apprise block does not suit me. We have our own messenger with specific logic in the company, so I would like to write my own custom block and be able to connect it to the prefect’s notification system.

you can already use it via code, there is just no UI support for custom block - I’m positive this will be more feasible with Automations in Cloud, but for now you can set it using custom code, here is an example:

I need a solution to send a notification to a custom webhook (AWS API Gateway tied to a Lambda Function). I want to send a json payload of all flow context available through cloud automations/notifications. I also need to be able to supply a header ‘x-api-key’ with the key. This is something we are doing on Cloud 1.0 but I haven’t found the equivalent (or at least how to do it) on 2.0. It looks like Apprise does have custom notification ability that would work here, I’m just not sure how to translate that to a block.

1 Like

gotcha, if you manage to do that using just apprise, feel free to share a minimal working example and we can discuss with integration engineers how to turn that into a block

Alternatively I could run this webhook as a deployment that is triggered by an automation, but it appears there is no way to pass the context of the flow run triggering the automation into the deployment, is that correct? i.e. I can’t use the jinja templating for notifications to pass context as deployment parameters… this isn’t my ideal implementation but that ability to pass context to deployment parameters would probably be useful.

excellent point, we have an open issue internally to support that pattern :slight_smile: we’ll keep you posted through #announcements with #release-notes tag

A better solution for us might just be an AWS SNS notification block which supports SNS topics. This would serve as a pretty useful swiss army knife to tie Prefect Notifications into everything AWS by way of SNS topics. This would be nice to if this block utilized the AWS credential block for auth.

do you want to give it a try and contribute to prefect-aws collection? happy to help review and QA

While I’d certainly like to contribute I really wouldn’t know where to start and I think it would take me a while to get up to speed here. We are enterprise clients with some unique use cases and integrations and I’m trying to make sure all our requirements are met so we can begin our 2.0 migration. This lack of parity with 1.0 around automations/notifications is one of my last items.

asking in the contributors channel in Slack would be a good way to get help on how to start

Is there a github ticket I can follow in regards to passing flow run context to a deployment?

we have an open internal issue about Automations, not flow run context

the ticket I mentioned is to support jinja templating in other actions than notifications