-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (68 loc) · 1.8 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
'use strict';
const StatsD = require('node-statsd');
let client;
module.exports = (cfg, options) => {
options = options || {};
options.clean = options.clean || '';
options.app = options.app || 'app';
options.env = options.env || 'dev';
options.hostname = options.hostname || 'localhost';
const init = () => {
if (client) {
return client;
}
client = new StatsD(cfg);
return client;
}
const clean = (stat) => {
stat = stat.replace(options.clean, '')
stat = stat.replace('._', '.');
stat = stat.replace('__.', '.');
stat = stat.replace('_.', '.');
return stat.replace(/\W+/g, '.').toLowerCase();
}
const underscore = (stat) => {
return stat.replace(/\W+/g, '_');
}
const metric = (stat) => {
return clean(`.${env}.${app}.${underscore(stat)}.${hostname}`);
}
let currentKey = '';
let env = underscore(options.env);
let app = underscore(options.app);
let hostname = underscore(options.hostname);
init();
return {
key: () => {
return currentKey;
},
timing: (stat, value) => {
currentKey = metric(stat);
client.timing(currentKey, value);
},
increment: (stat) => {
currentKey = metric(stat);
client.increment(currentKey);
},
decrement: (stat) => {
currentKey = metric(stat);
client.decrement(currentKey);
},
histogram: (stat, value) => {
currentKey = metric(stat);
client.histogram(currentKey, value);
},
gauge: (stat, value) => {
currentKey = metric(stat);
client.gauge(currentKey, value);
},
unique: (stat, value) => {
currentKey = metric(stat);
client.unique(currentKey, value);
},
set: (stat, value) => {
currentKey = metric(stat);
client.set(currentKey, value);
}
}
};