@adalo/metrics 0.0.0-staging.17 → 0.0.0-staging.18
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/lib/metrics/httpMetricsRedisCollector.d.ts.map +1 -1
- package/lib/metrics/httpMetricsRedisCollector.js +23 -1
- package/lib/metrics/httpMetricsRedisCollector.js.map +1 -1
- package/lib/metrics/httpMetricsRedisRecorder.d.ts +0 -3
- package/lib/metrics/httpMetricsRedisRecorder.d.ts.map +1 -1
- package/lib/metrics/httpMetricsRedisRecorder.js +0 -32
- package/lib/metrics/httpMetricsRedisRecorder.js.map +1 -1
- package/lib/metrics/httpMetricsRedisStore.d.ts.map +1 -1
- package/lib/metrics/httpMetricsRedisStore.js +18 -1
- package/lib/metrics/httpMetricsRedisStore.js.map +1 -1
- package/lib/metrics/metricsDatabaseClient.d.ts.map +1 -1
- package/lib/metrics/metricsDatabaseClient.js.map +1 -1
- package/lib/metrics/metricsProcessTypeUtils.d.ts.map +1 -1
- package/lib/metrics/metricsProcessTypeUtils.js.map +1 -1
- package/lib/metrics/metricsQueueRedisClient.d.ts.map +1 -1
- package/lib/metrics/metricsQueueRedisClient.js.map +1 -1
- package/package.json +1 -1
- package/src/metrics/httpMetricsRedisCollector.js +26 -1
- package/src/metrics/httpMetricsRedisRecorder.js +1 -43
- package/src/metrics/httpMetricsRedisStore.js +43 -4
- package/src/metrics/metricsDatabaseClient.js +5 -1
- package/src/metrics/metricsProcessTypeUtils.js +5 -1
- package/src/metrics/metricsQueueRedisClient.js +5 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpMetricsRedisCollector.d.ts","sourceRoot":"","sources":["../../src/metrics/httpMetricsRedisCollector.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"httpMetricsRedisCollector.d.ts","sourceRoot":"","sources":["../../src/metrics/httpMetricsRedisCollector.js"],"names":[],"mappings":"AAeA;;;;;;;GAOG;AACH;IACE;;;;;;;;;;;;;;;;OAgBG;IACH;qBAfW,OAAO,OAAO,EAAE,WAAW;;;;;;;;;;;;;;mBAoErC;IAhCC,8BAKE;CA6DL"}
|
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const cluster = require('cluster');
|
|
3
4
|
const {
|
|
4
5
|
BaseMetricsClient
|
|
5
6
|
} = require('./baseMetricsClient');
|
|
6
7
|
const {
|
|
7
8
|
HttpMetricsRedisStore
|
|
8
9
|
} = require('./httpMetricsRedisStore');
|
|
10
|
+
function clusterWorkerTag() {
|
|
11
|
+
try {
|
|
12
|
+
if (cluster.isWorker && cluster.worker) {
|
|
13
|
+
return ` cluster_worker_id=${cluster.worker.id}`;
|
|
14
|
+
}
|
|
15
|
+
} catch (_) {
|
|
16
|
+
/* ignore */
|
|
17
|
+
}
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
9
20
|
|
|
10
21
|
/**
|
|
11
22
|
* Drain worker: reads HTTP aggregates from Redis (written by {@link HttpMetricsRedisRecorder}),
|
|
@@ -74,10 +85,21 @@ class HttpMetricsRedisCollector extends BaseMetricsClient {
|
|
|
74
85
|
* @returns {Promise<void>}
|
|
75
86
|
*/
|
|
76
87
|
pushMetrics = async () => {
|
|
88
|
+
const {
|
|
89
|
+
pid
|
|
90
|
+
} = process;
|
|
91
|
+
const clusterTag = clusterWorkerTag();
|
|
92
|
+
// --- TEMP_HTTP_METRICS_DIAG: remove when done debugging ---
|
|
93
|
+
console.warn(`[TEMP_HTTP_METRICS_DIAG] http_metrics_publish_start pid=${pid}${clusterTag} dyno_id=${this.dynoId} process_type=${this.processType} app=${this.appName}`);
|
|
94
|
+
// --- end TEMP_HTTP_METRICS_DIAG ---
|
|
77
95
|
if (this._store && this.countersFunctions?.app_requests_total && this.countersFunctions?.app_requests_total_duration) {
|
|
78
96
|
await this._store.flushToCounters((labels, value) => this.countersFunctions.app_requests_total(labels, value), (labels, value) => this.countersFunctions.app_requests_total_duration(labels, value));
|
|
79
97
|
}
|
|
80
|
-
|
|
98
|
+
const out = await this._pushMetrics();
|
|
99
|
+
// --- TEMP_HTTP_METRICS_DIAG: remove when done debugging ---
|
|
100
|
+
console.warn(`[TEMP_HTTP_METRICS_DIAG] http_metrics_publish_finished pid=${pid}${clusterTag} dyno_id=${this.dynoId} (registry POST to VM-agent / import completed for this tick)`);
|
|
101
|
+
// --- end TEMP_HTTP_METRICS_DIAG ---
|
|
102
|
+
return out;
|
|
81
103
|
};
|
|
82
104
|
}
|
|
83
105
|
module.exports = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpMetricsRedisCollector.js","names":["
|
|
1
|
+
{"version":3,"file":"httpMetricsRedisCollector.js","names":["cluster","require","BaseMetricsClient","HttpMetricsRedisStore","clusterWorkerTag","isWorker","worker","id","_","HttpMetricsRedisCollector","constructor","config","redisClient","Error","blockNodeDefaultMetrics","keyProcessType","redisProcessTypeForKeys","process","env","METRICS_HTTP_REDIS_KEY_PROCESS_TYPE","defaultLabelsWithoutDynoId","app","appName","process_type","_store","processType","ttlSec","createCounter","name","help","labelNames","withDefaultLabelsWithoutDynoId","useLabelsWithoutDynoId","pushMetrics","pid","clusterTag","console","warn","dynoId","countersFunctions","app_requests_total","app_requests_total_duration","flushToCounters","labels","value","out","_pushMetrics","module","exports"],"sources":["../../src/metrics/httpMetricsRedisCollector.js"],"sourcesContent":["const cluster = require('cluster')\nconst { BaseMetricsClient } = require('./baseMetricsClient')\nconst { HttpMetricsRedisStore } = require('./httpMetricsRedisStore')\n\nfunction clusterWorkerTag() {\n try {\n if (cluster.isWorker && cluster.worker) {\n return ` cluster_worker_id=${cluster.worker.id}`\n }\n } catch (_) {\n /* ignore */\n }\n return ''\n}\n\n/**\n * Drain worker: reads HTTP aggregates from Redis (written by {@link HttpMetricsRedisRecorder}),\n * applies them to `app_requests_*` counters, then pushes the registry to the VM-agent (same as {@link BaseMetricsClient}).\n * Always passes `blockNodeDefaultMetrics: true` to the base client (HTTP-only registry; no default Node heap/event-loop metrics).\n * Redis key segment must match writers (`appName` + `processType` / {@link HttpMetricsRedisStore}).\n *\n * @extends BaseMetricsClient\n */\nclass HttpMetricsRedisCollector extends BaseMetricsClient {\n /**\n * @param {Object} [config]\n * @param {import('redis').RedisClient} config.redisClient **Required.** Injected client (same pattern as {@link RedisMetricsClient}).\n * @param {string} [config.appName] Application name (defaults per {@link BaseMetricsClient})\n * @param {string} [config.dynoId] Dyno/instance ID\n * @param {string} [config.processType] Label `process_type` on push (default from env / base)\n * @param {boolean} [config.enabled] Enable collection and push\n * @param {boolean} [config.logValues] Log metric JSON to console\n * @param {string} [config.pushgatewayUrl] VM-agent import URL\n * @param {string} [config.pushgatewaySecret] Basic auth secret (Base64)\n * @param {number} [config.intervalSec] Push interval (seconds)\n * @param {boolean} [config.removeOldMetrics] Clear old series on shutdown where supported\n * @param {function} [config.startupValidation] Run before first push\n * @param {boolean} [config.disablePushgateway] Skip POST to VM-agent\n * @param {string} [config.redisProcessTypeForKeys] Segment in Redis keys for HTTP hashes (default `web`; env `METRICS_HTTP_REDIS_KEY_PROCESS_TYPE` overrides)\n * @param {number} [config.ttlSec] Passed to {@link HttpMetricsRedisStore} (should match writers)\n */\n constructor(config = {}) {\n const { redisClient } = config\n if (redisClient == null) {\n throw new Error('HttpMetricsRedisCollector: redisClient is required')\n }\n\n super({\n ...config,\n blockNodeDefaultMetrics: true,\n })\n\n const keyProcessType =\n config.redisProcessTypeForKeys ||\n process.env.METRICS_HTTP_REDIS_KEY_PROCESS_TYPE ||\n 'web'\n\n this.defaultLabelsWithoutDynoId = {\n app: this.appName,\n process_type: keyProcessType,\n }\n\n this._store = new HttpMetricsRedisStore({\n redisClient,\n appName: this.appName,\n processType: keyProcessType,\n ttlSec: config.ttlSec,\n })\n\n this.createCounter({\n name: 'app_requests_total',\n help: 'Total number of HTTP requests',\n labelNames: this.withDefaultLabelsWithoutDynoId([\n 'method',\n 'route',\n 'appId',\n 'databaseId',\n 'status_code',\n ]),\n useLabelsWithoutDynoId: true,\n })\n\n this.createCounter({\n name: 'app_requests_total_duration',\n help: 'Total duration of HTTP requests in milliseconds',\n labelNames: this.withDefaultLabelsWithoutDynoId([\n 'method',\n 'route',\n 'appId',\n 'databaseId',\n 'status_code',\n ]),\n useLabelsWithoutDynoId: true,\n })\n }\n\n /**\n * Drains Redis into counters, then runs gauge updates and VM-agent push ({@link BaseMetricsClient#_pushMetrics}).\n * @returns {Promise<void>}\n */\n pushMetrics = async () => {\n const { pid } = process\n const clusterTag = clusterWorkerTag()\n // --- TEMP_HTTP_METRICS_DIAG: remove when done debugging ---\n console.warn(\n `[TEMP_HTTP_METRICS_DIAG] http_metrics_publish_start pid=${pid}${clusterTag} dyno_id=${this.dynoId} process_type=${this.processType} app=${this.appName}`\n )\n // --- end TEMP_HTTP_METRICS_DIAG ---\n if (\n this._store &&\n this.countersFunctions?.app_requests_total &&\n this.countersFunctions?.app_requests_total_duration\n ) {\n await this._store.flushToCounters(\n (labels, value) =>\n this.countersFunctions.app_requests_total(labels, value),\n (labels, value) =>\n this.countersFunctions.app_requests_total_duration(labels, value)\n )\n }\n const out = await this._pushMetrics()\n // --- TEMP_HTTP_METRICS_DIAG: remove when done debugging ---\n console.warn(\n `[TEMP_HTTP_METRICS_DIAG] http_metrics_publish_finished pid=${pid}${clusterTag} dyno_id=${this.dynoId} (registry POST to VM-agent / import completed for this tick)`\n )\n // --- end TEMP_HTTP_METRICS_DIAG ---\n return out\n }\n}\n\nmodule.exports = { HttpMetricsRedisCollector }\n"],"mappings":";;AAAA,MAAMA,OAAO,GAAGC,OAAO,CAAC,SAAS,CAAC;AAClC,MAAM;EAAEC;AAAkB,CAAC,GAAGD,OAAO,CAAC,qBAAqB,CAAC;AAC5D,MAAM;EAAEE;AAAsB,CAAC,GAAGF,OAAO,CAAC,yBAAyB,CAAC;AAEpE,SAASG,gBAAgBA,CAAA,EAAG;EAC1B,IAAI;IACF,IAAIJ,OAAO,CAACK,QAAQ,IAAIL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
|
|
21
21
|
processType: string;
|
|
22
22
|
appName: string;
|
|
23
23
|
_store: HttpMetricsRedisStore;
|
|
24
|
-
_diagWindowStart: number;
|
|
25
|
-
_diagWindowCount: number;
|
|
26
|
-
_diagIntervalMs: number;
|
|
27
24
|
/**
|
|
28
25
|
* @param {Object} params
|
|
29
26
|
* @param {string} params.method
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpMetricsRedisRecorder.d.ts","sourceRoot":"","sources":["../../src/metrics/httpMetricsRedisRecorder.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"httpMetricsRedisRecorder.d.ts","sourceRoot":"","sources":["../../src/metrics/httpMetricsRedisRecorder.js"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH;IACE;;;;;;OAMG;IACH;QAL6C,WAAW,EAA7C,OAAO,OAAO,EAAE,WAAW;QACd,OAAO,EAApB,MAAM;QACO,WAAW,EAAxB,MAAM;QACQ,MAAM;OAc9B;IARC,oBAA8B;IAC9B,gBAAsB;IACtB,8BAKE;IAGJ;;;;;;;;OAQG;IACH;QAP0B,MAAM,EAArB,MAAM;QACS,KAAK,EAApB,MAAM;QACS,WAAW,EAA1B,MAAM;QACU,KAAK;QACL,UAAU;QACX,QAAQ,EAAvB,MAAM;aAWhB;IAED;;;;;;;OAOG;IACH,kCAJW,OAAO,MAAM,EAAE,eAAe,OAC9B,OAAO,MAAM,EAAE,cAAc,0BAkCvC;CACF"}
|
|
@@ -1,19 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const cluster = require('cluster');
|
|
4
3
|
const {
|
|
5
4
|
HttpMetricsRedisStore
|
|
6
5
|
} = require('./httpMetricsRedisStore');
|
|
7
|
-
function clusterWorkerTag() {
|
|
8
|
-
try {
|
|
9
|
-
if (cluster.isWorker && cluster.worker) {
|
|
10
|
-
return ` cluster_worker_id=${cluster.worker.id}`;
|
|
11
|
-
}
|
|
12
|
-
} catch (_) {
|
|
13
|
-
/* ignore */
|
|
14
|
-
}
|
|
15
|
-
return '';
|
|
16
|
-
}
|
|
17
6
|
|
|
18
7
|
/**
|
|
19
8
|
* Records HTTP request aggregates only to Redis (no in-process Prometheus counters on this path).
|
|
@@ -46,12 +35,6 @@ class HttpMetricsRedisRecorder {
|
|
|
46
35
|
processType,
|
|
47
36
|
ttlSec
|
|
48
37
|
});
|
|
49
|
-
|
|
50
|
-
// --- TEMP_HTTP_METRICS_DIAG: remove this block when cluster / multi-web coverage is verified ---
|
|
51
|
-
this._diagWindowStart = 0;
|
|
52
|
-
this._diagWindowCount = 0;
|
|
53
|
-
this._diagIntervalMs = 5000;
|
|
54
|
-
// --- end TEMP_HTTP_METRICS_DIAG ---
|
|
55
38
|
}
|
|
56
39
|
|
|
57
40
|
/**
|
|
@@ -71,21 +54,6 @@ class HttpMetricsRedisRecorder {
|
|
|
71
54
|
databaseId = '',
|
|
72
55
|
duration
|
|
73
56
|
}) {
|
|
74
|
-
// --- TEMP_HTTP_METRICS_DIAG: remove this block when cluster / multi-web coverage is verified ---
|
|
75
|
-
this._diagWindowCount += 1;
|
|
76
|
-
const now = Date.now();
|
|
77
|
-
if (!this._diagWindowStart) {
|
|
78
|
-
this._diagWindowStart = now;
|
|
79
|
-
}
|
|
80
|
-
if (now - this._diagWindowStart >= this._diagIntervalMs) {
|
|
81
|
-
const pidTag = `pid=${process.pid}`;
|
|
82
|
-
const clusterTag = clusterWorkerTag();
|
|
83
|
-
console.warn(`[TEMP_HTTP_METRICS_DIAG] http_record_source ${pidTag}${clusterTag} process_type=${this.processType} app=${this.appName} batch_count=${this._diagWindowCount} (before Redis write)`);
|
|
84
|
-
this._diagWindowStart = now;
|
|
85
|
-
this._diagWindowCount = 0;
|
|
86
|
-
}
|
|
87
|
-
// --- end TEMP_HTTP_METRICS_DIAG ---
|
|
88
|
-
|
|
89
57
|
this._store.record(method, route, status_code, appId, databaseId, duration);
|
|
90
58
|
}
|
|
91
59
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpMetricsRedisRecorder.js","names":["
|
|
1
|
+
{"version":3,"file":"httpMetricsRedisRecorder.js","names":["HttpMetricsRedisStore","require","HttpMetricsRedisRecorder","constructor","redisClient","appName","processType","ttlSec","Error","_store","trackHttpRequest","method","route","status_code","appId","databaseId","duration","record","trackHttpRequestMiddleware","req","res","next","start","Date","now","on","path","params","body","query","datasourceId","statusCode","module","exports"],"sources":["../../src/metrics/httpMetricsRedisRecorder.js"],"sourcesContent":["const { HttpMetricsRedisStore } = require('./httpMetricsRedisStore')\n\n/**\n * Records HTTP request aggregates only to Redis (no in-process Prometheus counters on this path).\n * Pair with {@link HttpMetricsRedisCollector} on a drain process to flush into counters and push to the VM-agent.\n *\n * @see HttpMetricsRedisStore\n */\nclass HttpMetricsRedisRecorder {\n /**\n * @param {Object} opts\n * @param {import('redis').RedisClient} opts.redisClient **Required.** Injected client (same pattern as {@link RedisMetricsClient}).\n * @param {string} opts.appName Application name (must match collector; typically `BUILD_APP_NAME`).\n * @param {string} opts.processType Logical process segment in Redis keys (e.g. `web`; must match collector’s key segment).\n * @param {number} [opts.ttlSec] Redis hash key TTL in seconds (sliding; default 120).\n */\n constructor({ redisClient, appName, processType, ttlSec }) {\n if (redisClient == null) {\n throw new Error('HttpMetricsRedisRecorder: redisClient is required')\n }\n this.processType = processType\n this.appName = appName\n this._store = new HttpMetricsRedisStore({\n redisClient,\n appName,\n processType,\n ttlSec,\n })\n }\n\n /**\n * @param {Object} params\n * @param {string} params.method\n * @param {string} params.route\n * @param {number} params.status_code\n * @param {string} [params.appId]\n * @param {string} [params.databaseId]\n * @param {number} params.duration\n */\n trackHttpRequest({\n method,\n route,\n status_code,\n appId = '',\n databaseId = '',\n duration,\n }) {\n this._store.record(method, route, status_code, appId, databaseId, duration)\n }\n\n /**\n * Express middleware: appends a `finish` listener and writes one aggregate row per request to Redis.\n * Does not check `METRICS_ENABLED`; the app should only mount this when HTTP Redis metrics should run.\n *\n * @param {import('http').IncomingMessage} req\n * @param {import('http').ServerResponse} res\n * @param {function} next\n */\n trackHttpRequestMiddleware = (req, res, next) => {\n if (req.method === 'OPTIONS') {\n next()\n return\n }\n\n const start = Date.now()\n res.on('finish', () => {\n const route = req.route?.path || req.path || 'unknown'\n const appId =\n req.params?.appId || req.body?.appId || req.query?.appId || ''\n const databaseId =\n req.params?.databaseId ||\n req.body?.databaseId ||\n req.query?.databaseId ||\n req.params?.datasourceId ||\n req.body?.datasourceId ||\n req.query?.datasourceId ||\n ''\n\n this.trackHttpRequest({\n method: req.method,\n route,\n status_code: res.statusCode,\n appId,\n databaseId,\n duration: Date.now() - start,\n })\n })\n\n next()\n }\n}\n\nmodule.exports = { HttpMetricsRedisRecorder }\n"],"mappings":";;AAAA,MAAM;EAAEA;AAAsB,CAAC,GAAGC,OAAO,CAAC,yBAAyB,CAAC;;AAEpE;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,wBAAwB,CAAC;EAC7B;AACF;AACA;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAAC;IAAEC,WAAW;IAAEC,OAAO;IAAEC,WAAW;IAAEC;EAAO,CAAC,EAAE;IACzD,IAAIH,WAAW,IAAI,IAAI,EAAE;MACvB,MAAM,IAAII,KAAK,CAAC,mDAAmD,CAAC;IACtE;IACA,IAAI,CAACF,WAAW,GAAGA,WAAW;IAC9B,IAAI,CAACD,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACI,MAAM,GAAG,IAAIT,qBAAqB,CAAC;MACtCI,WAAW;MACXC,OAAO;MACPC,WAAW;MACXC;IACF,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEG,gBAAgBA,CAAC;IACfC,MAAM;IACNC,KAAK;IACLC,WAAW;IACXC,KAAK,GAAG,EAAE;IACVC,UAAU,GAAG,EAAE;IACfC;EACF,CAAC,EAAE;IACD,IAAI,CAACP,MAAM,CAACQ,MAAM,CAACN,MAAM,EAAEC,KAAK,EAAEC,WAAW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 @@
|
|
|
1
|
-
{"version":3,"file":"httpMetricsRedisStore.d.ts","sourceRoot":"","sources":["../../src/metrics/httpMetricsRedisStore.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"httpMetricsRedisStore.d.ts","sourceRoot":"","sources":["../../src/metrics/httpMetricsRedisStore.js"],"names":[],"mappings":"AA6DA;;;GAGG;AACH;IACE;;;;;;OAMG;IACH;QAL6C,WAAW,EAA7C,OAAO,OAAO,EAAE,WAAW;QACd,OAAO,EAApB,MAAM;QACO,WAAW,EAAxB,MAAM;QACQ,MAAM;OAiB9B;IAXC,qCAA0B;IAC1B,eAGwC;IAIxC,iBAAiD;IACjD,eAA6C;IAC7C,gBAAqD;IAGvD;;;OAGG;IACH,sBAEC;IAED;;;;;;;OAOG;IACH,eAPW,MAAM,SACN,MAAM,cACN,MAAM,SACN,MAAM,cACN,MAAM,cACN,MAAM,QAyBhB;IAED;;;;OAIG;IACH,qCAJoB,MAAM,SAAS,MAAM,KAAK,IAAI,0BAC9B,MAAM,SAAS,MAAM,KAAK,IAAI,GACrC,QAAQ,OAAO,CAAC,CAkG5B;CACF;AAxMD;;;;;;;GAOG;AACH,sCAPW,MAAM,SACN,MAAM,cACN,MAAM,SACN,MAAM,cACN,MAAM,GACJ,MAAM,CAIlB;AA3CD;;;GAGG;AACH,wBAFU,MAAM,CAEQ;AAiBxB;;GAEG;AACH,wCAFa,OAAO,CASnB;AAzBD;;;GAGG;AACH,iDAFU,MAAM,CAE8B"}
|
|
@@ -54,6 +54,11 @@ function hgetallPairsToObject(pairs) {
|
|
|
54
54
|
return o;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/** --- TEMP_HTTP_METRICS_DIAG: delete this helper and all calls when done debugging --- */
|
|
58
|
+
function tempHttpDiagLog(message) {
|
|
59
|
+
console.warn(`[TEMP_HTTP_METRICS_DIAG] ${message}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
57
62
|
/**
|
|
58
63
|
* Redis HTTP aggregate store. Uses an **injected** client (same pattern as {@link RedisMetricsClient}).
|
|
59
64
|
* Expects `redis` v3-style API: `multi().hincrby().expire().exec(cb)`, `eval`, `set`, `del`.
|
|
@@ -107,7 +112,9 @@ class HttpMetricsRedisStore {
|
|
|
107
112
|
client.multi().hincrby(this.countKey, field, 1).hincrby(this.durKey, field, dur).expire(this.countKey, this.ttlSec).expire(this.durKey, this.ttlSec).exec(err => {
|
|
108
113
|
if (err) {
|
|
109
114
|
console.error('[HttpMetricsRedisStore] record failed:', err.message);
|
|
115
|
+
return;
|
|
110
116
|
}
|
|
117
|
+
tempHttpDiagLog(`http_redis_write_sent pid=${process.pid} countKey=${this.countKey} durKey=${this.durKey} sample=${method} ${route} ${statusCode}`);
|
|
111
118
|
});
|
|
112
119
|
} catch (e) {
|
|
113
120
|
console.error('[HttpMetricsRedisStore] record:', e.message);
|
|
@@ -130,26 +137,32 @@ class HttpMetricsRedisStore {
|
|
|
130
137
|
return new Promise(resolve => {
|
|
131
138
|
client.set(this.lockKey, '1', 'EX', 25, 'NX', (setErr, ok) => {
|
|
132
139
|
if (setErr || ok !== 'OK') {
|
|
140
|
+
tempHttpDiagLog(`http_redis_drain_skipped pid=${process.pid} lockKey=${this.lockKey} reason=${setErr ? setErr.message : 'lock_not_acquired'}`);
|
|
133
141
|
resolve(false);
|
|
134
142
|
return;
|
|
135
143
|
}
|
|
144
|
+
tempHttpDiagLog(`http_redis_drain_request pid=${process.pid} countKey=${this.countKey} durKey=${this.durKey} (EVAL HGETALL+DEL)`);
|
|
136
145
|
client.eval(DRAIN_LUA, 2, this.countKey, this.durKey, (evalErr, raw) => {
|
|
137
146
|
const finish = () => {
|
|
138
147
|
client.del(this.lockKey, () => resolve(true));
|
|
139
148
|
};
|
|
140
149
|
if (evalErr) {
|
|
141
150
|
console.error('[HttpMetricsRedisStore] drain failed:', evalErr.message);
|
|
151
|
+
tempHttpDiagLog(`http_redis_drain_got_error pid=${process.pid} message=${evalErr.message}`);
|
|
142
152
|
finish();
|
|
143
153
|
return;
|
|
144
154
|
}
|
|
145
155
|
try {
|
|
146
156
|
if (!raw || !Array.isArray(raw) || raw.length < 2) {
|
|
157
|
+
tempHttpDiagLog(`http_redis_drain_got pid=${process.pid} distinct_fields=0 total_units=0 (empty raw)`);
|
|
147
158
|
finish();
|
|
148
159
|
return;
|
|
149
160
|
}
|
|
150
161
|
const counts = hgetallPairsToObject(raw[0]);
|
|
151
162
|
const durs = hgetallPairsToObject(raw[1]);
|
|
152
|
-
|
|
163
|
+
const fieldKeys = Object.keys(counts);
|
|
164
|
+
let totalUnits = 0;
|
|
165
|
+
for (const field of fieldKeys) {
|
|
153
166
|
const count = parseInt(counts[field], 10);
|
|
154
167
|
if (!count || count < 1) {
|
|
155
168
|
continue;
|
|
@@ -159,6 +172,7 @@ class HttpMetricsRedisStore {
|
|
|
159
172
|
if (parts.length !== 5) {
|
|
160
173
|
continue;
|
|
161
174
|
}
|
|
175
|
+
totalUnits += count;
|
|
162
176
|
const [m, route, statusStr, aid, did] = parts;
|
|
163
177
|
const labels = {
|
|
164
178
|
method: m,
|
|
@@ -172,8 +186,11 @@ class HttpMetricsRedisStore {
|
|
|
172
186
|
applyDuration(labels, dur);
|
|
173
187
|
}
|
|
174
188
|
}
|
|
189
|
+
const sampleFields = fieldKeys.slice(0, 3).join(' | ');
|
|
190
|
+
tempHttpDiagLog(`http_redis_drain_got pid=${process.pid} distinct_fields=${fieldKeys.length} total_request_units=${totalUnits} sample_fields=${sampleFields || '(none)'}`);
|
|
175
191
|
} catch (e) {
|
|
176
192
|
console.error('[HttpMetricsRedisStore] flush apply failed:', e.message);
|
|
193
|
+
tempHttpDiagLog(`http_redis_drain_apply_failed pid=${process.pid} ${e.message}`);
|
|
177
194
|
}
|
|
178
195
|
finish();
|
|
179
196
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpMetricsRedisStore.js","names":["FIELD_SEP","DEFAULT_HTTP_METRICS_REDIS_TTL_SEC","DRAIN_LUA","isRedisPeerInstalled","require","resolve","buildFieldKey","method","route","statusCode","appId","databaseId","String","join","hgetallPairsToObject","pairs","o","length","i","HttpMetricsRedisStore","constructor","redisClient","appName","processType","ttlSec","Error","_client","keySeg","encodeURIComponent","countKey","durKey","lockKey","_ensureClient","record","durationMs","client","field","dur","Math","max","round","Number","multi","hincrby","expire","exec","err","console","error","message","e","flushToCounters","applyCount","applyDuration","Promise","set","setErr","ok","eval","evalErr","raw","finish","del","Array","isArray","counts","durs","Object","keys","count","parseInt","parts","split","m","statusStr","aid","did","labels","status_code","module","exports"],"sources":["../../src/metrics/httpMetricsRedisStore.js"],"sourcesContent":["/**\n * Record separator for hash fields (avoids collisions when route contains \"|\").\n * @type {string}\n */\nconst FIELD_SEP = '\\x1e'\n\n/**\n * Default Redis key TTL in seconds (sliding: refreshed on each `record` write).\n * @type {number}\n */\nconst DEFAULT_HTTP_METRICS_REDIS_TTL_SEC = 120\n\nconst DRAIN_LUA = `\nlocal function drain(key)\n local v = redis.call('HGETALL', key)\n redis.call('DEL', key)\n return v\nend\nreturn {drain(KEYS[1]), drain(KEYS[2])}\n`\n\n/**\n * @returns {boolean} Whether the `redis` npm package is resolvable (optional peer).\n */\nfunction isRedisPeerInstalled() {\n try {\n require.resolve('redis')\n return true\n } catch {\n return false\n }\n}\n\n/**\n * @param {string} method\n * @param {string} route\n * @param {number} statusCode\n * @param {string} appId\n * @param {string} databaseId\n * @returns {string}\n */\nfunction buildFieldKey(method, route, statusCode, appId, databaseId) {\n return [method, route, String(statusCode), appId, databaseId].join(FIELD_SEP)\n}\n\nfunction hgetallPairsToObject(pairs) {\n const o = {}\n if (!pairs || !pairs.length) {\n return o\n }\n for (let i = 0; i < pairs.length; i += 2) {\n o[pairs[i]] = pairs[i + 1]\n }\n return o\n}\n\n/**\n * Redis HTTP aggregate store. Uses an **injected** client (same pattern as {@link RedisMetricsClient}).\n * Expects `redis` v3-style API: `multi().hincrby().expire().exec(cb)`, `eval`, `set`, `del`.\n */\nclass HttpMetricsRedisStore {\n /**\n * @param {Object} opts\n * @param {import('redis').RedisClient} opts.redisClient\n * @param {string} opts.appName BUILD_APP_NAME (key segment)\n * @param {string} opts.processType logical process for key (e.g. web)\n * @param {number} [opts.ttlSec] Expire keys after this many seconds (default 120)\n */\n constructor({ redisClient, appName, processType, ttlSec }) {\n if (redisClient == null) {\n throw new Error('HttpMetricsRedisStore: redisClient is required')\n }\n this._client = redisClient\n this.ttlSec =\n typeof ttlSec === 'number' && ttlSec > 0\n ? ttlSec\n : DEFAULT_HTTP_METRICS_REDIS_TTL_SEC\n const keySeg = `${encodeURIComponent(appName)}:${encodeURIComponent(processType)}`\n this.countKey = `metrics:http:v2:${keySeg}:count`\n this.durKey = `metrics:http:v2:${keySeg}:dur`\n this.lockKey = `metrics:http:v2:${keySeg}:drain_lock`\n }\n\n /**\n * @returns {import('redis').RedisClient}\n * @private\n */\n _ensureClient() {\n return this._client\n }\n\n /**\n * @param {string} method\n * @param {string} route\n * @param {number} statusCode\n * @param {string} appId\n * @param {string} databaseId\n * @param {number} durationMs\n */\n record(method, route, statusCode, appId, databaseId, durationMs) {\n try {\n const client = this._ensureClient()\n const field = buildFieldKey(method, route, statusCode, appId, databaseId)\n const dur = Math.max(0, Math.round(Number(durationMs) || 0))\n client\n .multi()\n .hincrby(this.countKey, field, 1)\n .hincrby(this.durKey, field, dur)\n .expire(this.countKey, this.ttlSec)\n .expire(this.durKey, this.ttlSec)\n .exec(err => {\n if (err) {\n console.error('[HttpMetricsRedisStore] record failed:', err.message)\n }\n })\n } catch (e) {\n console.error('[HttpMetricsRedisStore] record:', e.message)\n }\n }\n\n /**\n * @param {(labels: Object, value: number) => void} applyCount\n * @param {(labels: Object, value: number) => void} applyDuration\n * @returns {Promise<boolean>}\n */\n flushToCounters(applyCount, applyDuration) {\n let client\n try {\n client = this._ensureClient()\n } catch (e) {\n console.error('[HttpMetricsRedisStore] flush:', e.message)\n return Promise.resolve(false)\n }\n return new Promise(resolve => {\n client.set(this.lockKey, '1', 'EX', 25, 'NX', (setErr, ok) => {\n if (setErr || ok !== 'OK') {\n resolve(false)\n return\n }\n client.eval(\n DRAIN_LUA,\n 2,\n this.countKey,\n this.durKey,\n (evalErr, raw) => {\n const finish = () => {\n client.del(this.lockKey, () => resolve(true))\n }\n\n if (evalErr) {\n console.error('[HttpMetricsRedisStore] drain failed:', evalErr.message)\n finish()\n return\n }\n\n try {\n if (!raw || !Array.isArray(raw) || raw.length < 2) {\n finish()\n return\n }\n const counts = hgetallPairsToObject(raw[0])\n const durs = hgetallPairsToObject(raw[1])\n for (const field of Object.keys(counts)) {\n const count = parseInt(counts[field], 10)\n if (!count || count < 1) {\n continue\n }\n const dur = parseInt(durs[field] || '0', 10) || 0\n const parts = field.split(FIELD_SEP)\n if (parts.length !== 5) {\n continue\n }\n const [m, route, statusStr, aid, did] = parts\n const labels = {\n method: m,\n route,\n status_code: statusStr,\n appId: aid,\n databaseId: did,\n }\n applyCount(labels, count)\n if (dur > 0) {\n applyDuration(labels, dur)\n }\n }\n } catch (e) {\n console.error('[HttpMetricsRedisStore] flush apply failed:', e.message)\n }\n finish()\n }\n )\n })\n })\n }\n}\n\nmodule.exports = {\n HttpMetricsRedisStore,\n buildFieldKey,\n FIELD_SEP,\n isRedisPeerInstalled,\n DEFAULT_HTTP_METRICS_REDIS_TTL_SEC,\n}\n"],"mappings":";;AAAA;AACA;AACA;AACA;AACA,MAAMA,SAAS,GAAG,MAAM;;AAExB;AACA;AACA;AACA;AACA,MAAMC,kCAAkC,GAAG,GAAG;AAE9C,MAAMC,SAAS,GAAG;AAClB;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;;AAED;AACA;AACA;AACA,SAASC,oBAAoBA,CAAA,EAAG;EAC9B,IAAI;IACFC,OAAO,CAACC,OAAO,CAAC,OAAO,CAAC;IACxB,OAAO,IAAI;EACb,CAAC,CAAC,MAAM;IACN,OAAO,KAAK;EACd;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,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
+
{"version":3,"file":"httpMetricsRedisStore.js","names":["FIELD_SEP","DEFAULT_HTTP_METRICS_REDIS_TTL_SEC","DRAIN_LUA","isRedisPeerInstalled","require","resolve","buildFieldKey","method","route","statusCode","appId","databaseId","String","join","hgetallPairsToObject","pairs","o","length","i","tempHttpDiagLog","message","console","warn","HttpMetricsRedisStore","constructor","redisClient","appName","processType","ttlSec","Error","_client","keySeg","encodeURIComponent","countKey","durKey","lockKey","_ensureClient","record","durationMs","client","field","dur","Math","max","round","Number","multi","hincrby","expire","exec","err","error","process","pid","e","flushToCounters","applyCount","applyDuration","Promise","set","setErr","ok","eval","evalErr","raw","finish","del","Array","isArray","counts","durs","fieldKeys","Object","keys","totalUnits","count","parseInt","parts","split","m","statusStr","aid","did","labels","status_code","sampleFields","slice","module","exports"],"sources":["../../src/metrics/httpMetricsRedisStore.js"],"sourcesContent":["/**\n * Record separator for hash fields (avoids collisions when route contains \"|\").\n * @type {string}\n */\nconst FIELD_SEP = '\\x1e'\n\n/**\n * Default Redis key TTL in seconds (sliding: refreshed on each `record` write).\n * @type {number}\n */\nconst DEFAULT_HTTP_METRICS_REDIS_TTL_SEC = 120\n\nconst DRAIN_LUA = `\nlocal function drain(key)\n local v = redis.call('HGETALL', key)\n redis.call('DEL', key)\n return v\nend\nreturn {drain(KEYS[1]), drain(KEYS[2])}\n`\n\n/**\n * @returns {boolean} Whether the `redis` npm package is resolvable (optional peer).\n */\nfunction isRedisPeerInstalled() {\n try {\n require.resolve('redis')\n return true\n } catch {\n return false\n }\n}\n\n/**\n * @param {string} method\n * @param {string} route\n * @param {number} statusCode\n * @param {string} appId\n * @param {string} databaseId\n * @returns {string}\n */\nfunction buildFieldKey(method, route, statusCode, appId, databaseId) {\n return [method, route, String(statusCode), appId, databaseId].join(FIELD_SEP)\n}\n\nfunction hgetallPairsToObject(pairs) {\n const o = {}\n if (!pairs || !pairs.length) {\n return o\n }\n for (let i = 0; i < pairs.length; i += 2) {\n o[pairs[i]] = pairs[i + 1]\n }\n return o\n}\n\n/** --- TEMP_HTTP_METRICS_DIAG: delete this helper and all calls when done debugging --- */\nfunction tempHttpDiagLog(message) {\n console.warn(`[TEMP_HTTP_METRICS_DIAG] ${message}`)\n}\n\n/**\n * Redis HTTP aggregate store. Uses an **injected** client (same pattern as {@link RedisMetricsClient}).\n * Expects `redis` v3-style API: `multi().hincrby().expire().exec(cb)`, `eval`, `set`, `del`.\n */\nclass HttpMetricsRedisStore {\n /**\n * @param {Object} opts\n * @param {import('redis').RedisClient} opts.redisClient\n * @param {string} opts.appName BUILD_APP_NAME (key segment)\n * @param {string} opts.processType logical process for key (e.g. web)\n * @param {number} [opts.ttlSec] Expire keys after this many seconds (default 120)\n */\n constructor({ redisClient, appName, processType, ttlSec }) {\n if (redisClient == null) {\n throw new Error('HttpMetricsRedisStore: redisClient is required')\n }\n this._client = redisClient\n this.ttlSec =\n typeof ttlSec === 'number' && ttlSec > 0\n ? ttlSec\n : DEFAULT_HTTP_METRICS_REDIS_TTL_SEC\n const keySeg = `${encodeURIComponent(appName)}:${encodeURIComponent(\n processType\n )}`\n this.countKey = `metrics:http:v2:${keySeg}:count`\n this.durKey = `metrics:http:v2:${keySeg}:dur`\n this.lockKey = `metrics:http:v2:${keySeg}:drain_lock`\n }\n\n /**\n * @returns {import('redis').RedisClient}\n * @private\n */\n _ensureClient() {\n return this._client\n }\n\n /**\n * @param {string} method\n * @param {string} route\n * @param {number} statusCode\n * @param {string} appId\n * @param {string} databaseId\n * @param {number} durationMs\n */\n record(method, route, statusCode, appId, databaseId, durationMs) {\n try {\n const client = this._ensureClient()\n const field = buildFieldKey(method, route, statusCode, appId, databaseId)\n const dur = Math.max(0, Math.round(Number(durationMs) || 0))\n client\n .multi()\n .hincrby(this.countKey, field, 1)\n .hincrby(this.durKey, field, dur)\n .expire(this.countKey, this.ttlSec)\n .expire(this.durKey, this.ttlSec)\n .exec(err => {\n if (err) {\n console.error('[HttpMetricsRedisStore] record failed:', err.message)\n return\n }\n tempHttpDiagLog(\n `http_redis_write_sent pid=${process.pid} countKey=${this.countKey} durKey=${this.durKey} sample=${method} ${route} ${statusCode}`\n )\n })\n } catch (e) {\n console.error('[HttpMetricsRedisStore] record:', e.message)\n }\n }\n\n /**\n * @param {(labels: Object, value: number) => void} applyCount\n * @param {(labels: Object, value: number) => void} applyDuration\n * @returns {Promise<boolean>}\n */\n flushToCounters(applyCount, applyDuration) {\n let client\n try {\n client = this._ensureClient()\n } catch (e) {\n console.error('[HttpMetricsRedisStore] flush:', e.message)\n return Promise.resolve(false)\n }\n return new Promise(resolve => {\n client.set(this.lockKey, '1', 'EX', 25, 'NX', (setErr, ok) => {\n if (setErr || ok !== 'OK') {\n tempHttpDiagLog(\n `http_redis_drain_skipped pid=${process.pid} lockKey=${this.lockKey} reason=${setErr ? setErr.message : 'lock_not_acquired'}`\n )\n resolve(false)\n return\n }\n tempHttpDiagLog(\n `http_redis_drain_request pid=${process.pid} countKey=${this.countKey} durKey=${this.durKey} (EVAL HGETALL+DEL)`\n )\n client.eval(\n DRAIN_LUA,\n 2,\n this.countKey,\n this.durKey,\n (evalErr, raw) => {\n const finish = () => {\n client.del(this.lockKey, () => resolve(true))\n }\n\n if (evalErr) {\n console.error(\n '[HttpMetricsRedisStore] drain failed:',\n evalErr.message\n )\n tempHttpDiagLog(\n `http_redis_drain_got_error pid=${process.pid} message=${evalErr.message}`\n )\n finish()\n return\n }\n\n try {\n if (!raw || !Array.isArray(raw) || raw.length < 2) {\n tempHttpDiagLog(\n `http_redis_drain_got pid=${process.pid} distinct_fields=0 total_units=0 (empty raw)`\n )\n finish()\n return\n }\n const counts = hgetallPairsToObject(raw[0])\n const durs = hgetallPairsToObject(raw[1])\n const fieldKeys = Object.keys(counts)\n let totalUnits = 0\n for (const field of fieldKeys) {\n const count = parseInt(counts[field], 10)\n if (!count || count < 1) {\n continue\n }\n const dur = parseInt(durs[field] || '0', 10) || 0\n const parts = field.split(FIELD_SEP)\n if (parts.length !== 5) {\n continue\n }\n totalUnits += count\n const [m, route, statusStr, aid, did] = parts\n const labels = {\n method: m,\n route,\n status_code: statusStr,\n appId: aid,\n databaseId: did,\n }\n applyCount(labels, count)\n if (dur > 0) {\n applyDuration(labels, dur)\n }\n }\n const sampleFields = fieldKeys.slice(0, 3).join(' | ')\n tempHttpDiagLog(\n `http_redis_drain_got pid=${process.pid} distinct_fields=${fieldKeys.length} total_request_units=${totalUnits} sample_fields=${sampleFields || '(none)'}`\n )\n } catch (e) {\n console.error(\n '[HttpMetricsRedisStore] flush apply failed:',\n e.message\n )\n tempHttpDiagLog(\n `http_redis_drain_apply_failed pid=${process.pid} ${e.message}`\n )\n }\n finish()\n }\n )\n })\n })\n }\n}\n\nmodule.exports = {\n HttpMetricsRedisStore,\n buildFieldKey,\n FIELD_SEP,\n isRedisPeerInstalled,\n DEFAULT_HTTP_METRICS_REDIS_TTL_SEC,\n}\n"],"mappings":";;AAAA;AACA;AACA;AACA;AACA,MAAMA,SAAS,GAAG,MAAM;;AAExB;AACA;AACA;AACA;AACA,MAAMC,kCAAkC,GAAG,GAAG;AAE9C,MAAMC,SAAS,GAAG;AAClB;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;;AAED;AACA;AACA;AACA,SAASC,oBAAoBA,CAAA,EAAG;EAC9B,IAAI;IACFC,OAAO,CAACC,OAAO,CAAC,OAAO,CAAC;IACxB,OAAO,IAAI;EACb,CAAC,CAAC,MAAM;IACN,OAAO,KAAK;EACd;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,aAAaA,CAACC,MAAM,EAAEC,KAAK,EAAEC,UAAU,EAAEC,KAAK,EAAEC,UAAU,EAAE;EACnE,OAAO,CAACJ,MAAM,EAAEC,KAAK,EAAEI,MAAM,CAACH,UAAU,CAAC,EAAEC,KAAK,EAAEC,UAAU,CAAC,CAACE,IAAI,CAACb,SAAS,CAAC;AAC/E;AAEA,SAASc,oBAAoBA,CAACC,KAAK,EAAE;EACnC,MAAMC,CAAC,GAAG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E;cACd,CAAC;cACD1B,UAAU,CAAC2B,MAAM,EAAER,KAAK,CAAC;cACzB,IAAIlC,GAAG,GAAG,CAAC,EAAE;gBACXgB,aAAa,CAAC0B,MAAM,EAAE1C,GAAG,CAAC;cAC5B;YACF;YACA,MAAM4C,YAAY,GAAGd,SAAS,CAACe,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAACzE,IAAI,CAAC,KAAK,CAAC;YACtDM,eAAe,CACb,4BAA4BiC,OAAO,CAACC,GAAG,oBAAoBkB,SAAS,CAACtD,MAAM,wBAAwByD,UAAU,kBAAkBW,YAAY,IAAI,QAAQ,EACzJ,CAAC;UACH,CAAC,CAAC,OAAO/B,CAAC,EAAE;YACVjC,OAAO,CAAC8B,KAAK,CACX,6CAA6C,EAC7CG,CAAC,CAAClC,OACJ,CAAC;YACDD,eAAe,CACb,qCAAqCiC,OAAO,CAACC,GAAG,IAAIC,CAAC,CAAClC,OAAO,EAC/D,CAAC;UACH;UACA6C,MAAM,CAAC,CAAC;QACV,CACF,CAAC;MACH,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ;AACF;AAEAsB,MAAM,CAACC,OAAO,GAAG;EACfjE,qBAAqB;EACrBjB,aAAa;EACbN,SAAS;EACTG,oBAAoB;EACpBF;AACF,CAAC","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsDatabaseClient.d.ts","sourceRoot":"","sources":["../../src/metrics/metricsDatabaseClient.js"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH;IACE;;;;;;;;;;;;;;;;OAgBG;IACH;QAf2B,WAAW,EAA3B,MAAM;QACU,YAAY,EAA5B,MAAM;QAC2B,wBAAwB;;;QACxC,OAAO;QACP,MAAM;QACN,WAAW;QACV,OAAO;QACP,SAAS;QACV,cAAc;QACd,iBAAiB;QACjB,WAAW;QACV,gBAAgB;QACf,iBAAiB;QAClB,kBAAkB;
|
|
1
|
+
{"version":3,"file":"metricsDatabaseClient.d.ts","sourceRoot":"","sources":["../../src/metrics/metricsDatabaseClient.js"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH;IACE;;;;;;;;;;;;;;;;OAgBG;IACH;QAf2B,WAAW,EAA3B,MAAM;QACU,YAAY,EAA5B,MAAM;QAC2B,wBAAwB;;;QACxC,OAAO;QACP,MAAM;QACN,WAAW;QACV,OAAO;QACP,SAAS;QACV,cAAc;QACd,iBAAiB;QACjB,WAAW;QACV,gBAAgB;QACf,iBAAiB;QAClB,kBAAkB;OAwF9C;IAtBC,qBAAuB;IAUvB,mDAAmD;IACnD,8DAQE;IAKJ,yCAKC;IAED;;;OAGG;IACH,gCAHW,IAAI,KACF,QAAQ;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CA4BrE;IAED;;;OAGG;IACH,8BAFa,QAAQ,IAAI,CAAC,CAsBzB;IAED;;;OAGG;IACH,2BAFa,QAAQ,IAAI,CAAC,CAqBzB;IAED;;;OAGG;IACH,sDAMC;CAwBF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsDatabaseClient.js","names":["Pool","require","BaseMetricsClient","exitUnlessMetricsProcessType","DatabaseMetricsClient","constructor","databaseUrl","databaseName","additional_database_urls","metricsConfig","intervalSec","parseInt","process","env","METRICS_DATABASE_INTERVAL_SEC","tmpAppName","appName","BUILD_APP_NAME","mainDbName","startupValidation","console","error","mainPool","connectionString","query","end","info","err","message","dbKey","url","Object","entries","p","processType","databasePools","_addPool","databaseConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabelsWithoutDynoId","_setCleanupHandlers","pool","push","getDBConnectionsAndName","currentRes","current","rows","maxRes","max","dbName","options","database","URL","pathname","replace","collectDatabaseMetrics","set","defaultLabelsWithoutDynoId","database_name","max_connections","String","db_key","warn","pushDatabaseMetrics","gatewayPush","clearAllCounters","metricsLogValues","metricObjects","registry","getMetricsAsJSON","JSON","stringify","startPush","_startPush","catch","cleanup","exit","on","module","exports"],"sources":["../../src/metrics/metricsDatabaseClient.js"],"sourcesContent":["const { Pool } = require('pg')\nconst { BaseMetricsClient } = require('./baseMetricsClient')\nconst { exitUnlessMetricsProcessType } = require('./metricsProcessTypeUtils')\n\n/**\n * DatabaseMetricsClient collects Postgres connection metrics\n * and pushes them to Prometheus Pushgateway.\n *\n * @extends BaseMetricsClient\n */\nclass DatabaseMetricsClient extends BaseMetricsClient {\n /**\n * @param {Object} options\n * @param {string} options.databaseUrl - Required main database URL\n * @param {string} options.databaseName - Main database name for metrics\n * @param {Object<string, string>} [options.additional_database_urls] - Optional additional DBs, keyed by custom name with URL as value\n * @param {string} [options.appName] - Application name (from BaseMetricsClient)\n * @param {string} [options.dynoId] - Dyno/instance ID (from BaseMetricsClient)\n * @param {string} [options.processType] - Process type (from BaseMetricsClient)\n * @param {boolean} [options.enabled] - Enable metrics collection (from BaseMetricsClient)\n * @param {boolean} [options.logValues] - Log metrics values (from BaseMetricsClient)\n * @param {string} [options.pushgatewayUrl] - PushGateway URL (from BaseMetricsClient)\n * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from BaseMetricsClient)\n * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics\n * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service\n * @param {function} [options.startupValidation] - Function to validate startup\n * @param {boolean} [options.disablePushgateway] - Disable pushing to Pushgateway (use HTTP scraping instead)\n */\n constructor({\n databaseUrl,\n databaseName,\n additional_database_urls = {},\n ...metricsConfig\n } = {}) {\n exitUnlessMetricsProcessType(metricsConfig, 'database-metrics', 'database-metrics')\n\n const intervalSec =\n metricsConfig.intervalSec ||\n parseInt(process.env.METRICS_DATABASE_INTERVAL_SEC || '', 10) ||\n 60\n\n const tmpAppName =\n metricsConfig.appName || process.env.BUILD_APP_NAME || 'unknown-app'\n const mainDbName = databaseName || `${tmpAppName}_db`\n\n const startupValidation = async () => {\n if (!databaseUrl) {\n console.error(`[database-metrics] ❌ METRICS_DATABASE_URL is required`)\n return false\n }\n\n try {\n const mainPool = new Pool({ connectionString: databaseUrl })\n await mainPool.query('SELECT 1')\n await mainPool.end()\n console.info(`[database-metrics] ✓ Main database OK: ${mainDbName}`)\n } catch (err) {\n console.error(\n `[database-metrics] ❌ Cannot connect to main database: ${err.message}`\n )\n return false\n }\n\n for (const [dbKey, url] of Object.entries(additional_database_urls)) {\n try {\n const p = new Pool({ connectionString: url })\n await p.query('SELECT 1')\n await p.end()\n console.info(`[database-metrics] ✓ Additional database OK: ${dbKey}`)\n } catch (err) {\n console.error(\n `[database-metrics] ⚠ Skipping additional database: ${dbKey}`\n )\n console.error(`[database-metrics] ${err.message}`)\n }\n }\n\n console.info(`[database-metrics] Database metrics collection starting`)\n return true\n }\n\n super({\n ...metricsConfig,\n processType: metricsConfig.processType || 'database-metrics',\n intervalSec,\n startupValidation,\n })\n\n this.databasePools = []\n\n if (databaseUrl) {\n this._addPool(databaseUrl, mainDbName)\n }\n\n for (const [dbKey, url] of Object.entries(additional_database_urls)) {\n this._addPool(url, dbKey)\n }\n\n /** Gauge for Database connections (no dyno_id). */\n this.databaseConnectionsGauge = this.createGauge({\n name: 'app_database_connections',\n help: 'Postgres database connections',\n labelNames: this.withDefaultLabelsWithoutDynoId([\n 'max_connections',\n 'database_name',\n 'db_key',\n ]),\n })\n\n this._setCleanupHandlers()\n }\n\n _addPool = (url, dbKey) => {\n const pool = new Pool({ connectionString: url })\n pool.dbKey = dbKey\n this.databasePools.push(pool)\n return pool\n }\n\n /**\n * @param {Pool} pool - PG connection pool\n * @returns {Promise<{ current: number, max: number, dbName: string }>}\n */\n getDBConnectionsAndName = async pool => {\n try {\n const currentRes = await pool.query(\n 'SELECT COUNT(*) AS current FROM pg_stat_activity WHERE datname = current_database()'\n )\n const current = parseInt(currentRes.rows[0]?.current || 0, 10)\n\n const maxRes = await pool.query(\n \"SELECT current_setting('max_connections') AS max\"\n )\n const max = parseInt(maxRes.rows[0]?.max || 0, 10)\n\n let dbName = pool.options?.database\n if (!dbName && pool.options?.connectionString) {\n try {\n const url = new URL(pool.options.connectionString)\n dbName = url.pathname.replace(/^\\//, '')\n } catch {\n dbName = pool.options.connectionString\n }\n }\n\n return { current, max, dbName }\n } catch (err) {\n return { current: 0, max: 0, dbName: pool.options?.database || '' }\n }\n }\n\n /**\n * Collect database connection metrics for all configured pools\n * @returns {Promise<void>}\n */\n collectDatabaseMetrics = async () => {\n for (const pool of this.databasePools) {\n try {\n const { current, max, dbName } = await this.getDBConnectionsAndName(\n pool\n )\n\n this.databaseConnectionsGauge.set(\n {\n ...this.defaultLabelsWithoutDynoId,\n database_name: pool.dbKey,\n max_connections: String(max),\n db_key: dbName,\n },\n current\n )\n } catch (err) {\n console.warn(`[database-metrics] Failed to collect: ${err.message}`)\n }\n }\n }\n\n /**\n * Push database metrics to Prometheus Pushgateway\n * @returns {Promise<void>}\n */\n pushDatabaseMetrics = async () => {\n try {\n await this.collectDatabaseMetrics()\n await this.gatewayPush()\n this.clearAllCounters()\n\n if (this.metricsLogValues) {\n const metricObjects = await this.registry.getMetricsAsJSON()\n console.info(\n `[database-metrics] Collected DB metrics`,\n JSON.stringify(metricObjects, null, 2)\n )\n }\n } catch (error) {\n console.error(\n `[database-metrics] Failed to collect DB metrics: ${error.message}`\n )\n throw error\n }\n }\n\n /**\n * Start periodic collection.\n * @param {number} [intervalSec=this.intervalSec] - Interval in seconds\n */\n startPush = (intervalSec = this.intervalSec) => {\n this._startPush(intervalSec, () => {\n this.pushDatabaseMetrics().catch(err => {\n console.error(`[database-metrics] Failed to push DB metrics:`, err)\n })\n })\n }\n\n /**\n * Cleanup database pools and exit process\n * @returns {Promise<void>}\n */\n cleanup = async () => {\n try {\n if (this.databasePools) {\n for (const pool of this.databasePools) {\n await pool.end()\n }\n }\n } catch (err) {\n console.error('[database-metrics] Error during cleanup:', err)\n }\n\n process.exit(0)\n }\n\n _setCleanupHandlers = () => {\n process.on('SIGINT', this.cleanup)\n process.on('SIGTER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AACC,OAAO,GAAG;EAAE9E;AAAsB,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"metricsDatabaseClient.js","names":["Pool","require","BaseMetricsClient","exitUnlessMetricsProcessType","DatabaseMetricsClient","constructor","databaseUrl","databaseName","additional_database_urls","metricsConfig","intervalSec","parseInt","process","env","METRICS_DATABASE_INTERVAL_SEC","tmpAppName","appName","BUILD_APP_NAME","mainDbName","startupValidation","console","error","mainPool","connectionString","query","end","info","err","message","dbKey","url","Object","entries","p","processType","databasePools","_addPool","databaseConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabelsWithoutDynoId","_setCleanupHandlers","pool","push","getDBConnectionsAndName","currentRes","current","rows","maxRes","max","dbName","options","database","URL","pathname","replace","collectDatabaseMetrics","set","defaultLabelsWithoutDynoId","database_name","max_connections","String","db_key","warn","pushDatabaseMetrics","gatewayPush","clearAllCounters","metricsLogValues","metricObjects","registry","getMetricsAsJSON","JSON","stringify","startPush","_startPush","catch","cleanup","exit","on","module","exports"],"sources":["../../src/metrics/metricsDatabaseClient.js"],"sourcesContent":["const { Pool } = require('pg')\nconst { BaseMetricsClient } = require('./baseMetricsClient')\nconst { exitUnlessMetricsProcessType } = require('./metricsProcessTypeUtils')\n\n/**\n * DatabaseMetricsClient collects Postgres connection metrics\n * and pushes them to Prometheus Pushgateway.\n *\n * @extends BaseMetricsClient\n */\nclass DatabaseMetricsClient extends BaseMetricsClient {\n /**\n * @param {Object} options\n * @param {string} options.databaseUrl - Required main database URL\n * @param {string} options.databaseName - Main database name for metrics\n * @param {Object<string, string>} [options.additional_database_urls] - Optional additional DBs, keyed by custom name with URL as value\n * @param {string} [options.appName] - Application name (from BaseMetricsClient)\n * @param {string} [options.dynoId] - Dyno/instance ID (from BaseMetricsClient)\n * @param {string} [options.processType] - Process type (from BaseMetricsClient)\n * @param {boolean} [options.enabled] - Enable metrics collection (from BaseMetricsClient)\n * @param {boolean} [options.logValues] - Log metrics values (from BaseMetricsClient)\n * @param {string} [options.pushgatewayUrl] - PushGateway URL (from BaseMetricsClient)\n * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from BaseMetricsClient)\n * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics\n * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service\n * @param {function} [options.startupValidation] - Function to validate startup\n * @param {boolean} [options.disablePushgateway] - Disable pushing to Pushgateway (use HTTP scraping instead)\n */\n constructor({\n databaseUrl,\n databaseName,\n additional_database_urls = {},\n ...metricsConfig\n } = {}) {\n exitUnlessMetricsProcessType(\n metricsConfig,\n 'database-metrics',\n 'database-metrics'\n )\n\n const intervalSec =\n metricsConfig.intervalSec ||\n parseInt(process.env.METRICS_DATABASE_INTERVAL_SEC || '', 10) ||\n 60\n\n const tmpAppName =\n metricsConfig.appName || process.env.BUILD_APP_NAME || 'unknown-app'\n const mainDbName = databaseName || `${tmpAppName}_db`\n\n const startupValidation = async () => {\n if (!databaseUrl) {\n console.error(`[database-metrics] ❌ METRICS_DATABASE_URL is required`)\n return false\n }\n\n try {\n const mainPool = new Pool({ connectionString: databaseUrl })\n await mainPool.query('SELECT 1')\n await mainPool.end()\n console.info(`[database-metrics] ✓ Main database OK: ${mainDbName}`)\n } catch (err) {\n console.error(\n `[database-metrics] ❌ Cannot connect to main database: ${err.message}`\n )\n return false\n }\n\n for (const [dbKey, url] of Object.entries(additional_database_urls)) {\n try {\n const p = new Pool({ connectionString: url })\n await p.query('SELECT 1')\n await p.end()\n console.info(`[database-metrics] ✓ Additional database OK: ${dbKey}`)\n } catch (err) {\n console.error(\n `[database-metrics] ⚠ Skipping additional database: ${dbKey}`\n )\n console.error(`[database-metrics] ${err.message}`)\n }\n }\n\n console.info(`[database-metrics] Database metrics collection starting`)\n return true\n }\n\n super({\n ...metricsConfig,\n processType: metricsConfig.processType || 'database-metrics',\n intervalSec,\n startupValidation,\n })\n\n this.databasePools = []\n\n if (databaseUrl) {\n this._addPool(databaseUrl, mainDbName)\n }\n\n for (const [dbKey, url] of Object.entries(additional_database_urls)) {\n this._addPool(url, dbKey)\n }\n\n /** Gauge for Database connections (no dyno_id). */\n this.databaseConnectionsGauge = this.createGauge({\n name: 'app_database_connections',\n help: 'Postgres database connections',\n labelNames: this.withDefaultLabelsWithoutDynoId([\n 'max_connections',\n 'database_name',\n 'db_key',\n ]),\n })\n\n this._setCleanupHandlers()\n }\n\n _addPool = (url, dbKey) => {\n const pool = new Pool({ connectionString: url })\n pool.dbKey = dbKey\n this.databasePools.push(pool)\n return pool\n }\n\n /**\n * @param {Pool} pool - PG connection pool\n * @returns {Promise<{ current: number, max: number, dbName: string }>}\n */\n getDBConnectionsAndName = async pool => {\n try {\n const currentRes = await pool.query(\n 'SELECT COUNT(*) AS current FROM pg_stat_activity WHERE datname = current_database()'\n )\n const current = parseInt(currentRes.rows[0]?.current || 0, 10)\n\n const maxRes = await pool.query(\n \"SELECT current_setting('max_connections') AS max\"\n )\n const max = parseInt(maxRes.rows[0]?.max || 0, 10)\n\n let dbName = pool.options?.database\n if (!dbName && pool.options?.connectionString) {\n try {\n const url = new URL(pool.options.connectionString)\n dbName = url.pathname.replace(/^\\//, '')\n } catch {\n dbName = pool.options.connectionString\n }\n }\n\n return { current, max, dbName }\n } catch (err) {\n return { current: 0, max: 0, dbName: pool.options?.database || '' }\n }\n }\n\n /**\n * Collect database connection metrics for all configured pools\n * @returns {Promise<void>}\n */\n collectDatabaseMetrics = async () => {\n for (const pool of this.databasePools) {\n try {\n const { current, max, dbName } = await this.getDBConnectionsAndName(\n pool\n )\n\n this.databaseConnectionsGauge.set(\n {\n ...this.defaultLabelsWithoutDynoId,\n database_name: pool.dbKey,\n max_connections: String(max),\n db_key: dbName,\n },\n current\n )\n } catch (err) {\n console.warn(`[database-metrics] Failed to collect: ${err.message}`)\n }\n }\n }\n\n /**\n * Push database metrics to Prometheus Pushgateway\n * @returns {Promise<void>}\n */\n pushDatabaseMetrics = async () => {\n try {\n await this.collectDatabaseMetrics()\n await this.gatewayPush()\n this.clearAllCounters()\n\n if (this.metricsLogValues) {\n const metricObjects = await this.registry.getMetricsAsJSON()\n console.info(\n `[database-metrics] Collected DB metrics`,\n JSON.stringify(metricObjects, null, 2)\n )\n }\n } catch (error) {\n console.error(\n `[database-metrics] Failed to collect DB metrics: ${error.message}`\n )\n throw error\n }\n }\n\n /**\n * Start periodic collection.\n * @param {number} [intervalSec=this.intervalSec] - Interval in seconds\n */\n startPush = (intervalSec = this.intervalSec) => {\n this._startPush(intervalSec, () => {\n this.pushDatabaseMetrics().catch(err => {\n console.error(`[database-metrics] Failed to push DB metrics:`, err)\n })\n })\n }\n\n /**\n * Cleanup database pools and exit process\n * @returns {Promise<void>}\n */\n cleanup = async () => {\n try {\n if (this.databasePools) {\n for (const pool of this.databasePools) {\n await pool.end()\n }\n }\n } catch (err) {\n console.error('[database-metrics] Error during cleanup:', err)\n }\n\n process.exit(0)\n }\n\n _setCleanupHandlers = () => {\n process.on('SIGINT', this.cleanup)\n process.on('SIGTERM', this.cleanup)\n }\n}\n\nmodule.exports = { DatabaseMetricsClient }\n"],"mappings":";;AAAA,MAAM;EAAEA;AAAK,CAAC,GAAGC,OAAO,CAAC,IAAI,CAAC;AAC9B,MAAM;EAAEC;AAAkB,CAAC,GAAGD,OAAO,CAAC,qBAAqB,CAAC;AAC5D,MAAM;EAAEE;AAA6B,CAAC,GAAGF,OAAO,CAAC,2BAA2B,CAAC;;AAE7E;AACA;AACA;AACA;AACA;AACA;AACA,MAAMG,qBAAqB,SAASF,iBAAiB,CAAC;EACpD;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEG,WAAWA,CAAC;IACVC,WAAW;IACXC,YAAY;IACZC,wBAAwB,GAAG,CAAC,CAAC;IAC7B,GAAGC;EACL,CAAC,GAAG,CAAC,CAAC,EAAE;IACNN,4BAA4B,CAC1BM,aAAa,EACb,kBAAkB,EAClB,kBACF,CAAC;IAED,MAAMC,WAAW,GACfD,aAAa,CAAC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EACJ,CAAC;;EAED;AACF;AACA;AACA;EACEmD,OAAO,GAAG,MAAAA,CAAA,KAAY;IACpB,IAAI;MACF,IAAI,IAAI,CAAC3C,aAAa,EAAE;QACtB,KAAK,MAAMS,IAAI,IAAI,IAAI,CAACT,aAAa,EAAE;UACrC,MAAMS,IAAI,CAACnB,GAAG,CAAC,CAAC;QAClB;MACF;IACF,CAAC,CAAC,OAAOE,GAAG,EAAE;MACZP,OAAO,CAACC,KAAK,CAAC,0CAA0C,EAAEM,GAAG,CAAC;IAChE;IAEAf,OAAO,CAACmE,IAAI,CAAC,CAAC,CAAC;EACjB,CAAC;EAEDpC,mBAAmB,GAAGA,CAAA,KAAM;IAC1B/B,OAAO,CAACoE,EAAE,CAAC,QAAQ,EAAE,IAAI,CAACF,OAAO,CAAC;IAClClE,OAAO,CAACoE,EAAE,CAAC,SAAS,EAAE,IAAI,CAACF,OAAO,CAAC;EACrC,CAAC;AACH;AAEAG,MAAM,CAACC,OAAO,GAAG;EAAE9E;AAAsB,CAAC","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsProcessTypeUtils.d.ts","sourceRoot":"","sources":["../../src/metrics/metricsProcessTypeUtils.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;GAMG;AACH,yDAJW;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,sBACxB,MAAM,GACJ,MAAM,CAQlB;AAED;;;;;;;;GAQG;AACH,4DALW;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,sBACxB,MAAM,WACN,MAAM,GAAG,MAAM,EAAE,GACf,IAAI,
|
|
1
|
+
{"version":3,"file":"metricsProcessTypeUtils.d.ts","sourceRoot":"","sources":["../../src/metrics/metricsProcessTypeUtils.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;GAMG;AACH,yDAJW;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,sBACxB,MAAM,GACJ,MAAM,CAQlB;AAED;;;;;;;;GAQG;AACH,4DALW;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,sBACxB,MAAM,WACN,MAAM,GAAG,MAAM,EAAE,GACf,IAAI,CAYhB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsProcessTypeUtils.js","names":["resolveMetricsProcessType","metricsConfig","defaultProcessType","processType","process","env","BUILD_DYNO_PROCESS_TYPE","exitUnlessMetricsProcessType","allowed","resolved","list","Array","isArray","includes","exit","module","exports"],"sources":["../../src/metrics/metricsProcessTypeUtils.js"],"sourcesContent":["/**\n * Helpers for resolving `processType` and silently exiting when a specialized metrics client\n * is constructed on the wrong dyno / process (no log output).\n *\n * @module metrics/metricsProcessTypeUtils\n */\n\n/**\n * Resolve logical process type the same way specialized metrics clients do.\n *\n * @param {{ processType?: string }} metricsConfig - Remainder of constructor options (e.g. after destructuring `redisClient`, `databaseUrl`, …)\n * @param {string} defaultProcessType - Fallback when config and env are unset\n * @returns {string}\n */\nfunction resolveMetricsProcessType(metricsConfig, defaultProcessType) {\n return (\n metricsConfig.processType ||\n process.env.BUILD_DYNO_PROCESS_TYPE ||\n defaultProcessType\n )\n}\n\n/**\n * If the resolved process type is not in the allowlist, exit immediately with no logs (`process.exit(0)`).\n * Used so the wrong dyno type does not run a specialized metrics client.\n *\n * @param {{ processType?: string }} metricsConfig\n * @param {string} defaultProcessType - Fallback for resolution only (when `processType` and env are unset)\n * @param {string | string[]} allowed - Single type or allowlist (e.g. `['redis-metrics','queue-metrics']`)\n * @returns {void}\n */\nfunction exitUnlessMetricsProcessType(metricsConfig
|
|
1
|
+
{"version":3,"file":"metricsProcessTypeUtils.js","names":["resolveMetricsProcessType","metricsConfig","defaultProcessType","processType","process","env","BUILD_DYNO_PROCESS_TYPE","exitUnlessMetricsProcessType","allowed","resolved","list","Array","isArray","includes","exit","module","exports"],"sources":["../../src/metrics/metricsProcessTypeUtils.js"],"sourcesContent":["/**\n * Helpers for resolving `processType` and silently exiting when a specialized metrics client\n * is constructed on the wrong dyno / process (no log output).\n *\n * @module metrics/metricsProcessTypeUtils\n */\n\n/**\n * Resolve logical process type the same way specialized metrics clients do.\n *\n * @param {{ processType?: string }} metricsConfig - Remainder of constructor options (e.g. after destructuring `redisClient`, `databaseUrl`, …)\n * @param {string} defaultProcessType - Fallback when config and env are unset\n * @returns {string}\n */\nfunction resolveMetricsProcessType(metricsConfig, defaultProcessType) {\n return (\n metricsConfig.processType ||\n process.env.BUILD_DYNO_PROCESS_TYPE ||\n defaultProcessType\n )\n}\n\n/**\n * If the resolved process type is not in the allowlist, exit immediately with no logs (`process.exit(0)`).\n * Used so the wrong dyno type does not run a specialized metrics client.\n *\n * @param {{ processType?: string }} metricsConfig\n * @param {string} defaultProcessType - Fallback for resolution only (when `processType` and env are unset)\n * @param {string | string[]} allowed - Single type or allowlist (e.g. `['redis-metrics','queue-metrics']`)\n * @returns {void}\n */\nfunction exitUnlessMetricsProcessType(\n metricsConfig,\n defaultProcessType,\n allowed\n) {\n const resolved = resolveMetricsProcessType(metricsConfig, defaultProcessType)\n const list = Array.isArray(allowed) ? allowed : [allowed]\n if (!list.includes(resolved)) {\n process.exit(0)\n }\n}\n\nmodule.exports = {\n resolveMetricsProcessType,\n exitUnlessMetricsProcessType,\n}\n"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,yBAAyBA,CAACC,aAAa,EAAEC,kBAAkB,EAAE;EACpE,OACED,aAAa,CAACE,WAAW,IACzBC,OAAO,CAACC,GAAG,CAACC,uBAAuB,IACnCJ,kBAAkB;AAEtB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASK,4BAA4BA,CACnCN,aAAa,EACbC,kBAAkB,EAClBM,OAAO,EACP;EACA,MAAMC,QAAQ,GAAGT,yBAAyB,CAACC,aAAa,EAAEC,kBAAkB,CAAC;EAC7E,MAAMQ,IAAI,GAAGC,KAAK,CAACC,OAAO,CAACJ,OAAO,CAAC,GAAGA,OAAO,GAAG,CAACA,OAAO,CAAC;EACzD,IAAI,CAACE,IAAI,CAACG,QAAQ,CAACJ,QAAQ,CAAC,EAAE;IAC5BL,OAAO,CAACU,IAAI,CAAC,CAAC,CAAC;EACjB;AACF;AAEAC,MAAM,CAACC,OAAO,GAAG;EACfhB,yBAAyB;EACzBO;AACF,CAAC","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsQueueRedisClient.d.ts","sourceRoot":"","sources":["../../src/metrics/metricsQueueRedisClient.js"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH;
|
|
1
|
+
{"version":3,"file":"metricsQueueRedisClient.d.ts","sourceRoot":"","sources":["../../src/metrics/metricsQueueRedisClient.js"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH;IAsEI,wCAAsD;IACtD,iCAA0C;IAE1C,4DAA4D;IAa5D,0BAA2B;IAE3B,mDAAmD;IACnD,oDAIE;IAKJ;;;;;OAKG;IACH,cAHW,MAAM,EAAE,KACN,QAAQ,GAAG,CAAC,CA6BxB;IAED,8BAGC;IAED,2DA6BC;IAED;;;;;;OAmBC;IAED;;;;;;OAwBC;IAED;;;;;;;;;;;OAWG;IACH,uCAHW,MAAM,KACJ,QAAQ,IAAI,CAAC,CAyCzB;IAED;;;OAGG;IACH,2BAFa,QAAQ,IAAI,CAAC,CAuCzB;CAoCF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsQueueRedisClient.js","names":["RedisMetricsClient","require","IOREDIS","REDIS_V3","REDIS_V4","exitUnlessMetricsProcessType","QueueRedisMetricsClient","constructor","redisClient","metricsConfig","getConfiguredQueueNames","process","env","METRICS_APP_REDIS_BQ","Error","allQueues","split","map","q","trim","filter","Boolean","length","Set","startupValidation","queueNames","console","info","error","message","queueCache","Map","queueJobsGauge","createGauge","name","help","labelNames","withDefaultLabelsWithoutDynoId","_setCleanupHandlers","_send","args","Promise","reject","redisClientType","resolve","send_command","slice","err","result","sendCommand","call","_toNumber","v","n","parseInt","String","Number","isFinite","_getQueueType","queueName","forced","METRICS_QUEUE_TYPE","toLowerCase","bullPrefix","BULL_QUEUE_PREFIX","beePrefix","BEE_QUEUE_PREFIX","METRICS_BEE_QUEUE_PREFIX","bullWaitKey","beeWaitKey","bullExists","beeExists","all","_getBullHealth","prefix","base","waiting","active","succeeded","failed","delayed","_getBeeQueueHealth","collectSingleQueueMetrics","queueType","health","labels","defaultLabelsWithoutDynoId","queue_name","set","status","warn","collectQueueMetrics","collectRedisMetrics","allSettled","gatewayPush","clearAllCounters","metricsLogValues","metricObjects","registry","getMetricsAsJSON","JSON","stringify","includes","startPush","intervalSec","_startPush","catch","on","cleanup","queue","close","exit","module","exports"],"sources":["../../src/metrics/metricsQueueRedisClient.js"],"sourcesContent":["const { RedisMetricsClient } = require('./metricsRedisClient')\nconst { IOREDIS, REDIS_V3, REDIS_V4 } = require('../redisUtils')\nconst { exitUnlessMetricsProcessType } = require('./metricsProcessTypeUtils')\n\n/**\n * QueueRedisMetricsClient extends RedisMetricsClient to collect\n * Redis and Bee Queue metrics periodically and push them to Prometheus Pushgateway.\n *\n * @extends RedisMetricsClient\n */\nclass QueueRedisMetricsClient extends RedisMetricsClient {\n /**\n * @param {Object} options\n * @param {any} options.redisClient - Redis client instance (required)\n * @param {string} [options.appName] - Application name (from BaseMetricsClient)\n * @param {string} [options.dynoId] - Dyno/instance ID (from BaseMetricsClient)\n * @param {string} [options.processType] - Process type (from BaseMetricsClient)\n * @param {boolean} [options.enabled] - Enable metrics collection (from BaseMetricsClient)\n * @param {boolean} [options.logValues] - Log metrics values (from BaseMetricsClient)\n * @param {string} [options.pushgatewayUrl] - PushGateway URL (from BaseMetricsClient)\n * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from BaseMetricsClient)\n * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics (from BaseMetricsClient)\n * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service (from BaseMetricsClient)\n * @param {function} [options.startupValidation] - Function to validate startup (from BaseMetricsClient)\n * @param {boolean} [options.disablePushgateway] - Disable pushing to Pushgateway (use HTTP scraping instead)\n */\n constructor({ redisClient, ...metricsConfig } = {}) {\n exitUnlessMetricsProcessType(metricsConfig, 'queue-metrics', 'queue-metrics')\n\n const getConfiguredQueueNames = () => {\n if (!process.env.METRICS_APP_REDIS_BQ) {\n throw new Error(\n 'No queues configured for monitoring. Set METRICS_APP_REDIS_BQ with comma-separated queue names'\n )\n }\n\n const allQueues = process.env.METRICS_APP_REDIS_BQ.split(',')\n .map(q => q.trim())\n .filter(Boolean)\n\n if (allQueues.length === 0) {\n throw new Error(\n 'METRICS_APP_REDIS_BQ is empty or contains only whitespace. ' +\n 'Example: METRICS_APP_REDIS_BQ=\"adalo-compile,adalo-migrations\"'\n )\n }\n\n return [...new Set(allQueues)]\n }\n\n const startupValidation = () => {\n try {\n const queueNames = getConfiguredQueueNames()\n console.info(\n `[queue-metrics] Queue & Redis metrics collection starting for ${queueNames.length} queues`\n )\n return true\n } catch (error) {\n console.error(`[queue-metrics] ❌ Cannot start: ${error.message}`)\n console.error(\n `[queue-metrics] 💡 Example: METRICS_APP_REDIS_BQ=\"adalo-compile,adalo-migrations\"`\n )\n console.error(\n `[queue-metrics] 💡 Optional: METRICS_QUEUE_INTERVAL_SEC=\"10\"`\n )\n console.error(`[queue-metrics] Skipping queue metrics collection`)\n return false\n }\n }\n\n super({\n ...metricsConfig,\n redisClient,\n startupValidation,\n })\n\n this.getConfiguredQueueNames = getConfiguredQueueNames\n this.startupValidation = startupValidation\n\n /** Cache for queue objects to avoid multiple connections */\n // NOTE:\n // Historically we used `bee-queue`'s `Queue#checkHealth()` here.\n // But bee-queue depends on `redis@3`, which is not compatible with Node 22\n // in some environments and can crash with errors like:\n // - \"this._ready.then is not a function\"\n // - \"TypeError: this.stream.once is not a function\"\n //\n // To avoid pulling in node-redis v3 at runtime, we read queue counters\n // directly from Redis using the *provided* redis client (ioredis or node-redis).\n //\n // Keep this map reserved for possible future caching (e.g. key schema detection),\n // but we no longer store Queue instances.\n this.queueCache = new Map()\n\n /** Gauge for queue jobs by status (no dyno_id). */\n this.queueJobsGauge = this.createGauge({\n name: 'app_queue_jobs_count',\n help: 'Number of app jobs in the queue by status',\n labelNames: this.withDefaultLabelsWithoutDynoId(['queue_name', 'status']),\n })\n\n this._setCleanupHandlers()\n }\n\n /**\n * Execute a Redis command in a client-type safe way.\n *\n * @param {string[]} args Command args array, e.g. ['LLEN', 'key']\n * @returns {Promise<any>}\n */\n _send = args => {\n if (!this.redisClient) {\n return Promise.reject(new Error('Redis client not provided'))\n }\n\n // node-redis v3 (callback API)\n if (this.redisClientType === REDIS_V3) {\n return new Promise((resolve, reject) => {\n this.redisClient.send_command(args[0], args.slice(1), (err, result) => {\n if (err) reject(err)\n else resolve(result)\n })\n })\n }\n\n // node-redis v4 (Promise API)\n if (this.redisClientType === REDIS_V4) {\n return this.redisClient.sendCommand(args)\n }\n\n // ioredis (Promise API)\n if (this.redisClientType === IOREDIS) {\n // ioredis supports `.call(command, ...args)`\n return this.redisClient.call(args[0], ...args.slice(1))\n }\n\n return Promise.reject(new Error('Unsupported Redis client type'))\n }\n\n _toNumber = v => {\n const n = typeof v === 'number' ? v : parseInt(String(v || '0'), 10)\n return Number.isFinite(n) ? n : 0\n }\n\n _getQueueType = async queueName => {\n const forced = (process.env.METRICS_QUEUE_TYPE || '').trim().toLowerCase()\n if (forced === 'bull' || forced === 'bee') return forced\n\n const bullPrefix =\n (process.env.BULL_QUEUE_PREFIX || 'bull').trim() || 'bull'\n const beePrefix =\n (\n process.env.BEE_QUEUE_PREFIX ||\n process.env.METRICS_BEE_QUEUE_PREFIX ||\n 'bq'\n ).trim() || 'bq'\n\n // Detect by checking the canonical \"waiting\" key names.\n const bullWaitKey = `${bullPrefix}:${queueName}:wait`\n const beeWaitKey = `${beePrefix}:${queueName}:waiting`\n\n try {\n const [bullExists, beeExists] = await Promise.all([\n this._send(['EXISTS', bullWaitKey]),\n this._send(['EXISTS', beeWaitKey]),\n ])\n if (this._toNumber(bullExists) > 0) return 'bull'\n if (this._toNumber(beeExists) > 0) return 'bee'\n } catch {\n // If EXISTS is blocked/unavailable, fall back to trying bull first.\n }\n\n return 'bull'\n }\n\n _getBullHealth = async queueName => {\n const prefix = (process.env.BULL_QUEUE_PREFIX || 'bull').trim() || 'bull'\n const base = `${prefix}:${queueName}:`\n\n const [waiting, active, succeeded, failed, delayed] = await Promise.all([\n this._send(['LLEN', `${base}wait`]),\n this._send(['LLEN', `${base}active`]),\n this._send(['ZCARD', `${base}completed`]),\n this._send(['ZCARD', `${base}failed`]),\n this._send(['ZCARD', `${base}delayed`]),\n ])\n\n return {\n waiting: this._toNumber(waiting),\n active: this._toNumber(active),\n succeeded: this._toNumber(succeeded),\n failed: this._toNumber(failed),\n delayed: this._toNumber(delayed),\n }\n }\n\n _getBeeQueueHealth = async queueName => {\n const prefix =\n (\n process.env.BEE_QUEUE_PREFIX ||\n process.env.METRICS_BEE_QUEUE_PREFIX ||\n 'bq'\n ).trim() || 'bq'\n const base = `${prefix}:${queueName}:`\n\n const [waiting, active, succeeded, failed, delayed] = await Promise.all([\n this._send(['LLEN', `${base}waiting`]),\n this._send(['LLEN', `${base}active`]),\n this._send(['SCARD', `${base}succeeded`]),\n this._send(['SCARD', `${base}failed`]),\n this._send(['ZCARD', `${base}delayed`]),\n ])\n\n return {\n waiting: this._toNumber(waiting),\n active: this._toNumber(active),\n succeeded: this._toNumber(succeeded),\n failed: this._toNumber(failed),\n delayed: this._toNumber(delayed),\n }\n }\n\n /**\n * Collect metrics for a single queue and set gauges.\n *\n * Supports:\n * - Bull (default): keys like `bull:<queue>:wait`, `...:active`, `...:completed`, `...:failed`, `...:delayed`\n * - Bee-Queue: keys like `bq:<queue>:waiting`, `...:active`, `...:succeeded`, `...:failed`, `...:delayed`\n *\n * Auto-detects queue type unless `METRICS_QUEUE_TYPE` is set to `bull` or `bee`.\n *\n * @param {string} queueName - Name of the queue\n * @returns {Promise<void>}\n */\n collectSingleQueueMetrics = async queueName => {\n try {\n const queueType = await this._getQueueType(queueName)\n const health =\n queueType === 'bee'\n ? await this._getBeeQueueHealth(queueName)\n : await this._getBullHealth(queueName)\n\n const labels = {\n ...this.defaultLabelsWithoutDynoId,\n queue_name: queueName,\n }\n\n this.queueJobsGauge.set(\n { ...labels, status: 'waiting' },\n health.waiting || 0\n )\n this.queueJobsGauge.set(\n { ...labels, status: 'active' },\n health.active || 0\n )\n this.queueJobsGauge.set(\n { ...labels, status: 'succeeded' },\n health.succeeded || 0\n )\n this.queueJobsGauge.set(\n { ...labels, status: 'failed' },\n health.failed || 0\n )\n this.queueJobsGauge.set(\n { ...labels, status: 'delayed' },\n health.delayed || 0\n )\n } catch (error) {\n console.warn(\n `[queue-metrics] Failed to collect metrics for queue ${queueName}:`,\n error.message\n )\n }\n }\n\n /**\n * Collect metrics for all queues and Redis, then push to Pushgateway\n * @returns {Promise<void>}\n */\n collectQueueMetrics = async () => {\n try {\n const queueNames = this.getConfiguredQueueNames()\n\n await this.collectRedisMetrics()\n await Promise.allSettled(\n queueNames.map(queueName => this.collectSingleQueueMetrics(queueName))\n )\n\n await this.gatewayPush()\n this.clearAllCounters()\n\n if (this.metricsLogValues) {\n const metricObjects = await this.registry.getMetricsAsJSON()\n console.info(\n `[queue-metrics] Collected metrics for ${queueNames.length} queues:`,\n JSON.stringify(metricObjects, null, 2)\n )\n }\n } catch (error) {\n if (\n error.message?.includes('No queues configured') ||\n error.message?.includes('METRICS_APP_REDIS_BQ')\n ) {\n console.error(\n `[queue-metrics] ❌ Configuration error: ${error.message}`\n )\n console.error(\n `[queue-metrics] 💡 Example config: METRICS_APP_REDIS_BQ=\"adalo-compile,adalo-migrations\"`\n )\n } else {\n console.error(\n `[queue-metrics] Failed to collect queue metrics: ${error.message}`\n )\n }\n throw error\n }\n }\n\n /**\n * Start periodic collection.\n * @param {number} [intervalSec=this.intervalSec] - Interval in seconds\n */\n startPush = (intervalSec = this.intervalSec) => {\n this._startPush(intervalSec, () => {\n this.collectQueueMetrics().catch(err => {\n console.error(\n `[queue-metrics] Failed to collect queue & Redis metrics:`,\n err\n )\n })\n })\n }\n\n _setCleanupHandlers = () => {\n process.on('SIGINT', this.cleanup)\n process.on('SIGTERM', this.cleanup)\n }\n\n /**\n * Cleanup queues and exit process.\n * @returns {Promise<void>}\n */\n cleanup = async () => {\n for (const [queueName, queue] of this.queueCache) {\n try {\n await queue.close()\n } catch (err) {\n console.error(`[queue-metrics] Error closing queue ${queueName}:`, err)\n }\n }\n process.exit(0)\n }\n}\n\nmodule.exports = { QueueRedisMetricsClient }\n"],"mappings":";;AAAA,MAAM;EAAEA;AAAmB,CAAC,GAAGC,OAAO,CAAC,sBAAsB,CAAC;AAC9D,MAAM;EAAEC,OAAO;EAAEC,QAAQ;EAAEC;AAAS,CAAC,GAAGH,OAAO,CAAC,eAAe,CAAC;AAChE,MAAM;EAAEI;AAA6B,CAAC,GAAGJ,OAAO,CAAC,2BAA2B,CAAC;;AAE7E;AACA;AACA;AACA;AACA;AACA;AACA,MAAMK,uBAAuB,SAASN,kBAAkB,CAAC;EACvD;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEO,WAAWA,CAAC;IAAEC,WAAW;IAAE,GAAGC;EAAc,CAAC,GAAG,CAAC,CAAC,EAAE;IAClDJ,4BAA4B,CAACI,aAAa,EAAE,eAAe,EAAE,eAAe,CAAC;IAE7E,MAAMC,uBAAuB,GAAGA,CAAA,KAAM;MACpC,IAAI,CAACC,OAAO,CAACC,GAAG,CAACC,oBAAoB,EAAE;QACrC,MAAM,IAAIC,KAAK,CACb,gGACF,CAAC;MACH;MAEA,MAAMC,SAAS,GAAGJ,OAAO,CAACC,GAAG,CAACC,oBAAoB,CAACG,KAAK,CAAC,GAAG,CAAC,CAC1DC,GAAG,CAACC,CAAC,IAAIA,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C,SAAS,EAAE,IAAI,CAACC,OAAO,CAAC;EACrC,CAAC;;EAED;AACF;AACA;AACA;EACEA,OAAO,GAAG,MAAAA,CAAA,KAAY;IACpB,KAAK,MAAM,CAACjD,SAAS,EAAEkD,KAAK,CAAC,IAAI,IAAI,CAAC/E,UAAU,EAAE;MAChD,IAAI;QACF,MAAM+E,KAAK,CAACC,KAAK,CAAC,CAAC;MACrB,CAAC,CAAC,OAAO/D,GAAG,EAAE;QACZrB,OAAO,CAACE,KAAK,CAAC,uCAAuC+B,SAAS,GAAG,EAAEZ,GAAG,CAAC;MACzE;IACF;IACApC,OAAO,CAACoG,IAAI,CAAC,CAAC,CAAC;EACjB,CAAC;AACH;AAEAC,MAAM,CAACC,OAAO,GAAG;EAAE3G;AAAwB,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"metricsQueueRedisClient.js","names":["RedisMetricsClient","require","IOREDIS","REDIS_V3","REDIS_V4","exitUnlessMetricsProcessType","QueueRedisMetricsClient","constructor","redisClient","metricsConfig","getConfiguredQueueNames","process","env","METRICS_APP_REDIS_BQ","Error","allQueues","split","map","q","trim","filter","Boolean","length","Set","startupValidation","queueNames","console","info","error","message","queueCache","Map","queueJobsGauge","createGauge","name","help","labelNames","withDefaultLabelsWithoutDynoId","_setCleanupHandlers","_send","args","Promise","reject","redisClientType","resolve","send_command","slice","err","result","sendCommand","call","_toNumber","v","n","parseInt","String","Number","isFinite","_getQueueType","queueName","forced","METRICS_QUEUE_TYPE","toLowerCase","bullPrefix","BULL_QUEUE_PREFIX","beePrefix","BEE_QUEUE_PREFIX","METRICS_BEE_QUEUE_PREFIX","bullWaitKey","beeWaitKey","bullExists","beeExists","all","_getBullHealth","prefix","base","waiting","active","succeeded","failed","delayed","_getBeeQueueHealth","collectSingleQueueMetrics","queueType","health","labels","defaultLabelsWithoutDynoId","queue_name","set","status","warn","collectQueueMetrics","collectRedisMetrics","allSettled","gatewayPush","clearAllCounters","metricsLogValues","metricObjects","registry","getMetricsAsJSON","JSON","stringify","includes","startPush","intervalSec","_startPush","catch","on","cleanup","queue","close","exit","module","exports"],"sources":["../../src/metrics/metricsQueueRedisClient.js"],"sourcesContent":["const { RedisMetricsClient } = require('./metricsRedisClient')\nconst { IOREDIS, REDIS_V3, REDIS_V4 } = require('../redisUtils')\nconst { exitUnlessMetricsProcessType } = require('./metricsProcessTypeUtils')\n\n/**\n * QueueRedisMetricsClient extends RedisMetricsClient to collect\n * Redis and Bee Queue metrics periodically and push them to Prometheus Pushgateway.\n *\n * @extends RedisMetricsClient\n */\nclass QueueRedisMetricsClient extends RedisMetricsClient {\n /**\n * @param {Object} options\n * @param {any} options.redisClient - Redis client instance (required)\n * @param {string} [options.appName] - Application name (from BaseMetricsClient)\n * @param {string} [options.dynoId] - Dyno/instance ID (from BaseMetricsClient)\n * @param {string} [options.processType] - Process type (from BaseMetricsClient)\n * @param {boolean} [options.enabled] - Enable metrics collection (from BaseMetricsClient)\n * @param {boolean} [options.logValues] - Log metrics values (from BaseMetricsClient)\n * @param {string} [options.pushgatewayUrl] - PushGateway URL (from BaseMetricsClient)\n * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from BaseMetricsClient)\n * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics (from BaseMetricsClient)\n * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service (from BaseMetricsClient)\n * @param {function} [options.startupValidation] - Function to validate startup (from BaseMetricsClient)\n * @param {boolean} [options.disablePushgateway] - Disable pushing to Pushgateway (use HTTP scraping instead)\n */\n constructor({ redisClient, ...metricsConfig } = {}) {\n exitUnlessMetricsProcessType(\n metricsConfig,\n 'queue-metrics',\n 'queue-metrics'\n )\n\n const getConfiguredQueueNames = () => {\n if (!process.env.METRICS_APP_REDIS_BQ) {\n throw new Error(\n 'No queues configured for monitoring. Set METRICS_APP_REDIS_BQ with comma-separated queue names'\n )\n }\n\n const allQueues = process.env.METRICS_APP_REDIS_BQ.split(',')\n .map(q => q.trim())\n .filter(Boolean)\n\n if (allQueues.length === 0) {\n throw new Error(\n 'METRICS_APP_REDIS_BQ is empty or contains only whitespace. ' +\n 'Example: METRICS_APP_REDIS_BQ=\"adalo-compile,adalo-migrations\"'\n )\n }\n\n return [...new Set(allQueues)]\n }\n\n const startupValidation = () => {\n try {\n const queueNames = getConfiguredQueueNames()\n console.info(\n `[queue-metrics] Queue & Redis metrics collection starting for ${queueNames.length} queues`\n )\n return true\n } catch (error) {\n console.error(`[queue-metrics] ❌ Cannot start: ${error.message}`)\n console.error(\n `[queue-metrics] 💡 Example: METRICS_APP_REDIS_BQ=\"adalo-compile,adalo-migrations\"`\n )\n console.error(\n `[queue-metrics] 💡 Optional: METRICS_QUEUE_INTERVAL_SEC=\"10\"`\n )\n console.error(`[queue-metrics] Skipping queue metrics collection`)\n return false\n }\n }\n\n super({\n ...metricsConfig,\n redisClient,\n startupValidation,\n })\n\n this.getConfiguredQueueNames = getConfiguredQueueNames\n this.startupValidation = startupValidation\n\n /** Cache for queue objects to avoid multiple connections */\n // NOTE:\n // Historically we used `bee-queue`'s `Queue#checkHealth()` here.\n // But bee-queue depends on `redis@3`, which is not compatible with Node 22\n // in some environments and can crash with errors like:\n // - \"this._ready.then is not a function\"\n // - \"TypeError: this.stream.once is not a function\"\n //\n // To avoid pulling in node-redis v3 at runtime, we read queue counters\n // directly from Redis using the *provided* redis client (ioredis or node-redis).\n //\n // Keep this map reserved for possible future caching (e.g. key schema detection),\n // but we no longer store Queue instances.\n this.queueCache = new Map()\n\n /** Gauge for queue jobs by status (no dyno_id). */\n this.queueJobsGauge = this.createGauge({\n name: 'app_queue_jobs_count',\n help: 'Number of app jobs in the queue by status',\n labelNames: this.withDefaultLabelsWithoutDynoId(['queue_name', 'status']),\n })\n\n this._setCleanupHandlers()\n }\n\n /**\n * Execute a Redis command in a client-type safe way.\n *\n * @param {string[]} args Command args array, e.g. ['LLEN', 'key']\n * @returns {Promise<any>}\n */\n _send = args => {\n if (!this.redisClient) {\n return Promise.reject(new Error('Redis client not provided'))\n }\n\n // node-redis v3 (callback API)\n if (this.redisClientType === REDIS_V3) {\n return new Promise((resolve, reject) => {\n this.redisClient.send_command(args[0], args.slice(1), (err, result) => {\n if (err) reject(err)\n else resolve(result)\n })\n })\n }\n\n // node-redis v4 (Promise API)\n if (this.redisClientType === REDIS_V4) {\n return this.redisClient.sendCommand(args)\n }\n\n // ioredis (Promise API)\n if (this.redisClientType === IOREDIS) {\n // ioredis supports `.call(command, ...args)`\n return this.redisClient.call(args[0], ...args.slice(1))\n }\n\n return Promise.reject(new Error('Unsupported Redis client type'))\n }\n\n _toNumber = v => {\n const n = typeof v === 'number' ? v : parseInt(String(v || '0'), 10)\n return Number.isFinite(n) ? n : 0\n }\n\n _getQueueType = async queueName => {\n const forced = (process.env.METRICS_QUEUE_TYPE || '').trim().toLowerCase()\n if (forced === 'bull' || forced === 'bee') return forced\n\n const bullPrefix =\n (process.env.BULL_QUEUE_PREFIX || 'bull').trim() || 'bull'\n const beePrefix =\n (\n process.env.BEE_QUEUE_PREFIX ||\n process.env.METRICS_BEE_QUEUE_PREFIX ||\n 'bq'\n ).trim() || 'bq'\n\n // Detect by checking the canonical \"waiting\" key names.\n const bullWaitKey = `${bullPrefix}:${queueName}:wait`\n const beeWaitKey = `${beePrefix}:${queueName}:waiting`\n\n try {\n const [bullExists, beeExists] = await Promise.all([\n this._send(['EXISTS', bullWaitKey]),\n this._send(['EXISTS', beeWaitKey]),\n ])\n if (this._toNumber(bullExists) > 0) return 'bull'\n if (this._toNumber(beeExists) > 0) return 'bee'\n } catch {\n // If EXISTS is blocked/unavailable, fall back to trying bull first.\n }\n\n return 'bull'\n }\n\n _getBullHealth = async queueName => {\n const prefix = (process.env.BULL_QUEUE_PREFIX || 'bull').trim() || 'bull'\n const base = `${prefix}:${queueName}:`\n\n const [waiting, active, succeeded, failed, delayed] = await Promise.all([\n this._send(['LLEN', `${base}wait`]),\n this._send(['LLEN', `${base}active`]),\n this._send(['ZCARD', `${base}completed`]),\n this._send(['ZCARD', `${base}failed`]),\n this._send(['ZCARD', `${base}delayed`]),\n ])\n\n return {\n waiting: this._toNumber(waiting),\n active: this._toNumber(active),\n succeeded: this._toNumber(succeeded),\n failed: this._toNumber(failed),\n delayed: this._toNumber(delayed),\n }\n }\n\n _getBeeQueueHealth = async queueName => {\n const prefix =\n (\n process.env.BEE_QUEUE_PREFIX ||\n process.env.METRICS_BEE_QUEUE_PREFIX ||\n 'bq'\n ).trim() || 'bq'\n const base = `${prefix}:${queueName}:`\n\n const [waiting, active, succeeded, failed, delayed] = await Promise.all([\n this._send(['LLEN', `${base}waiting`]),\n this._send(['LLEN', `${base}active`]),\n this._send(['SCARD', `${base}succeeded`]),\n this._send(['SCARD', `${base}failed`]),\n this._send(['ZCARD', `${base}delayed`]),\n ])\n\n return {\n waiting: this._toNumber(waiting),\n active: this._toNumber(active),\n succeeded: this._toNumber(succeeded),\n failed: this._toNumber(failed),\n delayed: this._toNumber(delayed),\n }\n }\n\n /**\n * Collect metrics for a single queue and set gauges.\n *\n * Supports:\n * - Bull (default): keys like `bull:<queue>:wait`, `...:active`, `...:completed`, `...:failed`, `...:delayed`\n * - Bee-Queue: keys like `bq:<queue>:waiting`, `...:active`, `...:succeeded`, `...:failed`, `...:delayed`\n *\n * Auto-detects queue type unless `METRICS_QUEUE_TYPE` is set to `bull` or `bee`.\n *\n * @param {string} queueName - Name of the queue\n * @returns {Promise<void>}\n */\n collectSingleQueueMetrics = async queueName => {\n try {\n const queueType = await this._getQueueType(queueName)\n const health =\n queueType === 'bee'\n ? await this._getBeeQueueHealth(queueName)\n : await this._getBullHealth(queueName)\n\n const labels = {\n ...this.defaultLabelsWithoutDynoId,\n queue_name: queueName,\n }\n\n this.queueJobsGauge.set(\n { ...labels, status: 'waiting' },\n health.waiting || 0\n )\n this.queueJobsGauge.set(\n { ...labels, status: 'active' },\n health.active || 0\n )\n this.queueJobsGauge.set(\n { ...labels, status: 'succeeded' },\n health.succeeded || 0\n )\n this.queueJobsGauge.set(\n { ...labels, status: 'failed' },\n health.failed || 0\n )\n this.queueJobsGauge.set(\n { ...labels, status: 'delayed' },\n health.delayed || 0\n )\n } catch (error) {\n console.warn(\n `[queue-metrics] Failed to collect metrics for queue ${queueName}:`,\n error.message\n )\n }\n }\n\n /**\n * Collect metrics for all queues and Redis, then push to Pushgateway\n * @returns {Promise<void>}\n */\n collectQueueMetrics = async () => {\n try {\n const queueNames = this.getConfiguredQueueNames()\n\n await this.collectRedisMetrics()\n await Promise.allSettled(\n queueNames.map(queueName => this.collectSingleQueueMetrics(queueName))\n )\n\n await this.gatewayPush()\n this.clearAllCounters()\n\n if (this.metricsLogValues) {\n const metricObjects = await this.registry.getMetricsAsJSON()\n console.info(\n `[queue-metrics] Collected metrics for ${queueNames.length} queues:`,\n JSON.stringify(metricObjects, null, 2)\n )\n }\n } catch (error) {\n if (\n error.message?.includes('No queues configured') ||\n error.message?.includes('METRICS_APP_REDIS_BQ')\n ) {\n console.error(\n `[queue-metrics] ❌ Configuration error: ${error.message}`\n )\n console.error(\n `[queue-metrics] 💡 Example config: METRICS_APP_REDIS_BQ=\"adalo-compile,adalo-migrations\"`\n )\n } else {\n console.error(\n `[queue-metrics] Failed to collect queue metrics: ${error.message}`\n )\n }\n throw error\n }\n }\n\n /**\n * Start periodic collection.\n * @param {number} [intervalSec=this.intervalSec] - Interval in seconds\n */\n startPush = (intervalSec = this.intervalSec) => {\n this._startPush(intervalSec, () => {\n this.collectQueueMetrics().catch(err => {\n console.error(\n `[queue-metrics] Failed to collect queue & Redis metrics:`,\n err\n )\n })\n })\n }\n\n _setCleanupHandlers = () => {\n process.on('SIGINT', this.cleanup)\n process.on('SIGTERM', this.cleanup)\n }\n\n /**\n * Cleanup queues and exit process.\n * @returns {Promise<void>}\n */\n cleanup = async () => {\n for (const [queueName, queue] of this.queueCache) {\n try {\n await queue.close()\n } catch (err) {\n console.error(`[queue-metrics] Error closing queue ${queueName}:`, err)\n }\n }\n process.exit(0)\n }\n}\n\nmodule.exports = { QueueR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H;AAEAC,MAAM,CAACC,OAAO,GAAG;EAAE3G;AAAwB,CAAC","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
+
const cluster = require('cluster')
|
|
1
2
|
const { BaseMetricsClient } = require('./baseMetricsClient')
|
|
2
3
|
const { HttpMetricsRedisStore } = require('./httpMetricsRedisStore')
|
|
3
4
|
|
|
5
|
+
function clusterWorkerTag() {
|
|
6
|
+
try {
|
|
7
|
+
if (cluster.isWorker && cluster.worker) {
|
|
8
|
+
return ` cluster_worker_id=${cluster.worker.id}`
|
|
9
|
+
}
|
|
10
|
+
} catch (_) {
|
|
11
|
+
/* ignore */
|
|
12
|
+
}
|
|
13
|
+
return ''
|
|
14
|
+
}
|
|
15
|
+
|
|
4
16
|
/**
|
|
5
17
|
* Drain worker: reads HTTP aggregates from Redis (written by {@link HttpMetricsRedisRecorder}),
|
|
6
18
|
* applies them to `app_requests_*` counters, then pushes the registry to the VM-agent (same as {@link BaseMetricsClient}).
|
|
@@ -87,6 +99,13 @@ class HttpMetricsRedisCollector extends BaseMetricsClient {
|
|
|
87
99
|
* @returns {Promise<void>}
|
|
88
100
|
*/
|
|
89
101
|
pushMetrics = async () => {
|
|
102
|
+
const { pid } = process
|
|
103
|
+
const clusterTag = clusterWorkerTag()
|
|
104
|
+
// --- TEMP_HTTP_METRICS_DIAG: remove when done debugging ---
|
|
105
|
+
console.warn(
|
|
106
|
+
`[TEMP_HTTP_METRICS_DIAG] http_metrics_publish_start pid=${pid}${clusterTag} dyno_id=${this.dynoId} process_type=${this.processType} app=${this.appName}`
|
|
107
|
+
)
|
|
108
|
+
// --- end TEMP_HTTP_METRICS_DIAG ---
|
|
90
109
|
if (
|
|
91
110
|
this._store &&
|
|
92
111
|
this.countersFunctions?.app_requests_total &&
|
|
@@ -99,7 +118,13 @@ class HttpMetricsRedisCollector extends BaseMetricsClient {
|
|
|
99
118
|
this.countersFunctions.app_requests_total_duration(labels, value)
|
|
100
119
|
)
|
|
101
120
|
}
|
|
102
|
-
|
|
121
|
+
const out = await this._pushMetrics()
|
|
122
|
+
// --- TEMP_HTTP_METRICS_DIAG: remove when done debugging ---
|
|
123
|
+
console.warn(
|
|
124
|
+
`[TEMP_HTTP_METRICS_DIAG] http_metrics_publish_finished pid=${pid}${clusterTag} dyno_id=${this.dynoId} (registry POST to VM-agent / import completed for this tick)`
|
|
125
|
+
)
|
|
126
|
+
// --- end TEMP_HTTP_METRICS_DIAG ---
|
|
127
|
+
return out
|
|
103
128
|
}
|
|
104
129
|
}
|
|
105
130
|
|
|
@@ -1,17 +1,5 @@
|
|
|
1
|
-
const cluster = require('cluster')
|
|
2
1
|
const { HttpMetricsRedisStore } = require('./httpMetricsRedisStore')
|
|
3
2
|
|
|
4
|
-
function clusterWorkerTag() {
|
|
5
|
-
try {
|
|
6
|
-
if (cluster.isWorker && cluster.worker) {
|
|
7
|
-
return ` cluster_worker_id=${cluster.worker.id}`
|
|
8
|
-
}
|
|
9
|
-
} catch (_) {
|
|
10
|
-
/* ignore */
|
|
11
|
-
}
|
|
12
|
-
return ''
|
|
13
|
-
}
|
|
14
|
-
|
|
15
3
|
/**
|
|
16
4
|
* Records HTTP request aggregates only to Redis (no in-process Prometheus counters on this path).
|
|
17
5
|
* Pair with {@link HttpMetricsRedisCollector} on a drain process to flush into counters and push to the VM-agent.
|
|
@@ -38,12 +26,6 @@ class HttpMetricsRedisRecorder {
|
|
|
38
26
|
processType,
|
|
39
27
|
ttlSec,
|
|
40
28
|
})
|
|
41
|
-
|
|
42
|
-
// --- TEMP_HTTP_METRICS_DIAG: remove this block when cluster / multi-web coverage is verified ---
|
|
43
|
-
this._diagWindowStart = 0
|
|
44
|
-
this._diagWindowCount = 0
|
|
45
|
-
this._diagIntervalMs = 5000
|
|
46
|
-
// --- end TEMP_HTTP_METRICS_DIAG ---
|
|
47
29
|
}
|
|
48
30
|
|
|
49
31
|
/**
|
|
@@ -63,31 +45,7 @@ class HttpMetricsRedisRecorder {
|
|
|
63
45
|
databaseId = '',
|
|
64
46
|
duration,
|
|
65
47
|
}) {
|
|
66
|
-
|
|
67
|
-
this._diagWindowCount += 1
|
|
68
|
-
const now = Date.now()
|
|
69
|
-
if (!this._diagWindowStart) {
|
|
70
|
-
this._diagWindowStart = now
|
|
71
|
-
}
|
|
72
|
-
if (now - this._diagWindowStart >= this._diagIntervalMs) {
|
|
73
|
-
const pidTag = `pid=${process.pid}`
|
|
74
|
-
const clusterTag = clusterWorkerTag()
|
|
75
|
-
console.warn(
|
|
76
|
-
`[TEMP_HTTP_METRICS_DIAG] http_record_source ${pidTag}${clusterTag} process_type=${this.processType} app=${this.appName} batch_count=${this._diagWindowCount} (before Redis write)`
|
|
77
|
-
)
|
|
78
|
-
this._diagWindowStart = now
|
|
79
|
-
this._diagWindowCount = 0
|
|
80
|
-
}
|
|
81
|
-
// --- end TEMP_HTTP_METRICS_DIAG ---
|
|
82
|
-
|
|
83
|
-
this._store.record(
|
|
84
|
-
method,
|
|
85
|
-
route,
|
|
86
|
-
status_code,
|
|
87
|
-
appId,
|
|
88
|
-
databaseId,
|
|
89
|
-
duration
|
|
90
|
-
)
|
|
48
|
+
this._store.record(method, route, status_code, appId, databaseId, duration)
|
|
91
49
|
}
|
|
92
50
|
|
|
93
51
|
/**
|
|
@@ -54,6 +54,11 @@ function hgetallPairsToObject(pairs) {
|
|
|
54
54
|
return o
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/** --- TEMP_HTTP_METRICS_DIAG: delete this helper and all calls when done debugging --- */
|
|
58
|
+
function tempHttpDiagLog(message) {
|
|
59
|
+
console.warn(`[TEMP_HTTP_METRICS_DIAG] ${message}`)
|
|
60
|
+
}
|
|
61
|
+
|
|
57
62
|
/**
|
|
58
63
|
* Redis HTTP aggregate store. Uses an **injected** client (same pattern as {@link RedisMetricsClient}).
|
|
59
64
|
* Expects `redis` v3-style API: `multi().hincrby().expire().exec(cb)`, `eval`, `set`, `del`.
|
|
@@ -75,7 +80,9 @@ class HttpMetricsRedisStore {
|
|
|
75
80
|
typeof ttlSec === 'number' && ttlSec > 0
|
|
76
81
|
? ttlSec
|
|
77
82
|
: DEFAULT_HTTP_METRICS_REDIS_TTL_SEC
|
|
78
|
-
const keySeg = `${encodeURIComponent(appName)}:${encodeURIComponent(
|
|
83
|
+
const keySeg = `${encodeURIComponent(appName)}:${encodeURIComponent(
|
|
84
|
+
processType
|
|
85
|
+
)}`
|
|
79
86
|
this.countKey = `metrics:http:v2:${keySeg}:count`
|
|
80
87
|
this.durKey = `metrics:http:v2:${keySeg}:dur`
|
|
81
88
|
this.lockKey = `metrics:http:v2:${keySeg}:drain_lock`
|
|
@@ -111,7 +118,11 @@ class HttpMetricsRedisStore {
|
|
|
111
118
|
.exec(err => {
|
|
112
119
|
if (err) {
|
|
113
120
|
console.error('[HttpMetricsRedisStore] record failed:', err.message)
|
|
121
|
+
return
|
|
114
122
|
}
|
|
123
|
+
tempHttpDiagLog(
|
|
124
|
+
`http_redis_write_sent pid=${process.pid} countKey=${this.countKey} durKey=${this.durKey} sample=${method} ${route} ${statusCode}`
|
|
125
|
+
)
|
|
115
126
|
})
|
|
116
127
|
} catch (e) {
|
|
117
128
|
console.error('[HttpMetricsRedisStore] record:', e.message)
|
|
@@ -134,9 +145,15 @@ class HttpMetricsRedisStore {
|
|
|
134
145
|
return new Promise(resolve => {
|
|
135
146
|
client.set(this.lockKey, '1', 'EX', 25, 'NX', (setErr, ok) => {
|
|
136
147
|
if (setErr || ok !== 'OK') {
|
|
148
|
+
tempHttpDiagLog(
|
|
149
|
+
`http_redis_drain_skipped pid=${process.pid} lockKey=${this.lockKey} reason=${setErr ? setErr.message : 'lock_not_acquired'}`
|
|
150
|
+
)
|
|
137
151
|
resolve(false)
|
|
138
152
|
return
|
|
139
153
|
}
|
|
154
|
+
tempHttpDiagLog(
|
|
155
|
+
`http_redis_drain_request pid=${process.pid} countKey=${this.countKey} durKey=${this.durKey} (EVAL HGETALL+DEL)`
|
|
156
|
+
)
|
|
140
157
|
client.eval(
|
|
141
158
|
DRAIN_LUA,
|
|
142
159
|
2,
|
|
@@ -148,19 +165,30 @@ class HttpMetricsRedisStore {
|
|
|
148
165
|
}
|
|
149
166
|
|
|
150
167
|
if (evalErr) {
|
|
151
|
-
console.error(
|
|
168
|
+
console.error(
|
|
169
|
+
'[HttpMetricsRedisStore] drain failed:',
|
|
170
|
+
evalErr.message
|
|
171
|
+
)
|
|
172
|
+
tempHttpDiagLog(
|
|
173
|
+
`http_redis_drain_got_error pid=${process.pid} message=${evalErr.message}`
|
|
174
|
+
)
|
|
152
175
|
finish()
|
|
153
176
|
return
|
|
154
177
|
}
|
|
155
178
|
|
|
156
179
|
try {
|
|
157
180
|
if (!raw || !Array.isArray(raw) || raw.length < 2) {
|
|
181
|
+
tempHttpDiagLog(
|
|
182
|
+
`http_redis_drain_got pid=${process.pid} distinct_fields=0 total_units=0 (empty raw)`
|
|
183
|
+
)
|
|
158
184
|
finish()
|
|
159
185
|
return
|
|
160
186
|
}
|
|
161
187
|
const counts = hgetallPairsToObject(raw[0])
|
|
162
188
|
const durs = hgetallPairsToObject(raw[1])
|
|
163
|
-
|
|
189
|
+
const fieldKeys = Object.keys(counts)
|
|
190
|
+
let totalUnits = 0
|
|
191
|
+
for (const field of fieldKeys) {
|
|
164
192
|
const count = parseInt(counts[field], 10)
|
|
165
193
|
if (!count || count < 1) {
|
|
166
194
|
continue
|
|
@@ -170,6 +198,7 @@ class HttpMetricsRedisStore {
|
|
|
170
198
|
if (parts.length !== 5) {
|
|
171
199
|
continue
|
|
172
200
|
}
|
|
201
|
+
totalUnits += count
|
|
173
202
|
const [m, route, statusStr, aid, did] = parts
|
|
174
203
|
const labels = {
|
|
175
204
|
method: m,
|
|
@@ -183,8 +212,18 @@ class HttpMetricsRedisStore {
|
|
|
183
212
|
applyDuration(labels, dur)
|
|
184
213
|
}
|
|
185
214
|
}
|
|
215
|
+
const sampleFields = fieldKeys.slice(0, 3).join(' | ')
|
|
216
|
+
tempHttpDiagLog(
|
|
217
|
+
`http_redis_drain_got pid=${process.pid} distinct_fields=${fieldKeys.length} total_request_units=${totalUnits} sample_fields=${sampleFields || '(none)'}`
|
|
218
|
+
)
|
|
186
219
|
} catch (e) {
|
|
187
|
-
console.error(
|
|
220
|
+
console.error(
|
|
221
|
+
'[HttpMetricsRedisStore] flush apply failed:',
|
|
222
|
+
e.message
|
|
223
|
+
)
|
|
224
|
+
tempHttpDiagLog(
|
|
225
|
+
`http_redis_drain_apply_failed pid=${process.pid} ${e.message}`
|
|
226
|
+
)
|
|
188
227
|
}
|
|
189
228
|
finish()
|
|
190
229
|
}
|
|
@@ -32,7 +32,11 @@ class DatabaseMetricsClient extends BaseMetricsClient {
|
|
|
32
32
|
additional_database_urls = {},
|
|
33
33
|
...metricsConfig
|
|
34
34
|
} = {}) {
|
|
35
|
-
exitUnlessMetricsProcessType(
|
|
35
|
+
exitUnlessMetricsProcessType(
|
|
36
|
+
metricsConfig,
|
|
37
|
+
'database-metrics',
|
|
38
|
+
'database-metrics'
|
|
39
|
+
)
|
|
36
40
|
|
|
37
41
|
const intervalSec =
|
|
38
42
|
metricsConfig.intervalSec ||
|
|
@@ -29,7 +29,11 @@ function resolveMetricsProcessType(metricsConfig, defaultProcessType) {
|
|
|
29
29
|
* @param {string | string[]} allowed - Single type or allowlist (e.g. `['redis-metrics','queue-metrics']`)
|
|
30
30
|
* @returns {void}
|
|
31
31
|
*/
|
|
32
|
-
function exitUnlessMetricsProcessType(
|
|
32
|
+
function exitUnlessMetricsProcessType(
|
|
33
|
+
metricsConfig,
|
|
34
|
+
defaultProcessType,
|
|
35
|
+
allowed
|
|
36
|
+
) {
|
|
33
37
|
const resolved = resolveMetricsProcessType(metricsConfig, defaultProcessType)
|
|
34
38
|
const list = Array.isArray(allowed) ? allowed : [allowed]
|
|
35
39
|
if (!list.includes(resolved)) {
|
|
@@ -25,7 +25,11 @@ class QueueRedisMetricsClient extends RedisMetricsClient {
|
|
|
25
25
|
* @param {boolean} [options.disablePushgateway] - Disable pushing to Pushgateway (use HTTP scraping instead)
|
|
26
26
|
*/
|
|
27
27
|
constructor({ redisClient, ...metricsConfig } = {}) {
|
|
28
|
-
exitUnlessMetricsProcessType(
|
|
28
|
+
exitUnlessMetricsProcessType(
|
|
29
|
+
metricsConfig,
|
|
30
|
+
'queue-metrics',
|
|
31
|
+
'queue-metrics'
|
|
32
|
+
)
|
|
29
33
|
|
|
30
34
|
const getConfiguredQueueNames = () => {
|
|
31
35
|
if (!process.env.METRICS_APP_REDIS_BQ) {
|