Skip to content
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
vlasy merged 1 commit intomasterfrom
fix/oom-large-log
Nov 29, 2021
Merged

⚡️ Update transform stream to parse only on pretty#58
vlasy merged 1 commit intomasterfrom
fix/oom-large-log

Conversation

@smolijar
Copy link
Copy Markdown
Contributor

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:

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.

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.
@smolijar smolijar requested a review from vlasy November 25, 2021 07:40
Copy link
Copy Markdown
Contributor

@vlasy vlasy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. 👍

@vlasy vlasy merged commit 3c1830b into master Nov 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants