-
Notifications
You must be signed in to change notification settings - Fork 160
/
monitor.js
executable file
·175 lines (122 loc) · 4.98 KB
/
monitor.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
const Os = require('os');
const Hoek = require('@hapi/hoek');
const Oppsy = require('@hapi/oppsy');
const Pumpify = require('pumpify');
const Package = require('../package.json');
const Utils = require('./utils');
const internals = {
host: Os.hostname(),
appVer: Package.version
};
module.exports = internals.Monitor = class {
constructor(server, options) {
this.settings = options;
this._state = { report: false };
this._server = server;
this._reporters = new Map();
const reducer = (obj, value) => {
obj[value] = true;
return obj;
};
const reqOptions = this.settings.includes.request.reduce(reducer, {});
const resOptions = this.settings.includes.response.reduce(reducer, {});
this._ops = this.settings.ops && new Oppsy(server, this.settings.ops.config);
// Event handlers
this._requestLogHandler = (request, event) => {
this.push(() => new Utils.RequestLog(reqOptions, request, event));
};
this._logHandler = (event) => {
this.push(() => new Utils.ServerLog(event));
};
this._errorHandler = (request, error) => {
this.push(() => new Utils.RequestError(reqOptions, request, error));
};
this._responseHandler = (request) => {
this.push(() => new Utils.RequestSent(reqOptions, resOptions, request, this._server));
};
this._opsHandler = (results) => {
this.push(() => new Utils.Ops(results));
};
}
startOps(interval) {
this._ops && this._ops.start(interval);
}
start() {
for (const reporterName in this.settings.reporters) {
const streamsSpec = this.settings.reporters[reporterName];
if (!streamsSpec.length) {
continue;
}
const streamObjs = [];
for (let i = 0; i < streamsSpec.length; ++i) {
const spec = streamsSpec[i];
// Already created stream
if (typeof spec.pipe === 'function') {
streamObjs.push(spec);
continue;
}
// If this is stderr or stdout
if (process[spec]) {
streamObjs.push(process[spec]);
continue;
}
const isFn = typeof spec.module === 'function';
const moduleName = isFn ? spec.module.name || `The unnamed module at position ${i}` : spec.module;
let Ctor = isFn ? spec.module : require(require.resolve(spec.module, { paths: require.main.paths }));
Ctor = spec.name ? Ctor[spec.name] : Ctor;
Hoek.assert(typeof Ctor === 'function', `Error in ${reporterName}. ${moduleName} must be a constructor function.`);
const stream = spec.args ? new Ctor(...spec.args) : new Ctor();
Hoek.assert(typeof stream.pipe === 'function', `Error in ${reporterName}. ${moduleName} must create a stream that has a pipe function.`);
streamObjs.push(stream);
}
if (streamObjs.length === 1) {
streamObjs.unshift(new Utils.NoOp());
}
this._reporters.set(reporterName, Pumpify.obj(streamObjs)).get(reporterName).on('error', (err) => {
console.error(`There was a problem (${err}) in ${reporterName} and it has been destroyed.`);
console.error(err);
});
}
this._state.report = true;
// Initialize Events
this._server.events.on('log', this._logHandler);
this._server.events.on({ name: 'request', channels: ['error'] }, this._errorHandler);
this._server.events.on('response', this._responseHandler);
this._server.events.on({ name: 'request', channels: ['app'] }, this._requestLogHandler);
if (this._ops) {
this._ops.on('ops', this._opsHandler);
this._ops.on('error', console.error);
}
// Events can not be any of ['log', 'ops', 'request', 'response', 'tail']
for (const event of this.settings.extensions) {
this._server.events.on(event, (...args) => {
const payload = {
event,
timestamp: Date.now(),
payload: args
};
this.push(() => Object.assign({}, payload));
});
}
}
stop() {
this._state.report = false;
if (this._ops) {
this._ops.stop();
this._ops.removeAllListeners();
}
for (const reporter of this._reporters.values()) {
reporter.end();
}
}
push(value) {
if (this._state.report) {
for (const reporter of this._reporters.values()) {
if (reporter.destroyed === false) {
reporter.write(value());
}
}
}
}
};