Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Remove env from Airflow alerts, improve ES alert username & icon (#4652)

Madison Swain-Bowden7f5baa29f1c5
diff --git a/catalog/dags/common/slack.py b/catalog/dags/common/slack.py
index b3748ecf0..dfe9750b2 100644
--- a/catalog/dags/common/slack.py
+++ b/catalog/dags/common/slack.py
@@ -336,9 +336,8 @@ def send_message(
):
return
- environment = Variable.get("ENVIRONMENT", default_var="local")
s = SlackMessage(
- f"{username} | {environment}",
+ username,
icon_emoji,
unfurl_links,
unfurl_media,
diff --git a/catalog/dags/elasticsearch_cluster/healthcheck_dag.py b/catalog/dags/elasticsearch_cluster/healthcheck_dag.py
index 5191a6c90..7a3aa0724 100644
--- a/catalog/dags/elasticsearch_cluster/healthcheck_dag.py
+++ b/catalog/dags/elasticsearch_cluster/healthcheck_dag.py
@@ -36,6 +36,8 @@ logger = logging.getLogger(__name__)
_DAG_ID = "{env}_elasticsearch_cluster_healthcheck"
+ES_ICON = ":elasticsearch_bad:"
+ES_USERNAME = "{env} ES Cluster (via Airflow)"
EXPECTED_NODE_COUNT = 6
EXPECTED_DATA_NODE_COUNT = 3
@@ -142,10 +144,16 @@ def compose_notification(
def notify(env: str, message_type_and_string: tuple[MessageType, str]):
message_type, message = message_type_and_string
+ message_kwargs = {
+ "dag_id": _DAG_ID.format(env=env),
+ "username": ES_USERNAME.format(env=env.title()),
+ "icon_emoji": ES_ICON,
+ }
+
if message_type == "alert":
- send_alert(dedent(message), dag_id=_DAG_ID.format(env=env))
+ send_alert(dedent(message), **message_kwargs)
elif message_type == "notification":
- send_message(dedent(message), dag_id=_DAG_ID.format(env=env))
+ send_message(dedent(message), **message_kwargs)
else:
raise ValueError(
f"Invalid message_type. Expected 'alert' or 'notification', "
diff --git a/catalog/tests/dags/common/test_slack.py b/catalog/tests/dags/common/test_slack.py
index 0814017c3..6d6a8c4cb 100644
--- a/catalog/tests/dags/common/test_slack.py
+++ b/catalog/tests/dags/common/test_slack.py
@@ -503,23 +503,6 @@ def test_should_silence_message(silenced_notifications, should_silence):
)
[email protected]("environment", ["local", "production"])
-def test_send_message(environment, http_hook_mock):
- with mock.patch("common.slack.should_send_message", return_value=True), mock.patch(
- "common.slack.Variable"
- ) as MockVariable:
- MockVariable.get.side_effect = [environment]
- send_message("Sample text", dag_id="test_workflow", username="DifferentUser")
- http_hook_mock.run.assert_called_with(
- endpoint=None,
- data=f'{{"username": "DifferentUser | {environment}", "unfurl_links": false, "unfurl_media": false,'
- ' "icon_emoji": ":airflow:", "blocks": [{"type": "section", "text": '
- '{"type": "mrkdwn", "text": "Sample text"}}], "text": "Sample text"}',
- headers={"Content-type": "application/json"},
- extra_options={"verify": True},
- )
-
-
def test_send_message_does_not_send_if_checks_fail(http_hook_mock):
with mock.patch("common.slack.should_send_message", return_value=False):
send_message("Sample text", dag_id="test_workflow", username="DifferentUser")