Skip to content

Commit a6a6e77

Browse files
committed
losing my mind
1 parent 21c9902 commit a6a6e77

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/sentry/incidents/subscription_processor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None:
376376

377377
aggregation_value = self.get_aggregation_value(subscription_update, comparison_delta)
378378

379+
if aggregation_value is None:
380+
metrics.incr("incidents.alert_rules.skipping_update_invalid_aggregation_value")
381+
return
382+
379383
if aggregation_value is not None:
380384
if has_metric_alert_processing:
381385
if self.alert_rule.detection_type == AlertRuleDetectionType.DYNAMIC:
@@ -446,10 +450,6 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None:
446450
if potential_anomalies is None:
447451
return
448452

449-
if aggregation_value is None:
450-
metrics.incr("incidents.alert_rules.skipping_update_invalid_aggregation_value")
451-
return
452-
453453
fired_incident_triggers: list[IncidentTrigger] = []
454454
with transaction.atomic(router.db_for_write(AlertRule)):
455455
# Triggers is the threshold - NOT an instance of a trigger

tests/sentry/incidents/subscription_processor/test_subscription_processor.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from functools import cached_property
33
from unittest.mock import call, patch
44

5+
from django.utils import timezone
6+
57
from sentry.workflow_engine.models.data_condition import Condition, DataCondition
68
from sentry.workflow_engine.types import DetectorPriorityLevel
79
from tests.sentry.incidents.subscription_processor.test_subscription_processor_base import (
@@ -98,7 +100,9 @@ class ProcessUpdateComparisonAlertTest(ProcessUpdateBaseClass):
98100
def comparison_detector_above(self):
99101
detector = self.metric_detector
100102
detector.config.update({"comparison_delta": 60 * 60})
103+
detector.save()
101104
self.update_threshold(detector, DetectorPriorityLevel.HIGH, 150)
105+
self.update_threshold(detector, DetectorPriorityLevel.OK, 150)
102106
snuba_query = self.get_snuba_query(detector)
103107
snuba_query.update(time_window=60 * 60)
104108
return detector
@@ -107,6 +111,7 @@ def comparison_detector_above(self):
107111
def comparison_detector_below(self):
108112
detector = self.metric_detector
109113
detector.config.update({"comparison_delta": 60 * 60})
114+
detector.save()
110115
DataCondition.objects.filter(condition_group=detector.workflow_condition_group).delete()
111116
self.set_up_data_conditions(detector, Condition.LESS, 50, None, 50)
112117
snuba_query = self.get_snuba_query(detector)
@@ -116,8 +121,8 @@ def comparison_detector_below(self):
116121
@patch("sentry.incidents.utils.process_update_helpers.metrics")
117122
def test_comparison_alert_above(self, helper_metrics):
118123
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)
124+
comparison_delta = timedelta(seconds=detector.config["comparison_delta"])
125+
self.send_update(self.critical_threshold + 1, timedelta(minutes=-10))
121126

122127
# Shouldn't trigger, since there should be no data in the comparison period
123128
assert self.get_detector_state(detector) == DetectorPriorityLevel.OK
@@ -131,3 +136,31 @@ def test_comparison_alert_above(self, helper_metrics):
131136
call("incidents.alert_rules.skipping_update_invalid_aggregation_value"),
132137
]
133138
)
139+
comparison_date = timezone.now() - comparison_delta
140+
141+
for i in range(4):
142+
self.store_event(
143+
data={"timestamp": (comparison_date - timedelta(minutes=30 + i)).isoformat()},
144+
project_id=self.project.id,
145+
)
146+
147+
self.metrics.incr.reset_mock()
148+
self.send_update(2, timedelta(minutes=-9))
149+
# Shouldn't trigger, since there are 4 events in the comparison period, and 2/4 == 50%
150+
assert self.get_detector_state(detector) == DetectorPriorityLevel.OK
151+
152+
self.send_update(4, timedelta(minutes=-8))
153+
# Shouldn't trigger, since there are 4 events in the comparison period, and 4/4 == 100%
154+
assert self.get_detector_state(detector) == DetectorPriorityLevel.OK
155+
156+
self.send_update(6, timedelta(minutes=-7))
157+
# Shouldn't trigger: 6/4 == 150%, but we want > 150%
158+
assert self.get_detector_state(detector) == DetectorPriorityLevel.OK
159+
160+
self.send_update(7, timedelta(minutes=-6))
161+
# Should trigger: 7/4 == 175% > 150%
162+
assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH
163+
164+
# Check that we successfully resolve
165+
self.send_update(6, timedelta(minutes=-5))
166+
assert self.get_detector_state(detector) == DetectorPriorityLevel.OK

0 commit comments

Comments
 (0)