@arcblock/pm2-prom-module 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.prettierrc.js ADDED
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ bracketSpacing: true,
3
+ endOfLine: 'lf',
4
+ printWidth: 100,
5
+ singleQuote: true,
6
+ tabWidth: 4,
7
+ trailingComma: 'es5',
8
+ };
package/README.md ADDED
@@ -0,0 +1,220 @@
1
+ # PM2-Prom-Module [![npm version](https://badge.fury.io/js/pm2-prom-module.svg)](https://www.npmjs.com/package/pm2-prom-module)
2
+
3
+ PM2 module to help collect applications statistic and send it to Prometheus server
4
+
5
+ **NEW**: Starting from version 2.0.0, the plugin now supports the collection of all your Prometheus metrics from the running applications across multiple instances.
6
+
7
+ ## Motivation
8
+
9
+ Most of applications use Prometheus monitoring server to collect statistic and then show it on Grafana dashboard. PM2 gives you a way to monitor the resource usage of your application but unfortunately most of information is available from your terminal or with PM2 Plus dashboard. To solve this isses you should use additional module to export statistic data.
10
+
11
+ Also if you use [PM2-AutoScale](https://www.npmjs.com/package/pm2-autoscale) module you want to see how many active instances of the app is currently active and how many resources it using.
12
+
13
+ ## Solution
14
+
15
+ This module `pm2-prom-module` allows you to collect all PM2 monitoring data such as `CPU Usage`, `Memory Usage` and etc for your every applications and run HTTP server inside module to collect all metrics.
16
+
17
+ ### PM collected statistic
18
+
19
+ - Free memory
20
+ - CPUs count
21
+ - Count of running apps
22
+ - Count of instances for every app
23
+ - Average using memory for every app
24
+ - Total using memory for all instances of a app
25
+ - Average using CPU for every app
26
+ - Current CPU usage for every app instance
27
+ - Restarts count for every app instance
28
+ - Uptime for every app
29
+ - App status (0-unknown, 1-running, 2-pending, 3-stopped, 4-errored)
30
+ - Memory and CPU limits when run PM2 in docker container
31
+
32
+ Also collect all PM2 default metrics for every instance:
33
+
34
+ - Used Heap Size
35
+ - Heap Usage
36
+ - Heap Size
37
+ - Event Loop Latency p95
38
+ - Event Loop Latency
39
+ - Active handles
40
+ - Active requests
41
+ - HTTP req/min
42
+ - HTTP P95 Latency
43
+ - HTTP Mean Latency
44
+
45
+ ## Install
46
+
47
+ ```bash
48
+ pm2 install pm2-prom-module
49
+ ```
50
+
51
+ After installation module creates HTTPS server on address `http://localhost:9988/` and servers responds with Prometheus metrics
52
+
53
+ ## Uninstall
54
+
55
+ ```bash
56
+ pm2 uninstall pm2-prom-module
57
+ ```
58
+
59
+ ## Configuration
60
+
61
+ Default settings:
62
+
63
+ - `port` Connection port for Prometheus agent. (default to `9988`)
64
+ - `hostname` Listen address. (default to `0.0.0.0`)
65
+ - `unix_socket_path` Set if you want to listen on unix socket (default to `` - empty string)
66
+ - `service_name` Default label for registry (default to `` - empty string)
67
+ - `debug` Enable debug mode to show logs from the module (default to `false`)
68
+ - `aggregate_app_metrics` Enable to aggregate metrics from app instances (default to `true`)
69
+ - `app_check_interval` Interval to check available apps and collect statistic (default to `1000`)
70
+ - `prefix` Prefix for metrics (default to `pm2`)
71
+
72
+ To modify the module config values you can use the following commands:
73
+
74
+ ```bash
75
+ pm2 set pm2-prom-module:debug true
76
+ pm2 set pm2-prom-module:port 10801
77
+ pm2 set pm2-prom-module:hostname 127.0.0.1
78
+ pm2 set pm2-prom-module:service_name MyApp
79
+ pm2 set pm2-prom-module:aggregate_app_metrics true
80
+ pm2 set pm2-prom-module:app_check_interval 5000
81
+ ```
82
+
83
+ ## How to collect your own metrics from apps
84
+
85
+ As of plugin version `2.0.0`, it enables the collection of metrics from all currently running applications in PM2, presenting them in a unified output alongside other PM2 metrics.
86
+
87
+ To communicate between PM2 nodejs application and `pm2-prom-module`you can use [pm2-prom-module-client](https://www.npmjs.com/package/pm2-prom-module-client) which is using process messaging bus between module and an app.
88
+
89
+ Here an example how to use it:
90
+
91
+ ```typescript
92
+ import client from 'prom-client';
93
+ import { initMetrics } from 'pm2-prom-module-client';
94
+
95
+ const registry = new client.Registry();
96
+ const PREFIX = `nodejs_app_`;
97
+
98
+ const metricRequestCounter = new client.Counter({
99
+ name: `${PREFIX}request_counter`,
100
+ help: 'Show total request count',
101
+ registers: [registry],
102
+ });
103
+
104
+ // Register your prom-client Registry
105
+ initMetrics(registry);
106
+
107
+ // ...
108
+ app.get('/*', async (req: AppFastifyRequest, res) => {
109
+ // ...
110
+ metricRequestCounter?.inc();
111
+ // ...
112
+ });
113
+ ```
114
+
115
+ Plugin automatically collect metrics from all running instances of apps and all metrics will be available on the same endpoint with `pm2-prom-module`. Have a look on an example below.
116
+
117
+ Also you can add any of your own labels for any of `prom-client` counters, but `app`, and `instance` are reserved for plugin and will be overwritten.
118
+
119
+ Since version `2.1.0` module support aggregation data for all instances by default.
120
+ But you can disable it with module configuration.
121
+
122
+ ```bash
123
+ pm2 set pm2-prom-module:aggregate_app_metrics false
124
+ ```
125
+
126
+ Aggregated data from all running apps:
127
+
128
+ ```bash
129
+ # HELP nodejs_app_requests_total Show total request count
130
+ # TYPE nodejs_app_requests_total counter
131
+ nodejs_app_requests_total{app="app",serviceName="MyApp"} 8
132
+ nodejs_app_requests_total{app="app2",serviceName="MyApp"} 3
133
+ ```
134
+
135
+ ## Example output
136
+
137
+ ```bash
138
+ # HELP pm2_free_memory Show available host free memory
139
+ # TYPE pm2_free_memory gauge
140
+ pm2_free_memory{serviceName="my-app"} 377147392
141
+
142
+ # HELP pm2_cpu_count Show available CPUs count
143
+ # TYPE pm2_cpu_count gauge
144
+ pm2_cpu_count{serviceName="my-app"} 4
145
+
146
+ # HELP pm2_available_apps Show available apps to monitor
147
+ # TYPE pm2_available_apps gauge
148
+ pm2_available_apps{serviceName="my-app"} 1
149
+
150
+ # HELP pm2_app_instances Show app instances count
151
+ # TYPE pm2_app_instances gauge
152
+ pm2_app_instances{app="app",serviceName="my-app"} 2
153
+
154
+ # HELP pm2_app_average_memory Show average using memory of an app
155
+ # TYPE pm2_app_average_memory gauge
156
+ pm2_app_average_memory{app="app",serviceName="my-app"} 60813927
157
+
158
+ # HELP pm2_app_total_memory Show total using memory of an app
159
+ # TYPE pm2_app_total_memory gauge
160
+ pm2_app_total_memory{app="app",serviceName="my-app"} 121626624
161
+
162
+ # HELP pm2_event_loop_latency_p95 Event Loop Latency p95. Unit "ms"
163
+ # TYPE pm2_event_loop_latency_p95 gauge
164
+ pm2_event_loop_latency_p95{app="app",instance="13",serviceName="my-app"} 2.55
165
+ pm2_event_loop_latency_p95{app="app",instance="14",serviceName="my-app"} 2.48
166
+
167
+ # HELP nodejs_app_requests_total Show total request count
168
+ # TYPE nodejs_app_requests_total counter
169
+ nodejs_app_requests_total{app="app",instance="13",serviceName="my-app"} 10
170
+ nodejs_app_requests_total{app="app",instance="14",serviceName="my-app"} 17
171
+ ```
172
+
173
+ ## FAQ
174
+
175
+ ### How i can change `max-memory-restart`?
176
+
177
+ You can install plugin and restart it with the command:
178
+
179
+ ```
180
+ pm2 restart pm2-prom-module --max-memory-restart=3000M
181
+ ```
182
+
183
+ ## Change log
184
+
185
+ ### Version 2.6.0
186
+
187
+ - New logic to collect internal statistic from apps. Check status before send request to process via `sendDataToProcessId`
188
+
189
+ ### Version 2.5.4
190
+
191
+ - Change logic to detect app status to prevent failed request with `sendDataToProcessId`
192
+ - Add new metric `prefix` Prefix for metrics (default to `pm2`)
193
+
194
+ ```bash
195
+ pm2 set pm2-prom-module:prefix my_metrics
196
+ ```
197
+
198
+ ### Version 2.5.2
199
+
200
+ Additional metrics when PM2 running in docker container and you limit it with commands for example `docker run --memory="512m" --cpus="2"`
201
+
202
+ - Add new metric `pm2_container_total_memory` describes total available memory in docker container
203
+ - Add new metric `pm2_container_free_memory` describes free memory in docker container
204
+ - Add new metric `pm2_container_used_memory` describes used memory in container
205
+ - Add new metric `pm2_container_cpu_count` describes available CPUs limit in docker container
206
+
207
+ ### Version 2.4.0
208
+
209
+ - Add new metric `pm2_app_status` describes status of an app. (0-unknown,1-running,2-pending,3-stopped,4-errored)
210
+ All other metrics related to the app (not for pids) will be displayed. For example, `pm2_available_apps` will show app until
211
+ it will be deleted from the PM2 or `pm2_app_instances` will show 0 for `stopped` or `errored` status apps.
212
+
213
+ ### Version 2.3.2
214
+
215
+ - Add new config option `hostname`. Listen address. If you want to specify, for example, `localhost`, `127.0.0.1`
216
+ - Add new config option `unix_socket_path`. Run server on unix socket path.
217
+
218
+ ### Version 2.3.1
219
+
220
+ - Add new config option `app_check_interval`. Interval to check available apps and collect statistic
package/core/app.js ADDED
@@ -0,0 +1,236 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.App = exports.PM2_METRICS = exports.APP_STATUS = void 0;
4
+ const utils_1 = require("../utils");
5
+ const MONIT_ITEMS_LIMIT = 30;
6
+ var APP_STATUS;
7
+ (function (APP_STATUS) {
8
+ APP_STATUS[APP_STATUS["UNKNOWN"] = 0] = "UNKNOWN";
9
+ APP_STATUS[APP_STATUS["RUNNING"] = 1] = "RUNNING";
10
+ APP_STATUS[APP_STATUS["PENDING"] = 2] = "PENDING";
11
+ APP_STATUS[APP_STATUS["STOPPED"] = 3] = "STOPPED";
12
+ APP_STATUS[APP_STATUS["ERRORED"] = 4] = "ERRORED";
13
+ })(APP_STATUS || (exports.APP_STATUS = APP_STATUS = {}));
14
+ exports.PM2_METRICS = [
15
+ { name: 'Used Heap Size', unit: 'bytes' },
16
+ { name: 'Heap Usage', unit: '%' },
17
+ { name: 'Heap Size', unit: 'bytes' },
18
+ { name: 'Event Loop Latency p95', unit: 'ms' },
19
+ { name: 'Event Loop Latency', unit: 'ms' },
20
+ { name: 'Active handles', unit: 'number' },
21
+ { name: 'Active requests', unit: 'number' },
22
+ { name: 'HTTP', unit: 'req/min' },
23
+ { name: 'HTTP P95 Latency', unit: 'ms' },
24
+ { name: 'HTTP Mean Latency', unit: 'ms' },
25
+ ];
26
+ class App {
27
+ constructor(name) {
28
+ this.pids = {};
29
+ this.startTime = 0;
30
+ this.status = APP_STATUS.UNKNOWN;
31
+ this.isProcessing = false;
32
+ this.name = name;
33
+ }
34
+ removeNotActivePids(activePids) {
35
+ const removedValues = [];
36
+ Object.keys(this.pids).forEach((pid) => {
37
+ const pidData = this.pids[pid];
38
+ if (activePids.indexOf(Number(pid)) === -1) {
39
+ removedValues.push({ pid, pmId: pidData.pmId });
40
+ delete this.pids[pid];
41
+ }
42
+ });
43
+ return removedValues;
44
+ }
45
+ updatePid(pidData) {
46
+ const pid = pidData.id;
47
+ if (Object.keys(this.pids).length === 0) {
48
+ // Set start time first time when we update pids
49
+ this.startTime = pidData.createdAt;
50
+ }
51
+ if (!this.pids[pid]) {
52
+ this.pids[pid] = {
53
+ id: pid,
54
+ pmId: pidData.pmId,
55
+ memory: [pidData.memory],
56
+ cpu: [pidData.cpu],
57
+ restartCount: pidData.restartCount,
58
+ metrics: this.fillMetricsData(pidData.metrics),
59
+ status: pidData.status,
60
+ };
61
+ }
62
+ else {
63
+ const memoryValues = [pidData.memory, ...this.pids[pid].memory].slice(0, MONIT_ITEMS_LIMIT);
64
+ const cpuValues = [pidData.cpu, ...this.pids[pid].cpu].slice(0, MONIT_ITEMS_LIMIT);
65
+ this.pids[pid].memory = memoryValues;
66
+ this.pids[pid].cpu = cpuValues;
67
+ this.pids[pid].restartCount = pidData.restartCount;
68
+ this.pids[pid].metrics = this.fillMetricsData(pidData.metrics);
69
+ this.pids[pid].status = pidData.status;
70
+ }
71
+ return this;
72
+ }
73
+ updateStatus(status) {
74
+ switch (status) {
75
+ case 'online':
76
+ case 'one-launch-status':
77
+ this.status = APP_STATUS.RUNNING;
78
+ break;
79
+ case 'errored':
80
+ this.status = APP_STATUS.ERRORED;
81
+ break;
82
+ case 'stopped':
83
+ this.status = APP_STATUS.STOPPED;
84
+ break;
85
+ case 'launching':
86
+ case 'stopping':
87
+ this.status = APP_STATUS.PENDING;
88
+ break;
89
+ default:
90
+ this.status = APP_STATUS.UNKNOWN;
91
+ }
92
+ }
93
+ getStatus() {
94
+ return this.status;
95
+ }
96
+ getActivePm2Ids() {
97
+ const values = [];
98
+ for (const [, entry] of Object.entries(this.pids)) {
99
+ values.push(entry.pmId);
100
+ }
101
+ return values;
102
+ }
103
+ getMonitValues() {
104
+ return this.pids;
105
+ }
106
+ getAverageUsedMemory() {
107
+ const memoryValues = this.getAveragePidsMemory();
108
+ if (memoryValues.length) {
109
+ return Math.round(memoryValues.reduce((sum, value) => sum + value, 0) / memoryValues.length);
110
+ }
111
+ return 0;
112
+ }
113
+ getAverageCpu() {
114
+ const cpuValues = this.getAveragePidsCpu();
115
+ if (cpuValues.length) {
116
+ return Math.round(cpuValues.reduce((sum, value) => sum + value, 0) / cpuValues.length);
117
+ }
118
+ return 0;
119
+ }
120
+ getRestartCount() {
121
+ const values = [];
122
+ for (const [pid, entry] of Object.entries(this.pids)) {
123
+ values.push({
124
+ pid,
125
+ pmId: entry.pmId,
126
+ value: entry.restartCount,
127
+ });
128
+ }
129
+ return values;
130
+ }
131
+ getPidPm2Metrics() {
132
+ const values = [];
133
+ for (const [pid, entry] of Object.entries(this.pids)) {
134
+ values.push({
135
+ pid,
136
+ pmId: entry.pmId,
137
+ metrics: entry.metrics,
138
+ });
139
+ }
140
+ return values;
141
+ }
142
+ getCurrentPidsCpu() {
143
+ const values = [];
144
+ for (const [pid, entry] of Object.entries(this.pids)) {
145
+ values.push({
146
+ pid,
147
+ pmId: entry.pmId,
148
+ value: entry.cpu[0] || 0,
149
+ });
150
+ }
151
+ return values;
152
+ }
153
+ getCurrentPidsMemory() {
154
+ const values = [];
155
+ for (const [pid, entry] of Object.entries(this.pids)) {
156
+ values.push({
157
+ pid,
158
+ pmId: entry.pmId,
159
+ value: entry.memory[0] || 0,
160
+ });
161
+ }
162
+ return values;
163
+ }
164
+ getCpuThreshold() {
165
+ const values = [];
166
+ for (const [pid, entry] of Object.entries(this.pids)) {
167
+ const value = Math.round(entry.cpu.reduce((sum, value) => sum + value, 0) / entry.cpu.length);
168
+ values.push({
169
+ pid,
170
+ pmId: entry.pmId,
171
+ value: value,
172
+ });
173
+ }
174
+ return values;
175
+ }
176
+ getTotalUsedMemory() {
177
+ const memoryValues = [];
178
+ for (const [, entry] of Object.entries(this.pids)) {
179
+ if (entry.memory[0]) {
180
+ // Get the last memory value
181
+ memoryValues.push(entry.memory[0]);
182
+ }
183
+ }
184
+ return memoryValues.reduce((sum, value) => sum + value, 0);
185
+ }
186
+ getName() {
187
+ return this.name;
188
+ }
189
+ getActiveWorkersCount() {
190
+ return Object.keys(this.pids).length;
191
+ }
192
+ getUptime() {
193
+ if (Object.keys(this.pids).length === 0) {
194
+ return 0;
195
+ }
196
+ else {
197
+ return Math.round((Number(new Date()) - this.startTime) / 1000);
198
+ }
199
+ }
200
+ getAveragePidsMemory() {
201
+ const memoryValues = [];
202
+ for (const [, entry] of Object.entries(this.pids)) {
203
+ // Collect average memory for every pid
204
+ const value = Math.round(entry.memory.reduce((sum, value) => sum + value, 0) / entry.memory.length);
205
+ memoryValues.push(value);
206
+ }
207
+ return memoryValues;
208
+ }
209
+ getAveragePidsCpu() {
210
+ const cpuValues = [];
211
+ for (const [, entry] of Object.entries(this.pids)) {
212
+ const value = Math.round(entry.cpu.reduce((sum, value) => sum + value, 0) / entry.cpu.length);
213
+ cpuValues.push(value);
214
+ }
215
+ return cpuValues;
216
+ }
217
+ fillMetricsData(amxMetrics) {
218
+ const metrics = {};
219
+ if (amxMetrics) {
220
+ const availableMetrics = exports.PM2_METRICS.map((entry) => entry.name);
221
+ Object.keys(amxMetrics).forEach((key) => {
222
+ if (availableMetrics.indexOf(key) !== -1) {
223
+ const metricKey = (0, utils_1.toUndescore)(key);
224
+ // Force number for metrics
225
+ let value = Number(amxMetrics[key].value);
226
+ if (amxMetrics[key].unit === 'MiB') {
227
+ value = value * 1024 * 1024;
228
+ }
229
+ metrics[metricKey] = value;
230
+ }
231
+ });
232
+ }
233
+ return metrics;
234
+ }
235
+ }
236
+ exports.App = App;