@adalo/metrics 0.1.73 → 0.1.75

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/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './metricsClient';
2
2
  export * from './metricsRedisClient';
3
3
  export * from './metricsQueueRedisClient';
4
+ export * from './metricsDatabaseClient';
4
5
  export * from './redisUtils';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,yBAAyB,CAAA;AACvC,cAAc,cAAc,CAAA"}
package/lib/index.js CHANGED
@@ -36,6 +36,17 @@ Object.keys(_metricsQueueRedisClient).forEach(function (key) {
36
36
  }
37
37
  });
38
38
  });
39
+ var _metricsDatabaseClient = require("./metricsDatabaseClient");
40
+ Object.keys(_metricsDatabaseClient).forEach(function (key) {
41
+ if (key === "default" || key === "__esModule") return;
42
+ if (key in exports && exports[key] === _metricsDatabaseClient[key]) return;
43
+ Object.defineProperty(exports, key, {
44
+ enumerable: true,
45
+ get: function () {
46
+ return _metricsDatabaseClient[key];
47
+ }
48
+ });
49
+ });
39
50
  var _redisUtils = require("./redisUtils");
40
51
  Object.keys(_redisUtils).forEach(function (key) {
41
52
  if (key === "default" || key === "__esModule") return;
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["_metricsClient","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_metricsRedisClient","_metricsQueueRedisClient","_redisUtils"],"sources":["../src/index.ts"],"sourcesContent":["export * from './metricsClient'\nexport * from './metricsRedisClient'\nexport * from './metricsQueueRedisClient'\nexport * from './redisUtils'\n"],"mappings":";;;;;AAAA,IAAAA,cAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,cAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,cAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,cAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,mBAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,mBAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,mBAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,mBAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,wBAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,wBAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,wBAAA,CAAAN,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAE,wBAAA,CAAAN,GAAA;IAAA;EAAA;AAAA;AACA,IAAAO,WAAA,GAAAX,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAS,WAAA,EAAAR,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAO,WAAA,CAAAP,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAG,WAAA,CAAAP,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["_metricsClient","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_metricsRedisClient","_metricsQueueRedisClient","_metricsDatabaseClient","_redisUtils"],"sources":["../src/index.ts"],"sourcesContent":["export * from './metricsClient'\nexport * from './metricsRedisClient'\nexport * from './metricsQueueRedisClient'\nexport * from './metricsDatabaseClient'\nexport * from './redisUtils'\n"],"mappings":";;;;;AAAA,IAAAA,cAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,cAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,cAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,cAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,mBAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,mBAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,mBAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,O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@
1
+ /**
2
+ * DatabaseMetricsClient collects Postgres connection metrics
3
+ * and pushes them to Prometheus Pushgateway.
4
+ *
5
+ * @extends MetricsClient
6
+ */
7
+ export class DatabaseMetricsClient extends MetricsClient {
8
+ /**
9
+ * @param {Object} options
10
+ * @param {string} options.databaseUrl - Required main database URL
11
+ * @param {string[]} [options.additional_database_urls] - Optional additional DB URLs
12
+ * @param {string} [options.appName] - Application name (from MetricsClient)
13
+ * @param {string} [options.dynoId] - Dyno/instance ID (from MetricsClient)
14
+ * @param {string} [options.processType] - Process type (from MetricsClient)
15
+ * @param {boolean} [options.enabled] - Enable metrics collection (from MetricsClient)
16
+ * @param {boolean} [options.logValues] - Log metrics values (from MetricsClient)
17
+ * @param {string} [options.pushgatewayUrl] - PushGateway URL (from MetricsClient)
18
+ * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from MetricsClient)
19
+ * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics
20
+ * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service
21
+ * @param {boolean} [options.scripDefaultMetrics] - Skip default metrics creation
22
+ * @param {function} [options.startupValidation] - Function to validate startup
23
+ */
24
+ constructor({ databaseUrl, additional_database_urls, ...metricsConfig }?: {
25
+ databaseUrl: string;
26
+ additional_database_urls?: string[] | undefined;
27
+ appName?: string | undefined;
28
+ dynoId?: string | undefined;
29
+ processType?: string | undefined;
30
+ enabled?: boolean | undefined;
31
+ logValues?: boolean | undefined;
32
+ pushgatewayUrl?: string | undefined;
33
+ pushgatewaySecret?: string | undefined;
34
+ intervalSec?: number | undefined;
35
+ removeOldMetrics?: boolean | undefined;
36
+ scripDefaultMetrics?: boolean | undefined;
37
+ startupValidation?: Function | undefined;
38
+ });
39
+ databasePools: Pool[];
40
+ /** Gauge for Database connections */
41
+ databaseConnectionsGauge: import("prom-client").Gauge<string>;
42
+ /**
43
+ * @param {Pool} pool - PG connection pool
44
+ * @returns {Promise<{ current: number, max: number, dbName: string }>}
45
+ */
46
+ getDBConnectionsAndName: (pool: Pool) => Promise<{
47
+ current: number;
48
+ max: number;
49
+ dbName: string;
50
+ }>;
51
+ /**
52
+ * Collect database connection metrics for all configured pools
53
+ * @returns {Promise<void>}
54
+ */
55
+ collectDatabaseMetrics: () => Promise<void>;
56
+ /**
57
+ * Push database metrics to Prometheus Pushgateway
58
+ * @returns {Promise<void>}
59
+ */
60
+ pushDatabaseMetrics: () => Promise<void>;
61
+ /**
62
+ * Start periodic collection.
63
+ * @param {number} [intervalSec=this.intervalSec] - Interval in seconds
64
+ */
65
+ startPush: (intervalSec?: number | undefined) => void;
66
+ }
67
+ import { MetricsClient } from "./metricsClient";
68
+ import { Pool } from "pg";
69
+ //# sourceMappingURL=metricsDatabaseClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metricsDatabaseClient.d.ts","sourceRoot":"","sources":["../src/metricsDatabaseClient.js"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH;IACE;;;;;;;;;;;;;;;OAeG;IACH;QAd2B,WAAW,EAA3B,MAAM;QACa,wBAAwB;QAC1B,OAAO;QACP,MAAM;QACN,WAAW;QACV,OAAO;QACP,SAAS;QACV,cAAc;QACd,iBAAiB;QACjB,WAAW;QACV,gBAAgB;QAChB,mBAAmB;QAClB,iBAAiB;OA0E9C;IAlBC,sBAAuB;IAUvB,qCAAqC;IACrC,8DAIE;IAKJ;;;OAGG;IACH,gCAHW,IAAI,KACF,QAAQ;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAoBrE;IAED;;;OAGG;IACH,8BAFa,QAAQ,IAAI,CAAC,CAqBzB;IAED;;;OAGG;IACH,2BAFa,QAAQ,IAAI,CAAC,CAoBzB;IAED;;;OAGG;IACH,sDAMC;CAwBF"}
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+
3
+ const {
4
+ Pool
5
+ } = require('pg');
6
+ const {
7
+ MetricsClient
8
+ } = require('.');
9
+
10
+ /**
11
+ * DatabaseMetricsClient collects Postgres connection metrics
12
+ * and pushes them to Prometheus Pushgateway.
13
+ *
14
+ * @extends MetricsClient
15
+ */
16
+ class DatabaseMetricsClient extends MetricsClient {
17
+ /**
18
+ * @param {Object} options
19
+ * @param {string} options.databaseUrl - Required main database URL
20
+ * @param {string[]} [options.additional_database_urls] - Optional additional DB URLs
21
+ * @param {string} [options.appName] - Application name (from MetricsClient)
22
+ * @param {string} [options.dynoId] - Dyno/instance ID (from MetricsClient)
23
+ * @param {string} [options.processType] - Process type (from MetricsClient)
24
+ * @param {boolean} [options.enabled] - Enable metrics collection (from MetricsClient)
25
+ * @param {boolean} [options.logValues] - Log metrics values (from MetricsClient)
26
+ * @param {string} [options.pushgatewayUrl] - PushGateway URL (from MetricsClient)
27
+ * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from MetricsClient)
28
+ * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics
29
+ * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service
30
+ * @param {boolean} [options.scripDefaultMetrics] - Skip default metrics creation
31
+ * @param {function} [options.startupValidation] - Function to validate startup
32
+ */
33
+ constructor({
34
+ databaseUrl,
35
+ additional_database_urls = [],
36
+ ...metricsConfig
37
+ } = {}) {
38
+ const intervalSec = metricsConfig.intervalSec || parseInt(process.env.METRICS_DATABASE_INTERVAL_SEC || '', 10) || 60;
39
+ const startupValidation = async () => {
40
+ if (!databaseUrl) {
41
+ console.error(`[database-metrics] ❌ METRICS_DATABASE_URL is required`);
42
+ return false;
43
+ }
44
+ try {
45
+ const mainPool = new Pool({
46
+ connectionString: databaseUrl
47
+ });
48
+ await mainPool.query('SELECT 1');
49
+ await mainPool.end();
50
+ console.info(`[database-metrics] ✓ Main database OK`);
51
+ } catch (err) {
52
+ console.error(`[database-metrics] ❌ Cannot connect to main database: ${err.message}`);
53
+ return false;
54
+ }
55
+ for (const url of additional_database_urls) {
56
+ try {
57
+ const p = new Pool({
58
+ connectionString: url
59
+ });
60
+ await p.query('SELECT 1');
61
+ await p.end();
62
+ console.info(`[database-metrics] ✓ Additional database OK: ${url}`);
63
+ } catch (err) {
64
+ console.error(`[database-metrics] ⚠ Skipping additional database: ${url}`);
65
+ console.error(`[database-metrics] ${err.message}`);
66
+ }
67
+ }
68
+ console.info(`[database-metrics] Database metrics collection starting`);
69
+ return true;
70
+ };
71
+ super({
72
+ ...metricsConfig,
73
+ scripDefaultMetrics: true,
74
+ processType: metricsConfig.processType || 'database-metrics',
75
+ intervalSec,
76
+ startupValidation
77
+ });
78
+ this.databasePools = [];
79
+ if (databaseUrl) {
80
+ this.databasePools.push(new Pool({
81
+ connectionString: databaseUrl
82
+ }));
83
+ }
84
+ for (const url of additional_database_urls) {
85
+ this.databasePools.push(new Pool({
86
+ connectionString: url
87
+ }));
88
+ }
89
+
90
+ /** Gauge for Database connections */
91
+ this.databaseConnectionsGauge = this.createGauge({
92
+ name: 'app_database_connections',
93
+ help: 'Postgres database connections',
94
+ labelNames: this.withDefaultLabels(['max_connections', 'database_name'])
95
+ });
96
+ this._setCleanupHandlers();
97
+ }
98
+
99
+ /**
100
+ * @param {Pool} pool - PG connection pool
101
+ * @returns {Promise<{ current: number, max: number, dbName: string }>}
102
+ */
103
+ getDBConnectionsAndName = async pool => {
104
+ try {
105
+ const currentRes = await pool.query('SELECT COUNT(*) AS current FROM pg_stat_activity WHERE datname = current_database()');
106
+ const current = parseInt(currentRes.rows[0]?.current || 0, 10);
107
+ const maxRes = await pool.query("SELECT current_setting('max_connections') AS max");
108
+ const max = parseInt(maxRes.rows[0]?.max || 0, 10);
109
+ const dbName = pool.options?.database || pool.options?.connectionString;
110
+ return {
111
+ current,
112
+ max,
113
+ dbName
114
+ };
115
+ } catch (err) {
116
+ return {
117
+ current: 0,
118
+ max: 0,
119
+ dbName: pool.options?.database || ''
120
+ };
121
+ }
122
+ };
123
+
124
+ /**
125
+ * Collect database connection metrics for all configured pools
126
+ * @returns {Promise<void>}
127
+ */
128
+ collectDatabaseMetrics = async () => {
129
+ for (const pool of this.databasePools) {
130
+ try {
131
+ const {
132
+ current,
133
+ max,
134
+ dbName
135
+ } = await this.getDBConnectionsAndName(pool);
136
+ this.databaseConnectionsGauge.set({
137
+ ...this.getDefaultLabels(),
138
+ database_name: dbName,
139
+ max_connections: String(max)
140
+ }, current);
141
+ } catch (err) {
142
+ console.warn(`[database-metrics] Failed to collect: ${err.message}`);
143
+ }
144
+ }
145
+ };
146
+
147
+ /**
148
+ * Push database metrics to Prometheus Pushgateway
149
+ * @returns {Promise<void>}
150
+ */
151
+ pushDatabaseMetrics = async () => {
152
+ try {
153
+ await this.collectDatabaseMetrics();
154
+ await this.gatewayPush();
155
+ if (this.metricsLogValues) {
156
+ const metricObjects = await this.registry.getMetricsAsJSON();
157
+ console.info(`[database-metrics] Collected DB metrics`, JSON.stringify(metricObjects, null, 2));
158
+ }
159
+ } catch (error) {
160
+ console.error(`[database-metrics] Failed to collect DB metrics: ${error.message}`);
161
+ throw error;
162
+ }
163
+ };
164
+
165
+ /**
166
+ * Start periodic collection.
167
+ * @param {number} [intervalSec=this.intervalSec] - Interval in seconds
168
+ */
169
+ startPush = (intervalSec = this.intervalSec) => {
170
+ this._startPush(intervalSec, () => {
171
+ this.pushDatabaseMetrics().catch(err => {
172
+ console.error(`[database-metrics] Failed to push DB metrics:`, err);
173
+ });
174
+ });
175
+ };
176
+
177
+ /**
178
+ * Cleanup database pools and exit process
179
+ * @returns {Promise<void>}
180
+ */
181
+ cleanup = async () => {
182
+ try {
183
+ if (this.databasePools) {
184
+ for (const pool of this.databasePools) {
185
+ await pool.end();
186
+ }
187
+ }
188
+ } catch (err) {
189
+ console.error('[database-metrics] Error during cleanup:', err);
190
+ }
191
+ process.exit(0);
192
+ };
193
+ _setCleanupHandlers = () => {
194
+ process.on('SIGINT', this.cleanup);
195
+ process.on('SIGTERM', this.cleanup);
196
+ };
197
+ }
198
+ module.exports = {
199
+ DatabaseMetricsClient
200
+ };
201
+ //# sourceMappingURL=metricsDatabaseClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metricsDatabaseClient.js","names":["Pool","require","MetricsClient","DatabaseMetricsClient","constructor","databaseUrl","additional_database_urls","metricsConfig","intervalSec","parseInt","process","env","METRICS_DATABASE_INTERVAL_SEC","startupValidation","console","error","mainPool","connectionString","query","end","info","err","message","url","p","scripDefaultMetrics","processType","databasePools","push","databaseConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabels","_setCleanupHandlers","getDBConnectionsAndName","pool","currentRes","current","rows","maxRes","max","dbName","options","database","collectDatabaseMetrics","set","getDefaultLabels","database_name","max_connections","String","warn","pushDatabaseMetrics","gatewayPush","metricsLogValues","metricObjects","registry","getMetricsAsJSON","JSON","stringify","startPush","_startPush","catch","cleanup","exit","on","module","exports"],"sources":["../src/metricsDatabaseClient.js"],"sourcesContent":["const { Pool } = require('pg')\nconst { MetricsClient } = require('.')\n\n/**\n * DatabaseMetricsClient collects Postgres connection metrics\n * and pushes them to Prometheus Pushgateway.\n *\n * @extends MetricsClient\n */\nclass DatabaseMetricsClient extends MetricsClient {\n /**\n * @param {Object} options\n * @param {string} options.databaseUrl - Required main database URL\n * @param {string[]} [options.additional_database_urls] - Optional additional DB URLs\n * @param {string} [options.appName] - Application name (from MetricsClient)\n * @param {string} [options.dynoId] - Dyno/instance ID (from MetricsClient)\n * @param {string} [options.processType] - Process type (from MetricsClient)\n * @param {boolean} [options.enabled] - Enable metrics collection (from MetricsClient)\n * @param {boolean} [options.logValues] - Log metrics values (from MetricsClient)\n * @param {string} [options.pushgatewayUrl] - PushGateway URL (from MetricsClient)\n * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from MetricsClient)\n * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics\n * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service\n * @param {boolean} [options.scripDefaultMetrics] - Skip default metrics creation\n * @param {function} [options.startupValidation] - Function to validate startup\n */\n constructor({\n databaseUrl,\n additional_database_urls = [],\n ...metricsConfig\n } = {}) {\n const intervalSec =\n metricsConfig.intervalSec ||\n parseInt(process.env.METRICS_DATABASE_INTERVAL_SEC || '', 10) ||\n 60\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`)\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 url of 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: ${url}`)\n } catch (err) {\n console.error(\n `[database-metrics] ⚠ Skipping additional database: ${url}`\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 scripDefaultMetrics: true,\n processType: metricsConfig.processType || 'database-metrics',\n intervalSec,\n startupValidation,\n })\n\n this.databasePools = []\n\n if (databaseUrl) {\n this.databasePools.push(new Pool({ connectionString: databaseUrl }))\n }\n\n for (const url of additional_database_urls) {\n this.databasePools.push(new Pool({ connectionString: url }))\n }\n\n /** Gauge for Database connections */\n this.databaseConnectionsGauge = this.createGauge({\n name: 'app_database_connections',\n help: 'Postgres database connections',\n labelNames: this.withDefaultLabels(['max_connections', 'database_name']),\n })\n\n this._setCleanupHandlers()\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 const dbName = pool.options?.database || pool.options?.connectionString\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.getDefaultLabels(),\n database_name: dbName,\n max_connections: String(max),\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\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,CAA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B,OAAO,CAACwD,EAAE,CAAC,QAAQ,EAAE,IAAI,CAACF,OAAO,CAAC;IAClCtD,OAAO,CAACwD,EAAE,CAAC,SAAS,EAAE,IAAI,CAACF,OAAO,CAAC;EACrC,CAAC;AACH;AAEAG,MAAM,CAACC,OAAO,GAAG;EAAEjE;AAAsB,CAAC","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"metricsRedisClient.d.ts","sourceRoot":"","sources":["../src/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;QAChB,mBAAmB;QAClB,iBAAiB;OAyC9C;IA1BC,oCAAoC;IACpC,iBAA8B;IAC9B,wBAAsD;IAEtD,yCAAyC;IACzC,2DAIE;IAEF,mCAAmC;IACnC,sDAIE;IAEF,sCAAsC;IACtC,qDAIE;IAKJ,wCAwBC;IAED,6CAuBC;IAED,gDAeC;IAED;;;OAGG;IACH,2BAFa,QAAQ,IAAI,CAAC,CAoEzB;IAED;;;OAGG;IACH,wBAFa,QAAQ,IAAI,CAAC,CAoBzB;IAED;;;OAGG;IACH,sDAMC;CA4BF"}
1
+ {"version":3,"file":"metricsRedisClient.d.ts","sourceRoot":"","sources":["../src/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;QAChB,mBAAmB;QAClB,iBAAiB;OAyC9C;IA1BC,oCAAoC;IACpC,iBAA8B;IAC9B,wBAAsD;IAEtD,yCAAyC;IACzC,2DAIE;IAEF,mCAAmC;IACnC,sDAIE;IAEF,sCAAsC;IACtC,qDAIE;IAKJ,wCAwBC;IAED,6CAuBC;IAED,gDAeC;IAED;;;OAGG;IACH,2BAFa,QAAQ,IAAI,CAAC,CAgEzB;IAED;;;OAGG;IACH,wBAFa,QAAQ,IAAI,CAAC,CAoBzB;IAED;;;OAGG;IACH,sDAMC;CA4BF"}
@@ -10,7 +10,7 @@ const {
10
10
  REDIS_V3
11
11
  } = require('./redisUtils');
12
12
  const redisConnectionStableFields = ['id', 'name', 'flags', 'metric'];
13
- const redisConnectionFields = ['id', 'name', 'age', 'idle', 'flags', 'tot-mem'];
13
+ const redisConnectionFields = ['id', 'name', 'age', 'flags', 'tot-mem'];
14
14
 
15
15
  /**
16
16
  * RedisMetricsClient extends MetricsClient to collect
@@ -75,7 +75,7 @@ class RedisMetricsClient extends MetricsClient {
75
75
  getRedisConnections = async () => {
76
76
  if (!this.redisClient) throw new Error('Redis client not provided');
77
77
 
78
- // node-redis v3 MUST use callback to get the actual result
78
+ // node-redis v3 (uses callback)
79
79
  if (this.redisClientType === REDIS_V3) {
80
80
  return new Promise((resolve, reject) => {
81
81
  this.redisClient.send_command('CLIENT', ['LIST'], (err, result) => {
@@ -146,7 +146,6 @@ class RedisMetricsClient extends MetricsClient {
146
146
  id,
147
147
  name,
148
148
  age,
149
- idle,
150
149
  flags,
151
150
  'tot-mem': totMem
152
151
  } = connection;
@@ -160,10 +159,6 @@ class RedisMetricsClient extends MetricsClient {
160
159
  ...labelsForConnections,
161
160
  metric: 'age'
162
161
  }, parseInt(age, 10));
163
- this.redisConnectionsGauge.set({
164
- ...labelsForConnections,
165
- metric: 'idle'
166
- }, parseInt(idle, 10));
167
162
  this.redisConnectionsGauge.set({
168
163
  ...labelsForConnections,
169
164
  metric: 'tot-mem'
@@ -1 +1 @@
1
- {"version":3,"file":"metricsRedisClient.js","names":["MetricsClient","require","getRedisClientType","REDIS_V4","IOREDIS","REDIS_V3","redisConnectionStableFields","redisConnectionFields","RedisMetricsClient","constructor","redisClient","metricsConfig","intervalSec","parseInt","process","env","METRICS_QUEUE_INTERVAL_SEC","scripDefaultMetrics","processType","redisClientType","redisConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabels","redisMemoryGauge","redisStatsGauge","_setCleanupHandlers","getRedisConnections","Error","Promise","resolve","reject","send_command","err","result","message","sendCommand","getRedisInfo","section","info","parseRedisConnections","clientsStr","split","filter","line","trim","map","parts","client","forEach","p","k","v","includes","collectRedisMetrics","memoryInfoStr","statsInfoStr","connectionsInfoStr","all","labels","getDefaultLabels","connections","connection","id","age","idle","flags","totMem","labelsForConnections","set","metric","parseRedisInfo","infoStr","Object","fromEntries","startsWith","length","memory","stats","used_memory","memory_type","maxmemory","instantaneous_ops_per_sec","operation","error","console","warn","pushRedisMetrics","gatewayPush","metricsLogValues","metricObjects","registry","getMetricsAsJSON","JSON","stringify","startPush","_startPush","catch","cleanup","quit","disconnect","exit","on","module","exports"],"sources":["../src/metricsRedisClient.js"],"sourcesContent":["const { MetricsClient } = require('.')\nconst {\n getRedisClientType,\n REDIS_V4,\n IOREDIS,\n REDIS_V3,\n} = require('./redisUtils')\n\nconst redisConnectionStableFields = ['id', 'name', 'flags', 'metric']\nconst redisConnectionFields = ['id', 'name', 'age', 'idle', 'flags', 'tot-mem']\n\n/**\n * RedisMetricsClient extends MetricsClient to collect\n * Redis metrics periodically and push them to Prometheus Pushgateway.\n *\n * @extends MetricsClient\n */\nclass RedisMetricsClient extends MetricsClient {\n /**\n * @param {Object} options\n * @param {any} options.redisClient - Redis client instance (required)\n * @param {string} [options.appName] - Application name (from MetricsClient)\n * @param {string} [options.dynoId] - Dyno/instance ID (from MetricsClient)\n * @param {string} [options.processType] - Process type (from MetricsClient)\n * @param {boolean} [options.enabled] - Enable metrics collection (from MetricsClient)\n * @param {boolean} [options.logValues] - Log metrics values (from MetricsClient)\n * @param {string} [options.pushgatewayUrl] - PushGateway URL (from MetricsClient)\n * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from MetricsClient)\n * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics (from MetricsClient)\n * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service (from MetricsClient)\n * @param {boolean} [options.scripDefaultMetrics] - Skip default metrics creation (from MetricsClient)\n * @param {function} [options.startupValidation] - Function to validate startup (from MetricsClient)\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 scripDefaultMetrics: true,\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 /** Gauge for Redis client connections */\n this.redisConnectionsGauge = this.createGauge({\n name: 'app_redis_connections',\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 – MUST use callback to get the actual result\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 or ioredis – Promise API\n if (this.redisClientType === REDIS_V4 || this.redisClientType === IOREDIS) {\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 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 [k, v] = p.split('=')\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 connections.forEach(connection => {\n const { id, name, age, idle, flags, 'tot-mem': totMem } = connection\n const labelsForConnections = { ...labels, id, name, flags }\n this.redisConnectionsGauge.set(\n { ...labelsForConnections, metric: 'age' },\n parseInt(age, 10)\n )\n this.redisConnectionsGauge.set(\n { ...labelsForConnections, metric: 'idle' },\n parseInt(idle, 10)\n )\n this.redisConnectionsGauge.set(\n { ...labelsForConnections, metric: 'tot-mem' },\n parseInt(totMem, 10)\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\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;AAAc,CAAC,GAAGC,OAAO,CAAC,GAAG,CAAC;AACtC,MAAM;EACJC,kBAAkB;EAClBC,QAAQ;EACRC,OAAO;EACPC;AACF,CAAC,GAAGJ,OAAO,CAAC,cAAc,CAAC;AAE3B,MAAMK,2BAA2B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC;AACrE,MAAMC,qBAAqB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,CAAC;;AAE/E;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,kBAAkB,SAASR,aAAa,CAAC;EAC7C;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,mBAAmB,EAAE,IAAI;MACzBC,WAAW,EAAEP,aAAa,CAACO,WAAW,IAAI,eAAe;MACzDN;IACF,CAAC,CAAC;;IAEF;IACA,IAAI,CAACF,WAAW,GAAGA,WAAW;IAC9B,IAAI,CAACS,eAAe,GAAGjB,kBAAkB,CAACQ,WAAW,CAAC;;IAEtD;IACA,IAAI,CAACU,qBAAqB,GAAG,IAAI,CAACC,WAAW,CAAC;MAC5CC,IAAI,EAAE,uBAAuB;MAC7BC,IAAI,EAAE,0BAA0B;MAChCC,UAAU,EAAE,IAAI,CAACC,iBAAiB,CAACnB,2BAA2B;I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;;EAED;AACF;AACA;AACA;EACEiE,OAAO,GAAG,MAAAA,CAAA,KAAY;IACpB,IAAI;MACF,IAAI,CAAC,IAAI,CAAC1F,WAAW,EAAE;MAEvB,IACE,IAAI,CAACS,eAAe,KAAKd,QAAQ,IACjC,IAAI,CAACc,eAAe,KAAKhB,QAAQ,EACjC;QACA,MAAM,IAAI,CAACO,WAAW,CAAC2F,IAAI,CAAC,CAAC;MAC/B,CAAC,MAAM,IAAI,IAAI,CAAClF,eAAe,KAAKf,OAAO,EAAE;QAC3C,MAAM,IAAI,CAACM,WAAW,CAAC4F,UAAU,CAAC,CAAC;MACrC;IACF,CAAC,CAAC,OAAOnE,GAAG,EAAE;MACZoD,OAAO,CAACD,KAAK,CAAC,6CAA6C,EAAEnD,GAAG,CAAC;IACnE;IACArB,OAAO,CAACyF,IAAI,CAAC,CAAC,CAAC;EACjB,CAAC;EAED3E,mBAAmB,GAAGA,CAAA,KAAM;IAC1Bd,OAAO,CAAC0F,EAAE,CAAC,QAAQ,EAAE,IAAI,CAACJ,OAAO,CAAC;IAClCtF,OAAO,CAAC0F,EAAE,CAAC,SAAS,EAAE,IAAI,CAACJ,OAAO,CAAC;EACrC,CAAC;AACH;AAEAK,MAAM,CAACC,OAAO,GAAG;EAAElG;AAAmB,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"metricsRedisClient.js","names":["MetricsClient","require","getRedisClientType","REDIS_V4","IOREDIS","REDIS_V3","redisConnectionStableFields","redisConnectionFields","RedisMetricsClient","constructor","redisClient","metricsConfig","intervalSec","parseInt","process","env","METRICS_QUEUE_INTERVAL_SEC","scripDefaultMetrics","processType","redisClientType","redisConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabels","redisMemoryGauge","redisStatsGauge","_setCleanupHandlers","getRedisConnections","Error","Promise","resolve","reject","send_command","err","result","message","sendCommand","getRedisInfo","section","info","parseRedisConnections","clientsStr","split","filter","line","trim","map","parts","client","forEach","p","k","v","includes","collectRedisMetrics","memoryInfoStr","statsInfoStr","connectionsInfoStr","all","labels","getDefaultLabels","connections","connection","id","age","flags","totMem","labelsForConnections","set","metric","parseRedisInfo","infoStr","Object","fromEntries","startsWith","length","memory","stats","used_memory","memory_type","maxmemory","instantaneous_ops_per_sec","operation","error","console","warn","pushRedisMetrics","gatewayPush","metricsLogValues","metricObjects","registry","getMetricsAsJSON","JSON","stringify","startPush","_startPush","catch","cleanup","quit","disconnect","exit","on","module","exports"],"sources":["../src/metricsRedisClient.js"],"sourcesContent":["const { MetricsClient } = require('.')\nconst {\n getRedisClientType,\n REDIS_V4,\n IOREDIS,\n REDIS_V3,\n} = require('./redisUtils')\n\nconst redisConnectionStableFields = ['id', 'name', 'flags', 'metric']\nconst redisConnectionFields = ['id', 'name', 'age', 'flags', 'tot-mem']\n\n/**\n * RedisMetricsClient extends MetricsClient to collect\n * Redis metrics periodically and push them to Prometheus Pushgateway.\n *\n * @extends MetricsClient\n */\nclass RedisMetricsClient extends MetricsClient {\n /**\n * @param {Object} options\n * @param {any} options.redisClient - Redis client instance (required)\n * @param {string} [options.appName] - Application name (from MetricsClient)\n * @param {string} [options.dynoId] - Dyno/instance ID (from MetricsClient)\n * @param {string} [options.processType] - Process type (from MetricsClient)\n * @param {boolean} [options.enabled] - Enable metrics collection (from MetricsClient)\n * @param {boolean} [options.logValues] - Log metrics values (from MetricsClient)\n * @param {string} [options.pushgatewayUrl] - PushGateway URL (from MetricsClient)\n * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from MetricsClient)\n * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics (from MetricsClient)\n * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service (from MetricsClient)\n * @param {boolean} [options.scripDefaultMetrics] - Skip default metrics creation (from MetricsClient)\n * @param {function} [options.startupValidation] - Function to validate startup (from MetricsClient)\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 scripDefaultMetrics: true,\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 /** Gauge for Redis client connections */\n this.redisConnectionsGauge = this.createGauge({\n name: 'app_redis_connections',\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 or ioredis – Promise API\n if (this.redisClientType === REDIS_V4 || this.redisClientType === IOREDIS) {\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 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 [k, v] = p.split('=')\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 connections.forEach(connection => {\n const { id, name, age, flags, 'tot-mem': totMem } = connection\n const labelsForConnections = { ...labels, id, name, flags }\n this.redisConnectionsGauge.set(\n { ...labelsForConnections, metric: 'age' },\n parseInt(age, 10)\n )\n this.redisConnectionsGauge.set(\n { ...labelsForConnections, metric: 'tot-mem' },\n parseInt(totMem, 10)\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\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;AAAc,CAAC,GAAGC,OAAO,CAAC,GAAG,CAAC;AACtC,MAAM;EACJC,kBAAkB;EAClBC,QAAQ;EACRC,OAAO;EACPC;AACF,CAAC,GAAGJ,OAAO,CAAC,cAAc,CAAC;AAE3B,MAAMK,2BAA2B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC;AACrE,MAAMC,qBAAqB,GAAG,CAAC,IAAI,EAAE,MAAM,EAA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A,KAAM;IAC1Bd,OAAO,CAACyF,EAAE,CAAC,QAAQ,EAAE,IAAI,CAACJ,OAAO,CAAC;IAClCrF,OAAO,CAACyF,EAAE,CAAC,SAAS,EAAE,IAAI,CAACJ,OAAO,CAAC;EACrC,CAAC;AACH;AAEAK,MAAM,CAACC,OAAO,GAAG;EAAEjG;AAAmB,CAAC","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adalo/metrics",
3
- "version": "0.1.73",
3
+ "version": "0.1.75",
4
4
  "description": "Reusable metrics utilities for Node.js and Laravel apps",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -25,6 +25,7 @@
25
25
  "dependencies": {
26
26
  "bee-queue": "^1.2.2",
27
27
  "dotenv": "^8.2.0",
28
+ "pg": "^8.16.3",
28
29
  "prom-client": "^15.1.3"
29
30
  },
30
31
  "devDependencies": {
@@ -35,6 +36,7 @@
35
36
  "@types/ioredis": "^5.0.0",
36
37
  "@types/jest": "28.1.6",
37
38
  "@types/node": "18.0.4",
39
+ "@types/pg": "^8.15.6",
38
40
  "@types/redis": "2",
39
41
  "@typescript-eslint/eslint-plugin": "5.30.6",
40
42
  "@typescript-eslint/parser": "5.30.6",
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './metricsClient'
2
2
  export * from './metricsRedisClient'
3
3
  export * from './metricsQueueRedisClient'
4
+ export * from './metricsDatabaseClient'
4
5
  export * from './redisUtils'
@@ -0,0 +1,210 @@
1
+ const { Pool } = require('pg')
2
+ const { MetricsClient } = require('.')
3
+
4
+ /**
5
+ * DatabaseMetricsClient collects Postgres connection metrics
6
+ * and pushes them to Prometheus Pushgateway.
7
+ *
8
+ * @extends MetricsClient
9
+ */
10
+ class DatabaseMetricsClient extends MetricsClient {
11
+ /**
12
+ * @param {Object} options
13
+ * @param {string} options.databaseUrl - Required main database URL
14
+ * @param {string[]} [options.additional_database_urls] - Optional additional DB URLs
15
+ * @param {string} [options.appName] - Application name (from MetricsClient)
16
+ * @param {string} [options.dynoId] - Dyno/instance ID (from MetricsClient)
17
+ * @param {string} [options.processType] - Process type (from MetricsClient)
18
+ * @param {boolean} [options.enabled] - Enable metrics collection (from MetricsClient)
19
+ * @param {boolean} [options.logValues] - Log metrics values (from MetricsClient)
20
+ * @param {string} [options.pushgatewayUrl] - PushGateway URL (from MetricsClient)
21
+ * @param {string} [options.pushgatewaySecret] - PushGateway secret token (from MetricsClient)
22
+ * @param {number} [options.intervalSec] - Interval in seconds for pushing metrics
23
+ * @param {boolean} [options.removeOldMetrics] - Remove old metrics by service
24
+ * @param {boolean} [options.scripDefaultMetrics] - Skip default metrics creation
25
+ * @param {function} [options.startupValidation] - Function to validate startup
26
+ */
27
+ constructor({
28
+ databaseUrl,
29
+ additional_database_urls = [],
30
+ ...metricsConfig
31
+ } = {}) {
32
+ const intervalSec =
33
+ metricsConfig.intervalSec ||
34
+ parseInt(process.env.METRICS_DATABASE_INTERVAL_SEC || '', 10) ||
35
+ 60
36
+
37
+ const startupValidation = async () => {
38
+ if (!databaseUrl) {
39
+ console.error(`[database-metrics] ❌ METRICS_DATABASE_URL is required`)
40
+ return false
41
+ }
42
+
43
+ try {
44
+ const mainPool = new Pool({ connectionString: databaseUrl })
45
+ await mainPool.query('SELECT 1')
46
+ await mainPool.end()
47
+ console.info(`[database-metrics] ✓ Main database OK`)
48
+ } catch (err) {
49
+ console.error(
50
+ `[database-metrics] ❌ Cannot connect to main database: ${err.message}`
51
+ )
52
+ return false
53
+ }
54
+
55
+ for (const url of additional_database_urls) {
56
+ try {
57
+ const p = new Pool({ connectionString: url })
58
+ await p.query('SELECT 1')
59
+ await p.end()
60
+ console.info(`[database-metrics] ✓ Additional database OK: ${url}`)
61
+ } catch (err) {
62
+ console.error(
63
+ `[database-metrics] ⚠ Skipping additional database: ${url}`
64
+ )
65
+ console.error(`[database-metrics] ${err.message}`)
66
+ }
67
+ }
68
+
69
+ console.info(`[database-metrics] Database metrics collection starting`)
70
+ return true
71
+ }
72
+
73
+ super({
74
+ ...metricsConfig,
75
+ scripDefaultMetrics: true,
76
+ processType: metricsConfig.processType || 'database-metrics',
77
+ intervalSec,
78
+ startupValidation,
79
+ })
80
+
81
+ this.databasePools = []
82
+
83
+ if (databaseUrl) {
84
+ this.databasePools.push(new Pool({ connectionString: databaseUrl }))
85
+ }
86
+
87
+ for (const url of additional_database_urls) {
88
+ this.databasePools.push(new Pool({ connectionString: url }))
89
+ }
90
+
91
+ /** Gauge for Database connections */
92
+ this.databaseConnectionsGauge = this.createGauge({
93
+ name: 'app_database_connections',
94
+ help: 'Postgres database connections',
95
+ labelNames: this.withDefaultLabels(['max_connections', 'database_name']),
96
+ })
97
+
98
+ this._setCleanupHandlers()
99
+ }
100
+
101
+ /**
102
+ * @param {Pool} pool - PG connection pool
103
+ * @returns {Promise<{ current: number, max: number, dbName: string }>}
104
+ */
105
+ getDBConnectionsAndName = async pool => {
106
+ try {
107
+ const currentRes = await pool.query(
108
+ 'SELECT COUNT(*) AS current FROM pg_stat_activity WHERE datname = current_database()'
109
+ )
110
+ const current = parseInt(currentRes.rows[0]?.current || 0, 10)
111
+
112
+ const maxRes = await pool.query(
113
+ "SELECT current_setting('max_connections') AS max"
114
+ )
115
+ const max = parseInt(maxRes.rows[0]?.max || 0, 10)
116
+
117
+ const dbName = pool.options?.database || pool.options?.connectionString
118
+
119
+ return { current, max, dbName }
120
+ } catch (err) {
121
+ return { current: 0, max: 0, dbName: pool.options?.database || '' }
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Collect database connection metrics for all configured pools
127
+ * @returns {Promise<void>}
128
+ */
129
+ collectDatabaseMetrics = async () => {
130
+ for (const pool of this.databasePools) {
131
+ try {
132
+ const { current, max, dbName } = await this.getDBConnectionsAndName(
133
+ pool
134
+ )
135
+
136
+ this.databaseConnectionsGauge.set(
137
+ {
138
+ ...this.getDefaultLabels(),
139
+ database_name: dbName,
140
+ max_connections: String(max),
141
+ },
142
+ current
143
+ )
144
+ } catch (err) {
145
+ console.warn(`[database-metrics] Failed to collect: ${err.message}`)
146
+ }
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Push database metrics to Prometheus Pushgateway
152
+ * @returns {Promise<void>}
153
+ */
154
+ pushDatabaseMetrics = async () => {
155
+ try {
156
+ await this.collectDatabaseMetrics()
157
+ await this.gatewayPush()
158
+
159
+ if (this.metricsLogValues) {
160
+ const metricObjects = await this.registry.getMetricsAsJSON()
161
+ console.info(
162
+ `[database-metrics] Collected DB metrics`,
163
+ JSON.stringify(metricObjects, null, 2)
164
+ )
165
+ }
166
+ } catch (error) {
167
+ console.error(
168
+ `[database-metrics] Failed to collect DB metrics: ${error.message}`
169
+ )
170
+ throw error
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Start periodic collection.
176
+ * @param {number} [intervalSec=this.intervalSec] - Interval in seconds
177
+ */
178
+ startPush = (intervalSec = this.intervalSec) => {
179
+ this._startPush(intervalSec, () => {
180
+ this.pushDatabaseMetrics().catch(err => {
181
+ console.error(`[database-metrics] Failed to push DB metrics:`, err)
182
+ })
183
+ })
184
+ }
185
+
186
+ /**
187
+ * Cleanup database pools and exit process
188
+ * @returns {Promise<void>}
189
+ */
190
+ cleanup = async () => {
191
+ try {
192
+ if (this.databasePools) {
193
+ for (const pool of this.databasePools) {
194
+ await pool.end()
195
+ }
196
+ }
197
+ } catch (err) {
198
+ console.error('[database-metrics] Error during cleanup:', err)
199
+ }
200
+
201
+ process.exit(0)
202
+ }
203
+
204
+ _setCleanupHandlers = () => {
205
+ process.on('SIGINT', this.cleanup)
206
+ process.on('SIGTERM', this.cleanup)
207
+ }
208
+ }
209
+
210
+ module.exports = { DatabaseMetricsClient }
@@ -7,7 +7,7 @@ const {
7
7
  } = require('./redisUtils')
8
8
 
9
9
  const redisConnectionStableFields = ['id', 'name', 'flags', 'metric']
10
- const redisConnectionFields = ['id', 'name', 'age', 'idle', 'flags', 'tot-mem']
10
+ const redisConnectionFields = ['id', 'name', 'age', 'flags', 'tot-mem']
11
11
 
12
12
  /**
13
13
  * RedisMetricsClient extends MetricsClient to collect
@@ -75,7 +75,7 @@ class RedisMetricsClient extends MetricsClient {
75
75
  getRedisConnections = async () => {
76
76
  if (!this.redisClient) throw new Error('Redis client not provided')
77
77
 
78
- // node-redis v3 MUST use callback to get the actual result
78
+ // node-redis v3 (uses callback)
79
79
  if (this.redisClientType === REDIS_V3) {
80
80
  return new Promise((resolve, reject) => {
81
81
  this.redisClient.send_command('CLIENT', ['LIST'], (err, result) => {
@@ -157,16 +157,12 @@ class RedisMetricsClient extends MetricsClient {
157
157
 
158
158
  const connections = this.parseRedisConnections(connectionsInfoStr)
159
159
  connections.forEach(connection => {
160
- const { id, name, age, idle, flags, 'tot-mem': totMem } = connection
160
+ const { id, name, age, flags, 'tot-mem': totMem } = connection
161
161
  const labelsForConnections = { ...labels, id, name, flags }
162
162
  this.redisConnectionsGauge.set(
163
163
  { ...labelsForConnections, metric: 'age' },
164
164
  parseInt(age, 10)
165
165
  )
166
- this.redisConnectionsGauge.set(
167
- { ...labelsForConnections, metric: 'idle' },
168
- parseInt(idle, 10)
169
- )
170
166
  this.redisConnectionsGauge.set(
171
167
  { ...labelsForConnections, metric: 'tot-mem' },
172
168
  parseInt(totMem, 10)