Skip to content

ref(grouping): Cache enhancements in split form #92000

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

Merged
merged 6 commits into from
May 22, 2025
Merged
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
35 changes: 32 additions & 3 deletions src/sentry/grouping/enhancer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
]
LATEST_VERSION = VERSIONS[-1]

# A delimiter to insert between rulesets in the base64 represenation of enhancements (by spec,
# base64 strings never contain '#')
BASE64_ENHANCEMENTS_DELIMITER = b"#"

VALID_PROFILING_MATCHER_PREFIXES = (
"stack.abs_path",
"path", # stack.abs_path alias
Expand Down Expand Up @@ -807,7 +811,19 @@ def _get_base64_bytes_from_rules(self, rules: list[EnhancementRule]) -> bytes:
@cached_property
def base64_string(self) -> str:
"""A base64 string representation of the enhancements object"""
base64_bytes = self._get_base64_bytes_from_rules(self.rules)
rulesets = [self.rules]

if self.run_split_enhancements:
rulesets.extend([self.classifier_rules, self.contributes_rules])

# Create a base64 bytestring for each set of rules, and join them with a character we know
# can never appear in base64. We do it this way rather than combining all three sets of
# rules into a single bytestring because the rust enhancer only knows how to deal with
# bytestrings encoding data of the form `[version, bases, rules]` (not
# `[version, bases, rules, rules, rules]`).
base64_bytes = BASE64_ENHANCEMENTS_DELIMITER.join(
self._get_base64_bytes_from_rules(ruleset) for ruleset in rulesets
)
base64_str = base64_bytes.decode("ascii")
return base64_str

Expand Down Expand Up @@ -845,13 +861,25 @@ def from_base64_string(
with metrics.timer("grouping.enhancements.creation") as metrics_timer_tags:
metrics_timer_tags.update({"source": "base64_string", "referrer": referrer})

bytes_str = (
raw_bytes_str = (
base64_string.encode("ascii", "ignore")
if isinstance(base64_string, str)
else base64_string
)

unsplit_config = cls._get_config_from_base64_bytes(bytes_str)
# Split the string to get encoded data for each set of rules: unsplit rules (i.e., rules
# the way they're stored in project config), classifier rules, and contributes rules.
# Older base64 strings - such as those stored in events created before rule-splitting was
# introduced - will only have one part and thus will end up unchanged. (The delimiter is
# chosen specifically to be a character which can't appear in base64.)
bytes_strs = raw_bytes_str.split(BASE64_ENHANCEMENTS_DELIMITER)
configs = [cls._get_config_from_base64_bytes(bytes_str) for bytes_str in bytes_strs]

unsplit_config = configs[0]
split_configs = None

if len(configs) == 3:
split_configs = (configs[1], configs[2])

version = unsplit_config.version
bases = unsplit_config.bases
Expand All @@ -861,6 +889,7 @@ def from_base64_string(
return cls(
rules=unsplit_config.rules,
rust_enhancements=unsplit_config.rust_enhancements,
split_enhancement_configs=split_configs,
version=version,
bases=bases,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/sentry/grouping/test_enhancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ def test_loads_split_enhancements_from_base64_string(self, split_rules_spy: Magi
== "<EnhancementRule function:playFetch +group>"
)
assert strategy_config.enhancements.id is None
# Currently we re-split the rules when translating from base64 back to object
assert split_rules_spy.call_count == 2
# Rules didn't have to be split again because they were cached in split form
assert split_rules_spy.call_count == 1

def test_uses_default_enhancements_when_loading_string_with_invalid_version(self):
enhancements = Enhancements.from_rules_text("function:playFetch +app")
Expand Down
Loading