Skip to content

Commit

Permalink
feat(alerts): Add alert type filter backend (#83784)
Browse files Browse the repository at this point in the history
Supports a `alertType` query parameter (as a list) to filter down the
alerts

Supports

- `rule` (classic issue alert)
- `alert_rule` (metric alert)
- `uptime` (uptime monitor)
- `monitor` (cron monitor)

---------

Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
  • Loading branch information
evanpurkhiser and getsantry[bot] authored Jan 22, 2025
1 parent a11c5d9 commit fcc5f0c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/sentry/incidents/endpoints/organization_alert_rule_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,20 @@ def get(self, request: Request, organization) -> Response:
),
)

intermediaries = [
CombinedQuerysetIntermediary(alert_rules, sort_key),
CombinedQuerysetIntermediary(issue_rules, rule_sort_key),
CombinedQuerysetIntermediary(uptime_rules, sort_key),
]
type_filter = request.GET.getlist("alertType", [])

def has_type(type: str) -> bool:
return not type_filter or type in type_filter

intermediaries: list[CombinedQuerysetIntermediary] = []

if features.has("organizations:insights-crons", organization):
if has_type("alert_rule"):
intermediaries.append(CombinedQuerysetIntermediary(alert_rules, sort_key))
if has_type("rule"):
intermediaries.append(CombinedQuerysetIntermediary(issue_rules, rule_sort_key))
if has_type("uptime"):
intermediaries.append(CombinedQuerysetIntermediary(uptime_rules, sort_key))
if has_type("monitor") and features.has("organizations:insights-crons", organization):
intermediaries.append(CombinedQuerysetIntermediary(crons_rules, sort_key))

response = self.paginate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1309,3 +1309,65 @@ def test_dataset_filter(self):
# without performance-view, we should only see events rules
res = self.get_success_response(self.organization.slug)
self.assert_alert_rule_serialized(events_rule, res.data[0], skip_dates=True)

def test_alert_type_filter(self):
# Setup one of each type of alert rule
metric_rule = self.create_alert_rule()
issue_rule = self.create_issue_alert_rule(
data={
"project": self.project,
"name": "Issue Rule Test",
"conditions": [],
"actions": [],
"actionMatch": "all",
"date_added": before_now(minutes=4),
}
)
uptime_rule = self.create_project_uptime_subscription(project=self.project)
cron_rule = self.create_monitor(project=self.project)

features = [
"organizations:incidents",
"organizations:performance-view",
"organizations:insights-crons",
]

# Everything comes back without the query parameter
with self.feature(features):
request_data = {"per_page": "10", "project": [self.project.id]}
response = self.client.get(
path=self.combined_rules_url,
data=request_data,
content_type="application/json",
)
assert response.status_code == 200, response.content
assert {r["id"] for r in response.data} == {
str(metric_rule.id),
str(issue_rule.id),
str(uptime_rule.id),
str(cron_rule.guid),
}

test_cases: list[tuple[list[str], set[str]]] = [
(["rule"], {str(issue_rule.id)}),
(["alert_rule"], {str(metric_rule.id)}),
(["monitor"], {str(cron_rule.guid)}),
(["uptime"], {str(uptime_rule.id)}),
(["rule", "alert_rule"], {str(issue_rule.id), str(metric_rule.id)}),
(["uptime", "monitor"], {str(uptime_rule.id), str(cron_rule.guid)}),
]

for alert_type, expected_ids in test_cases:
with self.feature(features):
request_data = {
"per_page": "10",
"project": [self.project.id],
"alertType": alert_type,
}
response = self.client.get(
path=self.combined_rules_url,
data=request_data,
content_type="application/json",
)
assert response.status_code == 200, response.content
assert {r["id"] for r in response.data} == expected_ids

0 comments on commit fcc5f0c

Please sign in to comment.