Skip to content

Commit

Permalink
add alert details using slack block kit (#1751)
Browse files Browse the repository at this point in the history
* add alert details using slack block kit

* tidy up

* get rid of fstrings
  • Loading branch information
russellbrooks committed Feb 21, 2024
1 parent 05ec0ee commit 1bacc31
Showing 1 changed file with 84 additions and 21 deletions.
105 changes: 84 additions & 21 deletions metaflow/plugins/argo/argo_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,6 @@ def _container_templates(self):

# Return exit hook templates for workflow execution notifications.
def _exit_hook_templates(self):
# TODO: Add details to slack message
templates = []
if self.notify_on_error:
templates.append(self._slack_error_template())
Expand Down Expand Up @@ -1649,36 +1648,100 @@ def _pager_duty_notification_links(self):

return links

def _get_slack_blocks(self, message):
"""
Use Slack's Block Kit to add general information about the environment and
execution metadata, including a link to the UI and an optional message.
"""
ui_link = "%s%s/argo-{{workflow.name}}" % (UI_URL, self.flow.name)
# fmt: off
if getattr(current, "project_name", None):
# Add @project metadata when available.
environment_details_block = {
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":metaflow: Environment details"
},
"fields": [
{
"type": "mrkdwn",
"text": "*Project:* %s" % current.project_name
},
{
"type": "mrkdwn",
"text": "*Project Branch:* %s" % current.branch_name
}
]
}
else:
environment_details_block = {
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":metaflow: Environment details"
}
}

blocks = [
environment_details_block,
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": " :information_source: *<%s>*" % ui_link,
}
],
},
{
"type": "divider"
},
]

if message:
blocks += [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message
}
}
]
# fmt: on
return blocks

def _slack_error_template(self):
if self.notify_slack_webhook_url is None:
return None

message = (
":rotating_light: _%s/argo-{{workflow.name}}_ failed!" % self.flow.name
)
payload = {"text": message}
if UI_URL:
blocks = self._get_slack_blocks(message)
payload = {"text": message, "blocks": blocks}

return Template("notify-slack-on-error").http(
Http("POST")
.url(self.notify_slack_webhook_url)
.body(
json.dumps(
{
"text": ":rotating_light: _%s/argo-{{workflow.name}}_ failed!"
% self.flow.name
}
)
)
Http("POST").url(self.notify_slack_webhook_url).body(json.dumps(payload))
)

def _slack_success_template(self):
if self.notify_slack_webhook_url is None:
return None

message = (
":white_check_mark: _%s/argo-{{workflow.name}}_ succeeded!" % self.flow.name
)
payload = {"text": message}
if UI_URL:
blocks = self._get_slack_blocks(message)
payload = {"text": message, "blocks": blocks}

return Template("notify-slack-on-success").http(
Http("POST")
.url(self.notify_slack_webhook_url)
.body(
json.dumps(
{
"text": ":white_check_mark: _%s/argo-{{workflow.name}}_ succeeded!"
% self.flow.name
}
)
)
Http("POST").url(self.notify_slack_webhook_url).body(json.dumps(payload))
)

def _compile_sensor(self):
Expand Down

0 comments on commit 1bacc31

Please sign in to comment.