Skip to content
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

♻️ ref(metric alerts): refactor slack metric alert logic #87261

Merged
merged 5 commits into from
Mar 19, 2025

Conversation

iamrajjoshi
Copy link
Member

@iamrajjoshi iamrajjoshi commented Mar 18, 2025

this pr doesn't do anything new, it decomposes the slack metric alert action handler util so it is ready for me to make the open period refactors

it might be easier to review by cloning locally and taking a look

pr to add open period stuff to follow

@iamrajjoshi iamrajjoshi self-assigned this Mar 18, 2025
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Mar 18, 2025
Copy link

codecov bot commented Mar 18, 2025

❌ 3 Tests Failed:

Tests completed Failed Passed Skipped
24419 3 24416 299
View the top 3 failed test(s) by shortest run time
tests.sentry.api.endpoints.test_organization_spans_fields_stats.OrganizationSpansFieldsStatsEndpointTest::test_filter_query
Stack Traces | 3.77s run time
#x1B[1m#x1B[.../api/endpoints/test_organization_spans_fields_stats.py#x1B[0m:157: in test_filter_query
    assert distributions[0]["attributeName"] == "broswer"
#x1B[1m#x1B[31mE   AssertionError: assert 'sentry.transaction' == 'broswer'#x1B[0m
#x1B[1m#x1B[31mE     #x1B[0m
#x1B[1m#x1B[31mE     - broswer#x1B[0m
#x1B[1m#x1B[31mE     + sentry.transaction#x1B[0m
tests.sentry.api.endpoints.test_organization_spans_fields_stats.OrganizationSpansFieldsStatsEndpointTest::test_max_buckets
Stack Traces | 4.29s run time
#x1B[1m#x1B[.../api/endpoints/test_organization_spans_fields_stats.py#x1B[0m:117: in test_max_buckets
    assert len(distributions) == max_buckets - 1
#x1B[1m#x1B[31mE   AssertionError: assert 1 == (3 - 1)#x1B[0m
#x1B[1m#x1B[31mE    +  where 1 = len([{'label': 'foo', 'value': 3.0}])#x1B[0m
tests.sentry.api.endpoints.test_organization_spans_fields_stats.OrganizationSpansFieldsStatsEndpointTest::test_distribution_values
Stack Traces | 5.27s run time
#x1B[1m#x1B[.../api/endpoints/test_organization_spans_fields_stats.py#x1B[0m:134: in test_distribution_values
    assert distributions[0]["attributeName"] == "broswer"
#x1B[1m#x1B[31mE   AssertionError: assert 'sentry.transaction' == 'broswer'#x1B[0m
#x1B[1m#x1B[31mE     #x1B[0m
#x1B[1m#x1B[31mE     - broswer#x1B[0m
#x1B[1m#x1B[31mE     + sentry.transaction#x1B[0m

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Comment on lines +158 to +159
id=group.id,
# TODO(iamrajjoshi): This should probably be the id of the latest open period
Copy link
Member

Choose a reason for hiding this comment

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

Ty for the context comments on these! It makes it much easier to review these changes.

Comment on lines -53 to -75
def send_incident_alert_notification(
action: AlertRuleTriggerAction,
incident: Incident,
metric_value: float | int | None,
new_status: IncidentStatus,
notification_uuid: str | None = None,
) -> bool:
# Make sure organization integration is still active:
result = integration_service.organization_context(
organization_id=incident.organization_id, integration_id=action.integration_id
)
integration = result.integration
org_integration = result.organization_integration
if org_integration is None or integration is None or integration.status != ObjectStatus.ACTIVE:
# Integration removed, but rule is still active.
return False

organization = incident.organization
chart_url = None
if features.has("organizations:metric-alert-chartcuterie", incident.organization):
try:
alert_rule_serialized_response: AlertRuleSerializerResponse = serialize(
incident.alert_rule, None, AlertRuleSerializer()
Copy link
Member

Choose a reason for hiding this comment

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

If possible, could you please break down refactors like these into their own isolated PRs? Having a combination of changes like this makes it harder to review thoroughly when the diffs are bulky.

@iamrajjoshi iamrajjoshi marked this pull request as draft March 19, 2025 01:57
@iamrajjoshi iamrajjoshi force-pushed the raj/refactor-slack-metric-alerts branch from 74088fe to f6a1174 Compare March 19, 2025 01:57
@iamrajjoshi iamrajjoshi marked this pull request as ready for review March 19, 2025 01:57
@iamrajjoshi iamrajjoshi requested a review from Copilot March 19, 2025 05:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the Slack metric alert notification logic by decomposing the incident notification handling into several helper functions and updating the underlying context types.

  • Extracts helper functions (_fetch_parent_notification_message_for_incident, _build_notification_payload, _send_notification, _save_notification_message) to better modularize the notification logic.
  • Updates the SlackIncidentsMessageBuilder and corresponding tests to use MetricIssueContext instead of separately passing parameters like open_period_identifier, snuba_query, new_status, and metric_value.
  • Modifies the typings in metric_detector to include additional fields (e.g., id, subscription) for better traceability.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/sentry/integrations/slack/utils/notifications.py Refactors notification logic, extracting several helper functions and updating context usage.
tests/sentry/integrations/slack/test_message_builder.py Adjusts tests to use MetricIssueContext and ensures consistency with the new builder interface.
src/sentry/incidents/typings/metric_detector.py Updates context classes by adding new fields and modifying factory methods accordingly.
tests/sentry/incidents/action_handlers/test_slack.py Updates test setup to use MetricIssueContext in place of direct parameters.
src/sentry/integrations/slack/message_builder/incidents.py Updates builder to accept MetricIssueContext and assigns values accordingly.

@iamrajjoshi iamrajjoshi merged commit 0f24ad5 into master Mar 19, 2025
48 checks passed
@iamrajjoshi iamrajjoshi deleted the raj/refactor-slack-metric-alerts branch March 19, 2025 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants