Skip to content

Commit 0df8a3d

Browse files
chore(alerts): Clean up rate limiting flag references (#60483)
1 parent 21bafbe commit 0df8a3d

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

src/sentry/incidents/subscription_processor.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -552,38 +552,35 @@ def trigger_alert_threshold(
552552
"""
553553
self.trigger_alert_counts[trigger.id] += 1
554554

555-
if features.has(
556-
"organizations:metric-alert-rate-limiting", self.subscription.project.organization
555+
# If an incident was created for this rule, trigger type, and subscription
556+
# within the last 10 minutes, don't make another one
557+
last_it = (
558+
IncidentTrigger.objects.filter(alert_rule_trigger=trigger)
559+
.order_by("-incident_id")
560+
.select_related("incident")
561+
.first()
562+
)
563+
last_incident: Incident | None = last_it.incident if last_it else None
564+
last_incident_projects = (
565+
[project.id for project in last_incident.projects.all()] if last_incident else []
566+
)
567+
minutes_since_last_incident = (
568+
(timezone.now() - last_incident.date_added).seconds / 60 if last_incident else None
569+
)
570+
if (
571+
last_incident
572+
and self.subscription.project.id in last_incident_projects
573+
and minutes_since_last_incident <= 10
557574
):
558-
# If an incident was created for this rule, trigger type, and subscription
559-
# within the last 10 minutes, don't make another one
560-
last_it = (
561-
IncidentTrigger.objects.filter(alert_rule_trigger=trigger)
562-
.order_by("-incident_id")
563-
.select_related("incident")
564-
.first()
565-
)
566-
last_incident: Incident | None = last_it.incident if last_it else None
567-
last_incident_projects = (
568-
[project.id for project in last_incident.projects.all()] if last_incident else []
569-
)
570-
minutes_since_last_incident = (
571-
(timezone.now() - last_incident.date_added).seconds / 60 if last_incident else None
575+
metrics.incr(
576+
"incidents.alert_rules.hit_rate_limit",
577+
tags={
578+
"last_incident_id": last_incident.id,
579+
"project_id": self.subscription.project.id,
580+
"trigger_id": trigger.id,
581+
},
572582
)
573-
if (
574-
last_incident
575-
and self.subscription.project.id in last_incident_projects
576-
and minutes_since_last_incident <= 10
577-
):
578-
metrics.incr(
579-
"incidents.alert_rules.hit_rate_limit",
580-
tags={
581-
"last_incident_id": last_incident.id,
582-
"project_id": self.subscription.project.id,
583-
"trigger_id": trigger.id,
584-
},
585-
)
586-
return None
583+
return None
587584
if self.trigger_alert_counts[trigger.id] >= self.alert_rule.threshold_period:
588585
metrics.incr("incidents.alert_rules.trigger", tags={"type": "fire"})
589586

tests/sentry/incidents/test_subscription_processor.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from sentry.snuba.models import QuerySubscription, SnubaQueryEventType
4747
from sentry.testutils.cases import BaseMetricsTestCase, SnubaTestCase, TestCase
4848
from sentry.testutils.helpers.datetime import freeze_time, iso_format
49-
from sentry.testutils.helpers.features import with_feature
5049
from sentry.utils import json
5150
from sentry.utils.dates import to_timestamp
5251

@@ -2113,7 +2112,6 @@ def test_comparison_alert_different_aggregate(self):
21132112
incident, [self.action], [(150.0, IncidentStatus.CLOSED, mock.ANY)]
21142113
)
21152114

2116-
@with_feature("organizations:metric-alert-rate-limiting")
21172115
def test_no_new_incidents_within_ten_minutes(self):
21182116
# Verify that a new incident is not made for the same rule, trigger, and
21192117
# subscription if an incident was already made within the last 10 minutes.
@@ -2152,7 +2150,6 @@ def test_no_new_incidents_within_ten_minutes(self):
21522150
any_order=True,
21532151
)
21542152

2155-
@with_feature("organizations:metric-alert-rate-limiting")
21562153
def test_incident_made_after_ten_minutes(self):
21572154
# Verify that a new incident will be made for the same rule, trigger, and
21582155
# subscription if the last incident made for those was made more tha 10 minutes

0 commit comments

Comments
 (0)