Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(alerts): Pass org instead of rule #79236

Merged
merged 2 commits into from
Oct 17, 2024
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
12 changes: 10 additions & 2 deletions src/sentry/seer/anomaly_detection/get_historical_anomalies.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def get_historical_anomaly_data_from_seer(
"""
if alert_rule.status == AlertRuleStatus.NOT_ENOUGH_DATA.value:
return []
# don't think this can happen but mypy is yelling
# don't think these can happen but mypy is yelling
if not alert_rule.snuba_query:
logger.error(
"No snuba query associated with alert rule",
Expand All @@ -160,6 +160,14 @@ def get_historical_anomaly_data_from_seer(
},
)
return None
if not alert_rule.organization:
logger.error(
"No organization associated with alert rule",
extra={
"alert_rule_id": alert_rule.id,
},
)
return None
subscription = alert_rule.snuba_query.subscriptions.first()
# same deal as above
if not subscription:
Expand All @@ -175,7 +183,7 @@ def get_historical_anomaly_data_from_seer(
end = datetime.fromisoformat(end_string)
query_columns = get_query_columns([snuba_query.aggregate], snuba_query.time_window)
historical_data = fetch_historical_data(
alert_rule=alert_rule,
organization=alert_rule.organization,
snuba_query=snuba_query,
query_columns=query_columns,
project=project,
Expand Down
5 changes: 4 additions & 1 deletion src/sentry/seer/anomaly_detection/store_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ def send_historical_data_to_seer(
dataset = get_dataset_from_label(snuba_query.dataset)
query_columns = get_query_columns([snuba_query.aggregate], window_min)
event_types = get_event_types(snuba_query, event_types)
if not alert_rule.organization:
raise ValidationError("Alert rule doesn't belong to an organization")

historical_data = fetch_historical_data(
alert_rule=alert_rule,
organization=alert_rule.organization,
snuba_query=snuba_query,
query_columns=query_columns,
project=project,
Expand Down
12 changes: 5 additions & 7 deletions src/sentry/seer/anomaly_detection/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sentry import release_health
from sentry.api.bases.organization_events import resolve_axis_column
from sentry.api.serializers.snuba import SnubaTSResultSerializer
from sentry.incidents.models.alert_rule import AlertRule, AlertRuleThresholdType
from sentry.incidents.models.alert_rule import AlertRuleThresholdType
from sentry.models.organization import Organization
from sentry.models.project import Project
from sentry.search.events.types import SnubaParams
Expand Down Expand Up @@ -213,7 +213,7 @@ def get_dataset_from_label(dataset_label: str):


def fetch_historical_data(
alert_rule: AlertRule,
organization: Organization,
snuba_query: SnubaQuery,
query_columns: list[str],
project: Project,
Expand Down Expand Up @@ -245,15 +245,15 @@ def fetch_historical_data(
dataset_label = "metricsEnhanced"
dataset = get_dataset_from_label(dataset_label)

if not project or not dataset or not alert_rule.organization:
if not project or not dataset or not organization:
return None

environments = []
if snuba_query.environment:
environments = [snuba_query.environment]

snuba_params = SnubaParams(
organization=alert_rule.organization,
organization=organization,
projects=[project],
start=start,
end=end,
Expand All @@ -262,9 +262,7 @@ def fetch_historical_data(
)

if dataset == metrics_performance:
return get_crash_free_historical_data(
start, end, project, alert_rule.organization, granularity
)
return get_crash_free_historical_data(start, end, project, organization, granularity)
else:
event_types = get_event_types(snuba_query, event_types)
snuba_query_string = get_snuba_query_string(snuba_query, event_types)
Expand Down
8 changes: 4 additions & 4 deletions tests/sentry/seer/anomaly_detection/test_store_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_anomaly_detection_fetch_historical_data(self):
},
project_id=self.project.id,
)
result = fetch_historical_data(alert_rule, snuba_query, ["count()"], self.project)
result = fetch_historical_data(self.organization, snuba_query, ["count()"], self.project)
assert result
assert {"time": int(self.time_1_ts), "count": 1} in result.data.get("data")
assert {"time": int(self.time_2_ts), "count": 1} in result.data.get("data")
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_anomaly_detection_fetch_historical_data_is_unresolved_query(self):
},
project_id=self.project.id,
)
result = fetch_historical_data(alert_rule, snuba_query, ["count()"], self.project)
result = fetch_historical_data(self.organization, snuba_query, ["count()"], self.project)
assert result
assert {"time": int(self.time_1_ts), "count": 1} in result.data.get("data")
assert {"time": int(self.time_2_ts), "count": 1} in result.data.get("data")
Expand All @@ -188,7 +188,7 @@ def test_anomaly_detection_fetch_historical_data_performance_alert(self):

event2 = self.create_performance_issue(event_data=make_event(**event_data))

result = fetch_historical_data(alert_rule, snuba_query, ["count()"], self.project)
result = fetch_historical_data(self.organization, snuba_query, ["count()"], self.project)
assert result
assert {"time": int(event1.datetime.timestamp()), "count": 1} in result.data.get("data")
assert {"time": int(event2.datetime.timestamp()), "count": 1} in result.data.get("data")
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_anomaly_detection_fetch_historical_data_crash_rate_alert(self):
threshold_period=1,
)
snuba_query = SnubaQuery.objects.get(id=alert_rule.snuba_query_id)
result = fetch_historical_data(alert_rule, snuba_query, ["count()"], self.project)
result = fetch_historical_data(self.organization, snuba_query, ["count()"], self.project)
assert result
assert self.time_1 in result.data.get("data").get("intervals")
assert 1 in result.data.get("data").get("groups")[0].get("series").get("sum(session)")
Loading