Skip to content

Commit

Permalink
Alternative approach to prevent long-log truncation: explicitly setti…
Browse files Browse the repository at this point in the history
…ng process.stdout to blocking. See nodejs/node#6456 for a discussion of the issue and a version of the fix used here.

Signed-off-by: mrickard <mrickard@newrelic.com>
  • Loading branch information
mrickard committed Dec 3, 2019
1 parent 4746d57 commit 0e6cf56
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/collector/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ class ServerlessCollector {
]) + '\n'

if (sync) {
const s = process.stdout
s._handle && s._handle.setBlocking && s._handle.setBlocking(true)
fs.writeSync(process.stdout.fd, serializedPayload)
} else {
process.stdout.write(serializedPayload)
Expand Down
25 changes: 22 additions & 3 deletions test/unit/collector/serverless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,12 @@ describe('ServerlessCollector API', () => {
describe('#flushPayload', () => {
let logStub = null

before(() => {
logStub = sinon.stub(process.stdout, 'write').callsFake(() => {})
beforeEach(() => {
logStub = sinon.stub(process.stdout, 'write').callsFake(() => {
})
})

after(() => {
afterEach(() => {
logStub.restore()
})

Expand All @@ -172,5 +173,23 @@ describe('ServerlessCollector API', () => {
done()
})
})
it('handles very large payload and writes formatted to stdout', done => {
api.payload = {type: 'test payload'}
for (let i = 0; i < 4096; i++) {
api.payload[`customMetric${i}`] = Math.floor(Math.random() * 100000)
}

api.flushPayload(() => {
const logPayload = JSON.parse(logStub.getCall(0).args[0])
const buf = Buffer.from(logPayload[2], 'base64')
zlib.gunzip(buf, (err, unpack) => {
expect(err).to.be.null
const payload = JSON.parse(unpack)
expect(payload.data).to.be.ok
expect(Object.keys(payload.data)).to.have.lengthOf.above(4000)
done()
})
})
})
})
})

0 comments on commit 0e6cf56

Please sign in to comment.