-
Notifications
You must be signed in to change notification settings - Fork 1
Trim event title to a maximum length in grouper #574
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
11 commits
Select commit
Hold shift + click to select a range
133585d
Trim event title to a maximum length in grouper
Kuchizu b9fd948
refactor(grouper): move title trimming into DataFilter
Kuchizu 289bf60
Trim grouper event titles
Kuchizu 1157fd4
Add Sanitizer for event payload fields and drop per-event handle log
Kuchizu 1443d74
Keep sanitizer placeholder for context/addons wrapped in object
Kuchizu 9bc5c66
Update string context test to expect placeholder object
Kuchizu 690484d
Merge branch 'master' into feat/grouper-limit-event-title
Kuchizu dddb544
Document string-context sanitize handling
Kuchizu 95e8497
Merge branch 'master' into feat/grouper-limit-event-title
Kuchizu a29a0cc
Cut off extra object keys instead of replacing object with placeholder
Kuchizu c5bc39c
Merge branch 'feat/grouper-limit-event-title' of https://github.com/c…
Kuchizu 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { rightTrim } from './string'; | ||
|
|
||
| /** | ||
| * Maximum string length before appending ellipsis | ||
| */ | ||
| const MAX_STRING_LENGTH = 200; | ||
|
|
||
| /** | ||
| * Maximum number of object keys to keep, the rest are reported via META_FIELD | ||
| */ | ||
| const MAX_OBJECT_KEYS_COUNT = 20; | ||
|
|
||
| /** | ||
| * Key added to an object to report how many of its keys were skipped | ||
| */ | ||
| const META_FIELD = '__meta'; | ||
|
|
||
| /** | ||
| * Maximum depth of sanitized objects | ||
| */ | ||
| const MAX_DEPTH = 5; | ||
|
|
||
| /** | ||
| * Maximum length of sanitized arrays | ||
| */ | ||
| const MAX_ARRAY_LENGTH = 10; | ||
|
|
||
| /** | ||
| * Checks that the value is a plain object | ||
| * | ||
| * @param target - value to check | ||
| */ | ||
| function isPlainObject(target: unknown): target is Record<string, unknown> { | ||
| return Object.prototype.toString.call(target) === '[object Object]'; | ||
| } | ||
|
|
||
| /** | ||
| * Values that can be tracked by WeakSet to detect circular references | ||
| */ | ||
| type ObjectLike = Record<string, unknown> | unknown[]; | ||
|
|
||
| /** | ||
| * Prepares event data for storing: trims long strings, slices long arrays, | ||
| * cuts off extra object keys and replaces too deep objects and circular | ||
| * references with placeholders. | ||
| */ | ||
| export class Sanitizer { | ||
| /** | ||
| * Apply sanitizing for array/object/primitives | ||
| * | ||
| * @param data - any value to sanitize | ||
| * @param depth - current depth of recursion | ||
| * @param seen - already visited objects | ||
| */ | ||
| public static sanitize(data: unknown, depth = 0, seen = new WeakSet<ObjectLike>()): unknown { | ||
| if (data !== null && typeof data === 'object') { | ||
| if (seen.has(data as ObjectLike)) { | ||
| return '<circular>'; | ||
| } | ||
| seen.add(data as ObjectLike); | ||
| } | ||
|
|
||
| if (Array.isArray(data)) { | ||
| return Sanitizer.sanitizeArray(data, depth + 1, seen); | ||
| } | ||
|
|
||
| if (isPlainObject(data)) { | ||
| return Sanitizer.sanitizeObject(data, depth + 1, seen); | ||
| } | ||
|
|
||
| if (typeof data === 'string') { | ||
| return rightTrim(data, MAX_STRING_LENGTH); | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| /** | ||
| * Slices array to the maximum length and sanitizes each element | ||
| * | ||
| * @param arr - array to sanitize | ||
| * @param depth - current depth of recursion | ||
| * @param seen - already visited objects | ||
| */ | ||
| private static sanitizeArray(arr: unknown[], depth: number, seen: WeakSet<ObjectLike>): unknown[] { | ||
| const length = arr.length; | ||
|
|
||
| if (length > MAX_ARRAY_LENGTH) { | ||
| arr = arr.slice(0, MAX_ARRAY_LENGTH); | ||
| arr.push(`<${length - MAX_ARRAY_LENGTH} more items...>`); | ||
| } | ||
|
|
||
| return arr.map((item) => { | ||
| return Sanitizer.sanitize(item, depth, seen); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Sanitizes object values recursively | ||
| * | ||
| * @param data - object to sanitize | ||
| * @param depth - current depth of recursion | ||
| * @param seen - already visited objects | ||
| */ | ||
| private static sanitizeObject( | ||
| data: Record<string, unknown>, | ||
| depth: number, | ||
| seen: WeakSet<ObjectLike> | ||
| ): Record<string, unknown> | '<deep object>' { | ||
| if (depth > MAX_DEPTH) { | ||
| return '<deep object>'; | ||
| } | ||
|
|
||
| const keys = Object.keys(data); | ||
| const result: Record<string, unknown> = {}; | ||
|
|
||
| for (const key of keys.slice(0, MAX_OBJECT_KEYS_COUNT)) { | ||
| result[key] = Sanitizer.sanitize(data[key], depth, seen); | ||
| } | ||
|
|
||
| const skippedKeysCount = keys.length - MAX_OBJECT_KEYS_COUNT; | ||
|
|
||
| if (skippedKeysCount > 0) { | ||
| result[META_FIELD] = `${skippedKeysCount} more key(s) skipped`; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
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
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.