Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/sentry/incidents/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,6 @@ def create_alert_rule(
**_owner_kwargs_from_actor(owner),
)

if alert_rule.detection_type == AlertRuleDetectionType.DYNAMIC.value:
# NOTE: if adding a new metric alert type, take care to check that it's handled here
send_new_rule_data(alert_rule, projects[0], snuba_query)

if user:
create_audit_entry_from_user(
user,
Expand All @@ -664,6 +660,10 @@ def create_alert_rule(
# NOTE: This constructs the query in snuba
subscribe_projects_to_alert_rule(alert_rule, projects)

if alert_rule.detection_type == AlertRuleDetectionType.DYNAMIC.value:
# NOTE: if adding a new metric alert type, take care to check that it's handled here
send_new_rule_data(alert_rule, projects[0], snuba_query)

# Activity is an audit log of what's happened with this alert rule
AlertRuleActivity.objects.create(
alert_rule=alert_rule,
Expand Down
11 changes: 10 additions & 1 deletion src/sentry/seer/anomaly_detection/store_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sentry.seer.anomaly_detection.types import (
AlertInSeer,
AnomalyDetectionConfig,
DataSourceType,
StoreDataRequest,
StoreDataResponse,
TimeSeriesPoint,
Expand Down Expand Up @@ -191,13 +192,21 @@ def send_historical_data_to_seer(
# this won't happen because we've already gone through the serializer, but mypy insists
raise ValidationError("Missing expected configuration for a dynamic alert.")

query_subscription = snuba_query.subscriptions.first()
if query_subscription is None:
raise ValidationError("No QuerySubscription found for snuba query ID")

anomaly_detection_config = AnomalyDetectionConfig(
time_period=window_min,
sensitivity=alert_rule.sensitivity,
direction=translate_direction(alert_rule.threshold_type),
expected_seasonality=alert_rule.seasonality,
)
alert = AlertInSeer(id=alert_rule.id)
alert = AlertInSeer(
id=alert_rule.id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you no longer need to pass the rule id if passing source_id and source_type - https://github.com/getsentry/seer/blob/main/src/seer/anomaly_detection/models/external.py#L48-L62

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sending all three will automatically update the ids in seer for those alerts that are missing source_id and source_it. I would recommend keeping it until we fully move away from alert_rule.id.

source_id=query_subscription.id,
source_type=DataSourceType.SNUBA_QUERY_SUBSCRIPTION,
)
body = StoreDataRequest(
organization_id=alert_rule.organization.id,
project_id=project.id,
Expand Down
7 changes: 7 additions & 0 deletions tests/sentry/incidents/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
from sentry.testutils.helpers.features import with_feature
from sentry.testutils.silo import assume_test_silo_mode, assume_test_silo_mode_of
from sentry.types.actor import Actor
from sentry.utils import json
from sentry.workflow_engine.models.detector import Detector

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

assert mock_seer_request.call_count == 1
call_args_str = mock_seer_request.call_args_list[0].kwargs["body"].decode("utf-8")
assert json.loads(call_args_str)["alert"] == {
"id": alert_rule.id,
"source_id": alert_rule.snuba_query.subscriptions.get().id,
"source_type": 1,
}
assert alert_rule.name == self.dynamic_metric_alert_settings["name"]
assert alert_rule.user_id is None
assert alert_rule.team_id is None
Expand Down
Loading