Skip to content

feat(notifications): Pre compute notification fields prior to calling into the platform#112160

Merged
Christinarlong merged 2 commits into
masterfrom
crl/rewrite-metric-alert-notif-data-and-renderer
Apr 2, 2026
Merged

feat(notifications): Pre compute notification fields prior to calling into the platform#112160
Christinarlong merged 2 commits into
masterfrom
crl/rewrite-metric-alert-notif-data-and-renderer

Conversation

@Christinarlong
Copy link
Copy Markdown
Contributor

Summary

  • Hooks SlackMetricAlertHandler into the Notification Platform (NP) replacing the send_incident_alert_notification() call
  • Pre-computes attachment info and chart URL in the handler and placing those into the data (reducing lookups)
  • Eliminates Activity vs GroupEvent branching in the renderer — invoke_legacy_registry already resolves both paths into the same context objects before send_alert() is called

Note:

  • We're no longer hooking in at the noa layer atp (diff from original spec), so we didn;t really get rid of the invoke_legacy_registry part which may have been a goal?

…lert using different event types

- So we're going to hook in later and do more pre computation of the metric alert notif fields.
Pass those into the renderer and the renderer has to do less work to make the data usable for slack.
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 2, 2026
@Christinarlong Christinarlong marked this pull request as ready for review April 2, 2026 21:34
@Christinarlong Christinarlong requested review from a team as code owners April 2, 2026 21:34
Copy link
Copy Markdown
Contributor

@sentry-warden sentry-warden Bot left a comment

Choose a reason for hiding this comment

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

Detector.objects.get() can raise DoesNotExist without handler (src/sentry/notifications/notification_action/metric_alert_registry/handlers/slack_metric_alert_handler.py:142)

Line 142 uses Detector.objects.get(id=alert_context.action_identifier_id) without a try/except for Detector.DoesNotExist. If the detector is deleted between when the invocation is created and when send_alert runs, this will raise an unhandled exception. The check on line 143-144 if not detector is dead code since .get() either returns an object or throws - it never returns None.

Identified by Warden sentry-backend-bugs

# METRIC_ALERT
METRIC_ALERT = "metric-alert"
ACTIVITY_METRIC_ALERT = "activity-metric-alert"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: Removing ACTIVITY_METRIC_ALERT from NotificationSource will cause deserialization to fail for existing NotificationThread records with that key_type, raising an error.
Severity: MEDIUM

Suggested Fix

To prevent deserialization failures, either add a data migration to update or remove existing NotificationThread records with key_type="activity-metric-alert", or implement a fallback mechanism to handle the legacy source type gracefully during deserialization, perhaps by mapping it to the new MetricAlertNotificationData path or ignoring it.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/sentry/notifications/platform/types.py#L61

Potential issue: The pull request removes `ACTIVITY_METRIC_ALERT` from the
`NotificationSource` enum and its corresponding template from the registry. If any
`NotificationThread` records with `key_type="activity-metric-alert"` exist in the
database, any operation that requires deserializing them (such as replying to an old
notification) will fail. The `deserialize_notification_data` function will attempt to
look up the template for the source `"activity-metric-alert"`, which no longer exists in
the registry, causing a `NoRegistrationExistsError`. This will lead to a crash when
interacting with older metric alert notification threads.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ACTIVITY_METRIC_ALERT shouldnt be used anywehre anymore

Copy link
Copy Markdown
Member

@GabeVillalobos GabeVillalobos left a comment

Choose a reason for hiding this comment

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

Changes look good! Thanks for overhauling this.

metric_issue_context = _build_metric_issue_context_from_group_event(data)
elif isinstance(data, ActivityMetricAlertNotificationData):
metric_issue_context = _build_metric_issue_context_from_activity(data)
incident_text = f"{data.text}\n{get_started_at(data.open_period_context.date_started)}"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are we still planning to hoist this into the NOA layer? Seems like we don't need the full open_period_context anymore at the very least.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sadly we can't hoist this up as get_started_at turns the text into a slack sepcific format

@Christinarlong Christinarlong enabled auto-merge (squash) April 2, 2026 21:53
@Christinarlong Christinarlong merged commit e047451 into master Apr 2, 2026
65 checks passed
@Christinarlong Christinarlong deleted the crl/rewrite-metric-alert-notif-data-and-renderer branch April 2, 2026 21:57
@Christinarlong Christinarlong added the Trigger: Revert Add to a merged PR to revert it (skips CI) label Apr 2, 2026
@getsentry-bot
Copy link
Copy Markdown
Contributor

PR reverted: 36ffdae

getsentry-bot added a commit that referenced this pull request Apr 2, 2026
… calling into the platform (#112160)"

This reverts commit e047451.

Co-authored-by: Christinarlong <60594860+Christinarlong@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components Trigger: Revert Add to a merged PR to revert it (skips CI)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants