-
Notifications
You must be signed in to change notification settings - Fork 452
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
fix(log): handle large payload #2740
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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 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,60 @@ | ||
import safeStringify from 'fast-safe-stringify'; | ||
import truncateJsonPkg from 'truncate-json'; | ||
|
||
export const MAX_LOG_PAYLOAD = 99_000; // in bytes | ||
|
||
/** | ||
* Safely stringify an object (mostly handle circular ref and known problematic keys) | ||
*/ | ||
export function stringifyObject(value: any): string { | ||
return safeStringify.stableStringify( | ||
value, | ||
(key, value) => { | ||
if (value instanceof Buffer || key === '_sessionCache') { | ||
return '[Buffer]'; | ||
} | ||
if (key === 'Authorization') { | ||
return '[Redacted]'; | ||
} | ||
return value; | ||
}, | ||
undefined, | ||
{ depthLimit: 10, edgesLimit: 20 } | ||
); | ||
} | ||
|
||
/** | ||
* Stringify and truncate unknown value | ||
*/ | ||
export function stringifyAndTruncateValue(value: any, maxSize: number = MAX_LOG_PAYLOAD): string { | ||
if (value === null) { | ||
return 'null'; | ||
} | ||
if (value === undefined) { | ||
return 'undefined'; | ||
} | ||
|
||
let msg = typeof value === 'string' ? value : truncateJsonString(stringifyObject(value), maxSize); | ||
|
||
if (msg && msg.length > maxSize) { | ||
msg = `${msg.substring(0, maxSize)}... (truncated)`; | ||
} | ||
|
||
return msg; | ||
} | ||
|
||
/** | ||
* Truncate a JSON | ||
* Will entirely remove properties that are too big | ||
*/ | ||
export function truncateJson(value: Record<string, any>, maxSize: number = MAX_LOG_PAYLOAD) { | ||
return JSON.parse(truncateJsonPkg(JSON.stringify(value), maxSize).jsonString); | ||
} | ||
|
||
/** | ||
* Truncate a JSON as string | ||
* Will entirely remove properties that are too big | ||
*/ | ||
export function truncateJsonString(value: string, maxSize: number = MAX_LOG_PAYLOAD): string { | ||
return truncateJsonPkg(value, maxSize).jsonString; | ||
} | ||
24 changes: 12 additions & 12 deletions
24
packages/shared/lib/sdk/utils.unit.test.ts → packages/utils/lib/json.unit.test.ts
This file contains 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 |
---|---|---|
@@ -1,52 +1,52 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { stringifyAndTruncateLog } from './utils.js'; | ||
import { stringifyAndTruncateValue } from './json.js'; | ||
|
||
describe('stringifyAndTruncateLog', () => { | ||
describe('stringifyAndTruncateValue', () => { | ||
it('should not break a small string', () => { | ||
const str = stringifyAndTruncateLog('hello'); | ||
const str = stringifyAndTruncateValue('hello'); | ||
expect(str).toStrictEqual('hello'); | ||
}); | ||
|
||
it('should limit a string', () => { | ||
const str = stringifyAndTruncateLog('hello', 2); | ||
const str = stringifyAndTruncateValue('hello', 2); | ||
expect(str).toStrictEqual('he... (truncated)'); | ||
}); | ||
|
||
it('should limit an object', () => { | ||
const str = stringifyAndTruncateLog({ foo: 'bar' }, 10); | ||
expect(str).toStrictEqual('{"foo":"ba... (truncated)'); | ||
const str = stringifyAndTruncateValue({ foo: 'bar' }, 10); | ||
expect(str).toStrictEqual('{}'); | ||
}); | ||
|
||
it('should handle undefined', () => { | ||
const str = stringifyAndTruncateLog(undefined); | ||
const str = stringifyAndTruncateValue(undefined); | ||
expect(str).toStrictEqual('undefined'); | ||
}); | ||
|
||
it('should handle null', () => { | ||
const str = stringifyAndTruncateLog(null); | ||
const str = stringifyAndTruncateValue(null); | ||
expect(str).toStrictEqual('null'); | ||
}); | ||
|
||
it('should handle object', () => { | ||
const str = stringifyAndTruncateLog({ foo: 1 }); | ||
const str = stringifyAndTruncateValue({ foo: 1 }); | ||
expect(str).toStrictEqual('{"foo":1}'); | ||
}); | ||
|
||
it('should handle array', () => { | ||
const str = stringifyAndTruncateLog([{ foo: 1 }, 2]); | ||
const str = stringifyAndTruncateValue([{ foo: 1 }, 2]); | ||
expect(str).toStrictEqual('[{"foo":1},2]'); | ||
}); | ||
|
||
it('should handle circular', () => { | ||
const obj: Record<string, any> = { foo: 'bar' }; | ||
obj['circular'] = obj; | ||
const str = stringifyAndTruncateLog(obj); | ||
const str = stringifyAndTruncateValue(obj); | ||
expect(str).toStrictEqual('{"circular":"[Circular]","foo":"bar"}'); | ||
}); | ||
|
||
it('should redac known keys', () => { | ||
const obj: Record<string, any> = { Authorization: 'super secret key' }; | ||
const str = stringifyAndTruncateLog(obj); | ||
const str = stringifyAndTruncateValue(obj); | ||
expect(str).toStrictEqual('{"Authorization":"[Redacted]"}'); | ||
}); | ||
}); |
This file contains 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this file be called something like
stringUtils.ts
instead ofjson
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I asked myself the same question: it depends on how you interpret the name of the files: whether the input is important or the output.
This one takes a value as string but others takes Record, either they all related to json (no matter the encoding) or I move truncateJsonString in string.ts
Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel strongly either side,
json
is not a bad name it is just a bit generic. Another option could belogFormatUtils.ts
or something similar because those functions are mostly used in the context of logging. Feel free to keep it as it is thoughThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have many spots where json stringification could be an issue (that's why I moved it there actually); I'll leave it as is