Skip to content

Commit ac2fec6

Browse files
ref(uptime): Detector always exists (#94571)
1 parent 212ab54 commit ac2fec6

File tree

13 files changed

+23
-58
lines changed

13 files changed

+23
-58
lines changed

src/sentry/uptime/consumers/results_consumer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,10 @@ def handle_result(self, subscription: UptimeSubscription | None, result: CheckRe
309309
if should_run_region_checks(subscription, result):
310310
try_check_and_update_regions(subscription, subscription_regions)
311311

312-
detector = get_detector(subscription, prefetch_workflow_data=True)
313-
314-
# Nothing to do if there's an orphaned project subscription
315-
if not detector:
312+
try:
313+
detector = get_detector(subscription, prefetch_workflow_data=True)
314+
except Detector.DoesNotExist:
315+
# Nothing to do if there's an orphaned project subscription
316316
remove_uptime_subscription_if_unused(subscription)
317317
return
318318

src/sentry/uptime/endpoints/project_uptime_alert_details.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ def delete(
123123
Delete an uptime monitor.
124124
"""
125125
detector = get_detector(uptime_subscription.uptime_subscription)
126-
assert detector
127126
uptime_subscription_id = uptime_subscription.id
128127
audit_log_data = uptime_subscription.get_audit_log_data()
129128
delete_uptime_detector(detector)

src/sentry/uptime/models.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -340,29 +340,24 @@ def get_current_instance_count(org: Organization) -> int:
340340
raise NotImplementedError
341341

342342

343-
def get_detector(
344-
uptime_subscription: UptimeSubscription, prefetch_workflow_data=False
345-
) -> Detector | None:
343+
def get_detector(uptime_subscription: UptimeSubscription, prefetch_workflow_data=False) -> Detector:
346344
"""
347345
Fetches a workflow_engine Detector given an existing uptime_subscription.
348346
This is used during the transition period moving uptime to detector.
349347
"""
350-
try:
351-
data_source = DataSource.objects.filter(
352-
type=DATA_SOURCE_UPTIME_SUBSCRIPTION,
353-
source_id=str(uptime_subscription.id),
354-
)
355-
qs = Detector.objects_for_deletion.filter(
356-
type=GROUP_TYPE_UPTIME_DOMAIN_CHECK_FAILURE, data_sources=data_source[:1]
357-
)
358-
select_related = ["project", "project__organization"]
359-
if prefetch_workflow_data:
360-
select_related.append("workflow_condition_group")
361-
qs = qs.prefetch_related("workflow_condition_group__conditions")
362-
qs = qs.select_related(*select_related)
363-
return qs.get()
364-
except Detector.DoesNotExist:
365-
return None
348+
data_source = DataSource.objects.filter(
349+
type=DATA_SOURCE_UPTIME_SUBSCRIPTION,
350+
source_id=str(uptime_subscription.id),
351+
)
352+
qs = Detector.objects_for_deletion.filter(
353+
type=GROUP_TYPE_UPTIME_DOMAIN_CHECK_FAILURE, data_sources=data_source[:1]
354+
)
355+
select_related = ["project", "project__organization"]
356+
if prefetch_workflow_data:
357+
select_related.append("workflow_condition_group")
358+
qs = qs.prefetch_related("workflow_condition_group__conditions")
359+
qs = qs.select_related(*select_related)
360+
return qs.get()
366361

367362

368363
def get_uptime_subscription(detector: Detector) -> UptimeSubscription:

src/sentry/uptime/subscriptions/subscriptions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ def update_project_uptime_subscription(
325325
)
326326

327327
detector = get_detector(uptime_monitor.uptime_subscription)
328-
assert detector
329328
detector.update(
330329
name=default_if_not_set(uptime_monitor.name, name),
331330
owner_user_id=owner_user_id,

src/sentry/uptime/subscriptions/tasks.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ def broken_monitor_checker(**kwargs):
237237
)
238238
):
239239
detector = get_detector(uptime_subscription)
240-
assert detector
241240
if detector.config["mode"] == UptimeMonitorMode.AUTO_DETECTED_ACTIVE:
242241
try:
243242
disable_uptime_detector(detector)

tests/sentry/deletions/test_detector.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ def test_delete_uptime_detector(self) -> None:
127127
proj_sub = self.create_project_uptime_subscription()
128128
uptime_sub = proj_sub.uptime_subscription
129129
detector = get_detector(uptime_sub)
130-
assert detector
131130
self.ScheduledDeletion.schedule(instance=detector, days=0)
132131

133132
with self.tasks():

tests/sentry/uptime/consumers/test_results_consumer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ def setUp(self) -> None:
7373
uptime_subscription=self.subscription,
7474
owner=self.user,
7575
)
76-
detector = get_detector(self.subscription)
77-
assert detector
78-
self.detector = detector
76+
self.detector = get_detector(self.subscription)
7977

8078
def send_result(
8179
self, result: CheckResult, consumer: ProcessingStrategy[KafkaPayload] | None = None

tests/sentry/uptime/detectors/test_ranking.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ def test_quota(self) -> None:
206206
uptime_monitor = self.create_project_uptime_subscription()
207207
assert not should_detect_for_organization(self.organization)
208208
detector = get_detector(uptime_monitor.uptime_subscription)
209-
assert detector
210209
detector.delete()
211210
uptime_monitor.delete()
212211
assert should_detect_for_organization(self.organization)

tests/sentry/uptime/subscriptions/test_subscriptions.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ def test(self) -> None:
214214
).exists()
215215

216216
detector = get_detector(uptime_monitor.uptime_subscription)
217-
assert detector
218217
assert detector.config["mode"] == UptimeMonitorMode.AUTO_DETECTED_ACTIVE.value
219218
assert detector.config["environment"] == self.environment.name
220219
assert detector.project == self.project
@@ -386,7 +385,6 @@ def test_status_enable(self, mock_enable_uptime_detector: mock.MagicMock) -> Non
386385
status=ObjectStatus.ACTIVE,
387386
)
388387
detector = get_detector(proj_sub.uptime_subscription)
389-
assert detector
390388
mock_enable_uptime_detector.assert_called_with(detector, ensure_assignment=True)
391389

392390
@mock.patch("sentry.uptime.subscriptions.subscriptions.enable_uptime_detector")
@@ -426,7 +424,6 @@ def test_no_seat_assignment(self, _mock_check_assign_seat: mock.MagicMock) -> No
426424
assert proj_sub.uptime_subscription.status == UptimeSubscription.Status.DISABLED.value
427425

428426
detector = get_detector(proj_sub.uptime_subscription)
429-
assert detector
430427
assert not detector.enabled
431428

432429
def test_create_manual_removes_onboarding(self) -> None:
@@ -497,7 +494,6 @@ def test(self) -> None:
497494
assert prev_uptime_subscription.subscription_id == prev_subscription_id
498495

499496
detector = get_detector(proj_sub.uptime_subscription)
500-
assert detector
501497
assert detector.name == "New name"
502498
assert detector.owner_user_id == self.user.id
503499

@@ -633,7 +629,6 @@ def test_other_subscriptions(self, mock_remove_seat: mock.MagicMock) -> None:
633629
mode=UptimeMonitorMode.AUTO_DETECTED_ACTIVE,
634630
)
635631
detector = get_detector(proj_sub.uptime_subscription)
636-
assert detector
637632

638633
assert proj_sub.uptime_subscription_id != other_sub.uptime_subscription_id
639634

@@ -661,7 +656,6 @@ def test_single_subscriptions(self, mock_remove_seat: mock.MagicMock) -> None:
661656
mode=UptimeMonitorMode.AUTO_DETECTED_ACTIVE,
662657
)
663658
detector = get_detector(proj_sub.uptime_subscription)
664-
assert detector
665659
with self.tasks():
666660
delete_uptime_detector(detector)
667661
run_scheduled_deletions()
@@ -763,7 +757,6 @@ def test(self, mock_disable_seat: mock.MagicMock) -> None:
763757
mode=UptimeMonitorMode.MANUAL,
764758
)
765759
detector = get_detector(proj_sub.uptime_subscription)
766-
assert detector
767760

768761
with self.tasks():
769762
disable_uptime_detector(detector)
@@ -792,7 +785,6 @@ def test_disable_failed(self, mock_disable_seat: mock.MagicMock) -> None:
792785
uptime_status=UptimeStatus.FAILED,
793786
)
794787
detector = get_detector(proj_sub.uptime_subscription)
795-
assert detector
796788

797789
create_issue_platform_occurrence(
798790
self.create_uptime_result(
@@ -830,7 +822,6 @@ def test_already_disabled(self, mock_disable_seat: mock.MagicMock) -> None:
830822
mode=UptimeMonitorMode.MANUAL,
831823
)
832824
detector = get_detector(proj_sub.uptime_subscription)
833-
assert detector
834825

835826
proj_sub.update(status=ObjectStatus.DISABLED)
836827
detector.update(enabled=False)
@@ -850,7 +841,6 @@ def test_skip_quotas(self, mock_disable_seat: mock.MagicMock) -> None:
850841
mode=UptimeMonitorMode.MANUAL,
851842
)
852843
detector = get_detector(proj_sub.uptime_subscription)
853-
assert detector
854844

855845
with self.tasks():
856846
disable_uptime_detector(detector, skip_quotas=True)
@@ -888,7 +878,6 @@ def test(
888878
mode=UptimeMonitorMode.MANUAL,
889879
)
890880
detector = get_detector(proj_sub.uptime_subscription)
891-
assert detector
892881

893882
# Calling enable_uptime_detector on an already enabled
894883
# monitor does nothing
@@ -944,7 +933,6 @@ def test_no_seat_assignment(
944933
mode=UptimeMonitorMode.MANUAL,
945934
)
946935
detector = get_detector(proj_sub.uptime_subscription)
947-
assert detector
948936

949937
# We'll be unable to assign a seat
950938
with self.tasks(), raises(UptimeMonitorNoSeatAvailable) as exc_info:
@@ -974,7 +962,6 @@ def test_already_enabled(self, mock_check_assign_seat: mock.MagicMock) -> None:
974962
mode=UptimeMonitorMode.MANUAL,
975963
)
976964
detector = get_detector(proj_sub.uptime_subscription)
977-
assert detector
978965

979966
assert detector.enabled
980967
assert proj_sub.status == ObjectStatus.ACTIVE
@@ -1008,7 +995,6 @@ def test_skip_quotas(
1008995
mode=UptimeMonitorMode.MANUAL,
1009996
)
1010997
detector = get_detector(proj_sub.uptime_subscription)
1011-
assert detector
1012998

1013999
# Manually mark the monitor and subscription as disabled
10141000
proj_sub.update(status=ObjectStatus.DISABLED)

tests/sentry/uptime/subscriptions/test_tasks.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ def run_test(
549549
assert proj_sub.uptime_subscription.uptime_status == expected_uptime_status
550550

551551
detector = get_detector(proj_sub.uptime_subscription)
552-
assert detector
553552
if expected_status == ObjectStatus.ACTIVE:
554553
assert detector.enabled
555554
else:

0 commit comments

Comments
 (0)