Skip to content

Commit 9babdbd

Browse files
authored
fix(aci/anomaly detection): pass source ID and source type in store data request (#101045)
The problem: alerts created since July will not fire because their stored data is keyed using alert rule ID, which is not sent anywhere in the processing request. Alerts updated since then will have their source ID overwritten, so the same problem applies. The solution: pass source ID in the store data request.
1 parent cf503b7 commit 9babdbd

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/sentry/incidents/logic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,6 @@ def create_alert_rule(
643643
**_owner_kwargs_from_actor(owner),
644644
)
645645

646-
if alert_rule.detection_type == AlertRuleDetectionType.DYNAMIC.value:
647-
# NOTE: if adding a new metric alert type, take care to check that it's handled here
648-
send_new_rule_data(alert_rule, projects[0], snuba_query)
649-
650646
if user:
651647
create_audit_entry_from_user(
652648
user,
@@ -664,6 +660,10 @@ def create_alert_rule(
664660
# NOTE: This constructs the query in snuba
665661
subscribe_projects_to_alert_rule(alert_rule, projects)
666662

663+
if alert_rule.detection_type == AlertRuleDetectionType.DYNAMIC.value:
664+
# NOTE: if adding a new metric alert type, take care to check that it's handled here
665+
send_new_rule_data(alert_rule, projects[0], snuba_query)
666+
667667
# Activity is an audit log of what's happened with this alert rule
668668
AlertRuleActivity.objects.create(
669669
alert_rule=alert_rule,

src/sentry/seer/anomaly_detection/store_data.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from sentry.seer.anomaly_detection.types import (
1818
AlertInSeer,
1919
AnomalyDetectionConfig,
20+
DataSourceType,
2021
StoreDataRequest,
2122
StoreDataResponse,
2223
TimeSeriesPoint,
@@ -191,13 +192,21 @@ def send_historical_data_to_seer(
191192
# this won't happen because we've already gone through the serializer, but mypy insists
192193
raise ValidationError("Missing expected configuration for a dynamic alert.")
193194

195+
query_subscription = snuba_query.subscriptions.first()
196+
if query_subscription is None:
197+
raise ValidationError("No QuerySubscription found for snuba query ID")
198+
194199
anomaly_detection_config = AnomalyDetectionConfig(
195200
time_period=window_min,
196201
sensitivity=alert_rule.sensitivity,
197202
direction=translate_direction(alert_rule.threshold_type),
198203
expected_seasonality=alert_rule.seasonality,
199204
)
200-
alert = AlertInSeer(id=alert_rule.id)
205+
alert = AlertInSeer(
206+
id=alert_rule.id,
207+
source_id=query_subscription.id,
208+
source_type=DataSourceType.SNUBA_QUERY_SUBSCRIPTION,
209+
)
201210
body = StoreDataRequest(
202211
organization_id=alert_rule.organization.id,
203212
project_id=project.id,

tests/sentry/incidents/test_logic.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
from sentry.testutils.helpers.features import with_feature
9898
from sentry.testutils.silo import assume_test_silo_mode, assume_test_silo_mode_of
9999
from sentry.types.actor import Actor
100+
from sentry.utils import json
100101
from sentry.workflow_engine.models.detector import Detector
101102

102103
pytestmark = [pytest.mark.sentry_metrics]
@@ -663,6 +664,12 @@ def test_create_alert_rule_anomaly_detection(self, mock_seer_request: MagicMock)
663664
)
664665

665666
assert mock_seer_request.call_count == 1
667+
call_args_str = mock_seer_request.call_args_list[0].kwargs["body"].decode("utf-8")
668+
assert json.loads(call_args_str)["alert"] == {
669+
"id": alert_rule.id,
670+
"source_id": alert_rule.snuba_query.subscriptions.get().id,
671+
"source_type": 1,
672+
}
666673
assert alert_rule.name == self.dynamic_metric_alert_settings["name"]
667674
assert alert_rule.user_id is None
668675
assert alert_rule.team_id is None

0 commit comments

Comments
 (0)