@adalo/metrics 0.1.78 → 0.1.80
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.
|
@@ -1 +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,GAAG,KACD,QAAQ;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,
|
|
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,GAAG,KACD,QAAQ;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAkCrE;IAED;;;OAGG;IACH,8BAFa,QAAQ,IAAI,CAAC,CAqBzB;IAED;;;OAGG;IACH,2BAFa,QAAQ,IAAI,CAAC,CAoBzB;IAED;;;OAGG;IACH,sDAMC;CAwBF"}
|
|
@@ -106,7 +106,21 @@ class DatabaseMetricsClient extends MetricsClient {
|
|
|
106
106
|
const current = parseInt(currentRes.rows[0]?.current || 0, 10);
|
|
107
107
|
const maxRes = await pool.query("SELECT current_setting('max_connections') AS max");
|
|
108
108
|
const max = parseInt(maxRes.rows[0]?.max || 0, 10);
|
|
109
|
-
|
|
109
|
+
|
|
110
|
+
// Try .database first, then parse connectionString
|
|
111
|
+
let dbName = pool.options?.database;
|
|
112
|
+
console.log("getDBConnectionsAndName pool.options", pool.options);
|
|
113
|
+
console.log("getDBConnectionsAndName dbName", dbName);
|
|
114
|
+
if (!dbName && pool.options?.connectionString) {
|
|
115
|
+
console.log("getDBConnectionsAndName pool.options?.connectionString", pool.options?.connectionString);
|
|
116
|
+
try {
|
|
117
|
+
const url = new URL(pool.options.connectionString);
|
|
118
|
+
dbName = url.pathname.replace(/^\//, ''); // remove leading /
|
|
119
|
+
console.log("getDBConnectionsAndName dbName 2", dbName);
|
|
120
|
+
} catch {
|
|
121
|
+
dbName = pool.options.connectionString; // fallback
|
|
122
|
+
}
|
|
123
|
+
}
|
|
110
124
|
return {
|
|
111
125
|
current,
|
|
112
126
|
max,
|
|
@@ -1 +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 {any} 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,CAAC;AAC9B,MAAM;EAAEC;AAAc,CAAC,GAAGD,OAAO,CAAC,GAAG,CAAC;;AAEtC;AACA;AACA;AACA;AACA;AACA;AACA,MAAME,qBAAqB,SAASD,aAAa,CAAC;EAChD;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEE,WAAWA,CAAC;IACVC,WAAW;IACXC,wBAAwB,GAAG,EAAE;IAC7B,GAAGC;EACL,CAAC,GAAG,CAAC,CAAC,EAAE;IACN,MAAMC,WAAW,GACfD,aAAa,CAACC,WAAW,IACzBC,QAAQ,CAACC,OAAO,CAACC,GAAG,CAACC,6BAA6B,IAAI,EAAE,EAAE,E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KAAK,CAACO,OAAO,EACnE,CAAC;MACD,MAAMP,KAAK;IACb;EACF,CAAC;;EAED;AACF;AACA;AACA;EACE8C,SAAS,GAAGA,CAACrD,WAAW,GAAG,IAAI,CAACA,WAAW,KAAK;IAC9C,IAAI,CAACsD,UAAU,CAACtD,WAAW,EAAE,MAAM;MACjC,IAAI,CAAC6C,mBAAmB,CAAC,CAAC,CAACU,KAAK,CAAC1C,GAAG,IAAI;QACtCP,OAAO,CAACC,KAAK,CAAC,+CAA+C,EAAEM,GAAG,CAAC;MACrE,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ,CAAC;;EAED;AACF;AACA;AACA;EACE2C,OAAO,GAAG,MAAAA,CAAA,KAAY;IACpB,IAAI;MACF,IAAI,IAAI,CAACrC,aAAa,EAAE;QACtB,KAAK,MAAMU,IAAI,IAAI,IAAI,CAACV,aAAa,EAAE;UACrC,MAAMU,IAAI,CAAClB,GAAG,CAAC,CAAC;QAClB;MACF;IACF,CAAC,CAAC,OAAOE,GAAG,EAAE;MACZP,OAAO,CAACC,KAAK,CAAC,0CAA0C,EAAEM,GAAG,CAAC;IAChE;IAEAX,OAAO,CAACuD,IAAI,CAAC,CAAC,CAAC;EACjB,CAAC;EAED9B,mBAAmB,GAAGA,CAAA,KAAM;IAC1BzB,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
|
+
{"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","log","URL","pathname","replace","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 {any} 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 // Try .database first, then parse connectionString\n let dbName = pool.options?.database;\n console.log(\"getDBConnectionsAndName pool.options\", pool.options)\n console.log(\"getDBConnectionsAndName dbName\", dbName)\n if (!dbName && pool.options?.connectionString) {\n console.log(\"getDBConnectionsAndName pool.options?.connectionString\", pool.options?.connectionString)\n try {\n const url = new URL(pool.options.connectionString);\n dbName = url.pathname.replace(/^\\//, ''); // remove leading /\n console.log(\"getDBConnectionsAndName dbName 2\", dbName)\n } catch {\n dbName = pool.options.connectionString; // fallback\n }\n }\n\n\n return { current, max, dbName }\n } catch (err) {\n return { current: 0, max: 0, dbName: pool.options?.database || '' }\n }\n }\n\n /**\n * Collect database connection metrics for all configured pools\n * @returns {Promise<void>}\n */\n collectDatabaseMetrics = async () => {\n for (const pool of this.databasePools) {\n try {\n const { current, max, dbName } = await this.getDBConnectionsAndName(\n pool\n )\n\n this.databaseConnectionsGauge.set(\n {\n ...this.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;E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AACA,WAAW,KAAK;IAC9C,IAAI,CAAC0D,UAAU,CAAC1D,WAAW,EAAE,MAAM;MACjC,IAAI,CAACiD,mBAAmB,CAAC,CAAC,CAACU,KAAK,CAAC9C,GAAG,IAAI;QACtCP,OAAO,CAACC,KAAK,CAAC,+CAA+C,EAAEM,GAAG,CAAC;MACrE,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ,CAAC;;EAED;AACF;AACA;AACA;EACE+C,OAAO,GAAG,MAAAA,CAAA,KAAY;IACpB,IAAI;MACF,IAAI,IAAI,CAACzC,aAAa,EAAE;QACtB,KAAK,MAAMU,IAAI,IAAI,IAAI,CAACV,aAAa,EAAE;UACrC,MAAMU,IAAI,CAAClB,GAAG,CAAC,CAAC;QAClB;MACF;IACF,CAAC,CAAC,OAAOE,GAAG,EAAE;MACZP,OAAO,CAACC,KAAK,CAAC,0CAA0C,EAAEM,GAAG,CAAC;IAChE;IAEAX,OAAO,CAAC2D,IAAI,CAAC,CAAC,CAAC;EACjB,CAAC;EAEDlC,mBAAmB,GAAGA,CAAA,KAAM;IAC1BzB,OAAO,CAAC4D,EAAE,CAAC,QAAQ,EAAE,IAAI,CAACF,OAAO,CAAC;IAClC1D,OAAO,CAAC4D,EAAE,CAAC,SAAS,EAAE,IAAI,CAACF,OAAO,CAAC;EACrC,CAAC;AACH;AAEAG,MAAM,CAACC,OAAO,GAAG;EAAErE;AAAsB,CAAC","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adalo/metrics",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.80",
|
|
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",
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"bee-queue": "^1.2.2",
|
|
27
27
|
"dotenv": "^8.2.0",
|
|
28
28
|
"pg": "^8.16.3",
|
|
29
|
-
"prom-client": "^15.1.3"
|
|
29
|
+
"prom-client": "^15.1.3",
|
|
30
|
+
"@types/pg": "^8.15.6"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@babel/cli": "7.18.6",
|
|
@@ -36,7 +37,6 @@
|
|
|
36
37
|
"@types/ioredis": "^5.0.0",
|
|
37
38
|
"@types/jest": "28.1.6",
|
|
38
39
|
"@types/node": "18.0.4",
|
|
39
|
-
"@types/pg": "^8.15.6",
|
|
40
40
|
"@types/redis": "2",
|
|
41
41
|
"@typescript-eslint/eslint-plugin": "5.30.6",
|
|
42
42
|
"@typescript-eslint/parser": "5.30.6",
|
|
@@ -114,7 +114,21 @@ class DatabaseMetricsClient extends MetricsClient {
|
|
|
114
114
|
)
|
|
115
115
|
const max = parseInt(maxRes.rows[0]?.max || 0, 10)
|
|
116
116
|
|
|
117
|
-
|
|
117
|
+
// Try .database first, then parse connectionString
|
|
118
|
+
let dbName = pool.options?.database;
|
|
119
|
+
console.log("getDBConnectionsAndName pool.options", pool.options)
|
|
120
|
+
console.log("getDBConnectionsAndName dbName", dbName)
|
|
121
|
+
if (!dbName && pool.options?.connectionString) {
|
|
122
|
+
console.log("getDBConnectionsAndName pool.options?.connectionString", pool.options?.connectionString)
|
|
123
|
+
try {
|
|
124
|
+
const url = new URL(pool.options.connectionString);
|
|
125
|
+
dbName = url.pathname.replace(/^\//, ''); // remove leading /
|
|
126
|
+
console.log("getDBConnectionsAndName dbName 2", dbName)
|
|
127
|
+
} catch {
|
|
128
|
+
dbName = pool.options.connectionString; // fallback
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
118
132
|
|
|
119
133
|
return { current, max, dbName }
|
|
120
134
|
} catch (err) {
|