Browse Source

ref(dynamic-sampling): Remove rules v1 (#44581)

Riccardo Busetti 2 years ago
parent
commit
7daa270286

+ 2 - 2
src/sentry/api/endpoints/project_details.py

@@ -384,8 +384,8 @@ class ProjectDetailsEndpoint(ProjectEndpoint):
             include_rules = request.GET.get("includeDynamicSamplingRules") == "1"
             if include_rules and is_active_superuser(request):
                 data["dynamicSamplingRules"] = {
-                    "rules": generate_rules(project),
-                    "rulesV2": generate_rules(project, True),
+                    "rules": [],
+                    "rulesV2": generate_rules(project),
                 }
         else:
             data["dynamicSamplingBiases"] = None

+ 4 - 14
src/sentry/dynamic_sampling/rules/base.py

@@ -4,10 +4,7 @@ import sentry_sdk
 
 from sentry import quotas
 from sentry.dynamic_sampling.rules.biases.base import Bias, BiasParams
-from sentry.dynamic_sampling.rules.combine import (
-    get_relay_biases_combinator,
-    get_relay_biases_combinator_v2,
-)
+from sentry.dynamic_sampling.rules.combine import get_relay_biases_combinator
 from sentry.dynamic_sampling.rules.logging import log_rules
 from sentry.dynamic_sampling.rules.utils import PolymorphicRule, RuleType, get_enabled_user_biases
 from sentry.models import Project
@@ -29,7 +26,6 @@ def _get_rules_of_enabled_biases(
     base_sample_rate: float,
     enabled_biases: Set[str],
     combined_biases: OrderedDict[RuleType, Bias],
-    version_2: bool = False,
 ) -> List[PolymorphicRule]:
     rules = []
 
@@ -44,18 +40,13 @@ def _get_rules_of_enabled_biases(
             rules += bias.get_rules(BiasParams(project, base_sample_rate))
 
     # We want to log only rules v2, to avoid confusion and duplication.
-    if version_2:
-        log_rules(project.organization.id, project.id, rules)
+    log_rules(project.organization.id, project.id, rules)
 
     return rules
 
 
-def generate_rules(project: Project, version_2: bool = False) -> List[PolymorphicRule]:
+def generate_rules(project: Project) -> List[PolymorphicRule]:
     try:
-        biases_combinator = (
-            get_relay_biases_combinator_v2() if version_2 else get_relay_biases_combinator()
-        )
-
         return _get_rules_of_enabled_biases(
             project,
             get_guarded_blended_sample_rate(project),
@@ -65,8 +56,7 @@ def generate_rules(project: Project, version_2: bool = False) -> List[Polymorphi
             # * Rules generator
             # * Bias
             # check in the dynamic_sampling/rules/biases module how existing biases are implemented.
-            biases_combinator.get_combined_biases(),
-            version_2,
+            get_relay_biases_combinator().get_combined_biases(),
         )
     except Exception as e:
         sentry_sdk.capture_exception(e)

+ 0 - 28
src/sentry/dynamic_sampling/rules/biases/boost_environments_bias.py

@@ -23,29 +23,6 @@ class BoostEnvironmentsDataProvider(BiasDataProvider):
 
 
 class BoostEnvironmentsRulesGenerator(BiasRulesGenerator):
-    def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
-        return [
-            {
-                "sampleRate": 1,
-                "type": "trace",
-                "condition": {
-                    "op": "or",
-                    "inner": [
-                        {
-                            "op": "glob",
-                            "name": "trace.environment",
-                            "value": ENVIRONMENT_GLOBS,
-                            "options": {"ignoreCase": True},
-                        }
-                    ],
-                },
-                "active": True,
-                "id": bias_data["id"],
-            }
-        ]
-
-
-class BoostEnvironmentsRulesGeneratorV2(BiasRulesGenerator):
     def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
         return [
             {
@@ -74,8 +51,3 @@ class BoostEnvironmentsRulesGeneratorV2(BiasRulesGenerator):
 class BoostEnvironmentsBias(Bias):
     def __init__(self) -> None:
         super().__init__(BoostEnvironmentsDataProvider, BoostEnvironmentsRulesGenerator)
-
-
-class BoostEnvironmentsBiasV2(Bias):
-    def __init__(self) -> None:
-        super().__init__(BoostEnvironmentsDataProvider, BoostEnvironmentsRulesGeneratorV2)

+ 0 - 31
src/sentry/dynamic_sampling/rules/biases/boost_key_transactions_bias.py

@@ -26,32 +26,6 @@ class BoostKeyTransactionsDataProvider(BiasDataProvider):
 
 
 class BoostKeyTransactionsRulesGenerator(BiasRulesGenerator):
-    def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
-        if len(bias_data["keyTransactions"]) == 0:
-            return []
-
-        return [
-            {
-                "sampleRate": bias_data["sampleRate"],
-                "type": "transaction",
-                "condition": {
-                    "op": "or",
-                    "inner": [
-                        {
-                            "op": "eq",
-                            "name": "event.transaction",
-                            "value": bias_data["keyTransactions"],
-                            "options": {"ignoreCase": True},
-                        }
-                    ],
-                },
-                "active": True,
-                "id": bias_data["id"],
-            }
-        ]
-
-
-class BoostKeyTransactionsRulesGeneratorV2(BiasRulesGenerator):
     def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
         if len(bias_data["keyTransactions"]) == 0:
             return []
@@ -83,8 +57,3 @@ class BoostKeyTransactionsRulesGeneratorV2(BiasRulesGenerator):
 class BoostKeyTransactionsBias(Bias):
     def __init__(self) -> None:
         super().__init__(BoostKeyTransactionsDataProvider, BoostKeyTransactionsRulesGenerator)
-
-
-class BoostKeyTransactionsBiasV2(Bias):
-    def __init__(self) -> None:
-        super().__init__(BoostKeyTransactionsDataProvider, BoostKeyTransactionsRulesGeneratorV2)

+ 0 - 58
src/sentry/dynamic_sampling/rules/biases/boost_latest_releases_bias.py

@@ -32,59 +32,6 @@ class BoostLatestReleasesDataProvider(BiasDataProvider):
 
 
 class BoostLatestReleasesRulesGenerator(BiasRulesGenerator):
-    def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
-        boosted_releases = bias_data["boostedReleases"]
-
-        return cast(
-            List[PolymorphicRule],
-            [
-                {
-                    "sampleRate": bias_data["sampleRate"],
-                    "type": "trace",
-                    "active": True,
-                    "condition": {
-                        "op": "and",
-                        "inner": [
-                            {
-                                "op": "eq",
-                                "name": "trace.release",
-                                "value": [boosted_release.version],
-                            },
-                            {
-                                "op": "eq",
-                                "name": "trace.environment",
-                                # When environment is None, it will be mapped to equivalent null in json.
-                                # When Relay receives a rule with "value": null it will match it against events without
-                                # the environment tag set.
-                                "value": boosted_release.environment,
-                            },
-                        ],
-                    },
-                    "id": bias_data["id"] + idx,
-                    "timeRange": {
-                        "start": str(
-                            datetime.utcfromtimestamp(boosted_release.timestamp).replace(tzinfo=UTC)
-                        ),
-                        "end": str(
-                            datetime.utcfromtimestamp(
-                                boosted_release.timestamp
-                                + boosted_release.platform.time_to_adoption
-                            ).replace(tzinfo=UTC)
-                        ),
-                    },
-                    # We want to use the linear decaying function for latest release boosting, with the goal
-                    # of interpolating the adoption growth with the reduction in sample rate.
-                    "decayingFn": {
-                        "type": "linear",
-                        "decayedSampleRate": bias_data["baseSampleRate"],
-                    },
-                }
-                for idx, boosted_release in enumerate(boosted_releases)
-            ],
-        )
-
-
-class BoostLatestReleasesRulesGeneratorV2(BiasRulesGenerator):
     def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
         boosted_releases = bias_data["boostedReleases"]
 
@@ -143,8 +90,3 @@ class BoostLatestReleasesRulesGeneratorV2(BiasRulesGenerator):
 class BoostLatestReleasesBias(Bias):
     def __init__(self) -> None:
         super().__init__(BoostLatestReleasesDataProvider, BoostLatestReleasesRulesGenerator)
-
-
-class BoostLatestReleasesBiasV2(Bias):
-    def __init__(self) -> None:
-        super().__init__(BoostLatestReleasesDataProvider, BoostLatestReleasesRulesGeneratorV2)

+ 0 - 28
src/sentry/dynamic_sampling/rules/biases/ignore_health_checks_bias.py

@@ -37,29 +37,6 @@ class IgnoreHealthChecksDataProvider(BiasDataProvider):
 
 
 class IgnoreHealthChecksRulesGenerator(BiasRulesGenerator):
-    def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
-        return [
-            {
-                "sampleRate": bias_data["sampleRate"],
-                "type": "transaction",
-                "condition": {
-                    "op": "or",
-                    "inner": [
-                        {
-                            "op": "glob",
-                            "name": "event.transaction",
-                            "value": bias_data["healthCheckGlobs"],
-                            "options": {"ignoreCase": True},
-                        }
-                    ],
-                },
-                "active": True,
-                "id": bias_data["id"],
-            }
-        ]
-
-
-class IgnoreHealthChecksRulesGeneratorV2(BiasRulesGenerator):
     def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
         return [
             {
@@ -85,8 +62,3 @@ class IgnoreHealthChecksRulesGeneratorV2(BiasRulesGenerator):
 class IgnoreHealthChecksBias(Bias):
     def __init__(self) -> None:
         super().__init__(IgnoreHealthChecksDataProvider, IgnoreHealthChecksRulesGenerator)
-
-
-class IgnoreHealthChecksBiasV2(Bias):
-    def __init__(self) -> None:
-        super().__init__(IgnoreHealthChecksDataProvider, IgnoreHealthChecksRulesGeneratorV2)

+ 0 - 21
src/sentry/dynamic_sampling/rules/biases/uniform_bias.py

@@ -19,22 +19,6 @@ class UniformDataProvider(BiasDataProvider):
 
 
 class UniformRulesGenerator(BiasRulesGenerator):
-    def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
-        return [
-            {
-                "sampleRate": bias_data["sampleRate"],
-                "type": "trace",
-                "active": True,
-                "condition": {
-                    "op": "and",
-                    "inner": [],
-                },
-                "id": bias_data["id"],
-            }
-        ]
-
-
-class UniformRulesGeneratorV2(BiasRulesGenerator):
     def _generate_bias_rules(self, bias_data: BiasData) -> List[PolymorphicRule]:
         return [
             {
@@ -56,8 +40,3 @@ class UniformRulesGeneratorV2(BiasRulesGenerator):
 class UniformBias(Bias):
     def __init__(self) -> None:
         super().__init__(UniformDataProvider, UniformRulesGenerator)
-
-
-class UniformBiasV2(Bias):
-    def __init__(self) -> None:
-        super().__init__(UniformDataProvider, UniformRulesGeneratorV2)

+ 4 - 34
src/sentry/dynamic_sampling/rules/combine.py

@@ -1,35 +1,18 @@
-from sentry.dynamic_sampling.rules.biases.boost_environments_bias import (
-    BoostEnvironmentsBias,
-    BoostEnvironmentsBiasV2,
-)
+from sentry.dynamic_sampling.rules.biases.boost_environments_bias import BoostEnvironmentsBias
 from sentry.dynamic_sampling.rules.biases.boost_key_transactions_bias import (
     BoostKeyTransactionsBias,
-    BoostKeyTransactionsBiasV2,
-)
-from sentry.dynamic_sampling.rules.biases.boost_latest_releases_bias import (
-    BoostLatestReleasesBias,
-    BoostLatestReleasesBiasV2,
-)
-from sentry.dynamic_sampling.rules.biases.ignore_health_checks_bias import (
-    IgnoreHealthChecksBias,
-    IgnoreHealthChecksBiasV2,
 )
-from sentry.dynamic_sampling.rules.biases.uniform_bias import UniformBias, UniformBiasV2
+from sentry.dynamic_sampling.rules.biases.boost_latest_releases_bias import BoostLatestReleasesBias
+from sentry.dynamic_sampling.rules.biases.ignore_health_checks_bias import IgnoreHealthChecksBias
+from sentry.dynamic_sampling.rules.biases.uniform_bias import UniformBias
 from sentry.dynamic_sampling.rules.combinators.base import BiasesCombinator
 from sentry.dynamic_sampling.rules.combinators.ordered_combinator import OrderedBiasesCombinator
 from sentry.dynamic_sampling.rules.utils import RuleType
 
 
 def get_relay_biases_combinator() -> BiasesCombinator:
-    # The default combinator is the ordered combinator, which will keep the insertion order of the rules.
     default_combinator = OrderedBiasesCombinator()
 
-    # The combination depends on the default_combinator used but in case of the ordered combinator the first combined
-    # rule will be the first rule in the output (e.g., UNIFORM_RULE will be the last).
-    #
-    # The ordering is very important, especially because relay performs matching following a FIFO matching algorithm.
-    #
-    # If you need to add any new bias, add it here after having created all the necessary classes.
     default_combinator.add(RuleType.BOOST_KEY_TRANSACTIONS_RULE, BoostKeyTransactionsBias())
     default_combinator.add(RuleType.IGNORE_HEALTH_CHECKS_RULE, IgnoreHealthChecksBias())
 
@@ -38,16 +21,3 @@ def get_relay_biases_combinator() -> BiasesCombinator:
     default_combinator.add(RuleType.UNIFORM_RULE, UniformBias())
 
     return default_combinator
-
-
-def get_relay_biases_combinator_v2() -> BiasesCombinator:
-    default_combinator = OrderedBiasesCombinator()
-
-    default_combinator.add(RuleType.BOOST_KEY_TRANSACTIONS_RULE, BoostKeyTransactionsBiasV2())
-    default_combinator.add(RuleType.IGNORE_HEALTH_CHECKS_RULE, IgnoreHealthChecksBiasV2())
-
-    default_combinator.add(RuleType.BOOST_ENVIRONMENTS_RULE, BoostEnvironmentsBiasV2())
-    default_combinator.add(RuleType.BOOST_LATEST_RELEASES_RULE, BoostLatestReleasesBiasV2())
-    default_combinator.add(RuleType.UNIFORM_RULE, UniformBiasV2())
-
-    return default_combinator

+ 2 - 3
src/sentry/dynamic_sampling/rules/logging.py

@@ -4,8 +4,7 @@ from typing import Dict, List, Union
 import sentry_sdk
 
 from sentry.dynamic_sampling.rules.utils import (
-    DecayingFnV1,
-    DecayingFnV2,
+    DecayingFn,
     PolymorphicRule,
     RuleType,
     get_rule_hash,
@@ -99,7 +98,7 @@ def _format_rules(
 
 def _extract_info_from_rule(
     rule_type: RuleType, rule: PolymorphicRule
-) -> Dict[str, Union[DecayingFnV1, DecayingFnV2, List[str], str, None]]:
+) -> Dict[str, Union[DecayingFn, List[str], str, None]]:
     if rule_type == RuleType.BOOST_ENVIRONMENTS_RULE:
         return {"environments": rule["condition"]["inner"][0]["value"]}
     elif rule_type == RuleType.BOOST_LATEST_RELEASES_RULE:

+ 9 - 33
src/sentry/dynamic_sampling/rules/utils.py

@@ -74,46 +74,29 @@ class Condition(TypedDict):
     inner: List[Inner]
 
 
-class BaseRule(TypedDict):
+class Rule(TypedDict):
+    samplingValue: SamplingValue
     type: str
     active: bool
     condition: Condition
     id: int
 
 
-class RuleV1(BaseRule):
-    sampleRate: float
-
-
-class RuleV2(BaseRule):
-    samplingValue: SamplingValue
-
-
-class DecayingFnV1(TypedDict):
-    type: str
-    decayedSampleRate: Optional[str]
-
-
-class DecayingRuleV1(RuleV1):
-    timeRange: TimeRange
-    decayingFn: DecayingFnV1
-
-
-class DecayingFnV2(TypedDict):
+class DecayingFn(TypedDict):
     type: str
     decayedValue: Optional[str]
 
 
-class DecayingRuleV2(RuleV2):
+class DecayingRule(Rule):
     timeRange: TimeRange
-    decayingFn: DecayingFnV2
+    decayingFn: DecayingFn
 
 
 # Type defining the all the possible rules types that can exist.
-PolymorphicRule = Union[RuleV1, RuleV2, DecayingRuleV1, DecayingRuleV2]
+PolymorphicRule = Union[Rule, DecayingRule]
 
 
-def get_rule_type(rule: BaseRule) -> Optional[RuleType]:
+def get_rule_type(rule: Rule) -> Optional[RuleType]:
     # Edge case handled naively in which we check if the ID is within the possible bounds. This is done because the
     # latest release rules have ids from 1500 to 1500 + (limit - 1). For example if the limit is 2, we will only have
     # ids: 1500, 1501.
@@ -145,15 +128,8 @@ def get_rule_hash(rule: PolymorphicRule) -> int:
 
 
 def get_sampling_value(rule: PolymorphicRule) -> Optional[Tuple[str, float]]:
-    # Gets the sampling value from the rule, based on the type.
-    if "samplingValue" in rule:
-        sampling_value = rule["samplingValue"]  # type:ignore
-        return sampling_value["type"], float(sampling_value["value"])
-    # This should be removed once V1 is faded out.
-    elif "sampleRate" in rule:
-        return "sampleRate", rule["sampleRate"]  # type:ignore
-
-    return None
+    sampling = rule["samplingValue"]
+    return sampling["type"], float(sampling["value"])
 
 
 def _deep_sorted(value: Union[Any, Dict[Any, Any]]) -> Union[Any, Dict[Any, Any]]:

Some files were not shown because too many files changed in this diff