forked from elastic/apm-nodejs-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
332 lines (285 loc) · 9.85 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
'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 pump = require('pump')
const ndjson = require('ndjson')
const eos = require('end-of-stream')
const safeStringify = require('fast-safe-stringify')
const streamToBuffer = require('fast-stream-to-buffer')
const StreamChopper = require('stream-chopper')
const truncate = require('unicode-byte-truncate')
const pkg = require('./package')
module.exports = Client
const flush = Symbol('flush')
const hostname = os.hostname()
const requiredOpts = [
'agentName',
'agentVersion',
'serviceName',
'userAgent'
]
// 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)
function Client (opts) {
if (!(this instanceof Client)) return new Client(opts)
opts = normalizeOptions(opts)
Writable.call(this, opts)
const errorproxy = (err) => {
if (this._destroyed === false) this.emit('error', err)
}
const fail = () => {
if (this._writableState.ending === false) this.destroy()
}
this._active = false
this._destroyed = false
this._onflushed = null
this._transport = require(opts.serverUrl.protocol.slice(0, -1)) // 'http:' => 'http'
this._agent = new this._transport.Agent(opts)
this._stream = ndjson.serialize()
this._chopper = new StreamChopper({
size: opts.size,
time: opts.time,
type: StreamChopper.overflow
}).on('stream', onStream(opts, this, errorproxy))
this._stream.on('error', errorproxy)
this._chopper.on('error', errorproxy)
eos(this._stream, {error: false}, fail)
eos(this._chopper, {error: false}, fail)
pump(this._stream, this._chopper)
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 (this._destroyed) {
this.emit('error', new Error('write called on destroyed Elastic APM client'))
cb()
} else if (obj === flush) {
if (this._active) {
this._onflushed = cb
this._chopper.chop()
} else {
this._chopper.chop(cb)
}
} else {
this._stream.write(obj, cb)
}
}
Client.prototype.sendSpan = function (span, cb) {
return this.write({span}, cb)
}
Client.prototype.sendTransaction = function (transaction, cb) {
return this.write({transaction}, cb)
}
Client.prototype.sendError = function (error, cb) {
return this.write({error}, cb)
}
Client.prototype.flush = function (cb) {
if (this._destroyed) {
this.emit('error', new Error('flush called on destroyed Elastic APM client'))
if (cb) process.nextTick(cb)
return
}
// 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) {
if (this._destroyed) {
this.emit('error', new Error('end called on destroyed Elastic APM client'))
cb()
return
}
clients[this._index] = null // remove global reference to ease garbage collection
this._ref()
this._stream.end()
cb()
}
// Overwrite destroy instead of using _destroy because readable-stream@2 can't
// be trusted. After a stream is destroyed, we want a call to either
// client.write() or client.end() to both emit an error and call the provided
// callback. Unfortunately, in readable-stream@2 and Node.js <10, this is not
// consistent. This has been fixed in Node.js 10 and will also be fixed in
// readable-stream@3.
Client.prototype.destroy = function (err) {
if (this._destroyed) return
this._destroyed = true
if (err) this.emit('error', err)
clients[this._index] = null // remove global reference to ease garbage collection
this._stream.destroy()
this._chopper.destroy()
this._agent.destroy()
process.nextTick(() => {
this.emit('close')
})
}
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)
compressor.removeListener('error', onerrorproxy)
req.removeListener('error', onerrorproxy)
stream.destroy()
onerror(err)
}
client._active = true
const req = client._transport.request(requestOpts, onResult(onerror))
const compressor = zlib.createGzip()
// Mointor streams for errors so that we can make sure to destory the
// output stream as soon as that occurs
stream.on('error', onerrorproxy)
compressor.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, compressor, 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._active = false
if (client._onflushed) {
client._onflushed()
client._onflushed = null
}
next()
})
// All requests to the APM Server must start with a metadata object
stream.write(safeStringify({metadata: metadata(opts)}) + '\n')
}
}
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 response code from APM Server: ' + res.statusCode)
if (buf.length > 0) {
err.result = buf.toString('utf8')
if (res.headers['content-type'] === 'application/json') {
try {
err.result = JSON.parse(err.result).error || err.result
} catch (e) {}
}
}
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 = 1024 * 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.truncateStringsAt) normalized.truncateStringsAt = 1024
normalized.keepAlive = normalized.keepAlive !== false
// process
normalized.serverUrl = parseUrl(normalized.serverUrl)
return normalized
}
function getRequestOptions (opts, agent) {
const defaultPath = '/v2/intake'
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 metadata (opts) {
var payload = {
service: {
name: opts.serviceName,
runtime: {
name: process.release.name,
version: process.version
},
language: {
name: 'javascript'
},
agent: {
name: opts.agentName,
version: opts.agentVersion
}
},
process: {
pid: process.pid,
ppid: process.ppid,
title: truncate(String(process.title), opts.truncateStringsAt),
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
}
}
return payload
}