|
| 1 | +from datetime import timedelta |
| 2 | +from functools import cached_property |
| 3 | +from unittest.mock import call, patch |
| 4 | + |
| 5 | +from sentry.workflow_engine.models.data_condition import Condition, DataCondition |
| 6 | +from sentry.workflow_engine.types import DetectorPriorityLevel |
| 7 | +from tests.sentry.incidents.subscription_processor.test_subscription_processor_base import ( |
| 8 | + ProcessUpdateBaseClass, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class ProcessUpdateTest(ProcessUpdateBaseClass): |
| 13 | + """ |
| 14 | + Test early return scenarios + simple cases. |
| 15 | + """ |
| 16 | + |
| 17 | + # TODO: tests for early return scenarios. These will need to be added once |
| 18 | + # we've decoupled the subscription processor from the alert rule model. |
| 19 | + |
| 20 | + def test_simple(self) -> None: |
| 21 | + """ |
| 22 | + Verify that an alert can trigger. |
| 23 | + """ |
| 24 | + self.send_update(self.critical_threshold + 1) |
| 25 | + assert self.get_detector_state(self.metric_detector) == DetectorPriorityLevel.HIGH |
| 26 | + |
| 27 | + def test_resolve(self) -> None: |
| 28 | + detector = self.metric_detector |
| 29 | + self.send_update(self.critical_threshold + 1, timedelta(minutes=-2)) |
| 30 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH |
| 31 | + |
| 32 | + self.send_update(self.resolve_threshold - 1, timedelta(minutes=-1)) |
| 33 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 34 | + |
| 35 | + def test_resolve_percent_boundary(self) -> None: |
| 36 | + detector = self.metric_detector |
| 37 | + self.update_threshold(detector, DetectorPriorityLevel.HIGH, 0.5) |
| 38 | + self.update_threshold(detector, DetectorPriorityLevel.OK, 0.5) |
| 39 | + self.send_update(self.critical_threshold + 0.1, timedelta(minutes=-2)) |
| 40 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH |
| 41 | + |
| 42 | + self.send_update(self.resolve_threshold, timedelta(minutes=-1)) |
| 43 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 44 | + |
| 45 | + def test_reversed(self) -> None: |
| 46 | + """ |
| 47 | + Test that resolutions work when the threshold is reversed. |
| 48 | + """ |
| 49 | + detector = self.metric_detector |
| 50 | + DataCondition.objects.filter(condition_group=detector.workflow_condition_group).delete() |
| 51 | + self.set_up_data_conditions(detector, Condition.LESS, 100, None, 100) |
| 52 | + self.send_update(self.critical_threshold - 1, timedelta(minutes=-2)) |
| 53 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH |
| 54 | + |
| 55 | + self.send_update(self.resolve_threshold, timedelta(minutes=-1)) |
| 56 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 57 | + |
| 58 | + def test_multiple_triggers(self) -> None: |
| 59 | + detector = self.metric_detector |
| 60 | + DataCondition.objects.filter(condition_group=detector.workflow_condition_group).delete() |
| 61 | + self.set_up_data_conditions(detector, Condition.GREATER, 100, 50, 50) |
| 62 | + |
| 63 | + self.send_update(self.warning_threshold + 1, timedelta(minutes=-5)) |
| 64 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.MEDIUM |
| 65 | + |
| 66 | + self.send_update(self.critical_threshold + 1, timedelta(minutes=-4)) |
| 67 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH |
| 68 | + |
| 69 | + self.send_update(self.critical_threshold - 1, timedelta(minutes=-3)) |
| 70 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.MEDIUM |
| 71 | + |
| 72 | + self.send_update(self.warning_threshold - 1, timedelta(minutes=-2)) |
| 73 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 74 | + |
| 75 | + def test_multiple_triggers_reversed(self) -> None: |
| 76 | + detector = self.metric_detector |
| 77 | + DataCondition.objects.filter(condition_group=detector.workflow_condition_group).delete() |
| 78 | + self.set_up_data_conditions(detector, Condition.LESS, 50, 100, 100) |
| 79 | + |
| 80 | + self.send_update(self.warning_threshold - 1, timedelta(minutes=-5)) |
| 81 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.MEDIUM |
| 82 | + |
| 83 | + self.send_update(self.critical_threshold - 1, timedelta(minutes=-4)) |
| 84 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH |
| 85 | + |
| 86 | + self.send_update(self.critical_threshold + 1, timedelta(minutes=-3)) |
| 87 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.MEDIUM |
| 88 | + |
| 89 | + self.send_update(self.warning_threshold + 1, timedelta(minutes=-2)) |
| 90 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 91 | + |
| 92 | + # TODO: the subscription processor has a 10 minute cooldown period for creating new incidents |
| 93 | + # We probably need similar logic within workflow engine. |
| 94 | + |
| 95 | + |
| 96 | +class ProcessUpdateComparisonAlertTest(ProcessUpdateBaseClass): |
| 97 | + @cached_property |
| 98 | + def comparison_detector_above(self): |
| 99 | + detector = self.metric_detector |
| 100 | + detector.config.update({"comparison_delta": 60 * 60}) |
| 101 | + self.update_threshold(detector, DetectorPriorityLevel.HIGH, 150) |
| 102 | + snuba_query = self.get_snuba_query(detector) |
| 103 | + snuba_query.update(time_window=60 * 60) |
| 104 | + return detector |
| 105 | + |
| 106 | + @cached_property |
| 107 | + def comparison_detector_below(self): |
| 108 | + detector = self.metric_detector |
| 109 | + detector.config.update({"comparison_delta": 60 * 60}) |
| 110 | + DataCondition.objects.filter(condition_group=detector.workflow_condition_group).delete() |
| 111 | + self.set_up_data_conditions(detector, Condition.LESS, 50, None, 50) |
| 112 | + snuba_query = self.get_snuba_query(detector) |
| 113 | + snuba_query.update(time_window=60 * 60) |
| 114 | + return detector |
| 115 | + |
| 116 | + @patch("sentry.incidents.utils.process_update_helpers.metrics") |
| 117 | + def test_comparison_alert_above(self, helper_metrics): |
| 118 | + detector = self.comparison_detector_above |
| 119 | + # comparison_delta = timedelta(seconds=detector.config["comparison_delta"]) |
| 120 | + self.send_update(self.critical_threshold + 1, timedelta(minutes=-10), subscription=self.sub) |
| 121 | + |
| 122 | + # Shouldn't trigger, since there should be no data in the comparison period |
| 123 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 124 | + helper_metrics.incr.assert_has_calls( |
| 125 | + [ |
| 126 | + call("incidents.alert_rules.skipping_update_comparison_value_invalid"), |
| 127 | + ] |
| 128 | + ) |
| 129 | + self.metrics.incr.assert_has_calls( |
| 130 | + [ |
| 131 | + call("incidents.alert_rules.skipping_update_invalid_aggregation_value"), |
| 132 | + ] |
| 133 | + ) |
0 commit comments