@adalo/metrics 0.1.148 → 0.1.150
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/README-health.md +1 -0
- package/lib/health/databaseChecker.d.ts +6 -2
- package/lib/health/databaseChecker.d.ts.map +1 -1
- package/lib/health/databaseChecker.js +78 -4
- package/lib/health/databaseChecker.js.map +1 -1
- package/lib/health/healthCheckClient.d.ts +6 -8
- package/lib/health/healthCheckClient.d.ts.map +1 -1
- package/lib/health/healthCheckClient.js +28 -5
- package/lib/health/healthCheckClient.js.map +1 -1
- package/lib/metrics/metricsRedisClient.d.ts +2 -0
- package/lib/metrics/metricsRedisClient.d.ts.map +1 -1
- package/lib/metrics/metricsRedisClient.js +48 -3
- package/lib/metrics/metricsRedisClient.js.map +1 -1
- package/package.json +1 -1
- package/src/health/databaseChecker.js +75 -4
- package/src/health/healthCheckClient.js +33 -3
- package/src/metrics/metricsRedisClient.js +49 -4
package/README-health.md
CHANGED
|
@@ -26,6 +26,7 @@ preventing HTTP requests from triggering database queries.
|
|
|
26
26
|
| `HOSTNAME` | Dyno/instance ID for logging | `unknown-dyno` |
|
|
27
27
|
| `BUILD_DYNO_PROCESS_TYPE` | Process type for logging | `health-check-worker` |
|
|
28
28
|
| `HEALTH_LOG_VALUES` | Enable logging of health check refresh status (`true`/`false`) | `false` |
|
|
29
|
+
| `HEALTH_DB_MAX_CONNECT_LATENCY_MS` | Fail DB health if connection acquisition/connect time exceeds this (ms) | `1000` |
|
|
29
30
|
| `REDIS_URL` | Redis connection URL for cache | - |
|
|
30
31
|
| `DATABASE_URL` | Main PostgreSQL connection URL (for backend worker) | - |
|
|
31
32
|
| `META_DB_URL` | Main PostgreSQL connection URL (for database worker) | - |
|
|
@@ -28,9 +28,13 @@ export function createDatabasePool(env: string, url: string, connectionTimeoutMs
|
|
|
28
28
|
/**
|
|
29
29
|
* @param {any} pool
|
|
30
30
|
* @param {string} type
|
|
31
|
-
* @returns {Promise<
|
|
31
|
+
* @returns {Promise<{ connectMs: number, queryMs: number, totalMs: number }>}
|
|
32
32
|
*/
|
|
33
|
-
export function runHealthCheck(pool: any, type: string): Promise<
|
|
33
|
+
export function runHealthCheck(pool: any, type: string): Promise<{
|
|
34
|
+
connectMs: number;
|
|
35
|
+
queryMs: number;
|
|
36
|
+
totalMs: number;
|
|
37
|
+
}>;
|
|
34
38
|
/**
|
|
35
39
|
* @param {any} pool
|
|
36
40
|
* @returns {Promise<void>}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"databaseChecker.d.ts","sourceRoot":"","sources":["../../src/health/databaseChecker.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"databaseChecker.d.ts","sourceRoot":"","sources":["../../src/health/databaseChecker.js"],"names":[],"mappings":"AAaA;;;GAGG;AACH,qCAHW,MAAM,GACJ,MAAM,CAYlB;AAED;;;;GAIG;AACH,wCAJW,MAAM,8BAEJ;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAiBnG;AAED;;;;;GAKG;AACH,wCALW,MAAM,OACN,MAAM,uBACN,MAAM;UACI,GAAG;UAAQ,MAAM;EA8BrC;AAED;;;;GAIG;AACH,qCAJW,GAAG,QACH,MAAM;eACiB,MAAM;aAAW,MAAM;aAAW,MAAM;GAwEzE;AAED;;;GAGG;AACH,gCAHW,GAAG,GACD,QAAQ,IAAI,CAAC,CAQzB;AA3KD,0CAAmC;AACnC,oCAA6B"}
|
|
@@ -7,6 +7,13 @@ const mysql = require('mysql2/promise');
|
|
|
7
7
|
const DB_TYPE_POSTGRES = 'postgres';
|
|
8
8
|
const DB_TYPE_MYSQL = 'mysql';
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* @returns {number}
|
|
12
|
+
*/
|
|
13
|
+
function nowMs() {
|
|
14
|
+
return Number(process.hrtime.bigint() / 1_000_000n);
|
|
15
|
+
}
|
|
16
|
+
|
|
10
17
|
/**
|
|
11
18
|
* @param {string} url
|
|
12
19
|
* @returns {string} postgres | mysql
|
|
@@ -88,14 +95,81 @@ function createDatabasePool(env, url, connectionTimeoutMs) {
|
|
|
88
95
|
/**
|
|
89
96
|
* @param {any} pool
|
|
90
97
|
* @param {string} type
|
|
91
|
-
* @returns {Promise<
|
|
98
|
+
* @returns {Promise<{ connectMs: number, queryMs: number, totalMs: number }>}
|
|
92
99
|
*/
|
|
93
100
|
async function runHealthCheck(pool, type) {
|
|
94
101
|
if (type === DB_TYPE_MYSQL) {
|
|
95
|
-
|
|
96
|
-
|
|
102
|
+
const startTotal = nowMs();
|
|
103
|
+
let connectMs = 0;
|
|
104
|
+
let queryMs = 0;
|
|
105
|
+
|
|
106
|
+
/** @type {any} */
|
|
107
|
+
let conn;
|
|
108
|
+
try {
|
|
109
|
+
const startConnect = nowMs();
|
|
110
|
+
conn = await pool.getConnection();
|
|
111
|
+
connectMs = nowMs() - startConnect;
|
|
112
|
+
const startQuery = nowMs();
|
|
113
|
+
await conn.query('SELECT 1');
|
|
114
|
+
queryMs = nowMs() - startQuery;
|
|
115
|
+
return {
|
|
116
|
+
connectMs,
|
|
117
|
+
queryMs,
|
|
118
|
+
totalMs: nowMs() - startTotal
|
|
119
|
+
};
|
|
120
|
+
} catch (err) {
|
|
121
|
+
const totalMs = nowMs() - startTotal;
|
|
122
|
+
err.healthCheckTimings = {
|
|
123
|
+
connectMs,
|
|
124
|
+
queryMs,
|
|
125
|
+
totalMs
|
|
126
|
+
};
|
|
127
|
+
throw err;
|
|
128
|
+
} finally {
|
|
129
|
+
if (conn) {
|
|
130
|
+
try {
|
|
131
|
+
conn.release();
|
|
132
|
+
} catch {
|
|
133
|
+
// ignore
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const startTotal = nowMs();
|
|
139
|
+
let connectMs = 0;
|
|
140
|
+
let queryMs = 0;
|
|
141
|
+
|
|
142
|
+
/** @type {import('pg').PoolClient | null} */
|
|
143
|
+
let client = null;
|
|
144
|
+
try {
|
|
145
|
+
const startConnect = nowMs();
|
|
146
|
+
client = await pool.connect();
|
|
147
|
+
connectMs = nowMs() - startConnect;
|
|
148
|
+
const startQuery = nowMs();
|
|
149
|
+
await client.query('SELECT 1');
|
|
150
|
+
queryMs = nowMs() - startQuery;
|
|
151
|
+
return {
|
|
152
|
+
connectMs,
|
|
153
|
+
queryMs,
|
|
154
|
+
totalMs: nowMs() - startTotal
|
|
155
|
+
};
|
|
156
|
+
} catch (err) {
|
|
157
|
+
const totalMs = nowMs() - startTotal;
|
|
158
|
+
err.healthCheckTimings = {
|
|
159
|
+
connectMs,
|
|
160
|
+
queryMs,
|
|
161
|
+
totalMs
|
|
162
|
+
};
|
|
163
|
+
throw err;
|
|
164
|
+
} finally {
|
|
165
|
+
if (client) {
|
|
166
|
+
try {
|
|
167
|
+
client.release();
|
|
168
|
+
} catch {
|
|
169
|
+
// ignore
|
|
170
|
+
}
|
|
171
|
+
}
|
|
97
172
|
}
|
|
98
|
-
await pool.query('SELECT 1');
|
|
99
173
|
}
|
|
100
174
|
|
|
101
175
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"databaseChecker.js","names":["Pool","require","mysql","DB_TYPE_POSTGRES","DB_TYPE_MYSQL","getDatabaseType","url","lower","toLowerCase","startsWith","parseConnectionUrl","type","parsed","URL","defaultPort","defaultDb","host","hostname","port","parseInt","String","user","username","password","database","pathname","replace","createDatabasePool","env","connectionTimeoutMs","config","Error","pool","createPool","connectionLimit","connectTimeout","waitForConnections","connectionString","max","idleTimeoutMillis","connectionTimeoutMillis","runHealthCheck","
|
|
1
|
+
{"version":3,"file":"databaseChecker.js","names":["Pool","require","mysql","DB_TYPE_POSTGRES","DB_TYPE_MYSQL","nowMs","Number","process","hrtime","bigint","getDatabaseType","url","lower","toLowerCase","startsWith","parseConnectionUrl","type","parsed","URL","defaultPort","defaultDb","host","hostname","port","parseInt","String","user","username","password","database","pathname","replace","createDatabasePool","env","connectionTimeoutMs","config","Error","pool","createPool","connectionLimit","connectTimeout","waitForConnections","connectionString","max","idleTimeoutMillis","connectionTimeoutMillis","runHealthCheck","startTotal","connectMs","queryMs","conn","startConnect","getConnection","startQuery","query","totalMs","err","healthCheckTimings","release","client","connect","closePool","end","module","exports"],"sources":["../../src/health/databaseChecker.js"],"sourcesContent":["const { Pool } = require('pg')\nconst mysql = require('mysql2/promise')\n\nconst DB_TYPE_POSTGRES = 'postgres'\nconst DB_TYPE_MYSQL = 'mysql'\n\n/**\n * @returns {number}\n */\nfunction nowMs() {\n return Number(process.hrtime.bigint() / 1_000_000n)\n}\n\n/**\n * @param {string} url\n * @returns {string} postgres | mysql\n */\nfunction getDatabaseType(url) {\n if (!url || typeof url !== 'string') return DB_TYPE_POSTGRES\n const lower = url.toLowerCase()\n if (lower.startsWith('mysql://') || lower.startsWith('mysql2://')) {\n return DB_TYPE_MYSQL\n }\n if (lower.startsWith('mariadb://')) {\n return DB_TYPE_MYSQL\n }\n return DB_TYPE_POSTGRES\n}\n\n/**\n * @param {string} url\n * @param {string} [type]\n * @returns {{ host: string, port: number, user: string, password: string, database: string } | null}\n */\nfunction parseConnectionUrl(url, type) {\n try {\n const parsed = new URL(url)\n const defaultPort = type === DB_TYPE_MYSQL ? 3306 : 5432\n const defaultDb = type === DB_TYPE_MYSQL ? 'mysql' : 'postgres'\n return {\n host: parsed.hostname || 'localhost',\n port: parseInt(parsed.port || String(defaultPort), 10),\n user: parsed.username || '',\n password: parsed.password || '',\n database: (parsed.pathname || '/').replace(/^\\//, '') || defaultDb,\n }\n } catch {\n return null\n }\n}\n\n/**\n * @param {string} env\n * @param {string} url\n * @param {number} connectionTimeoutMs\n * @returns {{ pool: any, type: string }}\n */\nfunction createDatabasePool(env, url, connectionTimeoutMs) {\n const type = getDatabaseType(url)\n\n if (type === DB_TYPE_MYSQL) {\n const config = parseConnectionUrl(url, DB_TYPE_MYSQL)\n if (!config) {\n throw new Error(`Invalid MySQL URL for ${env}`)\n }\n const pool = mysql.createPool({\n host: config.host,\n port: config.port,\n user: config.user,\n password: config.password,\n database: config.database,\n connectionLimit: 1,\n connectTimeout: connectionTimeoutMs,\n waitForConnections: false,\n })\n return { pool, type: DB_TYPE_MYSQL }\n }\n\n const pool = new Pool({\n connectionString: url,\n max: 1,\n idleTimeoutMillis: 30000,\n connectionTimeoutMillis: connectionTimeoutMs,\n })\n return { pool, type: DB_TYPE_POSTGRES }\n}\n\n/**\n * @param {any} pool\n * @param {string} type\n * @returns {Promise<{ connectMs: number, queryMs: number, totalMs: number }>}\n */\nasync function runHealthCheck(pool, type) {\n if (type === DB_TYPE_MYSQL) {\n const startTotal = nowMs()\n let connectMs = 0\n let queryMs = 0\n\n /** @type {any} */\n let conn\n try {\n const startConnect = nowMs()\n conn = await pool.getConnection()\n connectMs = nowMs() - startConnect\n\n const startQuery = nowMs()\n await conn.query('SELECT 1')\n queryMs = nowMs() - startQuery\n\n return { connectMs, queryMs, totalMs: nowMs() - startTotal }\n } catch (err) {\n const totalMs = nowMs() - startTotal\n err.healthCheckTimings = {\n connectMs,\n queryMs,\n totalMs,\n }\n throw err\n } finally {\n if (conn) {\n try {\n conn.release()\n } catch {\n // ignore\n }\n }\n }\n }\n\n const startTotal = nowMs()\n let connectMs = 0\n let queryMs = 0\n\n /** @type {import('pg').PoolClient | null} */\n let client = null\n try {\n const startConnect = nowMs()\n client = await pool.connect()\n connectMs = nowMs() - startConnect\n\n const startQuery = nowMs()\n await client.query('SELECT 1')\n queryMs = nowMs() - startQuery\n\n return { connectMs, queryMs, totalMs: nowMs() - startTotal }\n } catch (err) {\n const totalMs = nowMs() - startTotal\n err.healthCheckTimings = {\n connectMs,\n queryMs,\n totalMs,\n }\n throw err\n } finally {\n if (client) {\n try {\n client.release()\n } catch {\n // ignore\n }\n }\n }\n}\n\n/**\n * @param {any} pool\n * @returns {Promise<void>}\n */\nasync function closePool(pool) {\n try {\n await pool.end()\n } catch {\n // ignore\n }\n}\n\nmodule.exports = {\n getDatabaseType,\n parseConnectionUrl,\n createDatabasePool,\n runHealthCheck,\n closePool,\n DB_TYPE_POSTGRES,\n DB_TYPE_MYSQL,\n}\n"],"mappings":";;AAAA,MAAM;EAAEA;AAAK,CAAC,GAAGC,OAAO,CAAC,IAAI,CAAC;AAC9B,MAAMC,KAAK,GAAGD,OAAO,CAAC,gBAAgB,CAAC;AAEvC,MAAME,gBAAgB,GAAG,UAAU;AACnC,MAAMC,aAAa,GAAG,OAAO;;AAE7B;AACA;AACA;AACA,SAASC,KAAKA,CAAA,EAAG;EACf,OAAOC,MAAM,CAACC,OAAO,CAACC,MAAM,CAACC,MAAM,CAAC,CAAC,GAAG,UAAU,CAAC;AACrD;;AAEA;AACA;AACA;AACA;AACA,SAASC,eAAeA,CAACC,GAAG,EAAE;EAC5B,IAAI,CAACA,GAAG,IAAI,OAAOA,GAAG,KAAK,QAAQ,EAAE,OAAOR,gBAAgB;EAC5D,MAAMS,KAAK,GAAGD,GAAG,CAACE,WAAW,CAAC,CAAC;EAC/B,IAAID,KAAK,CAACE,UAAU,CAAC,UAAU,CAAC,IAAIF,KAAK,CAACE,UAAU,CAAC,WAAW,CAAC,EAAE;IACjE,OAAOV,aAAa;EACtB;EACA,IAAIQ,KAAK,CAACE,UAAU,CAAC,YAAY,CAAC,EAAE;IAClC,OAAOV,aAAa;EACtB;EACA,OAAOD,gBAAgB;AACzB;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASY,kBAAkBA,CAACJ,GAAG,EAAEK,IAAI,EAAE;EACrC,IAAI;IACF,MAAMC,MAAM,GAAG,IAAIC,GAAG,CAACP,GAAG,CAAC;IAC3B,MAAMQ,WAAW,GAAGH,IAAI,KAAKZ,aAAa,GAAG,IAAI,GAAG,IAAI;IACxD,MAAMgB,SAAS,GAAGJ,IAAI,KAAK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List":[]}
|
|
@@ -33,6 +33,7 @@ export class HealthCheckClient {
|
|
|
33
33
|
checkIntervalMs: number;
|
|
34
34
|
staleThresholdMs: number;
|
|
35
35
|
checkTimeoutMs: number;
|
|
36
|
+
maxDbConnectLatencyMs: number;
|
|
36
37
|
};
|
|
37
38
|
appName: string;
|
|
38
39
|
prefixLogs: string;
|
|
@@ -52,13 +53,7 @@ export class HealthCheckClient {
|
|
|
52
53
|
pool: any;
|
|
53
54
|
type: string;
|
|
54
55
|
} | undefined;
|
|
55
|
-
_checkDatabase(resource: any): Promise<
|
|
56
|
-
status: string;
|
|
57
|
-
error: string;
|
|
58
|
-
} | {
|
|
59
|
-
status: string;
|
|
60
|
-
error?: undefined;
|
|
61
|
-
}>;
|
|
56
|
+
_checkDatabase(resource: any): Promise<any>;
|
|
62
57
|
_checkRedis(resource: any): Promise<{
|
|
63
58
|
status: string;
|
|
64
59
|
error?: undefined;
|
|
@@ -82,6 +77,7 @@ export class HealthCheckClient {
|
|
|
82
77
|
checkIntervalMs: number;
|
|
83
78
|
staleThresholdMs: number;
|
|
84
79
|
checkTimeoutMs: number;
|
|
80
|
+
maxDbConnectLatencyMs: number;
|
|
85
81
|
};
|
|
86
82
|
}>;
|
|
87
83
|
_formatResult(result: any, cached?: boolean): any;
|
|
@@ -103,17 +99,19 @@ export class HealthCheckClient {
|
|
|
103
99
|
checkIntervalMs: number;
|
|
104
100
|
staleThresholdMs: number;
|
|
105
101
|
checkTimeoutMs: number;
|
|
102
|
+
maxDbConnectLatencyMs: number;
|
|
106
103
|
};
|
|
107
104
|
}>;
|
|
108
105
|
clearCache(): void;
|
|
109
106
|
healthHandler(): (req: any, res: any) => Promise<void>;
|
|
110
107
|
cleanup(): Promise<void>;
|
|
111
108
|
}
|
|
112
|
-
/** @type {{ checkIntervalMs: number, staleThresholdMs: number, checkTimeoutMs: number }} */
|
|
109
|
+
/** @type {{ checkIntervalMs: number, staleThresholdMs: number, checkTimeoutMs: number, maxDbConnectLatencyMs: number }} */
|
|
113
110
|
export const DEFAULT_HEALTH_CONFIG: {
|
|
114
111
|
checkIntervalMs: number;
|
|
115
112
|
staleThresholdMs: number;
|
|
116
113
|
checkTimeoutMs: number;
|
|
114
|
+
maxDbConnectLatencyMs: number;
|
|
117
115
|
};
|
|
118
116
|
import { HealthCheckCache } from "./healthCheckCache";
|
|
119
117
|
//# sourceMappingURL=healthCheckClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"healthCheckClient.d.ts","sourceRoot":"","sources":["../../src/health/healthCheckClient.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"healthCheckClient.d.ts","sourceRoot":"","sources":["../../src/health/healthCheckClient.js"],"names":[],"mappings":"6BA6Ea;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,GAAG,CAAA;CAAE;AAD1E;;GAEG;AACH;IACE;;;;;;OAMG;IACH;QALqC,SAAS,EAAnC,cAAc,EAAE;QACC,MAAM;QACN,OAAO;QACP,QAAQ;OA2BnC;IAxBC;;;;;;;;;;;;MAAmE;IACnE,gBACgE;IAEhE,mBAAmD;IAEnD,qBAA2B;IAC3B,uDAAuD;IACvD;cAD+B,GAAG;cAAQ,MAAM;OACjB;IAE/B,+BAA+B;IAC/B,YADW,cAAc,EAAE,CACc;IAIvC,qCAAuD;IAGzD,yBAKE;IAGJ,4BAEC;IAED,+BAGC;IAED;cA5BiC,GAAG;cAAQ,MAAM;kBAsCjD;IAED,4CAkCC;IAED;;;;;;OA4BC;IAED;;;;;;;;;;;;;;;;;;OA8BC;IAED,kDAeC;IAED,mCAgBC;IAED,gCAiBC;IAED;;;;;;;;;;;;;;;;;;OAIC;IAED,mBAEC;IAED,uDAiCC;IAED,yBASC;CACF;AA9TD,2HAA2H;AAC3H,oCADW;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,gBAAgB,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAC;IAAC,qBAAqB,EAAE,MAAM,CAAA;CAAE,CAOtH"}
|
|
@@ -15,11 +15,23 @@ const {
|
|
|
15
15
|
HealthCheckCache
|
|
16
16
|
} = require('./healthCheckCache');
|
|
17
17
|
|
|
18
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} name
|
|
20
|
+
* @returns {number | undefined}
|
|
21
|
+
*/
|
|
22
|
+
function readNumberEnv(name) {
|
|
23
|
+
const raw = process.env[name];
|
|
24
|
+
if (raw == null || raw === '') return undefined;
|
|
25
|
+
const num = Number(raw);
|
|
26
|
+
return Number.isFinite(num) ? num : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @type {{ checkIntervalMs: number, staleThresholdMs: number, checkTimeoutMs: number, maxDbConnectLatencyMs: number }} */
|
|
19
30
|
const DEFAULT_HEALTH_CONFIG = {
|
|
20
31
|
checkIntervalMs: 30_000,
|
|
21
32
|
staleThresholdMs: 180_000,
|
|
22
|
-
checkTimeoutMs: 15_000
|
|
33
|
+
checkTimeoutMs: 15_000,
|
|
34
|
+
maxDbConnectLatencyMs: readNumberEnv('HEALTH_DB_MAX_CONNECT_LATENCY_MS') ?? 1000
|
|
23
35
|
};
|
|
24
36
|
const SENSITIVE_PATTERNS = [{
|
|
25
37
|
pattern: /(postgres(?:ql)?|mysql|mongodb|redis|amqp):\/\/([^:]+):([^@]+)@([^:/]+)(:\d+)?\/([^\s?]+)/gi,
|
|
@@ -126,14 +138,25 @@ class HealthCheckClient {
|
|
|
126
138
|
pool,
|
|
127
139
|
type
|
|
128
140
|
} = this._getPool(env, url);
|
|
129
|
-
await runHealthCheck(pool, type);
|
|
141
|
+
const timings = await runHealthCheck(pool, type);
|
|
142
|
+
const maxConnect = this.healthConfig.maxDbConnectLatencyMs;
|
|
143
|
+
if (typeof maxConnect === 'number' && Number.isFinite(maxConnect) && timings?.connectMs != null && timings.connectMs > maxConnect) {
|
|
144
|
+
return {
|
|
145
|
+
status: 'error',
|
|
146
|
+
error: `DB connect latency ${timings.connectMs}ms exceeds ${maxConnect}ms`,
|
|
147
|
+
...timings
|
|
148
|
+
};
|
|
149
|
+
}
|
|
130
150
|
return {
|
|
131
|
-
status: 'ok'
|
|
151
|
+
status: 'ok',
|
|
152
|
+
...timings
|
|
132
153
|
};
|
|
133
154
|
} catch (err) {
|
|
155
|
+
const timings = err?.healthCheckTimings;
|
|
134
156
|
return {
|
|
135
157
|
status: 'error',
|
|
136
|
-
error: maskSensitiveData(err.message)
|
|
158
|
+
error: maskSensitiveData(err.message),
|
|
159
|
+
...(timings && typeof timings === 'object' ? timings : {})
|
|
137
160
|
};
|
|
138
161
|
}
|
|
139
162
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"healthCheckClient.js","names":["createDatabasePool","runHealthCheck","closePool","require","getRedisClientType","REDIS_V4","IOREDIS","REDIS_V3","HealthCheckCache","DEFAULT_HEALTH_CONFIG","checkIntervalMs","staleThresholdMs","checkTimeoutMs","SENSITIVE_PATTERNS","pattern","replacement","maskSensitiveData","text","masked","replace","HealthCheckClient","constructor","options","healthConfig","config","appName","process","env","BUILD_APP_NAME","prefixLogs","_refreshPromise","_databasePools","Map","_resources","resources","redisClient","_getRedisClientForCache","_redisClientType","_cache","cacheKey","_getEnv","resource","name","redisResource","find","r","client","_getPool","url","has","pool","type","set","get","_checkDatabase","status","error","err","message","_checkRedis","pong","Promise","resolve","reject","ping","result","_performHealthCheckInternal","sortedResources","Object","keys","sort","reduce","acc","key","hasError","values","some","lastCheckAt","Date","now","isStale","_formatResult","cached","performHealthCheck","then","catch","getCachedResult","console","refreshCache","clearCache","healthHandler","req","res","json","statusCode","cleanup","clear","module","exports"],"sources":["../../src/health/healthCheckClient.js"],"sourcesContent":["const {\n createDatabasePool,\n runHealthCheck,\n closePool,\n} = require('./databaseChecker')\nconst {\n getRedisClientType,\n REDIS_V4,\n IOREDIS,\n REDIS_V3,\n} = require('../redisUtils')\nconst { HealthCheckCache } = require('./healthCheckCache')\n\n/** @type {{ checkIntervalMs: number, staleThresholdMs: number, checkTimeoutMs: number }} */\nconst DEFAULT_HEALTH_CONFIG = {\n checkIntervalMs: 30_000,\n staleThresholdMs: 180_000,\n checkTimeoutMs: 15_000,\n}\n\nconst SENSITIVE_PATTERNS = [\n {\n pattern:\n /(postgres(?:ql)?|mysql|mongodb|redis|amqp):\\/\\/([^:]+):([^@]+)@([^:/]+)(:\\d+)?\\/([^\\s?]+)/gi,\n replacement: '$1://***:***@***$5/***',\n },\n {\n pattern: /(\\w+):\\/\\/([^:]+):([^@]+)@([^\\s/]+)/gi,\n replacement: '$1://***:***@***',\n },\n {\n pattern:\n /(password|passwd|pwd|secret|token|api[_-]?key|auth[_-]?token)[\"\\s]*[:=][\"\\s]*([^\\s,}\"]+)/gi,\n replacement: '$1=***',\n },\n {\n pattern:\n /(database|table|schema|role|user|relation|column|index)\\s*[\"']([^\"']+)[\"']/gi,\n replacement: '$1 \"***\"',\n },\n {\n pattern: /\\b(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:\\d+)?\\b/g,\n replacement: '***$2',\n },\n {\n pattern: /\\b(host|hostname|server)[\"\\s]*[:=][\"\\s]*([^\\s,}\"]+)/gi,\n replacement: '$1=***',\n },\n]\n\n/**\n * @param {string} text\n * @returns {string}\n */\nfunction maskSensitiveData(text) {\n if (!text || typeof text !== 'string') return text\n let masked = text\n for (const { pattern, replacement } of SENSITIVE_PATTERNS) {\n masked = masked.replace(pattern, replacement)\n }\n return masked\n}\n\n/**\n * @typedef {{ env: string, url?: string } | { env: string, client?: any }} HealthResource\n */\nclass HealthCheckClient {\n /**\n * @param {Object} options\n * @param {HealthResource[]} options.resources - Must include Redis resource with client\n * @param {Object} [options.config]\n * @param {string} [options.appName] - For cache key: healthcheck:${appName}\n * @param {string} [options.cacheKey] - Redis key (overrides appName)\n */\n constructor(options = {}) {\n this.healthConfig = { ...DEFAULT_HEALTH_CONFIG, ...options.config }\n this.appName =\n options.appName || process.env.BUILD_APP_NAME || 'unknown-app'\n\n this.prefixLogs = `[${this.appName}] [HealthCheck]`\n\n this._refreshPromise = null\n /** @type {Map<string, { pool: any, type: string }>} */\n this._databasePools = new Map()\n\n /** @type {HealthResource[]} */\n this._resources = options.resources || []\n\n const redisClient = this._getRedisClientForCache()\n if (redisClient) {\n this._redisClientType = getRedisClientType(redisClient)\n }\n\n this._cache = new HealthCheckCache({\n redisClient: redisClient || null,\n cacheKey: options.cacheKey,\n appName: this.appName,\n staleThresholdMs: this.healthConfig.staleThresholdMs,\n })\n }\n\n _getEnv(resource) {\n return resource.env ?? resource.name\n }\n\n _getRedisClientForCache() {\n const redisResource = this._resources.find(r => 'client' in r && r.client)\n return redisResource?.client || null\n }\n\n _getPool(env, url) {\n if (!this._databasePools.has(env)) {\n const { pool, type } = createDatabasePool(\n env,\n url,\n this.healthConfig.checkTimeoutMs\n )\n this._databasePools.set(env, { pool, type })\n }\n return this._databasePools.get(env)\n }\n\n async _checkDatabase(resource) {\n const env = this._getEnv(resource)\n const url = 'url' in resource ? resource.url : process.env[env]\n if (!url) {\n return { status: 'error', error: `Env ${env} not set` }\n }\n\n try {\n const { pool, type } = this._getPool(env, url)\n await runHealthCheck(pool, type)\n return { status: 'ok' }\n } catch (err) {\n return {\n status: 'error',\n error: maskSensitiveData(err.message),\n }\n }\n }\n\n async _checkRedis(resource) {\n const { client } = resource\n if (!client) return { status: 'ok' }\n\n try {\n let pong\n if (this._redisClientType === REDIS_V3) {\n pong = await new Promise((resolve, reject) => {\n client.ping((err, result) => {\n if (err) reject(err)\n else resolve(result)\n })\n })\n } else if (\n this._redisClientType === REDIS_V4 ||\n this._redisClientType === IOREDIS\n ) {\n pong = await client.ping()\n } else {\n return { status: 'error', error: 'Unknown Redis client type' }\n }\n\n return pong === 'PONG'\n ? { status: 'ok' }\n : { status: 'error', error: `Unexpected: ${pong}` }\n } catch (err) {\n return { status: 'error', error: maskSensitiveData(err.message) }\n }\n }\n\n async _performHealthCheckInternal() {\n const resources = {}\n\n for (const resource of this._resources) {\n const env = this._getEnv(resource)\n\n if ('client' in resource && resource.client) {\n resources[env] = await this._checkRedis(resource)\n } else {\n resources[env] = await this._checkDatabase(resource)\n }\n }\n\n const sortedResources = Object.keys(resources)\n .sort()\n .reduce((acc, key) => {\n acc[key] = resources[key]\n return acc\n }, {})\n\n const hasError = Object.values(resources).some(r => r.status === 'error')\n const lastCheckAt = Date.now()\n\n return {\n status: hasError ? 'error' : 'ok',\n lastCheckAt,\n resources: sortedResources,\n isStale: false,\n config: this.healthConfig,\n }\n }\n\n _formatResult(result, cached = false) {\n const isStale =\n !result.lastCheckAt ||\n Date.now() - result.lastCheckAt > this.healthConfig.staleThresholdMs\n\n return {\n ...result,\n isStale,\n status: isStale ? 'stale' : result.status,\n ...(isStale && {\n error:\n 'Health check data is stale, health-check worker may not be running. Resource statuses are unknown.',\n }),\n ...(cached && { cached: true }),\n }\n }\n\n async performHealthCheck() {\n if (this._refreshPromise) {\n return this._refreshPromise\n }\n\n this._refreshPromise = this._performHealthCheckInternal()\n .then(result => {\n this._refreshPromise = null\n return this._formatResult(result)\n })\n .catch(err => {\n this._refreshPromise = null\n throw err\n })\n\n return this._refreshPromise\n }\n\n async getCachedResult() {\n try {\n const cached = await this._cache.get()\n if (cached) return this._formatResult(cached)\n return null\n } catch (err) {\n console.error(`${this.prefixLogs} Failed to read from cache:`, err)\n return {\n status: 'error',\n lastCheckAt: null,\n resources: {},\n isStale: true,\n error:\n 'Redis unavailable, unable to read health status of other resources',\n config: this.healthConfig,\n }\n }\n }\n\n async refreshCache() {\n const result = await this._performHealthCheckInternal()\n await this._cache.set(result)\n return result\n }\n\n clearCache() {\n this._refreshPromise = null\n }\n\n healthHandler() {\n return async (req, res) => {\n try {\n const result = await this.getCachedResult()\n\n if (!result) {\n res.status(503).json({\n status: 'error',\n lastCheckAt: null,\n resources: {},\n isStale: true,\n error:\n 'No health check data yet, health-check worker may not be running',\n config: this.healthConfig,\n })\n return\n }\n\n const statusCode = result.status === 'ok' ? 200 : 503\n res.status(statusCode).json(result)\n } catch (err) {\n console.error(`${this.prefixLogs} Health check failed:`, err)\n res.status(503).json({\n status: 'error',\n lastCheckAt: null,\n resources: {},\n isStale: true,\n error:\n 'Redis unavailable, unable to read health status of other resources',\n config: this.healthConfig,\n })\n }\n }\n }\n\n async cleanup() {\n for (const [, { pool }] of this._databasePools) {\n try {\n await closePool(pool)\n } catch (err) {\n console.error(`${this.prefixLogs} Error closing database pool:`, err)\n }\n }\n this._databasePools.clear()\n }\n}\n\nmodule.exports = { HealthCheckClient, DEFAULT_HEALTH_CONFIG }\n"],"mappings":";;AAAA,MAAM;EACJA,kBAAkB;EAClBC,cAAc;EACdC;AACF,CAAC,GAAGC,OAAO,CAAC,mBAAmB,CAAC;AAChC,MAAM;EACJC,kBAAkB;EAClBC,QAAQ;EACRC,OAAO;EACPC;AACF,CAAC,GAAGJ,OAAO,CAAC,eAAe,CAAC;AAC5B,MAAM;EAAEK;AAAiB,CAAC,GAAGL,OAAO,CAAC,oBAAoB,CAAC;;AAE1D;AACA,MAAMM,qBAAqB,GAAG;EAC5BC,eAAe,EAAE,MAAM;EACvBC,gBAAgB,EAAE,OAAO;EACzBC,cAAc,EAAE;AAClB,CAAC;AAED,MAAMC,kBAAkB,GAAG,CACzB;EACEC,OAAO,EACL,6FAA6F;EAC/FC,WAAW,EAAE;AACf,CAAC,EACD;EACED,OAAO,EAAE,uCAAuC;EAChDC,WAAW,EAAE;AACf,CAAC,EACD;EACED,OAAO,EACL,4FAA4F;EAC9FC,WAAW,EAAE;AACf,CAAC,EACD;EACED,OAAO,EACL,8EAA8E;EAChFC,WAAW,EAAE;AACf,CAAC,EACD;EACED,OAAO,EAAE,kDAAkD;EAC3DC,WAAW,EAAE;AACf,CAAC,EACD;EACED,OAAO,EAAE,uDAAuD;EAChEC,WAAW,EAAE;AACf,CAAC,C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BtC,MAAM,EAAE,OAAO;UACfsB,WAAW,EAAE,IAAI;UACjB3C,SAAS,EAAE,CAAC,CAAC;UACb8C,OAAO,EAAE,IAAI;UACbxB,KAAK,EACH,oEAAoE;UACtEhC,MAAM,EAAE,IAAI,CAACD;QACf,CAAC,CAAC;MACJ;IACF,CAAC;EACH;EAEA,MAAMwE,OAAOA,CAAA,EAAG;IACd,KAAK,MAAM,GAAG;MAAE7C;IAAK,CAAC,CAAC,IAAI,IAAI,CAACnB,cAAc,EAAE;MAC9C,IAAI;QACF,MAAM7B,SAAS,CAACgD,IAAI,CAAC;MACvB,CAAC,CAAC,OAAOO,GAAG,EAAE;QACZ8B,OAAO,CAAC/B,KAAK,CAAC,GAAG,IAAI,CAAC3B,UAAU,+BAA+B,EAAE4B,GAAG,CAAC;MACvE;IACF;IACA,IAAI,CAAC1B,cAAc,CAACiE,KAAK,CAAC,CAAC;EAC7B;AACF;AAEAC,MAAM,CAACC,OAAO,GAAG;EAAE9E,iBAAiB;EAAEX;AAAsB,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"healthCheckClient.js","names":["createDatabasePool","runHealthCheck","closePool","require","getRedisClientType","REDIS_V4","IOREDIS","REDIS_V3","HealthCheckCache","readNumberEnv","name","raw","process","env","undefined","num","Number","isFinite","DEFAULT_HEALTH_CONFIG","checkIntervalMs","staleThresholdMs","checkTimeoutMs","maxDbConnectLatencyMs","SENSITIVE_PATTERNS","pattern","replacement","maskSensitiveData","text","masked","replace","HealthCheckClient","constructor","options","healthConfig","config","appName","BUILD_APP_NAME","prefixLogs","_refreshPromise","_databasePools","Map","_resources","resources","redisClient","_getRedisClientForCache","_redisClientType","_cache","cacheKey","_getEnv","resource","redisResource","find","r","client","_getPool","url","has","pool","type","set","get","_checkDatabase","status","error","timings","maxConnect","connectMs","err","healthCheckTimings","message","_checkRedis","pong","Promise","resolve","reject","ping","result","_performHealthCheckInternal","sortedResources","Object","keys","sort","reduce","acc","key","hasError","values","some","lastCheckAt","Date","now","isStale","_formatResult","cached","performHealthCheck","then","catch","getCachedResult","console","refreshCache","clearCache","healthHandler","req","res","json","statusCode","cleanup","clear","module","exports"],"sources":["../../src/health/healthCheckClient.js"],"sourcesContent":["const {\n createDatabasePool,\n runHealthCheck,\n closePool,\n} = require('./databaseChecker')\nconst {\n getRedisClientType,\n REDIS_V4,\n IOREDIS,\n REDIS_V3,\n} = require('../redisUtils')\nconst { HealthCheckCache } = require('./healthCheckCache')\n\n/**\n * @param {string} name\n * @returns {number | undefined}\n */\nfunction readNumberEnv(name) {\n const raw = process.env[name]\n if (raw == null || raw === '') return undefined\n const num = Number(raw)\n return Number.isFinite(num) ? num : undefined\n}\n\n/** @type {{ checkIntervalMs: number, staleThresholdMs: number, checkTimeoutMs: number, maxDbConnectLatencyMs: number }} */\nconst DEFAULT_HEALTH_CONFIG = {\n checkIntervalMs: 30_000,\n staleThresholdMs: 180_000,\n checkTimeoutMs: 15_000,\n maxDbConnectLatencyMs:\n readNumberEnv('HEALTH_DB_MAX_CONNECT_LATENCY_MS') ?? 1000,\n}\n\nconst SENSITIVE_PATTERNS = [\n {\n pattern:\n /(postgres(?:ql)?|mysql|mongodb|redis|amqp):\\/\\/([^:]+):([^@]+)@([^:/]+)(:\\d+)?\\/([^\\s?]+)/gi,\n replacement: '$1://***:***@***$5/***',\n },\n {\n pattern: /(\\w+):\\/\\/([^:]+):([^@]+)@([^\\s/]+)/gi,\n replacement: '$1://***:***@***',\n },\n {\n pattern:\n /(password|passwd|pwd|secret|token|api[_-]?key|auth[_-]?token)[\"\\s]*[:=][\"\\s]*([^\\s,}\"]+)/gi,\n replacement: '$1=***',\n },\n {\n pattern:\n /(database|table|schema|role|user|relation|column|index)\\s*[\"']([^\"']+)[\"']/gi,\n replacement: '$1 \"***\"',\n },\n {\n pattern: /\\b(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:\\d+)?\\b/g,\n replacement: '***$2',\n },\n {\n pattern: /\\b(host|hostname|server)[\"\\s]*[:=][\"\\s]*([^\\s,}\"]+)/gi,\n replacement: '$1=***',\n },\n]\n\n/**\n * @param {string} text\n * @returns {string}\n */\nfunction maskSensitiveData(text) {\n if (!text || typeof text !== 'string') return text\n let masked = text\n for (const { pattern, replacement } of SENSITIVE_PATTERNS) {\n masked = masked.replace(pattern, replacement)\n }\n return masked\n}\n\n/**\n * @typedef {{ env: string, url?: string } | { env: string, client?: any }} HealthResource\n */\nclass HealthCheckClient {\n /**\n * @param {Object} options\n * @param {HealthResource[]} options.resources - Must include Redis resource with client\n * @param {Object} [options.config]\n * @param {string} [options.appName] - For cache key: healthcheck:${appName}\n * @param {string} [options.cacheKey] - Redis key (overrides appName)\n */\n constructor(options = {}) {\n this.healthConfig = { ...DEFAULT_HEALTH_CONFIG, ...options.config }\n this.appName =\n options.appName || process.env.BUILD_APP_NAME || 'unknown-app'\n\n this.prefixLogs = `[${this.appName}] [HealthCheck]`\n\n this._refreshPromise = null\n /** @type {Map<string, { pool: any, type: string }>} */\n this._databasePools = new Map()\n\n /** @type {HealthResource[]} */\n this._resources = options.resources || []\n\n const redisClient = this._getRedisClientForCache()\n if (redisClient) {\n this._redisClientType = getRedisClientType(redisClient)\n }\n\n this._cache = new HealthCheckCache({\n redisClient: redisClient || null,\n cacheKey: options.cacheKey,\n appName: this.appName,\n staleThresholdMs: this.healthConfig.staleThresholdMs,\n })\n }\n\n _getEnv(resource) {\n return resource.env ?? resource.name\n }\n\n _getRedisClientForCache() {\n const redisResource = this._resources.find(r => 'client' in r && r.client)\n return redisResource?.client || null\n }\n\n _getPool(env, url) {\n if (!this._databasePools.has(env)) {\n const { pool, type } = createDatabasePool(\n env,\n url,\n this.healthConfig.checkTimeoutMs\n )\n this._databasePools.set(env, { pool, type })\n }\n return this._databasePools.get(env)\n }\n\n async _checkDatabase(resource) {\n const env = this._getEnv(resource)\n const url = 'url' in resource ? resource.url : process.env[env]\n if (!url) {\n return { status: 'error', error: `Env ${env} not set` }\n }\n\n try {\n const { pool, type } = this._getPool(env, url)\n const timings = await runHealthCheck(pool, type)\n\n const maxConnect = this.healthConfig.maxDbConnectLatencyMs\n if (\n typeof maxConnect === 'number' &&\n Number.isFinite(maxConnect) &&\n timings?.connectMs != null &&\n timings.connectMs > maxConnect\n ) {\n return {\n status: 'error',\n error: `DB connect latency ${timings.connectMs}ms exceeds ${maxConnect}ms`,\n ...timings,\n }\n }\n\n return { status: 'ok', ...timings }\n } catch (err) {\n const timings = err?.healthCheckTimings\n return {\n status: 'error',\n error: maskSensitiveData(err.message),\n ...(timings && typeof timings === 'object' ? timings : {}),\n }\n }\n }\n\n async _checkRedis(resource) {\n const { client } = resource\n if (!client) return { status: 'ok' }\n\n try {\n let pong\n if (this._redisClientType === REDIS_V3) {\n pong = await new Promise((resolve, reject) => {\n client.ping((err, result) => {\n if (err) reject(err)\n else resolve(result)\n })\n })\n } else if (\n this._redisClientType === REDIS_V4 ||\n this._redisClientType === IOREDIS\n ) {\n pong = await client.ping()\n } else {\n return { status: 'error', error: 'Unknown Redis client type' }\n }\n\n return pong === 'PONG'\n ? { status: 'ok' }\n : { status: 'error', error: `Unexpected: ${pong}` }\n } catch (err) {\n return { status: 'error', error: maskSensitiveData(err.message) }\n }\n }\n\n async _performHealthCheckInternal() {\n const resources = {}\n\n for (const resource of this._resources) {\n const env = this._getEnv(resource)\n\n if ('client' in resource && resource.client) {\n resources[env] = await this._checkRedis(resource)\n } else {\n resources[env] = await this._checkDatabase(resource)\n }\n }\n\n const sortedResources = Object.keys(resources)\n .sort()\n .reduce((acc, key) => {\n acc[key] = resources[key]\n return acc\n }, {})\n\n const hasError = Object.values(resources).some(r => r.status === 'error')\n const lastCheckAt = Date.now()\n\n return {\n status: hasError ? 'error' : 'ok',\n lastCheckAt,\n resources: sortedResources,\n isStale: false,\n config: this.healthConfig,\n }\n }\n\n _formatResult(result, cached = false) {\n const isStale =\n !result.lastCheckAt ||\n Date.now() - result.lastCheckAt > this.healthConfig.staleThresholdMs\n\n return {\n ...result,\n isStale,\n status: isStale ? 'stale' : result.status,\n ...(isStale && {\n error:\n 'Health check data is stale, health-check worker may not be running. Resource statuses are unknown.',\n }),\n ...(cached && { cached: true }),\n }\n }\n\n async performHealthCheck() {\n if (this._refreshPromise) {\n return this._refreshPromise\n }\n\n this._refreshPromise = this._performHealthCheckInternal()\n .then(result => {\n this._refreshPromise = null\n return this._formatResult(result)\n })\n .catch(err => {\n this._refreshPromise = null\n throw err\n })\n\n return this._refreshPromise\n }\n\n async getCachedResult() {\n try {\n const cached = await this._cache.get()\n if (cached) return this._formatResult(cached)\n return null\n } catch (err) {\n console.error(`${this.prefixLogs} Failed to read from cache:`, err)\n return {\n status: 'error',\n lastCheckAt: null,\n resources: {},\n isStale: true,\n error:\n 'Redis unavailable, unable to read health status of other resources',\n config: this.healthConfig,\n }\n }\n }\n\n async refreshCache() {\n const result = await this._performHealthCheckInternal()\n await this._cache.set(result)\n return result\n }\n\n clearCache() {\n this._refreshPromise = null\n }\n\n healthHandler() {\n return async (req, res) => {\n try {\n const result = await this.getCachedResult()\n\n if (!result) {\n res.status(503).json({\n status: 'error',\n lastCheckAt: null,\n resources: {},\n isStale: true,\n error:\n 'No health check data yet, health-check worker may not be running',\n config: this.healthConfig,\n })\n return\n }\n\n const statusCode = result.status === 'ok' ? 200 : 503\n res.status(statusCode).json(result)\n } catch (err) {\n console.error(`${this.prefixLogs} Health check failed:`, err)\n res.status(503).json({\n status: 'error',\n lastCheckAt: null,\n resources: {},\n isStale: true,\n error:\n 'Redis unavailable, unable to read health status of other resources',\n config: this.healthConfig,\n })\n }\n }\n }\n\n async cleanup() {\n for (const [, { pool }] of this._databasePools) {\n try {\n await closePool(pool)\n } catch (err) {\n console.error(`${this.prefixLogs} Error closing database pool:`, err)\n }\n }\n this._databasePools.clear()\n }\n}\n\nmodule.exports = { HealthCheckClient, DEFAULT_HEALTH_CONFIG }\n"],"mappings":";;AAAA,MAAM;EACJA,kBAAkB;EAClBC,cAAc;EACdC;AACF,CAAC,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B,KAAK,EACH,kEAAkE;YACpE7B,MAAM,EAAE,IAAI,CAACD;UACf,CAAC,CAAC;UACF;QACF;QAEA,MAAMwE,UAAU,GAAG7B,MAAM,CAACd,MAAM,KAAK,IAAI,GAAG,GAAG,GAAG,GAAG;QACrDyC,GAAG,CAACzC,MAAM,CAAC2C,UAAU,CAAC,CAACD,IAAI,CAAC5B,MAAM,CAAC;MACrC,CAAC,CAAC,OAAOT,GAAG,EAAE;QACZ+B,OAAO,CAACnC,KAAK,CAAC,GAAG,IAAI,CAAC1B,UAAU,uBAAuB,EAAE8B,GAAG,CAAC;QAC7DoC,GAAG,CAACzC,MAAM,CAAC,GAAG,CAAC,CAAC0C,IAAI,CAAC;UACnB1C,MAAM,EAAE,OAAO;UACf0B,WAAW,EAAE,IAAI;UACjB9C,SAAS,EAAE,CAAC,CAAC;UACbiD,OAAO,EAAE,IAAI;UACb5B,KAAK,EACH,oEAAoE;UACtE7B,MAAM,EAAE,IAAI,CAACD;QACf,CAAC,CAAC;MACJ;IACF,CAAC;EACH;EAEA,MAAMyE,OAAOA,CAAA,EAAG;IACd,KAAK,MAAM,GAAG;MAAEjD;IAAK,CAAC,CAAC,IAAI,IAAI,CAAClB,cAAc,EAAE;MAC9C,IAAI;QACF,MAAMrC,SAAS,CAACuD,IAAI,CAAC;MACvB,CAAC,CAAC,OAAOU,GAAG,EAAE;QACZ+B,OAAO,CAACnC,KAAK,CAAC,GAAG,IAAI,CAAC1B,UAAU,+BAA+B,EAAE8B,GAAG,CAAC;MACvE;IACF;IACA,IAAI,CAAC5B,cAAc,CAACoE,KAAK,CAAC,CAAC;EAC7B;AACF;AAEAC,MAAM,CAACC,OAAO,GAAG;EAAE/E,iBAAiB;EAAEZ;AAAsB,CAAC","ignoreList":[]}
|
|
@@ -44,6 +44,8 @@ export class RedisMetricsClient extends BaseMetricsClient {
|
|
|
44
44
|
redisMemoryGauge: import("prom-client").Gauge<string>;
|
|
45
45
|
/** Gauge for Redis operation stats */
|
|
46
46
|
redisStatsGauge: import("prom-client").Gauge<string>;
|
|
47
|
+
_redisConnSeenKeys: Set<any>;
|
|
48
|
+
_redisConnZeroedKeys: Set<any>;
|
|
47
49
|
getRedisConnections: () => Promise<any>;
|
|
48
50
|
getRedisInfo: (section: any) => Promise<any>;
|
|
49
51
|
parseRedisConnections: (clientsStr: any) => any;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsRedisClient.d.ts","sourceRoot":"","sources":["../../src/metrics/metricsRedisClient.js"],"names":[],"mappings":"AAWA;;;;;GAKG;AACH;IACE;;;;;;;;;;;;;;OAcG;IACH;QAbwB,WAAW,EAAxB,GAAG;QACc,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":"metricsRedisClient.d.ts","sourceRoot":"","sources":["../../src/metrics/metricsRedisClient.js"],"names":[],"mappings":"AAWA;;;;;GAKG;AACH;IACE;;;;;;;;;;;;;;OAcG;IACH;QAbwB,WAAW,EAAxB,GAAG;QACc,OAAO;QACP,MAAM;QACN,WAAW;QACV,OAAO;QACP,SAAS;QACV,cAAc;QACd,iBAAiB;QACjB,WAAW;QACV,gBAAgB;QACf,iBAAiB;QAClB,kBAAkB;OAuD9C;IAzCC,oCAAoC;IACpC,iBAA8B;IAC9B,wBAAsD;IAEtD,2CAA2C;IAC3C,2DAIE;IAEF,iEAIE;IAEF,mCAAmC;IACnC,sDAIE;IAEF,sCAAsC;IACtC,qDAIE;IAQF,6BAAmC;IACnC,+BAAqC;IAKvC,wCAgCC;IAED,6CAuBC;IAED,gDAkBC;IAED;;;OAGG;IACH,2BAFa,QAAQ,IAAI,CAAC,CAkKzB;IAED;;;OAGG;IACH,wBAFa,QAAQ,IAAI,CAAC,CAqBzB;IAED;;;OAGG;IACH,sDAMC;CA4BF"}
|
|
@@ -74,6 +74,15 @@ class RedisMetricsClient extends BaseMetricsClient {
|
|
|
74
74
|
help: 'Redis operation statistics',
|
|
75
75
|
labelNames: this.withDefaultLabels(['operation'])
|
|
76
76
|
});
|
|
77
|
+
|
|
78
|
+
// Track emitted connection label combinations so we can:
|
|
79
|
+
// - emit a one-shot 0 for series that disappear (overwrite last non-zero sample)
|
|
80
|
+
// - then stop emitting them on subsequent scrapes
|
|
81
|
+
//
|
|
82
|
+
// This is mainly needed because we label connections by `cmd`,
|
|
83
|
+
// and Redis `CLIENT LIST` `cmd` is volatile.
|
|
84
|
+
this._redisConnSeenKeys = new Set();
|
|
85
|
+
this._redisConnZeroedKeys = new Set();
|
|
77
86
|
this._setCleanupHandlers();
|
|
78
87
|
}
|
|
79
88
|
getRedisConnections = async () => {
|
|
@@ -155,8 +164,6 @@ class RedisMetricsClient extends BaseMetricsClient {
|
|
|
155
164
|
const [memoryInfoStr, statsInfoStr, connectionsInfoStr] = await Promise.all([this.getRedisInfo('memory'), this.getRedisInfo('stats'), this.getRedisConnections()]);
|
|
156
165
|
const labels = this.getDefaultLabels();
|
|
157
166
|
const connections = this.parseRedisConnections(connectionsInfoStr);
|
|
158
|
-
this.redisConnectionsGauge.reset();
|
|
159
|
-
this.redisConnectionsMemoryGauge.reset();
|
|
160
167
|
if (this.logValues) {
|
|
161
168
|
// "IN" data: raw client list from Redis
|
|
162
169
|
console.log('[queue-metrics] Redis CLIENT LIST (raw):\n', connectionsInfoStr);
|
|
@@ -206,13 +213,51 @@ class RedisMetricsClient extends BaseMetricsClient {
|
|
|
206
213
|
};
|
|
207
214
|
}
|
|
208
215
|
grouped[key].count += 1;
|
|
209
|
-
|
|
216
|
+
const mem = parseInt(totMem, 10);
|
|
217
|
+
grouped[key].memory += Number.isFinite(mem) ? mem : 0;
|
|
210
218
|
});
|
|
219
|
+
|
|
220
|
+
// Ensure sets exist even if older instance is hot-reloaded.
|
|
221
|
+
if (!this._redisConnSeenKeys) this._redisConnSeenKeys = new Set();
|
|
222
|
+
if (!this._redisConnZeroedKeys) this._redisConnZeroedKeys = new Set();
|
|
223
|
+
const currentKeys = new Set(Object.keys(grouped));
|
|
224
|
+
for (const k of currentKeys) {
|
|
225
|
+
this._redisConnSeenKeys.add(k);
|
|
226
|
+
if (this._redisConnZeroedKeys.has(k)) this._redisConnZeroedKeys.delete(k);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Reset gauges each scrape so we only export:
|
|
230
|
+
// - current snapshot series
|
|
231
|
+
// - one-shot zeros for recently disappeared series
|
|
232
|
+
this.redisConnectionsGauge.reset();
|
|
233
|
+
this.redisConnectionsMemoryGauge.reset();
|
|
234
|
+
|
|
235
|
+
// Emit one-shot zeros for series that disappeared since last seen.
|
|
236
|
+
for (const k of this._redisConnSeenKeys) {
|
|
237
|
+
if (currentKeys.has(k)) continue;
|
|
238
|
+
if (this._redisConnZeroedKeys.has(k)) continue;
|
|
239
|
+
try {
|
|
240
|
+
const valueLabels = JSON.parse(k);
|
|
241
|
+
this.redisConnectionsGauge.set({
|
|
242
|
+
...labels,
|
|
243
|
+
...valueLabels
|
|
244
|
+
}, 0);
|
|
245
|
+
this.redisConnectionsMemoryGauge.set({
|
|
246
|
+
...labels,
|
|
247
|
+
...valueLabels
|
|
248
|
+
}, 0);
|
|
249
|
+
this._redisConnZeroedKeys.add(k);
|
|
250
|
+
} catch {
|
|
251
|
+
// ignore malformed key
|
|
252
|
+
}
|
|
253
|
+
}
|
|
211
254
|
if (this.logValues) {
|
|
212
255
|
const groups = Object.values(grouped);
|
|
213
256
|
const groupedTotal = groups.reduce((sum, g) => sum + (g.count || 0), 0);
|
|
214
257
|
console.log('[queue-metrics] Redis connections grouped buckets:', groups.length);
|
|
215
258
|
console.log('[queue-metrics] Redis connections grouped total:', groupedTotal);
|
|
259
|
+
console.log('[queue-metrics] Redis connections seen keys:', this._redisConnSeenKeys.size);
|
|
260
|
+
console.log('[queue-metrics] Redis connections zeroed keys:', this._redisConnZeroedKeys.size);
|
|
216
261
|
console.log('[queue-metrics] Redis connections grouped sample (first 10):', JSON.stringify(groups.slice(0, 10), null, 2));
|
|
217
262
|
}
|
|
218
263
|
Object.values(grouped).forEach(({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsRedisClient.js","names":["BaseMetricsClient","require","getRedisClientType","REDIS_V4","IOREDIS","REDIS_V3","redisConnectionStableFields","redisConnectionFields","RedisMetricsClient","constructor","redisClient","metricsConfig","intervalSec","parseInt","process","env","METRICS_QUEUE_INTERVAL_SEC","processType","redisClientType","redisConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabels","redisConnectionsMemoryGauge","redisMemoryGauge","redisStatsGauge","_setCleanupHandlers","getRedisConnections","Error","Promise","resolve","reject","send_command","err","result","message","sendCommand","client","getRedisInfo","section","info","parseRedisConnections","clientsStr","split","filter","line","trim","map","parts","forEach","p","eqIdx","indexOf","k","slice","v","includes","collectRedisMetrics","memoryInfoStr","statsInfoStr","connectionsInfoStr","all","labels","getDefaultLabels","connections","reset","logValues","console","log","length","JSON","stringify","missing","flags","cmd","c","grouped","conn","totMem","key","count","memory","groups","Object","values","groupedTotal","reduce","sum","g","valueLabels","set","parseRedisInfo","infoStr","fromEntries","startsWith","stats","used_memory","memory_type","maxmemory","instantaneous_ops_per_sec","operation","error","warn","pushRedisMetrics","gatewayPush","clearAllCounters","metricsLogValues","metricObjects","registry","getMetricsAsJSON","startPush","_startPush","catch","cleanup","quit","disconnect","exit","on","module","exports"],"sources":["../../src/metrics/metricsRedisClient.js"],"sourcesContent":["const { BaseMetricsClient } = require('./baseMetricsClient')\nconst {\n getRedisClientType,\n REDIS_V4,\n IOREDIS,\n REDIS_V3,\n} = require('../redisUtils')\n\nconst redisConnectionStableFields = ['name', 'flags', 'cmd']\nconst redisConnectionFields = ['name', 'flags', 'tot-mem', 'cmd']\n\n/**\n * RedisMetricsClient extends BaseMetricsClient to collect\n * Redis metrics periodically and push them to Prometheus Pushgateway.\n *\n * @extends BaseMetricsClient\n */\nclass RedisMetricsClient extends BaseMetricsClient {\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 const intervalSec =\n metricsConfig.intervalSec ||\n parseInt(process.env.METRICS_QUEUE_INTERVAL_SEC || '', 10) ||\n 5\n\n super({\n ...metricsConfig,\n processType: metricsConfig.processType || 'queue-metrics',\n intervalSec,\n })\n\n /** Redis client used for metrics */\n this.redisClient = redisClient\n this.redisClientType = getRedisClientType(redisClient)\n\n /** Counter for Redis client connections */\n this.redisConnectionsGauge = this.createGauge({\n name: 'app_redis_connections_count',\n help: 'Redis client connections',\n labelNames: this.withDefaultLabels(redisConnectionStableFields),\n })\n\n this.redisConnectionsMemoryGauge = this.createGauge({\n name: 'app_redis_connections_memory_usage_count',\n help: 'Redis client connections',\n labelNames: this.withDefaultLabels(redisConnectionStableFields),\n })\n\n /** Gauge for Redis memory usage */\n this.redisMemoryGauge = this.createGauge({\n name: 'app_redis_memory_bytes',\n help: 'Redis memory usage in bytes',\n labelNames: this.withDefaultLabels(['memory_type']),\n })\n\n /** Gauge for Redis operation stats */\n this.redisStatsGauge = this.createGauge({\n name: 'app_redis_stats_total',\n help: 'Redis operation statistics',\n labelNames: this.withDefaultLabels(['operation']),\n })\n\n this._setCleanupHandlers()\n }\n\n getRedisConnections = async () => {\n if (!this.redisClient) throw new Error('Redis client not provided')\n\n // node-redis v3 (uses callback)\n if (this.redisClientType === REDIS_V3) {\n return new Promise((resolve, reject) => {\n this.redisClient.send_command('CLIENT', ['LIST'], (err, result) => {\n if (err) {\n reject(new Error(`Failed to get CLIENT LIST: ${err.message}`))\n } else resolve(result)\n })\n })\n }\n\n // node-redis v4\n if (this.redisClientType === REDIS_V4) {\n try {\n return this.redisClient.sendCommand(['CLIENT', 'LIST'])\n } catch (err) {\n throw new Error(`Failed to get CLIENT LIST: ${err.message}`)\n }\n }\n\n if (this.redisClientType === IOREDIS) {\n try {\n return this.redisClient.client('LIST')\n } catch (err) {\n throw new Error(`Failed to get CLIENT LIST: ${err.message}`)\n }\n }\n\n throw new Error('Unsupported Redis client type')\n }\n\n getRedisInfo = async section => {\n if (!this.redisClient) throw new Error('Redis client not provided')\n\n // node-redis v3 (uses callback)\n if (this.redisClientType === REDIS_V3) {\n return new Promise((resolve, reject) => {\n this.redisClient.info(section, (err, result) => {\n if (err) reject(err)\n else resolve(result)\n })\n })\n }\n\n // node-redis v4 or ioredis (info returns Promise)\n if (this.redisClientType === REDIS_V4 || this.redisClientType === IOREDIS) {\n try {\n return this.redisClient.info(section)\n } catch (err) {\n throw new Error(`Failed to get Redis INFO: ${err.message}`)\n }\n }\n\n throw new Error('Unsupported Redis client type')\n }\n\n parseRedisConnections = clientsStr => {\n return clientsStr\n .split('\\n')\n .filter(line => line.trim() !== '')\n .map(line => {\n const parts = line.split(' ')\n const client = {}\n parts.forEach(p => {\n const eqIdx = p.indexOf('=')\n if (eqIdx === -1) return\n const k = p.slice(0, eqIdx)\n const v = p.slice(eqIdx + 1)\n if (redisConnectionFields.includes(k)) {\n client[k] = v\n }\n })\n return client\n })\n }\n\n /**\n * Collect basic Redis INFO metrics: clients, memory, stats\n * @returns {Promise<void>}\n */\n collectRedisMetrics = async () => {\n try {\n const [memoryInfoStr, statsInfoStr, connectionsInfoStr] =\n await Promise.all([\n this.getRedisInfo('memory'),\n this.getRedisInfo('stats'),\n this.getRedisConnections(),\n ])\n\n const labels = this.getDefaultLabels()\n\n const connections = this.parseRedisConnections(connectionsInfoStr)\n\n this.redisConnectionsGauge.reset()\n this.redisConnectionsMemoryGauge.reset()\n\n if (this.logValues) {\n // \"IN\" data: raw client list from Redis\n console.log('[queue-metrics] Redis CLIENT LIST (raw):\\n', connectionsInfoStr)\n console.log(\n '[queue-metrics] Redis CLIENT LIST (raw) length:',\n connectionsInfoStr.length\n )\n\n // \"OUT\" data: parsed fields used for metrics\n console.log(\n '[queue-metrics] Redis CLIENT LIST (parsed) count:',\n connections.length\n )\n console.log(\n '[queue-metrics] Redis CLIENT LIST (parsed sample, first 10):',\n JSON.stringify(connections.slice(0, 10), null, 2)\n )\n\n const missing = { name: 0, flags: 0, cmd: 0, 'tot-mem': 0 }\n connections.forEach(c => {\n if (!c.name) missing.name += 1\n if (!c.flags) missing.flags += 1\n if (!c.cmd) missing.cmd += 1\n if (!c['tot-mem']) missing['tot-mem'] += 1\n })\n console.log(\n '[queue-metrics] Redis CLIENT LIST (parsed) missing fields:',\n missing\n )\n }\n\n const grouped = {}\n connections.forEach(conn => {\n const { name, flags, 'tot-mem': totMem, cmd } = conn\n\n // build grouping key (includes default gauge labels later)\n const key = JSON.stringify({ name, flags, cmd })\n\n if (!grouped[key]) {\n grouped[key] = {\n labels: { name, flags, cmd },\n count: 0,\n memory: 0,\n }\n }\n\n grouped[key].count += 1\n grouped[key].memory += parseInt(totMem, 10)\n })\n\n if (this.logValues) {\n const groups = Object.values(grouped)\n const groupedTotal = groups.reduce((sum, g) => sum + (g.count || 0), 0)\n console.log('[queue-metrics] Redis connections grouped buckets:', groups.length)\n console.log('[queue-metrics] Redis connections grouped total:', groupedTotal)\n console.log(\n '[queue-metrics] Redis connections grouped sample (first 10):',\n JSON.stringify(groups.slice(0, 10), null, 2)\n )\n }\n\n Object.values(grouped).forEach(\n ({ labels: valueLabels, count, memory }) => {\n this.redisConnectionsGauge.set({ ...labels, ...valueLabels }, count)\n this.redisConnectionsMemoryGauge.set(\n { ...labels, ...valueLabels },\n memory\n )\n }\n )\n\n const parseRedisInfo = infoStr =>\n Object.fromEntries(\n infoStr\n .split('\\r\\n')\n .filter(line => line && !line.startsWith('#'))\n .map(line => line.split(':', 2))\n .filter(parts => parts.length === 2 && parts[0] && parts[1])\n )\n\n const memory = parseRedisInfo(memoryInfoStr)\n const stats = parseRedisInfo(statsInfoStr)\n\n if (memory.used_memory) {\n this.redisMemoryGauge.set(\n { ...labels, memory_type: 'used' },\n parseInt(memory.used_memory, 10) || 0\n )\n }\n if (memory.maxmemory) {\n this.redisMemoryGauge.set(\n { ...labels, memory_type: 'max' },\n parseInt(memory.maxmemory, 10) || 0\n )\n }\n\n if (stats.instantaneous_ops_per_sec) {\n this.redisStatsGauge.set(\n { ...labels, operation: 'ops_per_sec' },\n parseInt(stats.instantaneous_ops_per_sec, 10) || 0\n )\n }\n } catch (error) {\n console.warn(\n `[queue-metrics] Failed to collect Redis metrics:`,\n error.message\n )\n }\n }\n\n /**\n * Collect metrics for all Redis and push to Prometheus Pushgateway\n * @returns {Promise<void>}\n */\n pushRedisMetrics = async () => {\n try {\n await this.collectRedisMetrics()\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 Redis`,\n JSON.stringify(metricObjects, null, 2)\n )\n }\n } catch (error) {\n console.error(\n `[queue-metrics] Failed to collect Redis 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.pushRedisMetrics().catch(err => {\n console.error(`[queue-metrics] Failed to push Redis metrics:`, err)\n })\n })\n }\n\n /**\n * Cleanup Redis client and exit process.\n * @returns {Promise<void>}\n */\n cleanup = async () => {\n try {\n if (!this.redisClient) return\n\n if (\n this.redisClientType === REDIS_V3 ||\n this.redisClientType === REDIS_V4\n ) {\n await this.redisClient.quit()\n } else if (this.redisClientType === IOREDIS) {\n await this.redisClient.disconnect()\n }\n } catch (err) {\n console.error('[queue-metrics] Error closing Redis client:', err)\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 = { RedisMetricsClient }\n"],"mappings":";;AAAA,MAAM;EAAEA;AAAkB,CAAC,GAAGC,OAAO,CAAC,qBAAqB,CAAC;AAC5D,MAAM;EACJC,kBAAkB;EAClBC,QAAQ;EACRC,OAAO;EACPC;AACF,CAAC,GAAGJ,OAAO,CAAC,eAAe,CAAC;AAE5B,MAAMK,2BAA2B,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC;AAC5D,MAAMC,qBAAqB,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC;;AAEjE;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,kBAAkB,SAASR,iBAAiB,CAAC;EACjD;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACES,WAAWA,CAAC;IAAEC,WAAW;IAAE,GAAGC;EAAc,CAAC,GAAG,CAAC,CAAC,EAAE;IAClD,MAAMC,WAAW,GACfD,aAAa,CAACC,WAAW,IACzBC,QAAQ,CAACC,OAAO,CAACC,GAAG,CAACC,0BAA0B,IAAI,EAAE,EAAE,EAAE,CAAC,IAC1D,CAAC;IAEH,KAAK,CAAC;MACJ,GAAGL,aAAa;MAChBM,WAAW,EAAEN,aAAa,CAACM,WAAW,IAAI,eAAe;MACzDL;IACF,CAAC,CAAC;;IAEF;IACA,IAAI,CAAC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MAAMkE,KAAK;IACb;EACF,CAAC;;EAED;AACF;AACA;AACA;EACES,SAAS,GAAGA,CAACpG,WAAW,GAAG,IAAI,CAACA,WAAW,KAAK;IAC9C,IAAI,CAACqG,UAAU,CAACrG,WAAW,EAAE,MAAM;MACjC,IAAI,CAAC6F,gBAAgB,CAAC,CAAC,CAACS,KAAK,CAAC/E,GAAG,IAAI;QACnCkC,OAAO,CAACkC,KAAK,CAAC,+CAA+C,EAAEpE,GAAG,CAAC;MACrE,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ,CAAC;;EAED;AACF;AACA;AACA;EACEgF,OAAO,GAAG,MAAAA,CAAA,KAAY;IACpB,IAAI;MACF,IAAI,CAAC,IAAI,CAACzG,WAAW,EAAE;MAEvB,IACE,IAAI,CAACQ,eAAe,KAAKb,QAAQ,IACjC,IAAI,CAACa,eAAe,KAAKf,QAAQ,EACjC;QACA,MAAM,IAAI,CAACO,WAAW,CAAC0G,IAAI,CAAC,CAAC;MAC/B,CAAC,MAAM,IAAI,IAAI,CAAClG,eAAe,KAAKd,OAAO,EAAE;QAC3C,MAAM,IAAI,CAACM,WAAW,CAAC2G,UAAU,CAAC,CAAC;MACrC;IACF,CAAC,CAAC,OAAOlF,GAAG,EAAE;MACZkC,OAAO,CAACkC,KAAK,CAAC,6CAA6C,EAAEpE,GAAG,CAAC;IACnE;IACArB,OAAO,CAACwG,IAAI,CAAC,CAAC,CAAC;EACjB,CAAC;EAED1F,mBAAmB,GAAGA,CAAA,KAAM;IAC1Bd,OAAO,CAACyG,EAAE,CAAC,QAAQ,EAAE,IAAI,CAACJ,OAAO,CAAC;IAClCrG,OAAO,CAACyG,EAAE,CAAC,SAAS,EAAE,IAAI,CAACJ,OAAO,CAAC;EACrC,CAAC;AACH;AAEAK,MAAM,CAACC,OAAO,GAAG;EAAEjH;AAAmB,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"metricsRedisClient.js","names":["BaseMetricsClient","require","getRedisClientType","REDIS_V4","IOREDIS","REDIS_V3","redisConnectionStableFields","redisConnectionFields","RedisMetricsClient","constructor","redisClient","metricsConfig","intervalSec","parseInt","process","env","METRICS_QUEUE_INTERVAL_SEC","processType","redisClientType","redisConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabels","redisConnectionsMemoryGauge","redisMemoryGauge","redisStatsGauge","_redisConnSeenKeys","Set","_redisConnZeroedKeys","_setCleanupHandlers","getRedisConnections","Error","Promise","resolve","reject","send_command","err","result","message","sendCommand","client","getRedisInfo","section","info","parseRedisConnections","clientsStr","split","filter","line","trim","map","parts","forEach","p","eqIdx","indexOf","k","slice","v","includes","collectRedisMetrics","memoryInfoStr","statsInfoStr","connectionsInfoStr","all","labels","getDefaultLabels","connections","logValues","console","log","length","JSON","stringify","missing","flags","cmd","c","grouped","conn","totMem","key","count","memory","mem","Number","isFinite","currentKeys","Object","keys","add","has","delete","reset","valueLabels","parse","set","groups","values","groupedTotal","reduce","sum","g","size","parseRedisInfo","infoStr","fromEntries","startsWith","stats","used_memory","memory_type","maxmemory","instantaneous_ops_per_sec","operation","error","warn","pushRedisMetrics","gatewayPush","clearAllCounters","metricsLogValues","metricObjects","registry","getMetricsAsJSON","startPush","_startPush","catch","cleanup","quit","disconnect","exit","on","module","exports"],"sources":["../../src/metrics/metricsRedisClient.js"],"sourcesContent":["const { BaseMetricsClient } = require('./baseMetricsClient')\nconst {\n getRedisClientType,\n REDIS_V4,\n IOREDIS,\n REDIS_V3,\n} = require('../redisUtils')\n\nconst redisConnectionStableFields = ['name', 'flags', 'cmd']\nconst redisConnectionFields = ['name', 'flags', 'tot-mem', 'cmd']\n\n/**\n * RedisMetricsClient extends BaseMetricsClient to collect\n * Redis metrics periodically and push them to Prometheus Pushgateway.\n *\n * @extends BaseMetricsClient\n */\nclass RedisMetricsClient extends BaseMetricsClient {\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 const intervalSec =\n metricsConfig.intervalSec ||\n parseInt(process.env.METRICS_QUEUE_INTERVAL_SEC || '', 10) ||\n 5\n\n super({\n ...metricsConfig,\n processType: metricsConfig.processType || 'queue-metrics',\n intervalSec,\n })\n\n /** Redis client used for metrics */\n this.redisClient = redisClient\n this.redisClientType = getRedisClientType(redisClient)\n\n /** Counter for Redis client connections */\n this.redisConnectionsGauge = this.createGauge({\n name: 'app_redis_connections_count',\n help: 'Redis client connections',\n labelNames: this.withDefaultLabels(redisConnectionStableFields),\n })\n\n this.redisConnectionsMemoryGauge = this.createGauge({\n name: 'app_redis_connections_memory_usage_count',\n help: 'Redis client connections',\n labelNames: this.withDefaultLabels(redisConnectionStableFields),\n })\n\n /** Gauge for Redis memory usage */\n this.redisMemoryGauge = this.createGauge({\n name: 'app_redis_memory_bytes',\n help: 'Redis memory usage in bytes',\n labelNames: this.withDefaultLabels(['memory_type']),\n })\n\n /** Gauge for Redis operation stats */\n this.redisStatsGauge = this.createGauge({\n name: 'app_redis_stats_total',\n help: 'Redis operation statistics',\n labelNames: this.withDefaultLabels(['operation']),\n })\n\n // Track emitted connection label combinations so we can:\n // - emit a one-shot 0 for series that disappear (overwrite last non-zero sample)\n // - then stop emitting them on subsequent scrapes\n //\n // This is mainly needed because we label connections by `cmd`,\n // and Redis `CLIENT LIST` `cmd` is volatile.\n this._redisConnSeenKeys = new Set()\n this._redisConnZeroedKeys = new Set()\n\n this._setCleanupHandlers()\n }\n\n getRedisConnections = async () => {\n if (!this.redisClient) throw new Error('Redis client not provided')\n\n // node-redis v3 (uses callback)\n if (this.redisClientType === REDIS_V3) {\n return new Promise((resolve, reject) => {\n this.redisClient.send_command('CLIENT', ['LIST'], (err, result) => {\n if (err) {\n reject(new Error(`Failed to get CLIENT LIST: ${err.message}`))\n } else resolve(result)\n })\n })\n }\n\n // node-redis v4\n if (this.redisClientType === REDIS_V4) {\n try {\n return this.redisClient.sendCommand(['CLIENT', 'LIST'])\n } catch (err) {\n throw new Error(`Failed to get CLIENT LIST: ${err.message}`)\n }\n }\n\n if (this.redisClientType === IOREDIS) {\n try {\n return this.redisClient.client('LIST')\n } catch (err) {\n throw new Error(`Failed to get CLIENT LIST: ${err.message}`)\n }\n }\n\n throw new Error('Unsupported Redis client type')\n }\n\n getRedisInfo = async section => {\n if (!this.redisClient) throw new Error('Redis client not provided')\n\n // node-redis v3 (uses callback)\n if (this.redisClientType === REDIS_V3) {\n return new Promise((resolve, reject) => {\n this.redisClient.info(section, (err, result) => {\n if (err) reject(err)\n else resolve(result)\n })\n })\n }\n\n // node-redis v4 or ioredis (info returns Promise)\n if (this.redisClientType === REDIS_V4 || this.redisClientType === IOREDIS) {\n try {\n return this.redisClient.info(section)\n } catch (err) {\n throw new Error(`Failed to get Redis INFO: ${err.message}`)\n }\n }\n\n throw new Error('Unsupported Redis client type')\n }\n\n parseRedisConnections = clientsStr => {\n return clientsStr\n .split('\\n')\n .filter(line => line.trim() !== '')\n .map(line => {\n const parts = line.split(' ')\n const client = {}\n parts.forEach(p => {\n const eqIdx = p.indexOf('=')\n if (eqIdx === -1) return\n const k = p.slice(0, eqIdx)\n const v = p.slice(eqIdx + 1)\n if (redisConnectionFields.includes(k)) {\n client[k] = v\n }\n })\n return client\n })\n }\n\n /**\n * Collect basic Redis INFO metrics: clients, memory, stats\n * @returns {Promise<void>}\n */\n collectRedisMetrics = async () => {\n try {\n const [memoryInfoStr, statsInfoStr, connectionsInfoStr] =\n await Promise.all([\n this.getRedisInfo('memory'),\n this.getRedisInfo('stats'),\n this.getRedisConnections(),\n ])\n\n const labels = this.getDefaultLabels()\n\n const connections = this.parseRedisConnections(connectionsInfoStr)\n\n if (this.logValues) {\n // \"IN\" data: raw client list from Redis\n console.log('[queue-metrics] Redis CLIENT LIST (raw):\\n', connectionsInfoStr)\n console.log(\n '[queue-metrics] Redis CLIENT LIST (raw) length:',\n connectionsInfoStr.length\n )\n\n // \"OUT\" data: parsed fields used for metrics\n console.log(\n '[queue-metrics] Redis CLIENT LIST (parsed) count:',\n connections.length\n )\n console.log(\n '[queue-metrics] Redis CLIENT LIST (parsed sample, first 10):',\n JSON.stringify(connections.slice(0, 10), null, 2)\n )\n\n const missing = { name: 0, flags: 0, cmd: 0, 'tot-mem': 0 }\n connections.forEach(c => {\n if (!c.name) missing.name += 1\n if (!c.flags) missing.flags += 1\n if (!c.cmd) missing.cmd += 1\n if (!c['tot-mem']) missing['tot-mem'] += 1\n })\n console.log(\n '[queue-metrics] Redis CLIENT LIST (parsed) missing fields:',\n missing\n )\n }\n\n const grouped = {}\n connections.forEach(conn => {\n const { name, flags, 'tot-mem': totMem, cmd } = conn\n\n // build grouping key (includes default gauge labels later)\n const key = JSON.stringify({ name, flags, cmd })\n\n if (!grouped[key]) {\n grouped[key] = {\n labels: { name, flags, cmd },\n count: 0,\n memory: 0,\n }\n }\n\n grouped[key].count += 1\n const mem = parseInt(totMem, 10)\n grouped[key].memory += Number.isFinite(mem) ? mem : 0\n })\n\n // Ensure sets exist even if older instance is hot-reloaded.\n if (!this._redisConnSeenKeys) this._redisConnSeenKeys = new Set()\n if (!this._redisConnZeroedKeys) this._redisConnZeroedKeys = new Set()\n\n const currentKeys = new Set(Object.keys(grouped))\n for (const k of currentKeys) {\n this._redisConnSeenKeys.add(k)\n if (this._redisConnZeroedKeys.has(k)) this._redisConnZeroedKeys.delete(k)\n }\n\n // Reset gauges each scrape so we only export:\n // - current snapshot series\n // - one-shot zeros for recently disappeared series\n this.redisConnectionsGauge.reset()\n this.redisConnectionsMemoryGauge.reset()\n\n // Emit one-shot zeros for series that disappeared since last seen.\n for (const k of this._redisConnSeenKeys) {\n if (currentKeys.has(k)) continue\n if (this._redisConnZeroedKeys.has(k)) continue\n try {\n const valueLabels = JSON.parse(k)\n this.redisConnectionsGauge.set({ ...labels, ...valueLabels }, 0)\n this.redisConnectionsMemoryGauge.set({ ...labels, ...valueLabels }, 0)\n this._redisConnZeroedKeys.add(k)\n } catch {\n // ignore malformed key\n }\n }\n\n if (this.logValues) {\n const groups = Object.values(grouped)\n const groupedTotal = groups.reduce((sum, g) => sum + (g.count || 0), 0)\n console.log('[queue-metrics] Redis connections grouped buckets:', groups.length)\n console.log('[queue-metrics] Redis connections grouped total:', groupedTotal)\n console.log(\n '[queue-metrics] Redis connections seen keys:',\n this._redisConnSeenKeys.size\n )\n console.log(\n '[queue-metrics] Redis connections zeroed keys:',\n this._redisConnZeroedKeys.size\n )\n console.log(\n '[queue-metrics] Redis connections grouped sample (first 10):',\n JSON.stringify(groups.slice(0, 10), null, 2)\n )\n }\n\n Object.values(grouped).forEach(\n ({ labels: valueLabels, count, memory }) => {\n this.redisConnectionsGauge.set({ ...labels, ...valueLabels }, count)\n this.redisConnectionsMemoryGauge.set(\n { ...labels, ...valueLabels },\n memory\n )\n }\n )\n\n const parseRedisInfo = infoStr =>\n Object.fromEntries(\n infoStr\n .split('\\r\\n')\n .filter(line => line && !line.startsWith('#'))\n .map(line => line.split(':', 2))\n .filter(parts => parts.length === 2 && parts[0] && parts[1])\n )\n\n const memory = parseRedisInfo(memoryInfoStr)\n const stats = parseRedisInfo(statsInfoStr)\n\n if (memory.used_memory) {\n this.redisMemoryGauge.set(\n { ...labels, memory_type: 'used' },\n parseInt(memory.used_memory, 10) || 0\n )\n }\n if (memory.maxmemory) {\n this.redisMemoryGauge.set(\n { ...labels, memory_type: 'max' },\n parseInt(memory.maxmemory, 10) || 0\n )\n }\n\n if (stats.instantaneous_ops_per_sec) {\n this.redisStatsGauge.set(\n { ...labels, operation: 'ops_per_sec' },\n parseInt(stats.instantaneous_ops_per_sec, 10) || 0\n )\n }\n } catch (error) {\n console.warn(\n `[queue-metrics] Failed to collect Redis metrics:`,\n error.message\n )\n }\n }\n\n /**\n * Collect metrics for all Redis and push to Prometheus Pushgateway\n * @returns {Promise<void>}\n */\n pushRedisMetrics = async () => {\n try {\n await this.collectRedisMetrics()\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 Redis`,\n JSON.stringify(metricObjects, null, 2)\n )\n }\n } catch (error) {\n console.error(\n `[queue-metrics] Failed to collect Redis 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.pushRedisMetrics().catch(err => {\n console.error(`[queue-metrics] Failed to push Redis metrics:`, err)\n })\n })\n }\n\n /**\n * Cleanup Redis client and exit process.\n * @returns {Promise<void>}\n */\n cleanup = async () => {\n try {\n if (!this.redisClient) return\n\n if (\n this.redisClientType === REDIS_V3 ||\n this.redisClientType === REDIS_V4\n ) {\n await this.redisClient.quit()\n } else if (this.redisClientType === IOREDIS) {\n await this.redisClient.disconnect()\n }\n } catch (err) {\n console.error('[queue-metrics] Error closing Redis client:', err)\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 = { RedisMetricsClient }\n"],"mappings":";;AAAA,MAAM;EAAEA;AAAkB,CAAC,GAAGC,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WAAW,GAAG,IAAI,CAACA,WAAW,KAAK;IAC9C,IAAI,CAACkH,UAAU,CAAClH,WAAW,EAAE,MAAM;MACjC,IAAI,CAAC0G,gBAAgB,CAAC,CAAC,CAACS,KAAK,CAACzF,GAAG,IAAI;QACnCiC,OAAO,CAAC6C,KAAK,CAAC,+CAA+C,EAAE9E,GAAG,CAAC;MACrE,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ,CAAC;;EAED;AACF;AACA;AACA;EACE0F,OAAO,GAAG,MAAAA,CAAA,KAAY;IACpB,IAAI;MACF,IAAI,CAAC,IAAI,CAACtH,WAAW,EAAE;MAEvB,IACE,IAAI,CAACQ,eAAe,KAAKb,QAAQ,IACjC,IAAI,CAACa,eAAe,KAAKf,QAAQ,EACjC;QACA,MAAM,IAAI,CAACO,WAAW,CAACuH,IAAI,CAAC,CAAC;MAC/B,CAAC,MAAM,IAAI,IAAI,CAAC/G,eAAe,KAAKd,OAAO,EAAE;QAC3C,MAAM,IAAI,CAACM,WAAW,CAACwH,UAAU,CAAC,CAAC;MACrC;IACF,CAAC,CAAC,OAAO5F,GAAG,EAAE;MACZiC,OAAO,CAAC6C,KAAK,CAAC,6CAA6C,EAAE9E,GAAG,CAAC;IACnE;IACAxB,OAAO,CAACqH,IAAI,CAAC,CAAC,CAAC;EACjB,CAAC;EAEDpG,mBAAmB,GAAGA,CAAA,KAAM;IAC1BjB,OAAO,CAACsH,EAAE,CAAC,QAAQ,EAAE,IAAI,CAACJ,OAAO,CAAC;IAClClH,OAAO,CAACsH,EAAE,CAAC,SAAS,EAAE,IAAI,CAACJ,OAAO,CAAC;EACrC,CAAC;AACH;AAEAK,MAAM,CAACC,OAAO,GAAG;EAAE9H;AAAmB,CAAC","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -4,6 +4,13 @@ const mysql = require('mysql2/promise')
|
|
|
4
4
|
const DB_TYPE_POSTGRES = 'postgres'
|
|
5
5
|
const DB_TYPE_MYSQL = 'mysql'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @returns {number}
|
|
9
|
+
*/
|
|
10
|
+
function nowMs() {
|
|
11
|
+
return Number(process.hrtime.bigint() / 1_000_000n)
|
|
12
|
+
}
|
|
13
|
+
|
|
7
14
|
/**
|
|
8
15
|
* @param {string} url
|
|
9
16
|
* @returns {string} postgres | mysql
|
|
@@ -81,14 +88,78 @@ function createDatabasePool(env, url, connectionTimeoutMs) {
|
|
|
81
88
|
/**
|
|
82
89
|
* @param {any} pool
|
|
83
90
|
* @param {string} type
|
|
84
|
-
* @returns {Promise<
|
|
91
|
+
* @returns {Promise<{ connectMs: number, queryMs: number, totalMs: number }>}
|
|
85
92
|
*/
|
|
86
93
|
async function runHealthCheck(pool, type) {
|
|
87
94
|
if (type === DB_TYPE_MYSQL) {
|
|
88
|
-
|
|
89
|
-
|
|
95
|
+
const startTotal = nowMs()
|
|
96
|
+
let connectMs = 0
|
|
97
|
+
let queryMs = 0
|
|
98
|
+
|
|
99
|
+
/** @type {any} */
|
|
100
|
+
let conn
|
|
101
|
+
try {
|
|
102
|
+
const startConnect = nowMs()
|
|
103
|
+
conn = await pool.getConnection()
|
|
104
|
+
connectMs = nowMs() - startConnect
|
|
105
|
+
|
|
106
|
+
const startQuery = nowMs()
|
|
107
|
+
await conn.query('SELECT 1')
|
|
108
|
+
queryMs = nowMs() - startQuery
|
|
109
|
+
|
|
110
|
+
return { connectMs, queryMs, totalMs: nowMs() - startTotal }
|
|
111
|
+
} catch (err) {
|
|
112
|
+
const totalMs = nowMs() - startTotal
|
|
113
|
+
err.healthCheckTimings = {
|
|
114
|
+
connectMs,
|
|
115
|
+
queryMs,
|
|
116
|
+
totalMs,
|
|
117
|
+
}
|
|
118
|
+
throw err
|
|
119
|
+
} finally {
|
|
120
|
+
if (conn) {
|
|
121
|
+
try {
|
|
122
|
+
conn.release()
|
|
123
|
+
} catch {
|
|
124
|
+
// ignore
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const startTotal = nowMs()
|
|
131
|
+
let connectMs = 0
|
|
132
|
+
let queryMs = 0
|
|
133
|
+
|
|
134
|
+
/** @type {import('pg').PoolClient | null} */
|
|
135
|
+
let client = null
|
|
136
|
+
try {
|
|
137
|
+
const startConnect = nowMs()
|
|
138
|
+
client = await pool.connect()
|
|
139
|
+
connectMs = nowMs() - startConnect
|
|
140
|
+
|
|
141
|
+
const startQuery = nowMs()
|
|
142
|
+
await client.query('SELECT 1')
|
|
143
|
+
queryMs = nowMs() - startQuery
|
|
144
|
+
|
|
145
|
+
return { connectMs, queryMs, totalMs: nowMs() - startTotal }
|
|
146
|
+
} catch (err) {
|
|
147
|
+
const totalMs = nowMs() - startTotal
|
|
148
|
+
err.healthCheckTimings = {
|
|
149
|
+
connectMs,
|
|
150
|
+
queryMs,
|
|
151
|
+
totalMs,
|
|
152
|
+
}
|
|
153
|
+
throw err
|
|
154
|
+
} finally {
|
|
155
|
+
if (client) {
|
|
156
|
+
try {
|
|
157
|
+
client.release()
|
|
158
|
+
} catch {
|
|
159
|
+
// ignore
|
|
160
|
+
}
|
|
161
|
+
}
|
|
90
162
|
}
|
|
91
|
-
await pool.query('SELECT 1')
|
|
92
163
|
}
|
|
93
164
|
|
|
94
165
|
/**
|
|
@@ -11,11 +11,24 @@ const {
|
|
|
11
11
|
} = require('../redisUtils')
|
|
12
12
|
const { HealthCheckCache } = require('./healthCheckCache')
|
|
13
13
|
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} name
|
|
16
|
+
* @returns {number | undefined}
|
|
17
|
+
*/
|
|
18
|
+
function readNumberEnv(name) {
|
|
19
|
+
const raw = process.env[name]
|
|
20
|
+
if (raw == null || raw === '') return undefined
|
|
21
|
+
const num = Number(raw)
|
|
22
|
+
return Number.isFinite(num) ? num : undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @type {{ checkIntervalMs: number, staleThresholdMs: number, checkTimeoutMs: number, maxDbConnectLatencyMs: number }} */
|
|
15
26
|
const DEFAULT_HEALTH_CONFIG = {
|
|
16
27
|
checkIntervalMs: 30_000,
|
|
17
28
|
staleThresholdMs: 180_000,
|
|
18
29
|
checkTimeoutMs: 15_000,
|
|
30
|
+
maxDbConnectLatencyMs:
|
|
31
|
+
readNumberEnv('HEALTH_DB_MAX_CONNECT_LATENCY_MS') ?? 1000,
|
|
19
32
|
}
|
|
20
33
|
|
|
21
34
|
const SENSITIVE_PATTERNS = [
|
|
@@ -129,12 +142,29 @@ class HealthCheckClient {
|
|
|
129
142
|
|
|
130
143
|
try {
|
|
131
144
|
const { pool, type } = this._getPool(env, url)
|
|
132
|
-
await runHealthCheck(pool, type)
|
|
133
|
-
|
|
145
|
+
const timings = await runHealthCheck(pool, type)
|
|
146
|
+
|
|
147
|
+
const maxConnect = this.healthConfig.maxDbConnectLatencyMs
|
|
148
|
+
if (
|
|
149
|
+
typeof maxConnect === 'number' &&
|
|
150
|
+
Number.isFinite(maxConnect) &&
|
|
151
|
+
timings?.connectMs != null &&
|
|
152
|
+
timings.connectMs > maxConnect
|
|
153
|
+
) {
|
|
154
|
+
return {
|
|
155
|
+
status: 'error',
|
|
156
|
+
error: `DB connect latency ${timings.connectMs}ms exceeds ${maxConnect}ms`,
|
|
157
|
+
...timings,
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return { status: 'ok', ...timings }
|
|
134
162
|
} catch (err) {
|
|
163
|
+
const timings = err?.healthCheckTimings
|
|
135
164
|
return {
|
|
136
165
|
status: 'error',
|
|
137
166
|
error: maskSensitiveData(err.message),
|
|
167
|
+
...(timings && typeof timings === 'object' ? timings : {}),
|
|
138
168
|
}
|
|
139
169
|
}
|
|
140
170
|
}
|
|
@@ -74,6 +74,15 @@ class RedisMetricsClient extends BaseMetricsClient {
|
|
|
74
74
|
labelNames: this.withDefaultLabels(['operation']),
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
+
// Track emitted connection label combinations so we can:
|
|
78
|
+
// - emit a one-shot 0 for series that disappear (overwrite last non-zero sample)
|
|
79
|
+
// - then stop emitting them on subsequent scrapes
|
|
80
|
+
//
|
|
81
|
+
// This is mainly needed because we label connections by `cmd`,
|
|
82
|
+
// and Redis `CLIENT LIST` `cmd` is volatile.
|
|
83
|
+
this._redisConnSeenKeys = new Set()
|
|
84
|
+
this._redisConnZeroedKeys = new Set()
|
|
85
|
+
|
|
77
86
|
this._setCleanupHandlers()
|
|
78
87
|
}
|
|
79
88
|
|
|
@@ -173,9 +182,6 @@ class RedisMetricsClient extends BaseMetricsClient {
|
|
|
173
182
|
|
|
174
183
|
const connections = this.parseRedisConnections(connectionsInfoStr)
|
|
175
184
|
|
|
176
|
-
this.redisConnectionsGauge.reset()
|
|
177
|
-
this.redisConnectionsMemoryGauge.reset()
|
|
178
|
-
|
|
179
185
|
if (this.logValues) {
|
|
180
186
|
// "IN" data: raw client list from Redis
|
|
181
187
|
console.log('[queue-metrics] Redis CLIENT LIST (raw):\n', connectionsInfoStr)
|
|
@@ -223,14 +229,53 @@ class RedisMetricsClient extends BaseMetricsClient {
|
|
|
223
229
|
}
|
|
224
230
|
|
|
225
231
|
grouped[key].count += 1
|
|
226
|
-
|
|
232
|
+
const mem = parseInt(totMem, 10)
|
|
233
|
+
grouped[key].memory += Number.isFinite(mem) ? mem : 0
|
|
227
234
|
})
|
|
228
235
|
|
|
236
|
+
// Ensure sets exist even if older instance is hot-reloaded.
|
|
237
|
+
if (!this._redisConnSeenKeys) this._redisConnSeenKeys = new Set()
|
|
238
|
+
if (!this._redisConnZeroedKeys) this._redisConnZeroedKeys = new Set()
|
|
239
|
+
|
|
240
|
+
const currentKeys = new Set(Object.keys(grouped))
|
|
241
|
+
for (const k of currentKeys) {
|
|
242
|
+
this._redisConnSeenKeys.add(k)
|
|
243
|
+
if (this._redisConnZeroedKeys.has(k)) this._redisConnZeroedKeys.delete(k)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Reset gauges each scrape so we only export:
|
|
247
|
+
// - current snapshot series
|
|
248
|
+
// - one-shot zeros for recently disappeared series
|
|
249
|
+
this.redisConnectionsGauge.reset()
|
|
250
|
+
this.redisConnectionsMemoryGauge.reset()
|
|
251
|
+
|
|
252
|
+
// Emit one-shot zeros for series that disappeared since last seen.
|
|
253
|
+
for (const k of this._redisConnSeenKeys) {
|
|
254
|
+
if (currentKeys.has(k)) continue
|
|
255
|
+
if (this._redisConnZeroedKeys.has(k)) continue
|
|
256
|
+
try {
|
|
257
|
+
const valueLabels = JSON.parse(k)
|
|
258
|
+
this.redisConnectionsGauge.set({ ...labels, ...valueLabels }, 0)
|
|
259
|
+
this.redisConnectionsMemoryGauge.set({ ...labels, ...valueLabels }, 0)
|
|
260
|
+
this._redisConnZeroedKeys.add(k)
|
|
261
|
+
} catch {
|
|
262
|
+
// ignore malformed key
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
229
266
|
if (this.logValues) {
|
|
230
267
|
const groups = Object.values(grouped)
|
|
231
268
|
const groupedTotal = groups.reduce((sum, g) => sum + (g.count || 0), 0)
|
|
232
269
|
console.log('[queue-metrics] Redis connections grouped buckets:', groups.length)
|
|
233
270
|
console.log('[queue-metrics] Redis connections grouped total:', groupedTotal)
|
|
271
|
+
console.log(
|
|
272
|
+
'[queue-metrics] Redis connections seen keys:',
|
|
273
|
+
this._redisConnSeenKeys.size
|
|
274
|
+
)
|
|
275
|
+
console.log(
|
|
276
|
+
'[queue-metrics] Redis connections zeroed keys:',
|
|
277
|
+
this._redisConnZeroedKeys.size
|
|
278
|
+
)
|
|
234
279
|
console.log(
|
|
235
280
|
'[queue-metrics] Redis connections grouped sample (first 10):',
|
|
236
281
|
JSON.stringify(groups.slice(0, 10), null, 2)
|