Skip to content

ref(uptime): Simplify consumer with one-to-one proj_sub <-> sub #90202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions src/sentry/uptime/consumers/results_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
UptimeSubscription,
UptimeSubscriptionRegion,
get_detector,
get_project_subscriptions_for_uptime_subscription,
get_project_subscription_for_uptime_subscription,
get_top_hosting_provider_names,
load_regions_for_uptime_subscription,
)
Expand Down Expand Up @@ -293,29 +293,12 @@ def handle_result(self, subscription: UptimeSubscription | None, result: CheckRe

try_check_and_update_regions(subscription, result, subscription_regions)

project_subscriptions = get_project_subscriptions_for_uptime_subscription(subscription.id)
project_subscription = get_project_subscription_for_uptime_subscription(subscription.id)

cluster = _get_cluster()
last_updates: list[str | None] = cluster.mget(
build_last_update_key(sub) for sub in project_subscriptions
)

for last_update_raw, project_subscription in zip(last_updates, project_subscriptions):
last_update_ms = 0 if last_update_raw is None else int(last_update_raw)
self.handle_result_for_project(
project_subscription,
result,
last_update_ms,
metric_tags.copy(),
)
last_update_raw: str | None = cluster.get(build_last_update_key(project_subscription))
last_update_ms = 0 if last_update_raw is None else int(last_update_raw)

def handle_result_for_project(
self,
project_subscription: ProjectUptimeSubscription,
result: CheckResult,
last_update_ms: int,
metric_tags: dict[str, str],
):
if features.has(
"organizations:uptime-detailed-logging", project_subscription.project.organization
):
Expand Down
10 changes: 4 additions & 6 deletions src/sentry/uptime/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,11 @@ def get_top_hosting_provider_names(limit: int) -> set[str]:
recalculate=False,
cache_ttl=timedelta(hours=4),
)
def get_project_subscriptions_for_uptime_subscription(
def get_project_subscription_for_uptime_subscription(
uptime_subscription_id: int,
) -> list[ProjectUptimeSubscription]:
return list(
ProjectUptimeSubscription.objects.filter(
uptime_subscription_id=uptime_subscription_id
).select_related("project", "project__organization")
) -> ProjectUptimeSubscription:
return ProjectUptimeSubscription.objects.select_related("project", "project__organization").get(
uptime_subscription_id=uptime_subscription_id
)


Expand Down
28 changes: 1 addition & 27 deletions tests/sentry/uptime/consumers/test_results_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from sentry.conf.types import kafka_definition
from sentry.conf.types.uptime import UptimeRegionConfig
from sentry.constants import DataCategory, ObjectStatus
from sentry.constants import DataCategory
from sentry.models.group import Group, GroupStatus
from sentry.testutils.abstract import Abstract
from sentry.testutils.helpers.datetime import freeze_time
Expand Down Expand Up @@ -446,32 +446,6 @@ def test_no_subscription(self):
"default", UptimeSubscription(subscription_id=subscription_id), "delete", None
)

def test_multiple_project_subscriptions_with_disabled(self):
"""
Tests that we do not process results for disabled project subscriptions
"""
# Second disabled project subscription
self.create_project_uptime_subscription(
uptime_subscription=self.subscription,
project=self.create_project(),
status=ObjectStatus.DISABLED,
)
result = self.create_uptime_result(self.subscription.subscription_id)

with (
mock.patch("sentry.uptime.consumers.results_consumer.metrics") as metrics,
self.feature(["organizations:uptime", "organizations:uptime-create-issues"]),
):
self.send_result(result)
# We only process a single project result, the other is dropped,
# there should be only one handle_result_for_project metric call
handle_result_calls = [
c
for c in metrics.incr.mock_calls
if c[1][0] == "uptime.result_processor.handle_result_for_project"
]
assert len(handle_result_calls) == 1

def test_organization_feature_disabled(self):
"""
Tests that we do not process results for disabled project subscriptions
Expand Down
Loading