-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dynamic-sampling): enhancing and extracting `get_sliding_window_…
…org_sample_rate` (#79593)
- Loading branch information
1 parent
9a3d961
commit fcee09b
Showing
7 changed files
with
142 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from sentry import features | ||
from sentry.constants import TARGET_SAMPLE_RATE_DEFAULT | ||
from sentry.dynamic_sampling.rules.utils import get_redis_client_for_ds | ||
from sentry.dynamic_sampling.tasks.helpers.sliding_window import ( | ||
generate_sliding_window_org_cache_key, | ||
) | ||
from sentry.models.options.organization_option import OrganizationOption | ||
from sentry.models.organization import Organization | ||
|
||
__all__ = ["get_org_sample_rate"] | ||
|
||
|
||
def get_org_sample_rate( | ||
org_id: int, default_sample_rate: float | None | ||
) -> tuple[float | None, bool]: | ||
""" | ||
Returns the organization sample rate for dynamic sampling. This returns either the | ||
target_sample_rate organization option if custom dynamic sampling is enabled. Otherwise | ||
it will fall back on retrieving the sample rate from the sliding window calculations. | ||
""" | ||
|
||
# check if `organizations:dynamic-sampling-custom` feature flag is enabled for the | ||
# organization, if yes, return the `sentry:target_sample_rate` option | ||
try: | ||
org = Organization.objects.get_from_cache(id=org_id) | ||
except Organization.DoesNotExist: | ||
org = None | ||
|
||
has_dynamic_sampling_custom = features.has("organizations:dynamic-sampling-custom", org) | ||
if has_dynamic_sampling_custom: | ||
try: | ||
option_inst = OrganizationOption.objects.get( | ||
organization=org, key="sentry:target_sample_rate" | ||
) | ||
except OrganizationOption.DoesNotExist: | ||
option_inst = None | ||
|
||
if option_inst is None or option_inst.value is None: | ||
if default_sample_rate is not None: | ||
return default_sample_rate, False | ||
return TARGET_SAMPLE_RATE_DEFAULT, False | ||
|
||
return float(option_inst.value), True | ||
|
||
# fallback to sliding window calculation | ||
return _get_sliding_window_org_sample_rate(org_id, default_sample_rate) | ||
|
||
|
||
def _get_sliding_window_org_sample_rate( | ||
org_id: int, default_sample_rate: float | None | ||
) -> tuple[float | None, bool]: | ||
redis_client = get_redis_client_for_ds() | ||
cache_key = generate_sliding_window_org_cache_key(org_id) | ||
|
||
try: | ||
value = redis_client.get(cache_key) | ||
|
||
if value is not None: | ||
return float(value), True | ||
|
||
return default_sample_rate, False | ||
except (TypeError, ValueError): | ||
return default_sample_rate, False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tests/sentry/dynamic_sampling/tasks/helpers/test_sample_rate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from sentry.dynamic_sampling.tasks.helpers.sample_rate import get_org_sample_rate | ||
from sentry.models.options.organization_option import OrganizationOption | ||
from sentry.testutils.cases import BaseMetricsLayerTestCase, SnubaTestCase, TestCase | ||
from sentry.testutils.helpers.features import with_feature | ||
|
||
|
||
class PrioritiseProjectsSnubaQueryTest(BaseMetricsLayerTestCase, TestCase, SnubaTestCase): | ||
@with_feature(["organizations:dynamic-sampling", "organizations:dynamic-sampling-custom"]) | ||
def test_get_org_sample_rate_from_target_sample_rate(self): | ||
org1 = self.create_organization("test-org") | ||
|
||
OrganizationOption.objects.create( | ||
organization=org1, key="sentry:target_sample_rate", value=0.5 | ||
) | ||
|
||
sample_rate, success = get_org_sample_rate(org1.id, None) | ||
assert success | ||
assert sample_rate == 0.5 | ||
|
||
@with_feature(["organizations:dynamic-sampling", "organizations:dynamic-sampling-custom"]) | ||
def test_get_org_sample_rate_from_target_sample_rate_missing(self): | ||
org1 = self.create_organization("test-org") | ||
|
||
sample_rate, success = get_org_sample_rate(org1.id, None) | ||
assert not success | ||
assert sample_rate == 1.0 | ||
|
||
@with_feature(["organizations:dynamic-sampling", "organizations:dynamic-sampling-custom"]) | ||
def test_get_org_sample_rate_from_target_sample_rate_missing_default(self): | ||
org1 = self.create_organization("test-org") | ||
|
||
sample_rate, success = get_org_sample_rate(org1.id, 0.7) | ||
assert not success | ||
assert sample_rate == 0.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters