1
1
'use strict'
2
2
3
3
const util = require ( 'util' )
4
+ const os = require ( 'os' )
4
5
const parseUrl = require ( 'url' ) . parse
5
6
const zlib = require ( 'zlib' )
6
7
const Writable = require ( 'readable-stream' ) . Writable
@@ -10,11 +11,20 @@ const eos = require('end-of-stream')
10
11
const safeStringify = require ( 'fast-safe-stringify' )
11
12
const streamToBuffer = require ( 'fast-stream-to-buffer' )
12
13
const StreamChopper = require ( 'stream-chopper' )
14
+ const truncate = require ( 'unicode-byte-truncate' )
13
15
const pkg = require ( './package' )
14
16
17
+ module . exports = Client
18
+
15
19
const flush = Symbol ( 'flush' )
16
20
17
- module . exports = Client
21
+ const hostname = os . hostname ( )
22
+ const requiredOpts = [
23
+ 'agentName' ,
24
+ 'agentVersion' ,
25
+ 'serviceName' ,
26
+ 'userAgent'
27
+ ]
18
28
19
29
// All sockets on the agent are unreffed when they are created. This means that
20
30
// when those are the only handles left, the `beforeExit` event will be
@@ -155,9 +165,8 @@ Client.prototype.destroy = function (err) {
155
165
}
156
166
157
167
function onStream ( opts , client , onerror ) {
158
- const meta = opts . meta
159
168
const serverTimeout = opts . serverTimeout
160
- opts = getRequestOptions ( opts , client . _agent )
169
+ const requestOpts = getRequestOptions ( opts , client . _agent )
161
170
162
171
return function ( stream , next ) {
163
172
const onerrorproxy = ( err ) => {
@@ -170,7 +179,7 @@ function onStream (opts, client, onerror) {
170
179
171
180
client . _active = true
172
181
173
- const req = client . _transport . request ( opts , onResult ( onerror ) )
182
+ const req = client . _transport . request ( requestOpts , onResult ( onerror ) )
174
183
const compressor = zlib . createGzip ( )
175
184
176
185
// Mointor streams for errors so that we can make sure to destory the
@@ -219,7 +228,7 @@ function onStream (opts, client, onerror) {
219
228
} )
220
229
221
230
// All requests to the APM Server must start with a metadata object
222
- stream . write ( safeStringify ( { metadata : meta ( ) } ) + '\n' )
231
+ stream . write ( safeStringify ( { metadata : metadata ( opts ) } ) + '\n' )
223
232
}
224
233
}
225
234
@@ -242,8 +251,8 @@ function onResult (onerror) {
242
251
}
243
252
244
253
function normalizeOptions ( opts ) {
245
- if ( ! opts . userAgent ) throw new Error ( 'Missing required option: userAgent' )
246
- if ( ! opts . meta ) throw new Error ( 'Missing required option: meta' )
254
+ const missing = requiredOpts . filter ( name => ! opts [ name ] )
255
+ if ( missing . length > 0 ) throw new Error ( 'Missing required option(s): ' + missing . join ( ', ' ) )
247
256
248
257
const normalized = Object . assign ( { } , opts , { objectMode : true } )
249
258
@@ -252,6 +261,8 @@ function normalizeOptions (opts) {
252
261
if ( ! normalized . time && normalized . time !== 0 ) normalized . time = 10000
253
262
if ( ! normalized . serverTimeout && normalized . serverTimeout !== 0 ) normalized . serverTimeout = 15000
254
263
if ( ! normalized . serverUrl ) normalized . serverUrl = 'http://localhost:8200'
264
+ if ( ! normalized . hostname ) normalized . hostname = hostname
265
+ if ( ! normalized . truncateStringsAt ) normalized . truncateStringsAt = 1024
255
266
normalized . keepAlive = normalized . keepAlive !== false
256
267
257
268
// process
@@ -282,3 +293,44 @@ function getHeaders (opts) {
282
293
headers [ 'User-Agent' ] = opts . userAgent + ' ' + pkg . name + '/' + pkg . version
283
294
return Object . assign ( headers , opts . headers )
284
295
}
296
+
297
+ function metadata ( opts ) {
298
+ var payload = {
299
+ service : {
300
+ name : opts . serviceName ,
301
+ runtime : {
302
+ name : process . release . name ,
303
+ version : process . version
304
+ } ,
305
+ language : {
306
+ name : 'javascript'
307
+ } ,
308
+ agent : {
309
+ name : opts . agentName ,
310
+ version : opts . agentVersion
311
+ }
312
+ } ,
313
+ process : {
314
+ pid : process . pid ,
315
+ ppid : process . ppid ,
316
+ title : truncate ( String ( process . title ) , opts . truncateStringsAt ) ,
317
+ argv : process . argv
318
+ } ,
319
+ system : {
320
+ hostname : opts . hostname ,
321
+ architecture : process . arch ,
322
+ platform : process . platform
323
+ }
324
+ }
325
+
326
+ if ( opts . serviceVersion ) payload . service . version = opts . serviceVersion
327
+
328
+ if ( opts . frameworkName || opts . frameworkVersion ) {
329
+ payload . service . framework = {
330
+ name : opts . frameworkName ,
331
+ version : opts . frameworkVersion
332
+ }
333
+ }
334
+
335
+ return payload
336
+ }
0 commit comments