-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(np): Adds Discord metric alert renderer #112420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/sentry/notifications/platform/discord/renderers/metric_alert.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from sentry.incidents.models.incident import IncidentStatus | ||
| from sentry.integrations.discord.message_builder import INCIDENT_COLOR_MAPPING, LEVEL_TO_COLOR | ||
| from sentry.integrations.discord.message_builder.base.base import DiscordMessageBuilder | ||
| 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.discord.message_builder.metric_alerts import get_started_at | ||
| from sentry.integrations.metric_alerts import get_status_text | ||
| from sentry.notifications.platform.discord.provider import DiscordRenderable | ||
| from sentry.notifications.platform.renderer import NotificationRenderer | ||
| from sentry.notifications.platform.templates.metric_alert import MetricAlertNotificationData | ||
| from sentry.notifications.platform.types import ( | ||
| NotificationData, | ||
| NotificationProviderKey, | ||
| NotificationRenderedTemplate, | ||
| ) | ||
|
|
||
|
|
||
| class DiscordMetricAlertRenderer(NotificationRenderer[DiscordRenderable]): | ||
| provider_key = NotificationProviderKey.DISCORD | ||
|
|
||
| @classmethod | ||
| def render[DataT: NotificationData]( | ||
| cls, *, data: DataT, rendered_template: NotificationRenderedTemplate | ||
| ) -> DiscordRenderable: | ||
| if not isinstance(data, MetricAlertNotificationData): | ||
| raise ValueError( | ||
| f"DiscordMetricAlertRenderer does not support '{data.__class__.__name__}'. Provide a MetricAlertNotificationData instead." | ||
| ) | ||
|
|
||
| status = get_status_text(IncidentStatus(data.new_status)) | ||
| description = f"{data.text}{get_started_at(data.open_period_context.date_started)}" | ||
| color = LEVEL_TO_COLOR.get(INCIDENT_COLOR_MAPPING.get(status, "")) | ||
|
|
||
| embed = DiscordMessageEmbed( | ||
| title=data.title, | ||
| url=data.title_link, | ||
| description=description, | ||
| color=color, | ||
| image=(DiscordMessageEmbedImage(url=data.chart_url) if data.chart_url else None), | ||
| ) | ||
|
|
||
| return DiscordMessageBuilder(embeds=[embed]).build() |
169 changes: 169 additions & 0 deletions
169
tests/sentry/notifications/platform/discord/renderers/test_metric_alert.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime, timezone | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from sentry.incidents.models.incident import IncidentStatus | ||
| from sentry.incidents.typings.metric_detector import OpenPeriodContext | ||
| from sentry.integrations.discord.message_builder import INCIDENT_COLOR_MAPPING, LEVEL_TO_COLOR | ||
| from sentry.notifications.platform.discord.provider import ( | ||
| DiscordNotificationProvider, | ||
| ) | ||
| from sentry.notifications.platform.discord.renderers.metric_alert import ( | ||
| DiscordMetricAlertRenderer, | ||
| ) | ||
| from sentry.notifications.platform.templates.metric_alert import MetricAlertNotificationData | ||
| from sentry.notifications.platform.templates.seer import SeerAutofixError | ||
| from sentry.notifications.platform.types import ( | ||
| NotificationCategory, | ||
| NotificationRenderedTemplate, | ||
| ) | ||
| from sentry.testutils.cases import TestCase | ||
| from tests.sentry.notifications.notification_action.test_metric_alert_registry_handlers import ( | ||
| MetricAlertHandlerBase, | ||
| ) | ||
|
|
||
| MOCK_CHART_URL = "https://chart.example.com/metric.png" | ||
|
|
||
|
|
||
| def _make_notification_data(**overrides: Any) -> MetricAlertNotificationData: | ||
| defaults: dict[str, Any] = dict( | ||
| group_id=1, | ||
| organization_id=1, | ||
| notification_uuid="test-uuid", | ||
| action_id=1, | ||
| open_period_context=OpenPeriodContext( | ||
| id=1, | ||
| date_started=datetime(2024, 1, 1, tzinfo=timezone.utc), | ||
| ), | ||
| new_status=IncidentStatus.CRITICAL.value, | ||
| title="Critical: Test Alert", | ||
| title_link="https://sentry.io/alerts/1/", | ||
| text="123 events in the last 5 minutes", | ||
| ) | ||
| defaults.update(overrides) | ||
| return MetricAlertNotificationData(**defaults) | ||
|
|
||
|
|
||
| class DiscordMetricAlertRendererInvalidDataTest(TestCase): | ||
| def test_render_raises_on_invalid_data_type(self) -> None: | ||
| invalid_data = SeerAutofixError(error_message="not a metric alert") | ||
| rendered_template = NotificationRenderedTemplate(subject="Metric Alert", body=[]) | ||
|
|
||
| with pytest.raises(ValueError, match="does not support"): | ||
| DiscordMetricAlertRenderer.render( | ||
| data=invalid_data, | ||
| rendered_template=rendered_template, | ||
| ) | ||
|
|
||
|
|
||
| class DiscordMetricAlertProviderDispatchTest(TestCase): | ||
| def test_provider_returns_metric_alert_renderer(self) -> None: | ||
| data = _make_notification_data() | ||
| renderer = DiscordNotificationProvider.get_renderer( | ||
| data=data, | ||
| category=NotificationCategory.METRIC_ALERT, | ||
| ) | ||
| assert renderer is DiscordMetricAlertRenderer | ||
|
|
||
| def test_provider_returns_default_for_unknown_category(self) -> None: | ||
| data = _make_notification_data() | ||
| renderer = DiscordNotificationProvider.get_renderer( | ||
| data=data, | ||
| category=NotificationCategory.DEBUG, | ||
| ) | ||
| assert renderer is DiscordNotificationProvider.default_renderer | ||
|
|
||
|
|
||
| class DiscordMetricAlertRendererTest(MetricAlertHandlerBase): | ||
| def setUp(self) -> None: | ||
| super().setUp() | ||
| self.create_models() | ||
|
|
||
| open_period_context = OpenPeriodContext.from_group(self.group) | ||
|
|
||
| self.notification_data = _make_notification_data( | ||
| group_id=self.group.id, | ||
| organization_id=self.organization.id, | ||
| open_period_context=open_period_context, | ||
| title=f"Critical: {self.detector.name}", | ||
| title_link="https://sentry.io/alerts/1/", | ||
| text="123.45 events in the last minute", | ||
| ) | ||
| self.rendered_template = NotificationRenderedTemplate(subject="Metric Alert", body=[]) | ||
|
|
||
| def test_render_produces_embed(self) -> None: | ||
| result = DiscordMetricAlertRenderer.render( | ||
| data=self.notification_data, | ||
| rendered_template=self.rendered_template, | ||
| ) | ||
|
|
||
| assert "embeds" in result | ||
| embeds = result["embeds"] | ||
| assert len(embeds) == 1 | ||
|
|
||
| embed = embeds[0] | ||
| assert embed["title"] == f"Critical: {self.detector.name}" | ||
| assert embed["url"] == "https://sentry.io/alerts/1/" | ||
| assert "123.45 events in the last minute" in embed["description"] | ||
| assert "Started <t:" in embed["description"] | ||
| # Critical maps to "fatal" color | ||
| assert embed["color"] == LEVEL_TO_COLOR[INCIDENT_COLOR_MAPPING["Critical"]] | ||
|
|
||
| def test_render_includes_image_when_chart_url_set(self) -> None: | ||
| data_with_chart = _make_notification_data( | ||
| group_id=self.group.id, | ||
| organization_id=self.organization.id, | ||
| open_period_context=OpenPeriodContext.from_group(self.group), | ||
| title=f"Critical: {self.detector.name}", | ||
| title_link="https://sentry.io/alerts/1/", | ||
| text="123.45 events in the last minute", | ||
| chart_url=MOCK_CHART_URL, | ||
| ) | ||
|
|
||
| result = DiscordMetricAlertRenderer.render( | ||
| data=data_with_chart, | ||
| rendered_template=self.rendered_template, | ||
| ) | ||
|
|
||
| embed = result["embeds"][0] | ||
| assert embed["title"] == f"Critical: {self.detector.name}" | ||
| assert embed["url"] == "https://sentry.io/alerts/1/" | ||
| assert "123.45 events in the last minute" in embed["description"] | ||
| assert embed["image"]["url"] == MOCK_CHART_URL | ||
|
|
||
| def test_render_without_chart_url(self) -> None: | ||
| result = DiscordMetricAlertRenderer.render( | ||
| data=self.notification_data, | ||
| rendered_template=self.rendered_template, | ||
| ) | ||
|
|
||
| embed = result["embeds"][0] | ||
| assert embed["title"] == f"Critical: {self.detector.name}" | ||
| assert embed["url"] == "https://sentry.io/alerts/1/" | ||
| assert "123.45 events in the last minute" in embed["description"] | ||
| assert "image" not in embed or embed.get("image") is None | ||
|
|
||
| def test_render_resolved_status(self) -> None: | ||
| resolved_data = _make_notification_data( | ||
| group_id=self.group.id, | ||
| organization_id=self.organization.id, | ||
| open_period_context=OpenPeriodContext.from_group(self.group), | ||
| title=f"Resolved: {self.detector.name}", | ||
| title_link="https://sentry.io/alerts/1/", | ||
| text="", | ||
| new_status=IncidentStatus.CLOSED.value, | ||
| ) | ||
|
|
||
| result = DiscordMetricAlertRenderer.render( | ||
| data=resolved_data, | ||
| rendered_template=self.rendered_template, | ||
| ) | ||
|
|
||
| embed = result["embeds"][0] | ||
| assert embed["title"] == f"Resolved: {self.detector.name}" | ||
| assert embed["url"] == "https://sentry.io/alerts/1/" | ||
| assert "Started <t:" in embed["description"] | ||
| assert embed["color"] == LEVEL_TO_COLOR[INCIDENT_COLOR_MAPPING["Resolved"]] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i really like how you broke down these tests and organized everything 🙏