1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const prometheus = require("prom-client");
|
4 | const gcStats = require("prometheus-gc-stats");
|
5 | const NewrelicUtil_1 = require("../newrelic/NewrelicUtil");
|
6 | const AdminPortPlugin_1 = require("../web/AdminPortPlugin");
|
7 | const LogManager_1 = require("../log/LogManager");
|
8 | const OSMetricsService_1 = require("./OSMetricsService");
|
9 | const Logger = LogManager_1.LogManager.getLogger(__filename);
|
10 | class MetricsPlugin {
|
11 | constructor() {
|
12 | this.name = 'MetricsPlugin';
|
13 | this.osMetricsService = new OSMetricsService_1.OSMetricsService();
|
14 | }
|
15 | getName() {
|
16 | return this.name;
|
17 | }
|
18 | didStart(app, pluginContext) {
|
19 | this.prometheusTimer = prometheus.collectDefaultMetrics();
|
20 | prometheus.register.setDefaultLabels({ app: app.getConfig('app.name', 'not_set') });
|
21 | const startGcStats = gcStats();
|
22 | startGcStats();
|
23 | this.registerOSMetrics(app);
|
24 | const context = app.getContext();
|
25 | const adminExpress = pluginContext.get(AdminPortPlugin_1.default.CONTEXT_APP_KEY);
|
26 | if (adminExpress) {
|
27 | Logger.info('Registering metrics endpoint in Admin port');
|
28 | adminExpress.get('/metrics', async (req, res) => {
|
29 | NewrelicUtil_1.NewrelicUtil.setIgnoreTransaction(true);
|
30 | res.type('text/plain');
|
31 | res.send(prometheus.register.metrics());
|
32 | });
|
33 | }
|
34 | }
|
35 | registerOSMetrics(app) {
|
36 | if (app.getConfig('metrics.osmetrics.enabled', 'true') !== 'false') {
|
37 | this.lastMetrics = this.osMetricsService.getOSMetrics();
|
38 | if (this.lastMetrics.user !== undefined) {
|
39 |
|
40 | this.cpuStats = new prometheus.Counter({
|
41 | name: 'proc_cpu',
|
42 | help: 'CPU stats from the OS',
|
43 | labelNames: ['type'],
|
44 | });
|
45 | }
|
46 | else {
|
47 | Logger.info('Can\'t collect cpu stats (this OS doesn\'t have procfs mounted!. Skipping');
|
48 | }
|
49 | if (this.lastMetrics.load1 !== undefined) {
|
50 |
|
51 | this.loadStats = new prometheus.Gauge({
|
52 | name: 'proc_load',
|
53 | help: 'Load information',
|
54 | labelNames: ['period'],
|
55 | });
|
56 | }
|
57 | else {
|
58 | Logger.info('Can\'t collect load average stats (this OS doesn\'t have procfs mounted!. Skipping');
|
59 | }
|
60 | const interval = app.getConfig('metrics.osmetrics.refreshMillis', 20000);
|
61 | this.statsTimer = setInterval(() => { this.pushStats(); }, typeof interval === 'string' ? parseInt(interval, 10) : interval);
|
62 | }
|
63 | }
|
64 | pushStats() {
|
65 | const newStats = this.osMetricsService.getOSMetrics();
|
66 | if (this.cpuStats) {
|
67 | OSMetricsService_1.CPUOSMetricNames.forEach((name) => this.pushIndividualCPUStat(name, newStats));
|
68 | }
|
69 | if (this.loadStats) {
|
70 | OSMetricsService_1.LoadOSMetricNames.forEach((name) => this.pushIndividualLoadStat(name, newStats));
|
71 | }
|
72 | this.lastMetrics = newStats;
|
73 | }
|
74 | pushIndividualCPUStat(name, newStats) {
|
75 | if (newStats[name] !== undefined && this.lastMetrics[name] !== undefined) {
|
76 | this.cpuStats.inc({ type: name }, newStats[name] - this.lastMetrics[name]);
|
77 | }
|
78 | }
|
79 | pushIndividualLoadStat(name, newStats) {
|
80 | if (newStats[name] !== undefined && this.lastMetrics[name] !== undefined) {
|
81 | this.loadStats.set({ period: name }, newStats[name]);
|
82 | }
|
83 | }
|
84 | didStop() {
|
85 | if (this.statsTimer) {
|
86 | clearInterval(this.statsTimer);
|
87 | }
|
88 | prometheus.register.clear();
|
89 | if (this.prometheusTimer) {
|
90 | Logger.info('Shutting down prometheus interval');
|
91 | clearInterval(this.prometheusTimer);
|
92 | }
|
93 | }
|
94 | }
|
95 | exports.default = MetricsPlugin;
|
96 |
|
\ | No newline at end of file |