-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (40 loc) · 1.42 KB
/
app.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
const promClient = require("prom-client");
const registries = [promClient.Registry.globalRegistry];
const MetricsClient = require('./app/lib/metrics-client');
module.exports = app => {
// const index = app.config.coreMiddleware.indexOf('bodyParser');
// assert(index >= 0, 'bodyParser 中间件必须存在');
app.config.coreMiddleware.push('eggSwaggerStats');
const config = app.config.metricsClient;
app.metricsClient = new MetricsClient(Object.assign({}, config, { cluster: app.cluster }));
app.beforeStart(async () => {
await app.metricsClient.ready();
app.avaibleWorkers = new Set();
// send oneline signal to other worker
app.metricsClient.subscribe('$metrics$worker_on', worker => {
app.avaibleWorkers.add(worker.pid);
});
// subscribe other worker online signal
app.metricsClient.publish({
dataId: '$metrics$worker_on',
message: {
pid: process.pid
}
});
app.metricsClient.subscribe('revise-workers', message => {
if (message.workers.length > 0) {
app.avaibleWorkers = new Set(message.workers);
}
});
app.metricsClient.subscribe('gime_metrics', message => {
app.metricsClient.publish({
dataId: 'gime_metrics_back_' + message.target,
message: {
requestId: message.requestId,
pid: process.pid,
metrics: registries.map(r => r.getMetricsAsJSON())
}
});
})
});
};