This repository was archived by the owner on Jun 21, 2023. It is now read-only.
⚡️ Update transform stream to parse only on pretty#58
Merged
Conversation
TLDR; Remove `JSON.parse`/`JSON.stringify` in main transform stream,
when the `pretty` option is disabled.
**Context**
Using cosmas we had several issues with OOM during logging. The issue
can be reproduced with the following snippet:
```js
const message = { text: '.'.repeat(4e6) }
mb = (s) => `${((encodeURI(s).split(/%..|./).length - 1)/2**20).toFixed(2)} MB`
// --> 3.81 MB (node index.js, crashes with the 20 MB limit as cosmas)
// console.log(mb(JSON.stringify(message)))
// OOM (node --max-old-space-size=20 index.js) (20 MB)
require('cosmas').default({}).info(message)
// OK (node --max-old-space-size=20 index.js)
require('pino').default().info(message)
```
```
"cosmas": "^3.0.7",
"pino": "^7.4.0"
```
While the pino logger logs the thing without issues, cosmas log crashes.
This is caused by `JSON.parse`/`JSON.stringify`, which has surprisingly demanding memory
requirements (20 MB insufficient for stringifying 4 MB JSON). This pair
of functions is used in the main transform stream. The only utility now
is that it allows for `pretty` option, where `inspect` is used, however
it bubbles through both functions even if you don't need it (effectively
just adding a newline to the output).
Now the parsing/stringifying is only applied when `pretty` option is used.
It is assumed that it will only be used on dev environment, where
logging large data should not be an issue regarding memory safety.
After the change, all the tests pass and the snippet no longer crashes
on OOM.
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
TLDR; Remove
JSON.parse/JSON.stringifyin main transform stream,when the
prettyoption is disabled.Context
Using cosmas we had several issues with OOM during logging. The issue
can be reproduced with the following snippet:
While the pino logger logs the thing without issues, cosmas log crashes.
This is caused by
JSON.parse/JSON.stringify, which has surprisingly demanding memoryrequirements (20 MB insufficient for stringifying 4 MB JSON). This pair
of functions is used in the main transform stream. The only utility now
is that it allows for
prettyoption, whereinspectis used, howeverit bubbles through both functions even if you don't need it (effectively
just adding a newline to the output).
Now the parsing/stringifying is only applied when
prettyoption is used.It is assumed that it will only be used on dev environment, where
logging large data should not be an issue regarding memory safety.
After the change, all the tests pass and the snippet no longer crashes
on OOM.