This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
forked from dbader/node-datadog-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (47 loc) · 1.73 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
'use strict';
const loggers = require('./lib/loggers');
const reporters = require('./lib/reporters');
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 {loggers.BufferedMetricsLoggerOptions} [opts]
*/
function init(opts) {
opts = opts || {};
if (!opts.flushIntervalSeconds && opts.flushIntervalSeconds !== 0) {
opts.flushIntervalSeconds = 15;
}
sharedLogger = new loggers.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(loggers.BufferedMetricsLogger.prototype.flush),
gauge: callOnSharedLogger(loggers.BufferedMetricsLogger.prototype.gauge),
increment: callOnSharedLogger(loggers.BufferedMetricsLogger.prototype.increment),
histogram: callOnSharedLogger(loggers.BufferedMetricsLogger.prototype.histogram),
distribution: callOnSharedLogger(loggers.BufferedMetricsLogger.prototype.distribution),
BufferedMetricsLogger: loggers.BufferedMetricsLogger,
reporters
};