Skip to content

ref(uptime): Detector always exists #94571

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/sentry/uptime/consumers/results_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,10 @@ def handle_result(self, subscription: UptimeSubscription | None, result: CheckRe
if should_run_region_checks(subscription, result):
try_check_and_update_regions(subscription, subscription_regions)

detector = get_detector(subscription)

# Nothing to do if there's an orphaned project subscription
if not detector:
try:
detector = get_detector(subscription)
except Detector.DoesNotExist:
# Nothing to do if there's an orphaned project subscription
remove_uptime_subscription_if_unused(subscription)
return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ def delete(
Delete an uptime monitor.
"""
detector = get_detector(uptime_subscription.uptime_subscription)
assert detector
uptime_subscription_id = uptime_subscription.id
audit_log_data = uptime_subscription.get_audit_log_data()
delete_uptime_detector(detector)
Expand Down
14 changes: 8 additions & 6 deletions src/sentry/uptime/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def get_current_instance_count(org: Organization) -> int:
raise NotImplementedError


def get_detector(uptime_subscription: UptimeSubscription) -> Detector | None:
def get_detector(uptime_subscription: UptimeSubscription) -> Detector:
"""
Fetches a workflow_engine Detector given an existing uptime_subscription.
This is used during the transition period moving uptime to detector.
Expand All @@ -358,11 +358,13 @@ def get_detector(uptime_subscription: UptimeSubscription) -> Detector | None:
type=DATA_SOURCE_UPTIME_SUBSCRIPTION,
source_id=str(uptime_subscription.id),
)
return Detector.objects.get(
type=GROUP_TYPE_UPTIME_DOMAIN_CHECK_FAILURE, data_sources=data_source
)
except (DataSource.DoesNotExist, Detector.DoesNotExist):
return None
except DataSource.DoesNotExist:
raise Detector.DoesNotExist

return Detector.objects.get(
type=GROUP_TYPE_UPTIME_DOMAIN_CHECK_FAILURE,
data_sources=data_source,
)


def get_uptime_subscription(detector: Detector) -> UptimeSubscription:
Expand Down
1 change: 0 additions & 1 deletion src/sentry/uptime/subscriptions/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ def update_project_uptime_subscription(
)

detector = get_detector(uptime_monitor.uptime_subscription)
assert detector
detector.update(
name=default_if_not_set(uptime_monitor.name, name),
owner_user_id=owner_user_id,
Expand Down
1 change: 0 additions & 1 deletion src/sentry/uptime/subscriptions/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ def broken_monitor_checker(**kwargs):
)
):
detector = get_detector(uptime_subscription)
assert detector
if detector.config["mode"] == UptimeMonitorMode.AUTO_DETECTED_ACTIVE:
disable_uptime_detector(detector)
count += 1
Expand Down
4 changes: 1 addition & 3 deletions tests/sentry/uptime/consumers/test_results_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def setUp(self):
uptime_subscription=self.subscription,
owner=self.user,
)
detector = get_detector(self.subscription)
assert detector
self.detector = detector
self.detector = get_detector(self.subscription)

def send_result(
self, result: CheckResult, consumer: ProcessingStrategy[KafkaPayload] | None = None
Expand Down
1 change: 0 additions & 1 deletion tests/sentry/uptime/detectors/test_ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ def test_quota(self):
uptime_monitor = self.create_project_uptime_subscription()
assert not should_detect_for_organization(self.organization)
detector = get_detector(uptime_monitor.uptime_subscription)
assert detector
detector.delete()
uptime_monitor.delete()
assert should_detect_for_organization(self.organization)
14 changes: 0 additions & 14 deletions tests/sentry/uptime/subscriptions/test_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ def test(self):
).exists()

detector = get_detector(uptime_monitor.uptime_subscription)
assert detector
assert detector.config["mode"] == UptimeMonitorMode.AUTO_DETECTED_ACTIVE.value
assert detector.config["environment"] == self.environment.name
assert detector.project == self.project
Expand Down Expand Up @@ -384,7 +383,6 @@ def test_status_enable(self, mock_enable_uptime_detector):
status=ObjectStatus.ACTIVE,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector
mock_enable_uptime_detector.assert_called_with(detector, ensure_assignment=True)

@mock.patch("sentry.uptime.subscriptions.subscriptions.enable_uptime_detector")
Expand Down Expand Up @@ -422,7 +420,6 @@ def test_no_seat_assignment(self, _mock_check_assign_seat):
assert proj_sub.uptime_subscription.status == UptimeSubscription.Status.DISABLED.value

detector = get_detector(proj_sub.uptime_subscription)
assert detector
assert not detector.enabled

def test_create_manual_removes_onboarding(self):
Expand Down Expand Up @@ -493,7 +490,6 @@ def test(self):
assert prev_uptime_subscription.subscription_id == prev_subscription_id

detector = get_detector(proj_sub.uptime_subscription)
assert detector
assert detector.name == "New name"
assert detector.owner_user_id == self.user.id

Expand Down Expand Up @@ -629,7 +625,6 @@ def test_other_subscriptions(self, mock_remove_seat):
mode=UptimeMonitorMode.AUTO_DETECTED_ACTIVE,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

assert proj_sub.uptime_subscription_id != other_sub.uptime_subscription_id

Expand Down Expand Up @@ -657,7 +652,6 @@ def test_single_subscriptions(self, mock_remove_seat):
mode=UptimeMonitorMode.AUTO_DETECTED_ACTIVE,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector
with self.tasks():
delete_uptime_detector(detector)
run_scheduled_deletions()
Expand Down Expand Up @@ -759,7 +753,6 @@ def test(self, mock_disable_seat):
mode=UptimeMonitorMode.MANUAL,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

with self.tasks():
disable_uptime_detector(detector)
Expand Down Expand Up @@ -788,7 +781,6 @@ def test_disable_failed(self, mock_disable_seat):
uptime_status=UptimeStatus.FAILED,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

create_issue_platform_occurrence(
self.create_uptime_result(
Expand Down Expand Up @@ -826,7 +818,6 @@ def test_already_disabled(self, mock_disable_seat):
mode=UptimeMonitorMode.MANUAL,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

proj_sub.update(status=ObjectStatus.DISABLED)
detector.update(enabled=False)
Expand All @@ -846,7 +837,6 @@ def test_skip_quotas(self, mock_disable_seat):
mode=UptimeMonitorMode.MANUAL,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

with self.tasks():
disable_uptime_detector(detector, skip_quotas=True)
Expand Down Expand Up @@ -882,7 +872,6 @@ def test(self, mock_assign_seat, mock_check_assign_seat):
mode=UptimeMonitorMode.MANUAL,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

# Calling enable_uptime_detector on an already enabled
# monitor does nothing
Expand Down Expand Up @@ -936,7 +925,6 @@ def test_no_seat_assignment(self, mock_check_assign_seat, mock_assign_seat):
mode=UptimeMonitorMode.MANUAL,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

# We'll be unable to assign a seat
with self.tasks(), raises(UptimeMonitorNoSeatAvailable) as exc_info:
Expand Down Expand Up @@ -966,7 +954,6 @@ def test_already_enabled(self, mock_check_assign_seat):
mode=UptimeMonitorMode.MANUAL,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

assert detector.enabled
assert proj_sub.status == ObjectStatus.ACTIVE
Expand Down Expand Up @@ -998,7 +985,6 @@ def test_skip_quotas(self, mock_assign_seat, mock_check_assign_seat):
mode=UptimeMonitorMode.MANUAL,
)
detector = get_detector(proj_sub.uptime_subscription)
assert detector

# Manually mark the monitor and subscription as disabled
proj_sub.update(status=ObjectStatus.DISABLED)
Expand Down
1 change: 0 additions & 1 deletion tests/sentry/uptime/subscriptions/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ def run_test(
assert proj_sub.uptime_subscription.uptime_status == expected_uptime_status

detector = get_detector(proj_sub.uptime_subscription)
assert detector
if expected_status == ObjectStatus.ACTIVE:
assert detector.enabled
else:
Expand Down
6 changes: 0 additions & 6 deletions tests/sentry/uptime/test_grouptype.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class BuildDetectorFingerprintComponentTest(UptimeTestCase):
def test_build_detector_fingerprint_component(self):
project_subscription = self.create_project_uptime_subscription()
detector = get_detector(project_subscription.uptime_subscription)
assert detector

fingerprint_component = build_detector_fingerprint_component(detector)
assert fingerprint_component == f"uptime-detector:{detector.id}"
Expand All @@ -44,7 +43,6 @@ class BuildFingerprintForProjectSubscriptionTest(UptimeTestCase):
def test_build_fingerprint_for_project_subscription(self):
project_subscription = self.create_project_uptime_subscription()
detector = get_detector(project_subscription.uptime_subscription)
assert detector

fingerprint = build_fingerprint(detector)
expected_fingerprint = [build_detector_fingerprint_component(detector)]
Expand All @@ -68,7 +66,6 @@ def test_build_event_data(self):
result = self.create_uptime_result()
project_subscription = self.create_project_uptime_subscription()
detector = get_detector(project_subscription.uptime_subscription)
assert detector

assert build_event_data(result, detector) == {
"environment": "development",
Expand Down Expand Up @@ -107,7 +104,6 @@ def test_simple_evaluate(self):
project_subscription = self.create_project_uptime_subscription()
uptime_subscription = project_subscription.uptime_subscription
detector = get_detector(project_subscription.uptime_subscription)
assert detector

assert uptime_subscription.uptime_status == UptimeStatus.OK

Expand Down Expand Up @@ -156,7 +152,6 @@ def test_issue_creation_disabled(self):
project_subscription = self.create_project_uptime_subscription()
uptime_subscription = project_subscription.uptime_subscription
detector = get_detector(project_subscription.uptime_subscription)
assert detector

assert uptime_subscription.uptime_status == UptimeStatus.OK

Expand Down Expand Up @@ -231,7 +226,6 @@ def test_flapping_evaluate(self):
project_subscription = self.create_project_uptime_subscription()
uptime_subscription = project_subscription.uptime_subscription
detector = get_detector(project_subscription.uptime_subscription)
assert detector

assert uptime_subscription.uptime_status == UptimeStatus.OK

Expand Down
4 changes: 0 additions & 4 deletions tests/sentry/uptime/test_issue_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def test(self, mock_uuid, mock_produce_occurrence_to_kafka):
result = self.create_uptime_result()
project_subscription = self.create_project_uptime_subscription()
detector = get_detector(project_subscription.uptime_subscription)
assert detector
create_issue_platform_occurrence(result, detector)
assert mock_produce_occurrence_to_kafka.call_count == 1
assert mock_produce_occurrence_to_kafka.call_args_list[0][0] == ()
Expand All @@ -51,7 +50,6 @@ def test(self, mock_uuid):
result = self.create_uptime_result()
project_subscription = self.create_project_uptime_subscription()
detector = get_detector(project_subscription.uptime_subscription)
assert detector

assert build_occurrence_from_result(result, detector) == IssueOccurrence(
id=occurrence_id.hex,
Expand Down Expand Up @@ -85,7 +83,6 @@ def test(self):
result = self.create_uptime_result()
project_subscription = self.create_project_uptime_subscription()
detector = get_detector(project_subscription.uptime_subscription)
assert detector

event_id = uuid.uuid4()
occurrence = IssueOccurrence(
Expand Down Expand Up @@ -130,7 +127,6 @@ def test(self):
uptime_subscription=subscription
)
detector = get_detector(project_subscription.uptime_subscription)
assert detector
result = self.create_uptime_result(subscription.subscription_id)
with self.feature(UptimeDomainCheckFailure.build_ingest_feature_name()):
create_issue_platform_occurrence(result, detector)
Expand Down
1 change: 0 additions & 1 deletion tests/sentry/uptime/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ class CreateDetectorTest(UptimeTestCase):
def test_simple(self):
monitor = self.create_project_uptime_subscription()
detector = get_detector(monitor.uptime_subscription)
assert detector

assert detector.name == monitor.name
assert detector.owner_user_id == monitor.owner_user_id
Expand Down
Loading