Skip to content

Commit e587bb2

Browse files
committed
ref(uptime): Simplify consumer with one-to-one proj_sub <-> sub
This merges the handle_result_for_project into handler_result, since we can now assume there's a single ProjectUptimeSubscription for every UptimeSubscription, thus we don't need to fan out
1 parent 05a7a48 commit e587bb2

File tree

3 files changed

+9
-54
lines changed

3 files changed

+9
-54
lines changed

src/sentry/uptime/consumers/results_consumer.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
UptimeStatus,
3333
UptimeSubscription,
3434
UptimeSubscriptionRegion,
35-
get_project_subscriptions_for_uptime_subscription,
35+
get_project_subscription_for_uptime_subscription,
3636
get_top_hosting_provider_names,
3737
load_regions_for_uptime_subscription,
3838
)
@@ -292,29 +292,12 @@ def handle_result(self, subscription: UptimeSubscription | None, result: CheckRe
292292

293293
try_check_and_update_regions(subscription, result, subscription_regions)
294294

295-
project_subscriptions = get_project_subscriptions_for_uptime_subscription(subscription.id)
295+
project_subscription = get_project_subscription_for_uptime_subscription(subscription.id)
296296

297297
cluster = _get_cluster()
298-
last_updates: list[str | None] = cluster.mget(
299-
build_last_update_key(sub) for sub in project_subscriptions
300-
)
301-
302-
for last_update_raw, project_subscription in zip(last_updates, project_subscriptions):
303-
last_update_ms = 0 if last_update_raw is None else int(last_update_raw)
304-
self.handle_result_for_project(
305-
project_subscription,
306-
result,
307-
last_update_ms,
308-
metric_tags.copy(),
309-
)
298+
last_update_raw: str | None = cluster.get(build_last_update_key(project_subscription))
299+
last_update_ms = 0 if last_update_raw is None else int(last_update_raw)
310300

311-
def handle_result_for_project(
312-
self,
313-
project_subscription: ProjectUptimeSubscription,
314-
result: CheckResult,
315-
last_update_ms: int,
316-
metric_tags: dict[str, str],
317-
):
318301
if features.has(
319302
"organizations:uptime-detailed-logging", project_subscription.project.organization
320303
):

src/sentry/uptime/models.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,11 @@ def get_top_hosting_provider_names(limit: int) -> set[str]:
262262
recalculate=False,
263263
cache_ttl=timedelta(hours=4),
264264
)
265-
def get_project_subscriptions_for_uptime_subscription(
265+
def get_project_subscription_for_uptime_subscription(
266266
uptime_subscription_id: int,
267-
) -> list[ProjectUptimeSubscription]:
268-
return list(
269-
ProjectUptimeSubscription.objects.filter(
270-
uptime_subscription_id=uptime_subscription_id
271-
).select_related("project", "project__organization")
267+
) -> ProjectUptimeSubscription:
268+
return ProjectUptimeSubscription.objects.select_related("project", "project__organization").get(
269+
uptime_subscription_id=uptime_subscription_id
272270
)
273271

274272

tests/sentry/uptime/consumers/test_results_consumer.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from sentry.conf.types import kafka_definition
2424
from sentry.conf.types.uptime import UptimeRegionConfig
25-
from sentry.constants import DataCategory, ObjectStatus
25+
from sentry.constants import DataCategory
2626
from sentry.models.group import Group, GroupStatus
2727
from sentry.testutils.abstract import Abstract
2828
from sentry.testutils.helpers.datetime import freeze_time
@@ -446,32 +446,6 @@ def test_no_subscription(self):
446446
"default", UptimeSubscription(subscription_id=subscription_id), "delete", None
447447
)
448448

449-
def test_multiple_project_subscriptions_with_disabled(self):
450-
"""
451-
Tests that we do not process results for disabled project subscriptions
452-
"""
453-
# Second disabled project subscription
454-
self.create_project_uptime_subscription(
455-
uptime_subscription=self.subscription,
456-
project=self.create_project(),
457-
status=ObjectStatus.DISABLED,
458-
)
459-
result = self.create_uptime_result(self.subscription.subscription_id)
460-
461-
with (
462-
mock.patch("sentry.uptime.consumers.results_consumer.metrics") as metrics,
463-
self.feature(["organizations:uptime", "organizations:uptime-create-issues"]),
464-
):
465-
self.send_result(result)
466-
# We only process a single project result, the other is dropped,
467-
# there should be only one handle_result_for_project metric call
468-
handle_result_calls = [
469-
c
470-
for c in metrics.incr.mock_calls
471-
if c[1][0] == "uptime.result_processor.handle_result_for_project"
472-
]
473-
assert len(handle_result_calls) == 1
474-
475449
def test_organization_feature_disabled(self):
476450
"""
477451
Tests that we do not process results for disabled project subscriptions

0 commit comments

Comments
 (0)