-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathagent.js
690 lines (601 loc) · 23.9 KB
/
agent.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
'use strict'
var http = require('http')
var path = require('path')
var isError = require('core-util-is').isError
var Filters = require('object-filter-sequence')
var config = require('./config')
var connect = require('./middleware/connect')
const constants = require('./constants')
var errors = require('./errors')
const { InflightEventSet } = require('./InflightEventSet')
var Instrumentation = require('./instrumentation')
var { elasticApmAwsLambda } = require('./lambda')
var Metrics = require('./metrics')
var parsers = require('./parsers')
var symbols = require('./symbols')
const { frameCacheStats } = require('./stacktraces')
var IncomingMessage = http.IncomingMessage
var ServerResponse = http.ServerResponse
var version = require('../package').version
module.exports = Agent
function Agent () {
// Early configuration to ensure `agent.logger` works before `agent.start()`.
this.logger = config.configLogger()
// Get an initial pre-.start() configuration of agent defaults. This is a
// crutch for Agent APIs that depend on `agent._conf`.
this._conf = config.initialConfig(this.logger)
this._httpClient = null
this._uncaughtExceptionListener = null
this._inflightEvents = new InflightEventSet()
this._instrumentation = new Instrumentation(this)
this._metrics = new Metrics(this)
this._errorFilters = new Filters()
this._transactionFilters = new Filters()
this._spanFilters = new Filters()
this._transport = null
this.lambda = elasticApmAwsLambda(this)
this.middleware = { connect: connect.bind(this) }
}
Object.defineProperty(Agent.prototype, 'currentTransaction', {
get () {
return this._instrumentation.currTransaction()
}
})
Object.defineProperty(Agent.prototype, 'currentSpan', {
get () {
return this._instrumentation.currSpan()
}
})
Object.defineProperty(Agent.prototype, 'currentTraceparent', {
get () {
const current = this._instrumentation.currSpan() || this._instrumentation.currTransaction()
return current ? current.traceparent : null
}
})
Object.defineProperty(Agent.prototype, 'currentTraceIds', {
get () {
return this._instrumentation.ids()
}
})
// Destroy this agent. This prevents any new agent processing, communication
// with APM server, and resets changed global state *as much as is possible*.
//
// In the typical uses case -- a singleton Agent running for the full process
// lifetime -- it is *not* necessary to call `agent.destroy()`. It is used
// for some testing.
//
// Limitations:
// - Patching/wrapping of functions for instrumentation *is* undone, but
// references to the wrapped versions can remain.
// - There may be in-flight tasks (in ins.addEndedSpan() and
// agent.captureError() for example) that will complete after this destroy
// completes. They should have no impact other than CPU/resource use.
// - The patching of core node functions when `asyncHooks=false` is *not*
// undone. This means run context tracking for `asyncHooks=false` is broken
// with in-process multiple-Agent use.
Agent.prototype.destroy = function () {
if (this._transport && this._transport.destroy) {
this._transport.destroy()
}
// So in-flight tasks in ins.addEndedSpan() and agent.captureError() do
// not use the destroyed transport.
this._transport = null
// So in-flight tasks do not call user-added filters after the agent has
// been destroyed.
this._errorFilters = new Filters()
this._transactionFilters = new Filters()
this._spanFilters = new Filters()
if (this._uncaughtExceptionListener) {
process.removeListener('uncaughtException', this._uncaughtExceptionListener)
}
this._metrics.stop()
this._instrumentation.stop()
// Allow a new Agent instance to `.start()`. Typically this is only relevant
// for tests that may use multiple Agent instances in a single test process.
global[symbols.agentInitialized] = null
if (this._origStackTraceLimit && Error.stackTraceLimit !== this._origStackTraceLimit) {
Error.stackTraceLimit = this._origStackTraceLimit
}
}
// These are metrics about the agent itself -- separate from the metrics
// gathered on behalf of the using app and sent to APM server. Currently these
// are only useful for internal debugging of the APM agent itself.
//
// **These stats are NOT a promised interface.**
Agent.prototype._getStats = function () {
const stats = {
frameCache: frameCacheStats
}
if (this._instrumentation._runCtxMgr && this._instrumentation._runCtxMgr._runContextFromAsyncId) {
stats.runContextFromAsyncIdSize = this._instrumentation._runCtxMgr._runContextFromAsyncId.size
}
if (this._transport && typeof this._transport._getStats === 'function') {
stats.apmclient = this._transport._getStats()
}
return stats
}
Agent.prototype.addPatch = function (modules, handler) {
return this._instrumentation.addPatch.apply(this._instrumentation, arguments)
}
Agent.prototype.removePatch = function (modules, handler) {
return this._instrumentation.removePatch.apply(this._instrumentation, arguments)
}
Agent.prototype.clearPatches = function (modules) {
return this._instrumentation.clearPatches.apply(this._instrumentation, arguments)
}
Agent.prototype.startTransaction = function (name, type, subtype, action, { startTime, childOf } = {}) {
return this._instrumentation.startTransaction.apply(this._instrumentation, arguments)
}
Agent.prototype.endTransaction = function (result, endTime) {
return this._instrumentation.endTransaction.apply(this._instrumentation, arguments)
}
Agent.prototype.setTransactionName = function (name) {
return this._instrumentation.setTransactionName.apply(this._instrumentation, arguments)
}
/**
* Sets outcome value for current transaction
*
* The setOutcome method allows users to override the default
* outcome handling in the agent and set their own value.
*
* @param {string} outcome must be one of `failure`, `success`, or `unknown`
*/
Agent.prototype.setTransactionOutcome = function (outcome) {
return this._instrumentation.setTransactionOutcome.apply(this._instrumentation, arguments)
}
Agent.prototype.startSpan = function (name, type, subtype, action, { startTime, childOf, exitSpan } = {}) {
return this._instrumentation.startSpan.apply(this._instrumentation, arguments)
}
/**
* Sets outcome value for current active span
*
* The setOutcome method allows users to override the default
* outcome handling in the agent and set their own value.
*
* @param {string} outcome must be one of `failure`, `success`, or `unknown`
*/
Agent.prototype.setSpanOutcome = function (outcome) {
return this._instrumentation.setSpanOutcome.apply(this._instrumentation, arguments)
}
Agent.prototype._config = function (opts) {
this._conf = config.createConfig(opts, this.logger)
this.logger = this._conf.logger
const { host, port, protocol } = this._conf.serverUrl
? parsers.parseUrl(this._conf.serverUrl)
: { host: 'localhost:8200', port: '8200' }
this._conf.serverHost = host
this._conf.serverPort = port === ''
? (protocol === 'https:' ? 443 : 80)
: parseInt(port, 10)
}
Agent.prototype.isStarted = function () {
return global[symbols.agentInitialized]
}
Agent.prototype.start = function (opts) {
if (this.isStarted()) {
throw new Error('Do not call .start() more than once')
}
global[symbols.agentInitialized] = true
this._config(opts)
if (this._conf.filterHttpHeaders) {
this.addFilter(require('./filters/http-headers'))
}
if (!this._conf.active) {
this.logger.debug('Elastic APM agent disabled (`active` is false)')
return this
} else if (!this._conf.serviceName) {
this.logger.error('Elastic APM is incorrectly configured: Missing serviceName (APM will be disabled)')
this._conf.active = false
return this
} else if (!(this._conf.serverPort >= 1 && this._conf.serverPort <= 65535)) {
this.logger.error('Elastic APM is incorrectly configured: serverUrl "%s" contains an invalid port! (allowed: 1-65535)', this._conf.serverUrl)
this._conf.active = false
return this
} else if (this._conf.logLevel === 'trace') {
var stackObj = {}
Error.captureStackTrace(stackObj)
// Attempt to load package.json from process.argv.
var pkg = null
try {
var basedir = path.dirname(process.argv[1] || '.')
pkg = require(path.join(basedir, 'package.json'))
} catch (e) {}
this.logger.trace({
pid: process.pid,
ppid: process.ppid,
arch: process.arch,
platform: process.platform,
node: process.version,
agent: version,
startTrace: stackObj.stack.split(/\n */).slice(1),
main: pkg ? pkg.main : '<could not determine>',
dependencies: pkg ? pkg.dependencies : '<could not determine>',
conf: this._conf.toJSON()
}, 'agent configured correctly')
}
this._transport = this._conf.transport(this._conf, this)
this._instrumentation.start()
this._metrics.start()
this._origStackTraceLimit = Error.stackTraceLimit
Error.stackTraceLimit = this._conf.stackTraceLimit
if (this._conf.captureExceptions) this.handleUncaughtExceptions()
return this
}
Agent.prototype.getServiceName = function () {
return this._conf ? this._conf.serviceName : undefined
}
Agent.prototype.setFramework = function ({ name, version, overwrite = true }) {
if (!this._transport || !this._conf) {
return
}
const conf = {}
if (name && (overwrite || !this._conf.frameworkName)) this._conf.frameworkName = conf.frameworkName = name
if (version && (overwrite || !this._conf.frameworkVersion)) this._conf.frameworkVersion = conf.frameworkVersion = version
this._transport.config(conf)
}
Agent.prototype.setUserContext = function (context) {
var trans = this._instrumentation.currTransaction()
if (!trans) return false
trans.setUserContext(context)
return true
}
Agent.prototype.setCustomContext = function (context) {
var trans = this._instrumentation.currTransaction()
if (!trans) return false
trans.setCustomContext(context)
return true
}
Agent.prototype.setLabel = function (key, value, stringify) {
var trans = this._instrumentation.currTransaction()
if (!trans) return false
return trans.setLabel(key, value, stringify)
}
Agent.prototype.addLabels = function (labels, stringify) {
var trans = this._instrumentation.currTransaction()
if (!trans) return false
return trans.addLabels(labels, stringify)
}
Agent.prototype.addFilter = function (fn) {
this.addErrorFilter(fn)
this.addTransactionFilter(fn)
this.addSpanFilter(fn)
// Note: This does *not* add to *metadata* filters, partly for backward
// compat -- the structure of metadata objects is quite different and could
// break existing filters -- and partly because that different structure
// means it makes less sense to re-use the same function to filter them.
}
Agent.prototype.addErrorFilter = function (fn) {
if (typeof fn !== 'function') {
this.logger.error('Can\'t add filter of type %s', typeof fn)
return
}
this._errorFilters.push(fn)
}
Agent.prototype.addTransactionFilter = function (fn) {
if (typeof fn !== 'function') {
this.logger.error('Can\'t add filter of type %s', typeof fn)
return
}
this._transactionFilters.push(fn)
}
Agent.prototype.addSpanFilter = function (fn) {
if (typeof fn !== 'function') {
this.logger.error('Can\'t add filter of type %s', typeof fn)
return
}
this._spanFilters.push(fn)
}
Agent.prototype.addMetadataFilter = function (fn) {
if (typeof fn !== 'function') {
this.logger.error('Can\'t add filter of type %s', typeof fn)
return
} else if (!this._transport) {
this.logger.error('cannot add metadata filter to inactive or unconfigured agent (agent has no transport)')
return
} else if (typeof this._transport.addMetadataFilter !== 'function') {
// Graceful failure if unexpectedly using a too-old APM client.
this.logger.error('cannot add metadata filter: transport does not support addMetadataFilter')
return
}
// Metadata filters are handled by the APM client, where metadata is
// processed.
this._transport.addMetadataFilter(fn)
}
const EMPTY_OPTS = {}
// Capture an APM server "error" event for the given `err` and send it to APM
// server.
//
// Usage:
// captureError(err, opts, cb)
// captureError(err, opts)
// captureError(err, cb)
//
// where:
// - `err` is an Error instance, or a string message, or a "parameterized string
// message" object, e.g.:
// {
// message: "this is my message template: %d %s"},
// params: [ 42, "another param" ]
// }
// - `opts` can include any of the following (all optional):
// - `opts.timestamp` - Milliseconds since the Unix epoch. Defaults to now.
// - `opts.user` - Object to add to `error.context.user`.
// - `opts.tags` - Deprecated, use `opts.labels`. Object to add to
// `error.context.labels`.
// - `opts.labels` - Object to add to `error.context.labels`.
// - `opts.custom` - Object to add to `error.context.custom`.
// - `opts.message` - If `err` is an Error instance, this string is added to
// `error.log.message` (unless it matches err.message).
// - `opts.request` - HTTP request (node `IncomingMessage` instance) to use
// for `error.context.request`.
// - `opts.response` - HTTP response (node `ServerResponse` instance) to use
// for `error.context.response`.
// - `opts.handled` - Boolean indicating if this exception was handled by
// application code. Default true. Setting to `false` also results in the
// error being flushed to APM server as soon as it is processed.
// - `opts.captureAttributes` - Boolean. Default true. Set to false to *not*
// include properties of `err` as attributes on the APM error event.
// - `opts.skipOutcome` - Boolean. Default false. Set to true to not have
// this captured error set `<currentSpan>.outcome = failure`.
// - `cb` is a callback `function (captureErr, apmErrorIdString)`. If provided,
// the error will be flushed to APM server as soon as it is processed, and
// `cb` will be called when that send is complete.
Agent.prototype.captureError = function (err, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = EMPTY_OPTS
} else if (!opts) {
opts = EMPTY_OPTS
}
const id = errors.generateErrorId()
if (!this.isStarted()) {
if (cb) {
cb(new Error('cannot capture error before agent is started'), id)
}
return
}
// Avoid unneeded error/stack processing if only propagating trace-context.
if (this._conf.contextPropagationOnly) {
if (cb) {
process.nextTick(cb, null, id)
}
return
}
const agent = this
let callSiteLoc = null
const errIsError = isError(err)
const handled = opts.handled !== false // default true
const shouldCaptureAttributes = opts.captureAttributes !== false // default true
const skipOutcome = Boolean(opts.skipOutcome)
const span = this._instrumentation.currSpan()
const timestampUs = (opts.timestamp
? Math.floor(opts.timestamp * 1000)
: Date.now() * 1000)
const trans = this._instrumentation.currTransaction()
const traceContext = (span || trans || {})._context
const req = (opts.request instanceof IncomingMessage
? opts.request
: trans && trans.req)
const res = (opts.response instanceof ServerResponse
? opts.response
: trans && trans.res)
// As an added feature, for *some* cases, we capture a stacktrace at the point
// this `captureError` was called. This is added to `error.log.stacktrace`.
if (handled &&
(agent._conf.captureErrorLogStackTraces === config.CAPTURE_ERROR_LOG_STACK_TRACES_ALWAYS ||
(!errIsError && agent._conf.captureErrorLogStackTraces === config.CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES))
) {
callSiteLoc = {}
Error.captureStackTrace(callSiteLoc, Agent.prototype.captureError)
}
if (span && !skipOutcome) {
span._setOutcomeFromErrorCapture(constants.OUTCOME_FAILURE)
}
// Note this error as an "inflight" event. See Agent#flush().
const inflightEvents = this._inflightEvents
inflightEvents.add(id)
// Move the remaining captureError processing to a later tick because:
// 1. This allows the calling code to continue processing. For example, for
// Express instrumentation this can significantly improve latency in
// the app's endpoints because the response does not proceed until the
// error handlers return.
// 2. Gathering `error.context.response` in the same tick results in data
// for a response that hasn't yet completed (no headers, unset status_code,
// etc.).
setImmediate(() => {
// Gather `error.context.*`.
const errorContext = {
user: Object.assign(
{},
req && parsers.getUserContextFromRequest(req),
trans && trans._user,
opts.user
),
tags: Object.assign(
{},
trans && trans._labels,
opts.tags,
opts.labels
),
custom: Object.assign(
{},
trans && trans._custom,
opts.custom
)
}
if (req) {
errorContext.request = parsers.getContextFromRequest(req, agent._conf, 'errors')
}
if (res) {
errorContext.response = parsers.getContextFromResponse(res, agent._conf, true)
}
errors.createAPMError({
log: agent.logger,
id: id,
exception: errIsError ? err : null,
logMessage: errIsError ? null : err,
shouldCaptureAttributes,
timestampUs,
handled,
callSiteLoc,
message: opts.message,
sourceLinesAppFrames: agent._conf.sourceLinesErrorAppFrames,
sourceLinesLibraryFrames: agent._conf.sourceLinesErrorLibraryFrames,
trans,
traceContext,
errorContext
}, function filterAndSendError (_err, apmError) {
// _err is always null from createAPMError.
apmError = agent._errorFilters.process(apmError)
if (!apmError) {
agent.logger.debug('error ignored by filter %o', { id })
inflightEvents.delete(id)
if (cb) {
cb(null, id)
}
return
}
if (agent._transport) {
agent.logger.info('Sending error to Elastic APM: %o', { id })
agent._transport.sendError(apmError)
inflightEvents.delete(id)
if (!handled || cb) {
// Immediately flush *unhandled* errors -- those from
// `uncaughtException` -- on the assumption that the process may
// soon crash. Also flush when a `cb` is provided.
agent.flush(function (flushErr) {
if (cb) {
cb(flushErr, id)
}
})
}
} else {
inflightEvents.delete(id)
if (cb) {
cb(new Error('cannot send error: missing transport'), id)
}
}
})
})
}
// The optional callback will be called with the error object after the error
// have been sent to the intake API. If no callback have been provided we will
// automatically terminate the process, so if you provide a callback you must
// remember to terminate the process manually.
Agent.prototype.handleUncaughtExceptions = function (cb) {
var agent = this
if (this._uncaughtExceptionListener) {
process.removeListener('uncaughtException', this._uncaughtExceptionListener)
}
this._uncaughtExceptionListener = function (err) {
agent.logger.debug({ err }, 'Elastic APM caught unhandled exception')
// The stack trace of uncaught exceptions are normally written to STDERR.
// The `uncaughtException` listener inhibits this behavior, and it's
// therefore necessary to manually do this to not break expectations.
if (agent._conf && agent._conf.logUncaughtExceptions === true) {
console.error(err)
}
agent.captureError(err, { handled: false }, function () {
cb ? cb(err) : process.exit(1)
})
}
process.on('uncaughtException', this._uncaughtExceptionListener)
}
// Flush all ended APM events (transactions, spans, errors, metricsets) to APM
// server as soon as possible. If the optional `cb` is given, it will be called
// `cb(flushErr)` when this is complete.
//
// Encoding and passing event data to the agent's transport is *asynchronous*
// for some event types: spans and errors. This flush will make a *best effort*
// attempt to wait for those "inflight" events to finish processing before
// flushing data to APM server. To avoid `.flush()` hanging, this times out
// after one second.
//
// If flush is called while still waiting for inflight events in an earlier
// flush call, then the more recent flush will only wait for events that were
// newly inflight *since the last .flush()* call. I.e. the second flush does
// *not* wait for the set of events the first flush is waiting on. This makes
// the semantics of flush less than ideal (one cannot blindly call .flush() to
// flush *everything* that has come before). However it handles the common use
// case of flushing synchronously after ending a span or capturing an error:
// mySpan.end()
// apm.flush(function () { ... })
// and it simplifies the implementation.
//
// # Dev Notes
//
// To support the implementation, agent code that creates an inflight event
// must do the following:
// - Take a reference to the current set of inflight events:
// const inflightEvents = agent._inflightEvents
// - Add a unique ID for the event to the set:
// inflightEvents.add(id)
// - Delete the ID from the set when sent to the transport (`.sendSpan(...)` et
// al) or when dropping the event (e.g. because of a filter):
// inflightEvents.delete(id)
Agent.prototype.flush = function (cb) {
// This 1s timeout is a subjective balance between "long enough for spans
// and errors to reasonably encode" and "short enough to not block data
// being reported to APM server".
const DEFAULT_INFLIGHT_FLUSH_TIMEOUT_MS = 1000
return this._flush({ inflightTimeoutMs: DEFAULT_INFLIGHT_FLUSH_TIMEOUT_MS }, cb)
}
// The internal-use `.flush()` that supports some options not exposed to the
// public API.
//
// @param {Number} opts.inflightTimeoutMs - Required. The number of ms to wait
// for inflight events (spans, errors) to finish being send to the transport
// before flushing.
// @param {Boolean} opts.lambdaEnd - Optional, default false. Set this to true
// to signal to the transport that this is the flush at the end of a Lambda
// function invocation.
// https://github.com/elastic/apm/blob/main/specs/agents/tracing-instrumentation-aws-lambda.md#data-flushing
Agent.prototype._flush = function (opts, cb) {
const lambdaEnd = !!opts.lambdaEnd
if (!this._transport) {
// Log an *err* to provide a stack for the user.
const err = new Error('cannot flush agent before it is started')
this.logger.warn({ err }, err.message)
if (cb) {
process.nextTick(cb)
}
return
}
const boundCb = cb && this._instrumentation.bindFunction(cb)
// If there are no inflight events then avoid creating additional objects.
if (this._inflightEvents.size === 0) {
this._transport.flush({ lambdaEnd }, boundCb)
return
}
// Otherwise, there are inflight events to wait for. Setup a handler to
// callback when the current set of inflight events complete.
const flushingInflightEvents = this._inflightEvents
flushingInflightEvents.setDrainHandler((drainErr) => {
// The only possible drainErr is a timeout. This is best effort, so we only
// log this and move on.
this.logger.debug({
numRemainingInflightEvents: flushingInflightEvents.size,
err: drainErr
}, 'flush: drained inflight events')
// Then, flush the intake request to APM server.
this._transport.flush({ lambdaEnd }, boundCb)
}, opts.inflightTimeoutMs)
// Create a new empty set to collect subsequent inflight events.
this._inflightEvents = new InflightEventSet()
}
Agent.prototype.registerMetric = function (name, labelsOrCallback, callback) {
var labels
if (typeof labelsOrCallback === 'function') {
callback = labelsOrCallback
} else {
labels = labelsOrCallback
}
if (typeof callback !== 'function') {
this.logger.error('Can\'t add callback of type %s', typeof callback)
return
}
this._metrics.getOrCreateGauge(name, callback, labels)
}
Agent.prototype.registerMetricCounter = function (name, dimensions) {
return this._metrics.getOrCreateCounter(name, dimensions)
}