-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppEventTweeter.js
86 lines (77 loc) · 2.12 KB
/
AppEventTweeter.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
/**
* AppEventTweeter
*/
var appmetrics = require('appmetrics');
var monitor = appmetrics.monitor();
var Twitter = require('twitter');
var gcMetrics = {
used : 0,
count : 0
};
var client = new Twitter({
consumer_key : process.env.TWITTER_CONSUMER_KEY,
consumer_secret : process.env.TWITTER_CONSUMER_SECRET,
access_token_key : process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret : process.env.TWITTER_ACCESS_TOKEN_SECRET
});
function tweetMessage(message) {
client.post('statuses/update', {
status : message
}, function(error, tweet, response) {
if (!error) {
// console.log(tweet);
}
});
}
// When we get a gc event, update our metrics
monitor.on('gc', function(data) {
gcMetrics.used += data.used;
gcMetrics.count++;
});
function getGCHeapData() {
if (gcMetrics.count > 0) {
var usedHeap = gcMetrics.used / gcMetrics.count;
// Reset values
gcMetrics.used = 0;
gcMetrics.count = 0;
} else {
usedHeap = -1;
}
return usedHeap;
}
var interval;
var usedHeapOld = -1;
var timingInterval = 10000; // 10 seconds
var detectionCount = 0;
start();
function start() {
interval = setInterval(
function() {
var usedHeap = getGCHeapData();
// check we have a usedHeap average
if (usedHeap != -1) {
// if usedHeapOld is still unset, update to new value
if (usedHeapOld == -1) {
usedHeapOld = usedHeap;
} else {
// has used heap increased
if (usedHeap > usedHeapOld) {
detectionCount++;
// Once we have hit this 5 times in a row, tell someone
if (detectionCount > 5) {
tweetMessage("Average heap use is increasing. Currently at "
+ Number((usedHeap / (1024 * 1024)).toFixed(3)) + "MB");
detectionCount = 0;
}
} else if (usedHeap < usedHeapOld) {
// if its decreasing reset counter
detectionCount = 0;
}
usedHeapOld = usedHeap;
}
}
}, timingInterval).unref();
}
function stop() {
clearInterval(interval);
}