forked from elastic/apm-nodejs-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
561 lines (486 loc) · 17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
'use strict'
const util = require('util')
const os = require('os')
const parseUrl = require('url').parse
const zlib = require('zlib')
const Writable = require('readable-stream').Writable
const getContainerInfo = require('container-info')
const pump = require('pump')
const eos = require('end-of-stream')
const streamToBuffer = require('fast-stream-to-buffer')
const StreamChopper = require('stream-chopper')
const ndjson = require('./lib/ndjson')
const truncate = require('./lib/truncate')
const pkg = require('./package')
module.exports = Client
const flush = Symbol('flush')
const hostname = os.hostname()
const requiredOpts = [
'agentName',
'agentVersion',
'serviceName',
'userAgent'
]
const containerInfo = getContainerInfo.sync()
const node8 = process.version.indexOf('v8.') === 0
// All sockets on the agent are unreffed when they are created. This means that
// when those are the only handles left, the `beforeExit` event will be
// emitted. By listening for this we can make sure to end the requests properly
// before exiting. This way we don't keep the process running until the `time`
// timeout happens.
const clients = []
process.once('beforeExit', function () {
clients.forEach(function (client) {
if (!client) return // clients remove them selfs from the array when they end
client.end()
})
})
util.inherits(Client, Writable)
Client.encoding = Object.freeze({
METADATA: Symbol('metadata'),
TRANSACTION: Symbol('transaction'),
SPAN: Symbol('span'),
ERROR: Symbol('error'),
METRICSET: Symbol('metricset')
})
function Client (opts) {
if (!(this instanceof Client)) return new Client(opts)
this._opts = opts = normalizeOptions(opts)
Writable.call(this, opts)
const errorproxy = (err) => {
if (this.destroyed === false) this.emit('request-error', err)
}
const fail = () => {
if (this._writableState.ending === false) this.destroy()
}
this._corkTimer = null
this._received = 0 // number of events given to the client for reporting
this.sent = 0 // number of events written to the socket
this._active = false
this._onflushed = null
this._transport = null
switch (opts.serverUrl.protocol.slice(0, -1)) { // 'http:' => 'http'
case 'http': {
this._transport = require('http')
break
}
case 'https': {
this._transport = require('https')
break
}
default: {
throw new Error('Unknown protocol ' + opts.serverUrl.protocol.slice(0, -1))
}
}
this._agent = new this._transport.Agent(opts)
this._chopper = new StreamChopper({
size: opts.size,
time: opts.time,
type: StreamChopper.overflow,
transform () {
return zlib.createGzip()
}
}).on('stream', onStream(opts, this, errorproxy))
eos(this._chopper, fail)
this._index = clients.length
clients.push(this)
}
// re-ref the open socket handles
Client.prototype._ref = function () {
Object.keys(this._agent.sockets).forEach(remote => {
this._agent.sockets[remote].forEach(function (socket) {
socket.ref()
})
})
}
Client.prototype._write = function (obj, enc, cb) {
if (obj === flush) {
this._writeFlush(cb)
} else {
this._received++
this._chopper.write(this._encode(obj, enc), cb)
}
}
Client.prototype._writev = function (objs, cb) {
let offset = 0
const processBatch = () => {
let index = -1
for (let i = offset; i < objs.length; i++) {
if (objs[i].chunk === flush) {
index = i
break
}
}
if (offset === 0 && index === -1) {
// normally there's no flush object queued, so here's a shortcut that just
// skips all the complicated splitting logic
this._writevCleaned(objs, cb)
} else if (index === -1) {
// no more flush elements in the queue, just write the rest
this._writevCleaned(objs.slice(offset), cb)
} else if (index > offset) {
// there's a few items in the queue before we need to flush, let's first write those
this._writevCleaned(objs.slice(offset, index), processBatch)
offset = index
} else if (index === objs.length - 1) {
// the last item in the queue is a flush
this._writeFlush(cb)
} else {
// the next item in the queue is a flush
this._writeFlush(processBatch)
offset++
}
}
processBatch()
}
Client.prototype._writevCleaned = function (objs, cb) {
const chunk = objs.reduce((result, obj) => {
return result + this._encode(obj.chunk, obj.encoding)
}, '')
this._received += objs.length
this._chopper.write(chunk, cb)
}
Client.prototype._writeFlush = function (cb) {
if (this._active) {
this._onflushed = cb
this._chopper.chop()
} else {
this._chopper.chop(cb)
}
}
Client.prototype._maybeCork = function () {
if (!this._writableState.corked && this._opts.bufferWindowTime !== -1) {
this.cork()
if (this._corkTimer && this._corkTimer.refresh) {
// the refresh function was added in Node 10.2.0
this._corkTimer.refresh()
} else {
this._corkTimer = setTimeout(() => {
this.uncork()
}, this._opts.bufferWindowTime)
}
} else if (this._writableState.length >= this._opts.bufferWindowSize) {
this._maybeUncork()
}
}
Client.prototype._maybeUncork = function () {
if (this._writableState.corked) {
// Wait till next tick, so that the current write that triggered the call
// to `_maybeUncork` have time to be added to the queue. If we didn't do
// this, that last write would trigger a single call to `_write`.
process.nextTick(() => {
if (this.destroyed === false) this.uncork()
})
if (this._corkTimer) clearTimeout(this._corkTimer)
}
}
Client.prototype._encode = function (obj, enc) {
switch (enc) {
case Client.encoding.SPAN:
truncate.span(obj.span, this._opts)
break
case Client.encoding.TRANSACTION:
truncate.transaction(obj.transaction, this._opts)
break
case Client.encoding.METADATA:
truncate.metadata(obj.metadata, this._opts)
break
case Client.encoding.ERROR:
truncate.error(obj.error, this._opts)
break
case Client.encoding.METRICSET:
truncate.metricset(obj.metricset, this._opts)
break
}
return ndjson.serialize(obj)
}
Client.prototype.sendSpan = function (span, cb) {
this._maybeCork()
return this.write({ span }, Client.encoding.SPAN, cb)
}
Client.prototype.sendTransaction = function (transaction, cb) {
this._maybeCork()
return this.write({ transaction }, Client.encoding.TRANSACTION, cb)
}
Client.prototype.sendError = function (error, cb) {
this._maybeCork()
return this.write({ error }, Client.encoding.ERROR, cb)
}
Client.prototype.sendMetricSet = function (metricset, cb) {
this._maybeCork()
return this.write({ metricset }, Client.encoding.METRICSET, cb)
}
Client.prototype.flush = function (cb) {
this._maybeUncork()
// Write the special "flush" signal. We do this so that the order of writes
// and flushes are kept. If we where to just flush the client right here, the
// internal Writable buffer might still contain data that hasn't yet been
// given to the _write function.
return this.write(flush, cb)
}
Client.prototype._final = function (cb) {
clients[this._index] = null // remove global reference to ease garbage collection
this._ref()
this._chopper.end()
cb()
}
Client.prototype._destroy = function (err, cb) {
clients[this._index] = null // remove global reference to ease garbage collection
this._chopper.destroy()
this._agent.destroy()
cb(err)
}
function onStream (opts, client, onerror) {
const serverTimeout = opts.serverTimeout
const requestOpts = getRequestOptions(opts, client._agent)
return function (stream, next) {
const onerrorproxy = (err) => {
stream.removeListener('error', onerrorproxy)
req.removeListener('error', onerrorproxy)
destroyStream(stream)
onerror(err)
}
client._active = true
const req = client._transport.request(requestOpts, onResult(onerror))
// Abort the current request if the server responds prior to the request
// being finished
req.on('response', function (res) {
if (!req.finished) {
// In Node.js 8, the zlib stream will emit a 'zlib binding closed'
// error when destroyed. Furthermore, the HTTP response will not emit
// any data events after the request have been destroyed, so it becomes
// impossible to see the error returned by the server if we abort the
// request. So for Node.js 8, we'll work around this by closing the
// stream gracefully.
//
// This results in the gzip buffer being flushed and a little more data
// being sent to the APM Server, but it's better than not getting the
// error body.
if (node8) {
stream.end()
} else {
destroyStream(stream)
}
}
})
// Mointor streams for errors so that we can make sure to destory the
// output stream as soon as that occurs
stream.on('error', onerrorproxy)
req.on('error', onerrorproxy)
req.on('socket', function (socket) {
// Sockets will automatically be unreffed by the HTTP agent when they are
// not in use by an HTTP request, but as we're keeping the HTTP request
// open, we need to unref the socket manually
socket.unref()
})
if (Number.isFinite(serverTimeout)) {
req.setTimeout(serverTimeout, function () {
req.abort()
})
}
pump(stream, req, function () {
// This function is technically called with an error, but because we
// manually attach error listeners on all the streams in the pipeline
// above, we can safely ignore it.
//
// We do this for two reasons:
//
// 1) This callback might be called a few ticks too late, in which case a
// race condition could occur where the user would write to the output
// stream before the rest of the system discovered that it was
// unwritable
//
// 2) The error might occur post the end of the stream. In that case we
// would not get it here as the internal error listener would have
// been removed and the stream would throw the error instead
client.sent = client._received
client._active = false
if (client._onflushed) {
client._onflushed()
client._onflushed = null
}
next()
})
// Only intended for local debugging
if (opts.payloadLogFile) {
if (!client._payloadLogFile) {
client._payloadLogFile = require('fs').createWriteStream(opts.payloadLogFile, { flags: 'a' })
}
// Manually write to the file instead of using pipe/pump so that the file
// handle isn't closed when the stream ends
stream.pipe(zlib.createGunzip()).on('data', function (chunk) {
client._payloadLogFile.write(chunk)
})
}
// All requests to the APM Server must start with a metadata object
stream.write(client._encode({ metadata: getMetadata(opts) }, Client.encoding.METADATA))
}
}
function onResult (onerror) {
return streamToBuffer.onStream(function (err, buf, res) {
if (err) return onerror(err)
if (res.statusCode < 200 || res.statusCode > 299) {
const err = new Error('Unexpected APM Server response')
err.code = res.statusCode
if (buf.length > 0) {
const body = buf.toString('utf8')
const contentType = res.headers['content-type']
if (contentType && contentType.indexOf('application/json') === 0) {
try {
const data = JSON.parse(body)
err.accepted = data.accepted
err.errors = data.errors
if (!err.errors) err.response = body
} catch (e) {
err.response = body
}
} else {
err.response = body
}
}
onerror(err)
}
})
}
function normalizeOptions (opts) {
const missing = requiredOpts.filter(name => !opts[name])
if (missing.length > 0) throw new Error('Missing required option(s): ' + missing.join(', '))
const normalized = Object.assign({}, opts, { objectMode: true })
// default values
if (!normalized.size && normalized.size !== 0) normalized.size = 750 * 1024
if (!normalized.time && normalized.time !== 0) normalized.time = 10000
if (!normalized.serverTimeout && normalized.serverTimeout !== 0) normalized.serverTimeout = 15000
if (!normalized.serverUrl) normalized.serverUrl = 'http://localhost:8200'
if (!normalized.hostname) normalized.hostname = hostname
if (!normalized.truncateKeywordsAt) normalized.truncateKeywordsAt = 1024
if (!normalized.truncateErrorMessagesAt) normalized.truncateErrorMessagesAt = 2048
if (!normalized.truncateStringsAt) normalized.truncateStringsAt = 1024
if (!normalized.truncateQueriesAt) normalized.truncateQueriesAt = 10000
if (!normalized.bufferWindowTime) normalized.bufferWindowTime = 20
if (!normalized.bufferWindowSize) normalized.bufferWindowSize = 50
normalized.keepAlive = normalized.keepAlive !== false
// process
normalized.serverUrl = parseUrl(normalized.serverUrl)
if (containerInfo) {
if (!normalized.containerId && containerInfo.containerId) {
normalized.containerId = containerInfo.containerId
}
if (!normalized.kubernetesPodUID && containerInfo.podId) {
normalized.kubernetesPodUID = containerInfo.podId
}
if (!normalized.kubernetesPodName && containerInfo.podId) {
normalized.kubernetesPodName = hostname
}
}
return normalized
}
function getRequestOptions (opts, agent) {
const defaultPath = '/intake/v2/events'
return {
agent: agent,
rejectUnauthorized: opts.rejectUnauthorized !== false,
hostname: opts.serverUrl.hostname,
port: opts.serverUrl.port,
method: 'POST',
path: opts.serverUrl.path === '/' ? defaultPath : opts.serverUrl.path + defaultPath,
headers: getHeaders(opts)
}
}
function getHeaders (opts) {
const headers = {}
if (opts.secretToken) headers['Authorization'] = 'Bearer ' + opts.secretToken
headers['Content-Type'] = 'application/x-ndjson'
headers['Content-Encoding'] = 'gzip'
headers['Accept'] = 'application/json'
headers['User-Agent'] = opts.userAgent + ' ' + pkg.name + '/' + pkg.version
return Object.assign(headers, opts.headers)
}
function getMetadata (opts) {
var payload = {
service: {
name: opts.serviceName,
runtime: {
name: process.release.name,
version: process.versions.node
},
language: {
name: 'javascript'
},
agent: {
name: opts.agentName,
version: opts.agentVersion
}
},
process: {
pid: process.pid,
ppid: process.ppid,
title: process.title,
argv: process.argv
},
system: {
hostname: opts.hostname,
architecture: process.arch,
platform: process.platform
}
}
if (opts.serviceVersion) payload.service.version = opts.serviceVersion
if (opts.frameworkName || opts.frameworkVersion) {
payload.service.framework = {
name: opts.frameworkName,
version: opts.frameworkVersion
}
}
if (opts.containerId) {
payload.system.container = {
id: opts.containerId
}
}
if (opts.kubernetesNodeName || opts.kubernetesNamespace || opts.kubernetesPodName || opts.kubernetesPodUID) {
payload.kubernetes = {
namespace: opts.kubernetesNamespace,
node: undefined,
pod: undefined
}
if (opts.kubernetesNodeName) {
payload.kubernetes.node = { name: opts.kubernetesNodeName }
}
if (opts.kubernetesPodName || opts.kubernetesPodUID) {
payload.kubernetes.pod = { name: opts.kubernetesPodName, uid: opts.kubernetesPodUID }
}
}
return payload
}
function destroyStream (stream) {
if (stream instanceof zlib.Gzip ||
stream instanceof zlib.Gunzip ||
stream instanceof zlib.Deflate ||
stream instanceof zlib.DeflateRaw ||
stream instanceof zlib.Inflate ||
stream instanceof zlib.InflateRaw ||
stream instanceof zlib.Unzip) {
// Zlib streams doesn't have a destroy function in Node.js 6. On top of
// that simply calling destroy on a zlib stream in Node.js 8+ will result
// in a memory leak as the handle isn't closed (an operation normally done
// by calling close). So until that is fixed, we need to manually close the
// handle after destroying the stream.
//
// PR: https://github.com/nodejs/node/pull/23734
if (typeof stream.destroy === 'function') {
// Manually close the stream instead of calling `close()` as that would
// have emitted 'close' again when calling `destroy()`
if (stream._handle && typeof stream._handle.close === 'function') {
stream._handle.close()
stream._handle = null
}
stream.destroy()
} else if (typeof stream.close === 'function') {
stream.close()
}
} else {
// For other streams we assume calling destroy is enough
if (typeof stream.destroy === 'function') stream.destroy()
// Or if there's no destroy (which Node.js 6 will not have on regular
// streams), emit `close` as that should trigger almost the same effect
else if (typeof stream.emit === 'function') stream.emit('close')
}
}