-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
55 lines (48 loc) · 1.76 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
'use strict';
const { BufferedMetricsLogger } = require('./lib/loggers');
const reporters = require('./lib/reporters');
/** @typedef {import("./lib/loggers").BufferedMetricsLoggerOptions} BufferedMetricsLoggerOptions */
let sharedLogger = null;
/**
* Configure the datadog-metrics library.
*
* Any settings used here will apply to the top-level metrics functions (e.g.
* `increment()`, `gauge()`). If you need multiple separate configurations, use
* the `BufferedMetricsLogger` class.
* @param {BufferedMetricsLoggerOptions} [opts]
*/
function init(opts) {
opts = opts || {};
if (!opts.flushIntervalSeconds && opts.flushIntervalSeconds !== 0) {
opts.flushIntervalSeconds = 15;
}
sharedLogger = new BufferedMetricsLogger(opts);
}
/**
* Wrap a function so that it gets called as a method of `sharedLogger`. If
* `sharedLogger` does not exist when the function is called, it will be
* created with default values.
* @template {Function} T
* @param {T} func The function to wrap.
* @returns {T}
*/
function callOnSharedLogger(func) {
// @ts-expect-error Can't find a good way to prove to the TypeScript
// compiler that this satisfies the types. :(
return (...args) => {
if (sharedLogger === null) {
init();
}
return func.apply(sharedLogger, args);
};
}
module.exports = {
init,
flush: callOnSharedLogger(BufferedMetricsLogger.prototype.flush),
gauge: callOnSharedLogger(BufferedMetricsLogger.prototype.gauge),
increment: callOnSharedLogger(BufferedMetricsLogger.prototype.increment),
histogram: callOnSharedLogger(BufferedMetricsLogger.prototype.histogram),
distribution: callOnSharedLogger(BufferedMetricsLogger.prototype.distribution),
BufferedMetricsLogger,
reporters
};