@arcblock/pm2-prom-module 2.6.5 → 2.6.7
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/core/app.js +3 -0
- package/core/pm2.js +36 -0
- package/metrics/index.js +8 -1
- package/package.json +6 -1
- package/utils/domain.js +56 -0
package/core/app.js
CHANGED
package/core/pm2.js
CHANGED
|
@@ -17,10 +17,13 @@ const pm2_1 = __importDefault(require("pm2"));
|
|
|
17
17
|
const app_1 = require("./app");
|
|
18
18
|
const utils_1 = require("../utils");
|
|
19
19
|
const cpu_1 = require("../utils/cpu");
|
|
20
|
+
const keyBy_1 = __importDefault(require("lodash/keyBy"));
|
|
20
21
|
const metrics_1 = require("../metrics");
|
|
21
22
|
const app_2 = require("../metrics/app");
|
|
22
23
|
const logger_1 = require("../utils/logger");
|
|
23
24
|
const docker_1 = require("../utils/docker");
|
|
25
|
+
const domain_1 = require("../utils/domain");
|
|
26
|
+
const p_all_1 = __importDefault(require("p-all"));
|
|
24
27
|
const WORKER_CHECK_INTERVAL = 1000;
|
|
25
28
|
const SHOW_STAT_INTERVAL = 10000;
|
|
26
29
|
const APPS = {};
|
|
@@ -44,6 +47,9 @@ const updateAppPidsData = (workingApp, pidData) => {
|
|
|
44
47
|
createdAt: pidData.createdAt,
|
|
45
48
|
metrics: pidData.metrics,
|
|
46
49
|
status: pidData.status,
|
|
50
|
+
appUrl: pidData.appUrl,
|
|
51
|
+
appName: pidData.appName,
|
|
52
|
+
appPid: pidData.appPid,
|
|
47
53
|
});
|
|
48
54
|
};
|
|
49
55
|
const detectActiveApps = () => {
|
|
@@ -88,6 +94,9 @@ const detectActiveApps = () => {
|
|
|
88
94
|
createdAt: pm2_env.created_at || 0,
|
|
89
95
|
metrics: pm2_env.axm_monitor,
|
|
90
96
|
status: pm2_env.status,
|
|
97
|
+
appUrl: pm2_env.BLOCKLET_APP_URL,
|
|
98
|
+
appName: pm2_env.BLOCKLET_APP_NAME,
|
|
99
|
+
appPid: pm2_env.BLOCKLET_APP_PID,
|
|
91
100
|
};
|
|
92
101
|
}
|
|
93
102
|
});
|
|
@@ -197,6 +206,33 @@ const detectActiveApps = () => {
|
|
|
197
206
|
.catch((err) => {
|
|
198
207
|
console.error(err.stack || err);
|
|
199
208
|
});
|
|
209
|
+
const uniqAppMaps = (0, keyBy_1.default)(
|
|
210
|
+
// @ts-expect-error
|
|
211
|
+
apps.filter((x) => { var _a; return (_a = x.pm2_env) === null || _a === void 0 ? void 0 : _a.BLOCKLET_APP_PID; }), (x) => { var _a; return (_a = x.pm2_env) === null || _a === void 0 ? void 0 : _a.BLOCKLET_APP_PID; });
|
|
212
|
+
(0, p_all_1.default)(Object.values(uniqAppMaps)
|
|
213
|
+
.map((x) => x.pid)
|
|
214
|
+
.map((pid) => {
|
|
215
|
+
const app = pidsMonit[pid];
|
|
216
|
+
if (!app) {
|
|
217
|
+
throw new Error(`App ${pid} does not have active PIDs. Clear app metrics`);
|
|
218
|
+
}
|
|
219
|
+
return app;
|
|
220
|
+
})
|
|
221
|
+
.map((app) => {
|
|
222
|
+
return () => __awaiter(void 0, void 0, void 0, function* () {
|
|
223
|
+
return {
|
|
224
|
+
appName: app.appName,
|
|
225
|
+
appPid: app.appPid,
|
|
226
|
+
urls: yield (0, domain_1.getAppDomainList)(app.appUrl),
|
|
227
|
+
};
|
|
228
|
+
});
|
|
229
|
+
}), { concurrency: 8 }).then((apps) => {
|
|
230
|
+
for (const app of apps) {
|
|
231
|
+
for (const url of app.urls) {
|
|
232
|
+
metrics_1.metricAppDomainList === null || metrics_1.metricAppDomainList === void 0 ? void 0 : metrics_1.metricAppDomainList.set({ appName: app.appName, domain: url, appPid: app.appPid }, app.urls.length);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}).catch(error => console.error(error));
|
|
200
236
|
});
|
|
201
237
|
};
|
|
202
238
|
const startPm2Connect = (conf) => {
|
package/metrics/index.js
CHANGED
|
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.deletePromAppInstancesMetrics = exports.deletePromAppMetrics = exports.combineAllRegistries = exports.initDynamicGaugeMetricClients = exports.initMetrics = exports.dynamicGaugeMetricClients = exports.metricAppStatus = exports.metricAppPidsMemory = exports.metricAppUptime = exports.metricAppRestartCount = exports.metricAppPidsCpuThreshold = exports.metricAppPidsCpuLast = exports.metricAppAverageCpu = exports.metricAppTotalMemory = exports.metricAppAverageMemory = exports.metricAppInstances = exports.metricAvailableApps = exports.registry = void 0;
|
|
15
|
+
exports.deletePromAppInstancesMetrics = exports.deletePromAppMetrics = exports.combineAllRegistries = exports.initDynamicGaugeMetricClients = exports.initMetrics = exports.dynamicGaugeMetricClients = exports.metricAppStatus = exports.metricAppPidsMemory = exports.metricAppUptime = exports.metricAppRestartCount = exports.metricAppPidsCpuThreshold = exports.metricAppPidsCpuLast = exports.metricAppAverageCpu = exports.metricAppTotalMemory = exports.metricAppAverageMemory = exports.metricAppDomainList = exports.metricAppInstances = exports.metricAvailableApps = exports.registry = void 0;
|
|
16
16
|
const prom_client_1 = __importDefault(require("prom-client"));
|
|
17
17
|
const node_os_1 = __importDefault(require("node:os"));
|
|
18
18
|
const cpu_1 = require("../utils/cpu");
|
|
@@ -23,6 +23,7 @@ const METRIC_FREE_MEMORY = 'free_memory';
|
|
|
23
23
|
const METRIC_AVAILABLE_CPU = 'cpu_count';
|
|
24
24
|
const METRIC_AVAILABLE_APPS = 'available_apps';
|
|
25
25
|
const METRIC_APP_INSTANCES = 'app_instances';
|
|
26
|
+
const METRIC_APP_DOMAIN_LIST = 'app_domain_list';
|
|
26
27
|
const METRIC_APP_AVERAGE_MEMORY = 'app_average_memory';
|
|
27
28
|
const METRIC_APP_PIDS_MEMORY = 'app_pids_memory';
|
|
28
29
|
const METRIC_APP_TOTAL_MEMORY = 'app_total_memory';
|
|
@@ -146,6 +147,12 @@ const initMetrics = (prefix) => {
|
|
|
146
147
|
registers: [exports.registry],
|
|
147
148
|
labelNames: ['app'],
|
|
148
149
|
});
|
|
150
|
+
exports.metricAppDomainList = new prom_client_1.default.Gauge({
|
|
151
|
+
name: `${prefix}_${METRIC_APP_DOMAIN_LIST}`,
|
|
152
|
+
help: 'Show app domain list',
|
|
153
|
+
registers: [exports.registry],
|
|
154
|
+
labelNames: ['appName', 'domain', 'appPid'],
|
|
155
|
+
});
|
|
149
156
|
exports.metricAppAverageMemory = new prom_client_1.default.Gauge({
|
|
150
157
|
name: `${prefix}_${METRIC_APP_AVERAGE_MEMORY}`,
|
|
151
158
|
help: 'Show average using memory of an app',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcblock/pm2-prom-module",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.7",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -8,14 +8,19 @@
|
|
|
8
8
|
"main": "index.js",
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@abtnode/util": "^1.16.44",
|
|
11
|
+
"@keyv/sqlite": "^4.0.6",
|
|
11
12
|
"@types/dockerode": "^3.3.42",
|
|
12
13
|
"dockerode": "^4.0.7",
|
|
13
14
|
"internal-ip": "^6.2.0",
|
|
15
|
+
"is-url": "^1.2.4",
|
|
16
|
+
"keyv": "^5.5.3",
|
|
14
17
|
"lodash": "^4.17.21",
|
|
18
|
+
"p-all": "^3.0.0",
|
|
15
19
|
"pidusage": "^3.0.2",
|
|
16
20
|
"pm2": "^6.0.8",
|
|
17
21
|
"pmx": "beta",
|
|
18
22
|
"prom-client": "^15.1.3",
|
|
23
|
+
"ufo": "^1.6.1",
|
|
19
24
|
"xbytes": "^1.9.1",
|
|
20
25
|
"zx": "^8.8.0"
|
|
21
26
|
},
|
package/utils/domain.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.getAppDomainList = getAppDomainList;
|
|
16
|
+
const is_url_1 = __importDefault(require("is-url"));
|
|
17
|
+
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
18
|
+
const ufo_1 = require("ufo");
|
|
19
|
+
const keyv_1 = __importDefault(require("keyv"));
|
|
20
|
+
const sqlite_1 = __importDefault(require("@keyv/sqlite"));
|
|
21
|
+
const appDomainListCache = new keyv_1.default({
|
|
22
|
+
store: new sqlite_1.default({
|
|
23
|
+
// 这里只能相对路径才能工作
|
|
24
|
+
uri: 'sqlite://./cache.db',
|
|
25
|
+
table: 'app_domain_list_cache',
|
|
26
|
+
busyTimeout: 10000,
|
|
27
|
+
}),
|
|
28
|
+
// 默认缓存一个小时
|
|
29
|
+
ttl: 1000 * 60 * 60,
|
|
30
|
+
});
|
|
31
|
+
function getAppDomainList(url) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
var _a;
|
|
34
|
+
try {
|
|
35
|
+
if (!url) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
if (!(0, is_url_1.default)(url)) {
|
|
39
|
+
return [url];
|
|
40
|
+
}
|
|
41
|
+
if (yield appDomainListCache.has(url)) {
|
|
42
|
+
return yield appDomainListCache.get(url);
|
|
43
|
+
}
|
|
44
|
+
const response = yield fetch((0, ufo_1.joinURL)(url, '__blocklet__.js?type=json'));
|
|
45
|
+
const domainAliases = ((_a = (yield response.json())) === null || _a === void 0 ? void 0 : _a.domainAliases) || [];
|
|
46
|
+
if (!(0, isEmpty_1.default)(domainAliases)) {
|
|
47
|
+
yield appDomainListCache.set(url, domainAliases);
|
|
48
|
+
}
|
|
49
|
+
return domainAliases || [url];
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error(error);
|
|
53
|
+
return [url];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|