Skip to content

Commit 0e6cf56

Browse files
committed
Alternative approach to prevent long-log truncation: explicitly setting 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 <[email protected]>
1 parent 4746d57 commit 0e6cf56

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/collector/serverless.js

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ class ServerlessCollector {
246246
]) + '\n'
247247

248248
if (sync) {
249+
const s = process.stdout
250+
s._handle && s._handle.setBlocking && s._handle.setBlocking(true)
249251
fs.writeSync(process.stdout.fd, serializedPayload)
250252
} else {
251253
process.stdout.write(serializedPayload)

test/unit/collector/serverless.test.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ describe('ServerlessCollector API', () => {
153153
describe('#flushPayload', () => {
154154
let logStub = null
155155

156-
before(() => {
157-
logStub = sinon.stub(process.stdout, 'write').callsFake(() => {})
156+
beforeEach(() => {
157+
logStub = sinon.stub(process.stdout, 'write').callsFake(() => {
158+
})
158159
})
159160

160-
after(() => {
161+
afterEach(() => {
161162
logStub.restore()
162163
})
163164

@@ -172,5 +173,23 @@ describe('ServerlessCollector API', () => {
172173
done()
173174
})
174175
})
176+
it('handles very large payload and writes formatted to stdout', done => {
177+
api.payload = {type: 'test payload'}
178+
for (let i = 0; i < 4096; i++) {
179+
api.payload[`customMetric${i}`] = Math.floor(Math.random() * 100000)
180+
}
181+
182+
api.flushPayload(() => {
183+
const logPayload = JSON.parse(logStub.getCall(0).args[0])
184+
const buf = Buffer.from(logPayload[2], 'base64')
185+
zlib.gunzip(buf, (err, unpack) => {
186+
expect(err).to.be.null
187+
const payload = JSON.parse(unpack)
188+
expect(payload.data).to.be.ok
189+
expect(Object.keys(payload.data)).to.have.lengthOf.above(4000)
190+
done()
191+
})
192+
})
193+
})
175194
})
176195
})

0 commit comments

Comments
 (0)