Skip to content

Commit c5802df

Browse files
ref(uptime): Simplify consumer with one-to-one proj_sub <-> sub (#90202)
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 d52388a commit c5802df

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
@@ -33,7 +33,7 @@
3333
UptimeSubscription,
3434
UptimeSubscriptionRegion,
3535
get_detector,
36-
get_project_subscriptions_for_uptime_subscription,
36+
get_project_subscription_for_uptime_subscription,
3737
get_top_hosting_provider_names,
3838
load_regions_for_uptime_subscription,
3939
)
@@ -293,29 +293,12 @@ def handle_result(self, subscription: UptimeSubscription | None, result: CheckRe
293293

294294
try_check_and_update_regions(subscription, result, subscription_regions)
295295

296-
project_subscriptions = get_project_subscriptions_for_uptime_subscription(subscription.id)
296+
project_subscription = get_project_subscription_for_uptime_subscription(subscription.id)
297297

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

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

src/sentry/uptime/models.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,11 @@ def get_top_hosting_provider_names(limit: int) -> set[str]:
266266
recalculate=False,
267267
cache_ttl=timedelta(hours=4),
268268
)
269-
def get_project_subscriptions_for_uptime_subscription(
269+
def get_project_subscription_for_uptime_subscription(
270270
uptime_subscription_id: int,
271-
) -> list[ProjectUptimeSubscription]:
272-
return list(
273-
ProjectUptimeSubscription.objects.filter(
274-
uptime_subscription_id=uptime_subscription_id
275-
).select_related("project", "project__organization")
271+
) -> ProjectUptimeSubscription:
272+
return ProjectUptimeSubscription.objects.select_related("project", "project__organization").get(
273+
uptime_subscription_id=uptime_subscription_id
276274
)
277275

278276

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)