-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (71 loc) · 2.15 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
/**
* @file 监控平台打点组件
* @author ss.feng
*/
const graphite = require('graphite');
const util = require('./libs/utils');
class Watcher {
constructor(options, callback) {
let __rate = options.rate || 1;
let __host = options.host || '';
let __port = options.port || 2013;
let __callback = callback || function() {};
__rate = __rate * (1000 * 60);
let __url = 'plaintext://' + __host + ':' + __port;
let __timeCache = {};
let __metrics = this.metrics = {};
this.count = function(key) {
key += util.suffix(key);
if (__metrics[key]) {
__metrics[key]++;
} else {
__metrics[key] = 1;
}
};
this.time = function(key) {
key += util.suffix(key);
if (__timeCache[key]) {
__timeCache[key]['count'] += 1;
__timeCache[key]['timestamp'] += timestamp;
} else {
__timeCache[key] = {
count: 1,
timestamp: timestamp,
};
}
let count = __timeCache[key].count;
let time = __timeCache[key].timestamp;
__metrics[key] = time / count;
};
function send() {
setTimeout(() => {
sendLogToWatcher();
}, __rate);
}
function sendLogToWatcher() {
let client = graphite.createClient(__url);
client.write(__metrics, err => {
client.end();
if (err) {
__callback(err);
return 'watcher日志发送错误';
}
Object.keys(__metrics).forEach(key => {
__metrics[key] = 0;
});
Object.keys(__timeCache).forEach(key => {
__timeCache[key] = null;
});
});
}
// 入口
send();
}
set metrics(newValue) {
this._metrics = newValue;
}
get metrics() {
return this._metrics;
}
}
module.exports = Watcher;