Skip to content

Add general-purpose "notifier" concept to DAGs for easier slack/teams/email notifications #28405

Description

@ashb

Extracted out of a discussion that I hijacked, below is my proposal for a general purpose notification system, and the relevant parts of the discussion.

In response to someone asking this on the mailing list I proposed this API: https://lists.apache.org/thread.html/rcd7fbd99408f5cdc1cc09e8a1619b629743da158ecf4c9d0eeb411f1%40%3Cdev.airflow.apache.org%3E

from airflow.providers.slack.notifiers import send_slack_message

task = MyOperator(
    task_id = "something",
    on_failure_callback= send_slack_message(
        slack_conn_id='slack-default', # Default, not actually required 
here, for example only
        channels=['#data-ops'],
        mentions=['@ash'],
    ),
)

Names can be changed/discussed, this is just a rough idea.

Using this approach means that a) We don't need to add a whole bunch of
new config, and b) it can be easily extended/created by providers
without needing changes to core -- after all the feature to run code
after failure/success already exists, we just want to "package up" the
common task of sending a slack message.

I think for consistency of interface too we should deprecate the
email_on_failure task attribute too in favour of a similar function.

And finally, perhaps on_*_callback gets extended to allow a list of
functions instead of just one.

Originally posted by @ashb in #12611 (comment)


I don't have time to look at the details, but I found a library that we can use to build notifications.
https://github.com/caronc/apprise
Is it compatible with your proposal?

Originally posted by @mik-laj in #12611 (comment)


I do think it would be good to have a plan for this one, for example:

  • Will we leverage a 3rd party library e.g. apprise?
  • What will the notification abstraction in code look like?
  • How will the configuration of each notification system be supported?
  • Will notifications be registered in the metastore?
  • Will there be any UI changes to view sent notification configuration/sent notifications?
  • How will it be part of providers packages

Originally posted by @BasPH in #12611 (comment)


  • Will we leverage a 3rd party library e.g. apprise?
  • How will it be part of providers packages

Both of these have the same answer -- yes part of providers, so it doesn't need a "core" decision. So we could have an apache-airflow-providers-apprise that provides a load of notifications, and/or we could have a notification in apache-airflow-providers-slack too.

What will the notification abstraction in code look like?

It could be as simple as this:

class Notifier(Protocol):
    def __call__(self, **context): -> None

i.e. a function that gets called with the TI context.

  • Will notifications be registered in the metastore?

The notifications for a TI: they will exist in the serialized_dag table, but I don't think a "notifications" table or anything like that.
All the possible notification classes: no, not in the metastore.

  • Will there be any UI changes to view sent notification configuration/sent notifications?

Configuration should use existing Connections mechanism, so no on that front.
Sent notifications: possibly, but that isn't needed in the first version I don't think. At least not in a general case. (I can see that being useful if we have a HTTP Webhook notification to debug issues, much like how GitHub has for apps - you can see recent deliveries and the responses etc.)

Originally posted by @ashb in #12611 (comment)


+1 on everything @ashb wrote with some update. I thought about using some existing APIs (a'la open-telemetry) but it add 0 value in our case. I agree with @ashb that simple interface where we simply notify with context is enough - with one caveat: we also need to pass "what" we are notifying about. So likely something like that:

class Notifier(Protocol):
    def __call__(self, type: NotificationType, message: str, **context): -> None

Originally posted by @potiuk in #12611 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions