-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(segment-enrichment): Introduce segment name normalization #103739
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67e5195
feat(span-enrichment): introduce segment name normalization
mjq 008d2c9
fix attribute use
mjq fce2d21
work around duplicate test file name
mjq 88c229b
move remark serialization into class
mjq ed6acf0
wrap normalization in safe_execute
mjq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
115 changes: 115 additions & 0 deletions
115
src/sentry/ingest/transaction_clusterer/normalization.py
This file contains hidden or 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,115 @@ | ||
| import re | ||
| from dataclasses import dataclass | ||
|
|
||
| import orjson | ||
| from sentry_conventions.attributes import ATTRIBUTE_NAMES | ||
|
|
||
| from sentry.spans.consumers.process_segments.types import CompatibleSpan, attribute_value | ||
|
|
||
| # Ported from Relay: | ||
| # https://github.com/getsentry/relay/blob/aad4b6099d12422e88dd5df49abae11247efdd99/relay-event-normalization/src/regexes.rs#L9 | ||
| TRANSACTION_NAME_NORMALIZER_REGEX = re.compile( | ||
| r"""(?x) | ||
| (?P<uuid>[^/\\]* | ||
| (?a:\b)[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(?a:\b) | ||
| [^/\\]*) | | ||
| (?P<sha1>[^/\\]* | ||
| (?a:\b)[0-9a-fA-F]{40}(?a:\b) | ||
| [^/\\]*) | | ||
| (?P<md5>[^/\\]* | ||
| (?a:\b)[0-9a-fA-F]{32}(?a:\b) | ||
| [^/\\]*) | | ||
| (?P<date>[^/\\]* | ||
| (?: | ||
| (?:[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\.[0-9]+([+-][0-2][0-9]:[0-5][0-9]|Z))| | ||
| (?:[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]([+-][0-2][0-9]:[0-5][0-9]|Z))| | ||
| (?:[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]([+-][0-2][0-9]:[0-5][0-9]|Z)) | ||
| ) | | ||
| (?: | ||
| (?a:\b)(?:(Sun|Mon|Tue|Wed|Thu|Fri|Sat)(?a:\s)+)? | ||
| (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(?a:\s)+ | ||
| (?:[0-9]{1,2})(?a:\s)+ | ||
| (?:[0-9]{2}:[0-9]{2}:[0-9]{2})(?a:\s)+ | ||
| [0-9]{4} | ||
| ) | | ||
| (?: | ||
| (?a:\b)(?:(Sun|Mon|Tue|Wed|Thu|Fri|Sat),(?a:\s)+)? | ||
| (?:0[1-9]|[1-2]?[0-9]|3[01])(?a:\s)+ | ||
| (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(?a:\s)+ | ||
| (?:19[0-9]{2}|[2-9][0-9]{3})(?a:\s)+ | ||
| (?:2[0-3]|[0-1][0-9]):([0-5][0-9]) | ||
| (?::(60|[0-5][0-9]))?(?a:\s)+ | ||
| (?:[-\+][0-9]{2}[0-5][0-9]|(?:UT|GMT|(?:E|C|M|P)(?:ST|DT)|[A-IK-Z])) | ||
| ) | ||
| [^/\\]*) | | ||
| (?P<hex>[^/\\]* | ||
| (?a:\b)0[xX][0-9a-fA-F]+(?a:\b) | ||
| [^/\\]*) | | ||
| (?:^|[/\\]) | ||
| (?P<int> | ||
| (:?[^%/\\]|%[0-9a-fA-F]{2})*[0-9]{2,} | ||
| [^/\\]*)""", | ||
| re.UNICODE, | ||
| ) | ||
|
|
||
|
|
||
| def normalize_segment_name(segment_span: CompatibleSpan): | ||
| segment_name = attribute_value( | ||
| segment_span, ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME | ||
| ) or segment_span.get("name") | ||
| if segment_name: | ||
| _scrub_identifiers(segment_span, segment_name) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Remark: | ||
| ty: str | ||
| rule_id: str | ||
| range: tuple[int, int] | ||
|
|
||
| def serialize(self) -> list: | ||
| return [self.rule_id, self.ty, self.range[0], self.range[1]] | ||
|
|
||
|
|
||
| # Ported from Relay: | ||
| # https://github.com/getsentry/relay/blob/aad4b6099d12422e88dd5df49abae11247efdd99/relay-event-normalization/src/transactions/processor.rs#L350 | ||
| def _scrub_identifiers(segment_span: CompatibleSpan, segment_name: str): | ||
| matches = TRANSACTION_NAME_NORMALIZER_REGEX.finditer(segment_name) | ||
| remarks = [] | ||
| for m in matches: | ||
| remarks.extend( | ||
| [ | ||
| Remark(ty="s", rule_id=group_name, range=(m.start(group_name), m.end(group_name))) | ||
| for group_name in m.groupdict().keys() | ||
| if m.start(group_name) > -1 | ||
| ] | ||
| ) | ||
| if len(remarks) == 0: | ||
| return | ||
|
|
||
| remarks.sort(key=lambda remark: remark.range[1]) | ||
| str_parts: list[str] = [] | ||
| last_end = 0 | ||
| for remark in remarks: | ||
| start, end = remark.range | ||
| str_parts.append(segment_name[last_end:start]) | ||
| str_parts.append("*") | ||
| last_end = end | ||
| str_parts.append(segment_name[last_end:]) | ||
| normalized_segment_name = "".join(str_parts) | ||
|
|
||
| segment_span["name"] = normalized_segment_name | ||
| attributes = segment_span.get("attributes") or {} | ||
| attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] = { | ||
| "type": "string", | ||
| "value": normalized_segment_name, | ||
| } | ||
| attributes[ATTRIBUTE_NAMES.SENTRY_SPAN_SOURCE] = { | ||
| "type": "string", | ||
| "value": "sanitized", | ||
| } | ||
| attributes[f"sentry._meta.fields.attributes.{ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME}"] = { | ||
| "type": "string", | ||
| "value": orjson.dumps({"meta": {"": {"rem": [r.serialize() for r in remarks]}}}).decode(), | ||
| } | ||
| segment_span["attributes"] = attributes |
This file contains hidden or 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
Empty file.
89 changes: 89 additions & 0 deletions
89
tests/sentry/ingest/transaction_clusterer/test_normalization.py
This file contains hidden or 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,89 @@ | ||
| import orjson | ||
| from sentry_conventions.attributes import ATTRIBUTE_NAMES | ||
|
|
||
| from sentry.ingest.transaction_clusterer.normalization import normalize_segment_name | ||
| from sentry.spans.consumers.process_segments.types import CompatibleSpan | ||
|
|
||
|
|
||
| def _segment_span(**kwargs) -> CompatibleSpan: | ||
| segment_span: CompatibleSpan = { | ||
| "organization_id": 1, | ||
| "project_id": 1, | ||
| "trace_id": "94576097f3a64b68b85a59c7d4e3ee2a", | ||
| "span_id": "a49b42af9fb69da0", | ||
| "start_timestamp": 1707953018.865, | ||
| "end_timestamp": 1707953018.972, | ||
| "retention_days": 90, | ||
| "received": 1707953019.044972, | ||
| "status": "ok", | ||
| "exclusive_time": 0.1, | ||
| "op": "default", | ||
| "sentry_tags": {}, | ||
| "name": "default", | ||
| } | ||
| segment_span.update(**kwargs) # type:ignore[call-arg] | ||
| return segment_span | ||
|
|
||
|
|
||
| # Ported from Relay: | ||
| # https://github.com/getsentry/relay/blob/aad4b6099d12422e88dd5df49abae11247efdd99/relay-event-normalization/src/transactions/processor.rs#L789 | ||
| def test_identifiers_scrubbed(): | ||
| segment_span = _segment_span(name="/foo/2fd4e1c67a2d28fced849ee1bb76e7391b93eb12/user/123/0") | ||
|
|
||
| normalize_segment_name(segment_span) | ||
|
|
||
| assert segment_span["name"] == "/foo/*/user/*/0" | ||
| attributes = segment_span.get("attributes") or {} | ||
| assert attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == { | ||
| "type": "string", | ||
| "value": "/foo/*/user/*/0", | ||
| } | ||
| assert attributes[ATTRIBUTE_NAMES.SENTRY_SPAN_SOURCE] == { | ||
| "type": "string", | ||
| "value": "sanitized", | ||
| } | ||
| assert attributes[f"sentry._meta.fields.attributes.{ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME}"] == { | ||
| "type": "string", | ||
| "value": orjson.dumps( | ||
| {"meta": {"": {"rem": [["int", "s", 5, 45], ["int", "s", 51, 54]]}}} | ||
| ).decode(), | ||
| } | ||
|
|
||
|
|
||
| def test_name_attribute_takes_precedence_over_name(): | ||
| segment_span = _segment_span( | ||
| name="/foo/2fd4e1c67a2d28fced849ee1bb76e7391b93eb12/user/123/0", | ||
| attributes={ | ||
| ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME: { | ||
| "type": "string", | ||
| "value": "/bar/2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| normalize_segment_name(segment_span) | ||
|
|
||
| assert segment_span["name"] == "/bar/*" | ||
| attributes = segment_span.get("attributes") or {} | ||
| assert attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == { | ||
| "type": "string", | ||
| "value": "/bar/*", | ||
| } | ||
| assert attributes[ATTRIBUTE_NAMES.SENTRY_SPAN_SOURCE] == { | ||
| "type": "string", | ||
| "value": "sanitized", | ||
| } | ||
| assert attributes[f"sentry._meta.fields.attributes.{ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME}"] == { | ||
| "type": "string", | ||
| "value": orjson.dumps({"meta": {"": {"rem": [["int", "s", 5, 45]]}}}).decode(), | ||
| } | ||
|
|
||
|
|
||
| def test_no_meta_changes_if_no_name_changes(): | ||
| segment_span = _segment_span(name="/foo") | ||
|
|
||
| normalize_segment_name(segment_span) | ||
|
|
||
| assert segment_span["name"] == "/foo" | ||
| attributes = segment_span.get("attributes") or {} | ||
| assert len(attributes) == 0 |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.