Skip to content

Commit 2118b23

Browse files
authored
feat(uptime): Use organization level autodetection flag to gate whether we should run detection (#74652)
Respect the org level flag that controls whether to run detection or not.
1 parent b41b098 commit 2118b23

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed

src/sentry/uptime/detectors/detector.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
from typing import TYPE_CHECKING
44

55
from sentry import features
6-
from sentry.uptime.detectors.ranking import add_base_url_to_rank, should_detect_for_project
6+
from sentry.uptime.detectors.ranking import (
7+
add_base_url_to_rank,
8+
should_detect_for_organization,
9+
should_detect_for_project,
10+
)
711
from sentry.uptime.detectors.url_extraction import extract_base_url
812
from sentry.utils import metrics
913

@@ -14,9 +18,11 @@
1418
def detect_base_url_for_project(project: Project, url: str) -> None:
1519
# Note: We might end up removing the `should_detect_for_project` check here if/when we decide to use detected
1620
# urls as suggestions as well.
17-
if not features.has(
18-
"organizations:uptime-automatic-hostname-detection", project.organization
19-
) or not should_detect_for_project(project):
21+
if (
22+
not features.has("organizations:uptime-automatic-hostname-detection", project.organization)
23+
or not should_detect_for_project(project)
24+
or not should_detect_for_organization(project.organization)
25+
):
2026
return
2127

2228
base_url = extract_base_url(url)

src/sentry/uptime/detectors/ranking.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from redis.client import StrictRedis
99
from rediscluster import RedisCluster
1010

11+
from sentry.constants import UPTIME_AUTODETECTION
1112
from sentry.utils import redis
1213

1314
if TYPE_CHECKING:
@@ -162,7 +163,8 @@ def delete_organization_bucket(bucket: datetime) -> None:
162163

163164

164165
def should_detect_for_organization(organization: Organization) -> bool:
165-
# TODO: Check setting here
166+
if not organization.get_option("sentry:uptime_autodetection", UPTIME_AUTODETECTION):
167+
return False
166168
return True
167169

168170

tests/sentry/uptime/detectors/test_detector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ def test_disabled_for_project(self):
2525
self.project.update_option("sentry:uptime_autodetection", False)
2626
detect_base_url_for_project(self.project, "https://sentry.io")
2727
self.assert_organization_key(self.organization, False)
28+
29+
@with_feature("organizations:uptime-automatic-hostname-detection")
30+
def test_disabled_for_organization(self):
31+
self.organization.update_option("sentry:uptime_autodetection", False)
32+
detect_base_url_for_project(self.project, "https://sentry.io")
33+
self.assert_organization_key(self.organization, False)

tests/sentry/uptime/detectors/test_ranking.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
get_candidate_urls_for_project,
1515
get_organization_bucket,
1616
get_project_base_url_rank_key,
17+
should_detect_for_organization,
1718
should_detect_for_project,
1819
)
1920

@@ -174,3 +175,12 @@ def test(self):
174175
assert not should_detect_for_project(self.project)
175176
self.project.update_option("sentry:uptime_autodetection", True)
176177
assert should_detect_for_project(self.project)
178+
179+
180+
class ShouldDetectForOrgTest(TestCase):
181+
def test(self):
182+
assert should_detect_for_organization(self.organization)
183+
self.organization.update_option("sentry:uptime_autodetection", False)
184+
assert not should_detect_for_organization(self.organization)
185+
self.organization.update_option("sentry:uptime_autodetection", True)
186+
assert should_detect_for_organization(self.organization)

tests/sentry/uptime/detectors/test_tasks.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
_get_cluster,
1919
add_base_url_to_rank,
2020
get_organization_bucket,
21+
get_project_base_url_rank_key,
2122
)
2223
from sentry.uptime.detectors.tasks import (
2324
LAST_PROCESSED_KEY,
@@ -144,17 +145,38 @@ def test(self):
144145
]
145146
)
146147

147-
def test_should_not_detect(self):
148+
def test_should_not_detect_project(self):
148149
with mock.patch(
149-
# TODO: Replace this mock with real tests when we implement this function properly
150-
"sentry.uptime.detectors.tasks.should_detect_for_project",
151-
return_value=False,
152-
), mock.patch(
153150
"sentry.uptime.detectors.tasks.get_candidate_urls_for_project"
154151
) as mock_get_candidate_urls_for_project:
152+
self.project.update_option("sentry:uptime_autodetection", False)
155153
assert not process_project_url_ranking(self.project, 5)
156154
mock_get_candidate_urls_for_project.assert_not_called()
157155

156+
def test_should_not_detect_organization(self):
157+
url_1 = "https://sentry.io"
158+
url_2 = "https://sentry.sentry.io"
159+
project_2 = self.create_project()
160+
add_base_url_to_rank(self.project, url_2)
161+
add_base_url_to_rank(self.project, url_1)
162+
add_base_url_to_rank(self.project, url_1)
163+
add_base_url_to_rank(project_2, url_1)
164+
165+
keys = [
166+
get_project_base_url_rank_key(self.project),
167+
get_project_base_url_rank_key(project_2),
168+
]
169+
redis = _get_cluster()
170+
assert all(redis.exists(key) for key in keys)
171+
172+
with mock.patch(
173+
"sentry.uptime.detectors.tasks.get_candidate_urls_for_project"
174+
) as mock_get_candidate_urls_for_project:
175+
self.organization.update_option("sentry:uptime_autodetection", False)
176+
assert not process_organization_url_ranking(self.organization.id)
177+
mock_get_candidate_urls_for_project.assert_not_called()
178+
assert all(not redis.exists(key) for key in keys)
179+
158180

159181
@freeze_time()
160182
class ProcessProjectUrlRankingTest(TestCase):

0 commit comments

Comments
 (0)