-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (90 loc) · 2.12 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
102
103
104
105
106
/**
* @autor Kristof Friess
* @company Zebresel
* @copyright Since 2018 by Zebresel - Deine Agentur für digitale Medien
*/
/*jshint esversion: 6 */
/*jshint node: true*/
const request = require('./core/request.js');
const CPULoad = require('./core/cpu.js');
const MemoryLoad = require('./core/memory.js');
const { fork } = require('child_process');
module.exports = function(opts)
{
const defaultOpts = {
ssl: true,
appId: null,
appSecret: null,
host: 'localhost',
port: 3000,
path: '/posts',
debug: false,
cpuLoad: true,
stackUse: false,
updateInterval: 100000, // 1mins
cpu: {
updateInterval: 10000 // 10 second
},
memoryLoad: true,
memory: {
updateInterval: 10000 // 10 second
},
diskUsage: true,
disk: {
updateInterval: 60000 // 60 second
},
};
opts = Object.assign(defaultOpts, opts) || defaultOpts;
opts.parentPid = process.pid;
let optsString = JSON.stringify(opts);
function startFork()
{
forked = fork(__dirname + '/core/fork.js', [
optsString
]);
forked.on('close', function(code, signal){
process.nextTick(function(){
startFork();
});
});
// forked.on('exit', function(code, signal){
// console.log('Exit: ', code, signal);
// });
// forked.on('error', function(err){
// console.log('Error: ', err);
// });
}
startFork();
return function(req, res, next){
const start = Date.now();
// The 'finish' event will emit once the response is done sending
res.once('finish', function()
{
const end = Date.now();
try
{
forked.send({
post: {
type: 'http',
startTime: start,
endTime: end,
status: res.statusCode >= 400 ? 2 : 0,
data: {
duration: (end-start),
dunit: 'ms',
method: req.method,
httpCode: res.statusCode,
path: req.originalUrl
}
}
});
}
catch(err)
{
console.error('[APM][Middleware] Error on sending data to forked process');
console.error(err);
}
});
next();
}
};