forked from VeXell/pm2-prom-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
67 lines (54 loc) · 2.01 KB
/
index.ts
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
// @ts-ignore
import pmx from 'pmx';
import { createServer, ServerResponse, IncomingMessage } from 'http';
import { startPm2Connect } from './core/pm2';
import { initLogger } from './utils/logger';
import { initMetrics, combineAllRegistries } from './metrics';
const DEFAULT_PREFIX = 'pm2';
const startPromServer = (prefix: string, moduleConfig: IConfig) => {
initMetrics(prefix);
const serviceName = moduleConfig.service_name;
const port = moduleConfig.port;
const promServer = createServer(async (_req: IncomingMessage, res: ServerResponse) => {
const mergedRegistry = combineAllRegistries(Boolean(moduleConfig.aggregate_app_metrics));
mergedRegistry.setDefaultLabels({ serviceName });
res.setHeader('Content-Type', mergedRegistry.contentType);
res.end(await mergedRegistry.metrics());
return;
});
promServer.listen(port, () => console.log(`Metrics server is available on port ${port}.`));
};
pmx.initModule(
{
widget: {
el: {
probes: true,
actions: true,
},
block: {
actions: false,
issues: true,
meta: true,
},
},
},
function (err: any, conf: IPMXConfig) {
if (err) return console.error(err.stack || err);
const moduleConfig = conf.module_conf;
initLogger({ isDebug: moduleConfig.debug });
startPm2Connect(moduleConfig);
startPromServer(DEFAULT_PREFIX, moduleConfig);
pmx.configureModule({
human_info: [
['Status', 'Module enabled'],
['Debug', moduleConfig.debug ? 'Enabled' : 'Disabled'],
[
'Aggregate apps metrics',
moduleConfig.aggregate_app_metrics ? 'Enabled' : 'Disabled',
],
['Port', moduleConfig.port],
['Service name', moduleConfig.service_name ? moduleConfig.service_name : `N/A`],
],
});
}
);