Skip to content

Commit

Permalink
feat(alerts): Support apdex alerts for anomaly detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ceorourke committed Sep 4, 2024
1 parent 9425d73 commit a1292d1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/sentry/seer/anomaly_detection/store_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from django.utils import timezone
from urllib3.exceptions import MaxRetryError, TimeoutError

from sentry import release_health
from sentry.api.endpoints.organization_on_demand_metrics_estimation_stats import get_stats_generator
from sentry.conf.server import SEER_ANOMALY_DETECTION_STORE_DATA_URL
from sentry.incidents.models.alert_rule import AlertRule, AlertRuleStatus
from sentry.models.project import Project
Expand Down Expand Up @@ -142,24 +144,38 @@ def fetch_historical_data(
dataset_label = "errors"
dataset = get_dataset(dataset_label)

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

snuba_params = SnubaParams(
organization=alert_rule.organization,
projects=[project],
start=start,
end=end,
)

if dataset == metrics_performance:
return get_crash_free_historical_data(
historical_data = get_crash_free_historical_data(
start, end, project, alert_rule.organization, granularity
)

elif "apdex" in snuba_query.aggregate:
generator = get_stats_generator(use_discover=True, remove_on_demand=False)
historical_data = generator(
query_columns=[snuba_query.aggregate],
query=snuba_query.query,
snuba_params=snuba_params,
rollup=granularity,
zerofill_results=True,
comparison_delta=alert_rule.comparison_delta,
)
else:
if not dataset:
return None

historical_data = dataset.timeseries_query(
selected_columns=[snuba_query.aggregate],
query=snuba_query.query,
snuba_params=SnubaParams(
organization=alert_rule.organization,
projects=[project],
start=start,
end=end,
),
snuba_params=snuba_params,
rollup=granularity,
referrer=Referrer.ANOMALY_DETECTION_HISTORICAL_DATA_QUERY.value,
zerofill_results=True,
Expand Down
27 changes: 27 additions & 0 deletions tests/sentry/seer/anomaly_detection/test_store_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from sentry.testutils.factories import EventType
from sentry.testutils.helpers.datetime import iso_format
from sentry.testutils.performance_issues.event_generators import get_event
from sentry.utils.samples import load_data
from sentry.utils.snuba import SnubaTSResult
from tests.sentry.incidents.endpoints.test_organization_alert_rule_index import AlertRuleBase

Expand Down Expand Up @@ -157,3 +158,29 @@ def test_anomaly_detection_fetch_historical_data_crash_rate_alert(self):
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)")

def test_anomaly_detection_fetch_historical_data_apdex_alert(self):
# TODO generate whatever apdex data is
# store a transaction event
# apply the apdex formula(?)
event_data = load_data("transaction")
event_data["timestamp"] = iso_format(self.time_1_dt)
event_data["start_timestamp"] = iso_format(self.time_1_dt)
event_data["event_id"] = "a" * 32
self.store_event(data=event_data, project_id=self.project.id)

alert_rule = self.create_alert_rule(
projects=[self.project],
dataset=Dataset.PerformanceMetrics,
name="JustAValidRule",
query="",
aggregate="apdex(300)",
time_window=1,
threshold_type=AlertRuleThresholdType.BELOW,
threshold_period=1,
)
snuba_query = SnubaQuery.objects.get(id=alert_rule.snuba_query_id)
result = fetch_historical_data(alert_rule, snuba_query, 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)} in result.data.get("data")

0 comments on commit a1292d1

Please sign in to comment.