-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(aci milestone 3): anomaly detection dual write helpers #92693
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
+287
−25
Merged
Changes from all commits
Commits
Show all changes
3 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
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |||||||
| from sentry.incidents.grouptype import MetricIssue | ||||||||
| from sentry.incidents.models.alert_rule import ( | ||||||||
| AlertRule, | ||||||||
| AlertRuleDetectionType, | ||||||||
| AlertRuleThresholdType, | ||||||||
| AlertRuleTrigger, | ||||||||
| AlertRuleTriggerAction, | ||||||||
|
|
@@ -285,12 +286,24 @@ def migrate_metric_data_conditions( | |||||||
| ) | ||||||||
| condition_result = PRIORITY_MAP.get(alert_rule_trigger.label, DetectorPriorityLevel.HIGH) | ||||||||
|
|
||||||||
| detector_trigger = DataCondition.objects.create( | ||||||||
| comparison=alert_rule_trigger.alert_threshold, | ||||||||
| condition_result=condition_result, | ||||||||
| type=threshold_type, | ||||||||
| condition_group=detector_data_condition_group, | ||||||||
| ) | ||||||||
| if alert_rule.detection_type == AlertRuleDetectionType.DYNAMIC: | ||||||||
| detector_trigger = DataCondition.objects.create( | ||||||||
| type=Condition.ANOMALY_DETECTION, | ||||||||
| comparison={ | ||||||||
| "sensitivity": alert_rule.sensitivity, | ||||||||
| "seasonality": alert_rule.seasonality, | ||||||||
| "threshold_type": alert_rule.threshold_type, | ||||||||
| }, | ||||||||
| condition_result=condition_result, | ||||||||
| condition_group=detector_data_condition_group, | ||||||||
| ) | ||||||||
| else: | ||||||||
| detector_trigger = DataCondition.objects.create( | ||||||||
| comparison=alert_rule_trigger.alert_threshold, | ||||||||
| condition_result=condition_result, | ||||||||
| type=threshold_type, | ||||||||
| condition_group=detector_data_condition_group, | ||||||||
| ) | ||||||||
| DataConditionAlertRuleTrigger.objects.create( | ||||||||
| data_condition=detector_trigger, | ||||||||
| alert_rule_trigger_id=alert_rule_trigger.id, | ||||||||
|
|
@@ -584,7 +597,9 @@ def dual_write_alert_rule(alert_rule: AlertRule, user: RpcUser | None = None) -> | |||||||
| for trigger_action in trigger_actions: | ||||||||
| migrate_metric_action(trigger_action) | ||||||||
| # step 4: migrate alert rule resolution | ||||||||
| migrate_resolve_threshold_data_condition(alert_rule) | ||||||||
| # if the alert rule is an anomaly detection alert, then this is handled by the anomaly detection data condition | ||||||||
| if alert_rule.detection_type != AlertRuleDetectionType.DYNAMIC: | ||||||||
| migrate_resolve_threshold_data_condition(alert_rule) | ||||||||
|
|
||||||||
|
|
||||||||
| def dual_update_alert_rule(alert_rule: AlertRule) -> None: | ||||||||
|
|
@@ -699,20 +714,28 @@ def dual_update_resolve_condition(alert_rule: AlertRule) -> DataCondition | None | |||||||
| ) | ||||||||
| raise MissingDataConditionGroup | ||||||||
|
|
||||||||
| data_conditions = DataCondition.objects.filter(condition_group=detector_data_condition_group) | ||||||||
| resolve_condition = data_conditions.filter(condition_result=DetectorPriorityLevel.OK).first() | ||||||||
|
|
||||||||
| # changing detector priority level for anomaly detection alerts is handled by the data condition | ||||||||
| # so we should delete the explicit resolution condition if it exists | ||||||||
| if alert_rule.detection_type == AlertRuleDetectionType.DYNAMIC: | ||||||||
| if resolve_condition is not None: | ||||||||
|
Comment on lines
+722
to
+723
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote it this way because we should always return before the resolve detector trigger logic for anomaly detection alerts! |
||||||||
| resolve_condition.delete() | ||||||||
| return None | ||||||||
|
|
||||||||
| # if we're changing from anomaly detection alert to regular metric alert, we need to recreate the resolve condition | ||||||||
| if resolve_condition is None: | ||||||||
| resolve_condition = migrate_resolve_threshold_data_condition(alert_rule) | ||||||||
| return resolve_condition | ||||||||
|
|
||||||||
| if alert_rule.resolve_threshold is not None: | ||||||||
| resolve_threshold = alert_rule.resolve_threshold | ||||||||
| else: | ||||||||
| resolve_threshold = get_resolve_threshold(detector_data_condition_group) | ||||||||
| if resolve_threshold == -1: | ||||||||
| raise UnresolvableResolveThreshold | ||||||||
|
|
||||||||
| data_conditions = DataCondition.objects.filter(condition_group=detector_data_condition_group) | ||||||||
| try: | ||||||||
| resolve_condition = data_conditions.get(condition_result=DetectorPriorityLevel.OK) | ||||||||
| except DataCondition.DoesNotExist: | ||||||||
| # In the serializer, we call handle triggers before migrating the resolve data condition, | ||||||||
| # so the resolve condition may not exist yet. Return early. | ||||||||
| return None | ||||||||
| resolve_condition.update(comparison=resolve_threshold) | ||||||||
|
|
||||||||
| return resolve_condition | ||||||||
|
|
@@ -732,14 +755,26 @@ def dual_update_migrated_alert_rule_trigger( | |||||||
| condition_group=action_filter.condition_group, | ||||||||
| type=Condition.ISSUE_PRIORITY_DEESCALATING, | ||||||||
| ).first() | ||||||||
|
|
||||||||
| updated_detector_trigger_fields: dict[str, Any] = {} | ||||||||
| updated_action_filter_fields: dict[str, Any] = {} | ||||||||
| label = alert_rule_trigger.label | ||||||||
| updated_detector_trigger_fields["condition_result"] = PRIORITY_MAP.get( | ||||||||
| label, DetectorPriorityLevel.HIGH | ||||||||
| ) | ||||||||
| updated_action_filter_fields["comparison"] = PRIORITY_MAP.get(label, DetectorPriorityLevel.HIGH) | ||||||||
| updated_detector_trigger_fields["comparison"] = alert_rule_trigger.alert_threshold | ||||||||
| alert_rule = alert_rule_trigger.alert_rule | ||||||||
|
|
||||||||
| # if we're changing to anomaly detection, we need to set the comparison JSON | ||||||||
| if alert_rule.detection_type == AlertRuleDetectionType.DYNAMIC: | ||||||||
| updated_detector_trigger_fields["type"] = Condition.ANOMALY_DETECTION | ||||||||
| updated_detector_trigger_fields["comparison"] = { | ||||||||
| "sensitivity": alert_rule.sensitivity, | ||||||||
| "seasonality": alert_rule.seasonality, | ||||||||
| "threshold_type": alert_rule.threshold_type, | ||||||||
| } | ||||||||
| else: | ||||||||
| updated_detector_trigger_fields["comparison"] = alert_rule_trigger.alert_threshold | ||||||||
|
|
||||||||
| detector_trigger.update(**updated_detector_trigger_fields) | ||||||||
| if updated_action_filter_fields: | ||||||||
|
|
||||||||
Oops, something went wrong.
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.
does this mean this was a regular alert that was changed to become an anomaly detection alert?
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.
Yes, that's what is happening here