|
| 1 | +import copy |
1 | 2 | from datetime import timedelta
|
2 | 3 | from functools import cached_property
|
3 | 4 | from unittest.mock import call, patch
|
4 | 5 |
|
5 | 6 | from django.utils import timezone
|
6 | 7 |
|
| 8 | +from sentry.testutils.factories import DEFAULT_EVENT_DATA |
7 | 9 | from sentry.workflow_engine.models.data_condition import Condition, DataCondition
|
8 | 10 | from sentry.workflow_engine.types import DetectorPriorityLevel
|
9 | 11 | from tests.sentry.incidents.subscription_processor.test_subscription_processor_base import (
|
@@ -164,3 +166,164 @@ def test_comparison_alert_above(self, helper_metrics):
|
164 | 166 | # Check that we successfully resolve
|
165 | 167 | self.send_update(6, timedelta(minutes=-5))
|
166 | 168 | assert self.get_detector_state(detector) == DetectorPriorityLevel.OK
|
| 169 | + |
| 170 | + @patch("sentry.incidents.utils.process_update_helpers.metrics") |
| 171 | + def test_comparison_alert_below(self, helper_metrics): |
| 172 | + detector = self.comparison_detector_below |
| 173 | + comparison_delta = timedelta(seconds=detector.config["comparison_delta"]) |
| 174 | + self.send_update(self.critical_threshold - 1, timedelta(minutes=-10)) |
| 175 | + |
| 176 | + # Shouldn't trigger, since there should be no data in the comparison period |
| 177 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 178 | + helper_metrics.incr.assert_has_calls( |
| 179 | + [ |
| 180 | + call("incidents.alert_rules.skipping_update_comparison_value_invalid"), |
| 181 | + ] |
| 182 | + ) |
| 183 | + self.metrics.incr.assert_has_calls( |
| 184 | + [ |
| 185 | + call("incidents.alert_rules.skipping_update_invalid_aggregation_value"), |
| 186 | + ] |
| 187 | + ) |
| 188 | + comparison_date = timezone.now() - comparison_delta |
| 189 | + |
| 190 | + for i in range(4): |
| 191 | + self.store_event( |
| 192 | + data={"timestamp": (comparison_date - timedelta(minutes=30 + i)).isoformat()}, |
| 193 | + project_id=self.project.id, |
| 194 | + ) |
| 195 | + |
| 196 | + self.metrics.incr.reset_mock() |
| 197 | + self.send_update(6, timedelta(minutes=-9)) |
| 198 | + # Shouldn't trigger, since there are 4 events in the comparison period, and 6/4 == 150% |
| 199 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 200 | + |
| 201 | + self.send_update(4, timedelta(minutes=-8)) |
| 202 | + # Shouldn't trigger, since there are 4 events in the comparison period, and 4/4 == 100% |
| 203 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 204 | + |
| 205 | + self.send_update(2, timedelta(minutes=-7)) |
| 206 | + # Shouldn't trigger: 2/4 == 50%, but we want < 50% |
| 207 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 208 | + |
| 209 | + self.send_update(1, timedelta(minutes=-6)) |
| 210 | + # Should trigger: 1/4 == 25% < 50% |
| 211 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH |
| 212 | + |
| 213 | + # Check that we successfully resolve |
| 214 | + self.send_update(2, timedelta(minutes=-5)) |
| 215 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 216 | + |
| 217 | + @patch("sentry.incidents.utils.process_update_helpers.metrics") |
| 218 | + def test_is_unresolved_comparison_query(self, helper_metrics): |
| 219 | + """ |
| 220 | + Test that uses the ErrorsQueryBuilder (because of the specific query) |
| 221 | + """ |
| 222 | + detector = self.comparison_detector_above |
| 223 | + comparison_delta = timedelta(seconds=detector.config["comparison_delta"]) |
| 224 | + snuba_query = self.get_snuba_query(detector) |
| 225 | + snuba_query.update(query="(event.type:error) AND (is:unresolved)") |
| 226 | + |
| 227 | + self.send_update(self.critical_threshold + 1, timedelta(minutes=-10), subscription=self.sub) |
| 228 | + helper_metrics.incr.assert_has_calls( |
| 229 | + [ |
| 230 | + call("incidents.alert_rules.skipping_update_comparison_value_invalid"), |
| 231 | + ] |
| 232 | + ) |
| 233 | + self.metrics.incr.assert_has_calls( |
| 234 | + [ |
| 235 | + call("incidents.alert_rules.skipping_update_invalid_aggregation_value"), |
| 236 | + ] |
| 237 | + ) |
| 238 | + comparison_date = timezone.now() - comparison_delta |
| 239 | + |
| 240 | + for i in range(4): |
| 241 | + data = { |
| 242 | + "timestamp": (comparison_date - timedelta(minutes=30 + i)).isoformat(), |
| 243 | + "stacktrace": copy.deepcopy(DEFAULT_EVENT_DATA["stacktrace"]), |
| 244 | + "fingerprint": ["group2"], |
| 245 | + "level": "error", |
| 246 | + "exception": { |
| 247 | + "values": [ |
| 248 | + { |
| 249 | + "type": "IntegrationError", |
| 250 | + "value": "Identity not found.", |
| 251 | + } |
| 252 | + ] |
| 253 | + }, |
| 254 | + } |
| 255 | + self.store_event( |
| 256 | + data=data, |
| 257 | + project_id=self.project.id, |
| 258 | + ) |
| 259 | + |
| 260 | + self.metrics.incr.reset_mock() |
| 261 | + self.send_update(2, timedelta(minutes=-9)) |
| 262 | + # Shouldn't trigger, since there are 4 events in the comparison period, and 2/4 == 50% |
| 263 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 264 | + |
| 265 | + self.send_update(4, timedelta(minutes=-8)) |
| 266 | + # Shouldn't trigger, since there are 4 events in the comparison period, and 4/4 == 100% |
| 267 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 268 | + |
| 269 | + self.send_update(6, timedelta(minutes=-7)) |
| 270 | + # Shouldn't trigger: 6/4 == 150%, but we want > 150% |
| 271 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 272 | + |
| 273 | + self.send_update(7, timedelta(minutes=-6)) |
| 274 | + # Should trigger: 7/4 == 175% > 150% |
| 275 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH |
| 276 | + |
| 277 | + # Check that we successfully resolve |
| 278 | + self.send_update(6, timedelta(minutes=-5)) |
| 279 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 280 | + |
| 281 | + @patch("sentry.incidents.utils.process_update_helpers.metrics") |
| 282 | + def test_is_unresolved_different_aggregate(self, helper_metrics): |
| 283 | + detector = self.comparison_detector_above |
| 284 | + comparison_delta = timedelta(seconds=detector.config["comparison_delta"]) |
| 285 | + snuba_query = self.get_snuba_query(detector) |
| 286 | + snuba_query.update(aggregate="count_unique(tags[sentry:user])") |
| 287 | + |
| 288 | + self.send_update(self.critical_threshold + 1, timedelta(minutes=-10), subscription=self.sub) |
| 289 | + helper_metrics.incr.assert_has_calls( |
| 290 | + [ |
| 291 | + call("incidents.alert_rules.skipping_update_comparison_value_invalid"), |
| 292 | + ] |
| 293 | + ) |
| 294 | + self.metrics.incr.assert_has_calls( |
| 295 | + [ |
| 296 | + call("incidents.alert_rules.skipping_update_invalid_aggregation_value"), |
| 297 | + ] |
| 298 | + ) |
| 299 | + comparison_date = timezone.now() - comparison_delta |
| 300 | + |
| 301 | + for i in range(4): |
| 302 | + self.store_event( |
| 303 | + data={ |
| 304 | + "timestamp": (comparison_date - timedelta(minutes=30 + i)).isoformat(), |
| 305 | + "tags": {"sentry:user": i}, |
| 306 | + }, |
| 307 | + project_id=self.project.id, |
| 308 | + ) |
| 309 | + |
| 310 | + self.metrics.incr.reset_mock() |
| 311 | + self.send_update(2, timedelta(minutes=-9)) |
| 312 | + # Shouldn't trigger, since there are 4 events in the comparison period, and 2/4 == 50% |
| 313 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 314 | + |
| 315 | + self.send_update(4, timedelta(minutes=-8)) |
| 316 | + # Shouldn't trigger, since there are 4 events in the comparison period, and 4/4 == 100% |
| 317 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 318 | + |
| 319 | + self.send_update(6, timedelta(minutes=-7)) |
| 320 | + # Shouldn't trigger: 6/4 == 150%, but we want > 150% |
| 321 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
| 322 | + |
| 323 | + self.send_update(7, timedelta(minutes=-6)) |
| 324 | + # Should trigger: 7/4 == 175% > 150% |
| 325 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.HIGH |
| 326 | + |
| 327 | + # Check that we successfully resolve |
| 328 | + self.send_update(6, timedelta(minutes=-5)) |
| 329 | + assert self.get_detector_state(detector) == DetectorPriorityLevel.OK |
0 commit comments