Skip to content

Commit

Permalink
fix(discord): Fix alert formatting (#57290)
Browse files Browse the repository at this point in the history
Closes #57289

<img width="449" alt="Screenshot 2023-10-02 at 9 12 37 AM"
src="https://github.com/getsentry/sentry/assets/22582037/4fc6e1c0-4335-4ed0-9ea2-46a896751775">
  • Loading branch information
Julia Hoge authored Oct 3, 2023
1 parent 2057c3e commit 2500a36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/sentry/integrations/discord/message_builder/metric_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from sentry.integrations.discord.message_builder.base.embed.base import DiscordMessageEmbed
from sentry.integrations.discord.message_builder.base.embed.image import DiscordMessageEmbedImage
from sentry.integrations.metric_alerts import metric_alert_attachment_info
from sentry.integrations.slack.utils.escape import escape_slack_text


class DiscordMetricAlertMessageBuilder(DiscordMessageBuilder):
Expand Down Expand Up @@ -39,15 +38,17 @@ def build(self, notification_uuid: str | None = None) -> dict[str, object]:
DiscordMessageEmbed(
title=data["title"],
url=f"{data['title_link']}&referrer=discord",
description=f"<{data['title_link']}|*{escape_slack_text(data['title'])}*> \n{data['text']}",
description=f"{data['text']}{get_started_at(data['date_started'])}",
color=LEVEL_TO_COLOR[INCIDENT_COLOR_MAPPING.get(data["status"], "")],
image=DiscordMessageEmbedImage(self.chart_url) if self.chart_url else None,
image=DiscordMessageEmbedImage(url=self.chart_url) if self.chart_url else None,
)
]

return self._build(embeds=embeds)


def get_started_at(timestamp: datetime) -> str:
unix_timestamp = int(time.mktime(timestamp.timetuple()))
return f"Started <t:{unix_timestamp}:R>"
if timestamp:
unix_timestamp = int(time.mktime(timestamp.timetuple()))
return f"\nStarted <t:{unix_timestamp}:R>"
return ""
27 changes: 14 additions & 13 deletions tests/sentry/integrations/discord/test_message_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sentry.integrations.discord.message_builder import LEVEL_TO_COLOR
from sentry.integrations.discord.message_builder.metric_alerts import (
DiscordMetricAlertMessageBuilder,
get_started_at,
)
from sentry.testutils.cases import TestCase
from sentry.testutils.silo import region_silo_test
Expand All @@ -30,12 +31,13 @@ def test_metric_alert_without_incidents(self):
},
)
)

assert DiscordMetricAlertMessageBuilder(self.alert_rule).build() == {
"content": "",
"embeds": [
{
"title": title,
"description": f"<{link}|*{title}*> \n",
"description": "",
"url": f"{link}&referrer=discord",
"color": LEVEL_TO_COLOR["_incident_resolved"],
}
Expand Down Expand Up @@ -69,7 +71,7 @@ def test_metric_alert_with_selected_incident(self):
"embeds": [
{
"title": title,
"description": f"<{link}|*{title}*> \n",
"description": get_started_at(incident.date_started),
"url": f"{link}&referrer=discord",
"color": LEVEL_TO_COLOR["_incident_resolved"],
}
Expand All @@ -78,9 +80,8 @@ def test_metric_alert_with_selected_incident(self):
}

def test_metric_alert_with_active_incident(self):
incident = self.create_incident(
alert_rule=self.alert_rule, status=IncidentStatus.CRITICAL.value
)
new_status = IncidentStatus.CRITICAL.value
incident = self.create_incident(alert_rule=self.alert_rule, status=new_status)
trigger = self.create_alert_rule_trigger(self.alert_rule, CRITICAL_TRIGGER_LABEL, 100)
self.create_alert_rule_trigger_action(
alert_rule_trigger=trigger, triggered_for_incident=incident
Expand All @@ -102,7 +103,7 @@ def test_metric_alert_with_active_incident(self):
{
"color": LEVEL_TO_COLOR["fatal"],
"title": title,
"description": f"<{link}|*{title}*> \n0 events in the last 10 minutes",
"description": "0 events in the last 10 minutes",
"url": f"{link}&referrer=discord",
}
],
Expand All @@ -113,7 +114,6 @@ def test_metric_value(self):
incident = self.create_incident(
alert_rule=self.alert_rule, status=IncidentStatus.CLOSED.value
)

# This test will use the action/method and not the incident to build status
title = f"Critical: {self.alert_rule.name}"
metric_value = 5000
Expand All @@ -130,6 +130,7 @@ def test_metric_value(self):
},
)
)

assert DiscordMetricAlertMessageBuilder(
self.alert_rule, incident, IncidentStatus.CRITICAL, metric_value=metric_value
).build() == {
Expand All @@ -138,15 +139,17 @@ def test_metric_value(self):
{
"title": title,
"color": LEVEL_TO_COLOR["fatal"],
"description": f"<{link}?alert={incident.identifier}|*{title}*> \n"
f"{metric_value} events in the last 10 minutes",
"description": f"{metric_value} events in the last 10 minutes{get_started_at(incident.date_started)}",
"url": f"{link}?alert={incident.identifier}&referrer=discord",
}
],
"components": [],
}

def test_metric_alert_chart(self):
incident = self.create_incident(
alert_rule=self.alert_rule, status=IncidentStatus.OPEN.value
)
title = f"Resolved: {self.alert_rule.name}"
link = absolute_uri(
reverse(
Expand All @@ -157,9 +160,7 @@ def test_metric_alert_chart(self):
},
)
)
incident = self.create_incident(
alert_rule=self.alert_rule, status=IncidentStatus.OPEN.value
)

new_status = IncidentStatus.CLOSED
assert DiscordMetricAlertMessageBuilder(
self.alert_rule, incident, new_status, chart_url="chart_url"
Expand All @@ -168,7 +169,7 @@ def test_metric_alert_chart(self):
"embeds": [
{
"title": title,
"description": f"<{link}?alert={incident.identifier}|*{title}*> \n",
"description": get_started_at(incident.date_started),
"url": f"{link}?alert={incident.identifier}&referrer=discord",
"color": 5097329,
"image": {"url": "chart_url"},
Expand Down

1 comment on commit 2500a36

@vercel
Copy link

@vercel vercel bot commented on 2500a36 Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sentry – ./

sentry.vercel.app
sentry.sentry.dev
sentry.dev
sentry-git-master.sentry.dev

Please sign in to comment.