-
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
Open
Kuchizu
wants to merge
5
commits into
master
Choose a base branch
from
feat/grouper-limit-event-title
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 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 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,122 @@ | ||
| import { rightTrim } from './string'; | ||
|
|
||
| /** | ||
| * Maximum string length before appending ellipsis | ||
| */ | ||
| const MAX_STRING_LENGTH = 200; | ||
|
|
||
| /** | ||
| * Objects with more keys are represented as "<big object>" | ||
| */ | ||
| const MAX_OBJECT_KEYS_COUNT = 20; | ||
|
|
||
| /** | ||
| * 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, | ||
| * replaces too deep/big 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>' | '<big object>' { | ||
| if (depth > MAX_DEPTH) { | ||
| return '<deep object>'; | ||
| } | ||
|
|
||
| if (Object.keys(data).length > MAX_OBJECT_KEYS_COUNT) { | ||
| return '<big object>'; | ||
| } | ||
|
|
||
| const result: Record<string, unknown> = {}; | ||
|
|
||
| for (const key in data) { | ||
| if (Object.prototype.hasOwnProperty.call(data, key)) { | ||
| result[key] = Sanitizer.sanitize(data[key], depth, seen); | ||
| } | ||
| } | ||
|
|
||
| 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.