@adalo/metrics 0.1.62 → 0.1.64
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/metricsRedisClient.d.ts.map +1 -1
- package/lib/metricsRedisClient.js +20 -10
- package/lib/metricsRedisClient.js.map +1 -1
- package/lib/redisUtils.d.ts +13 -11
- package/lib/redisUtils.d.ts.map +1 -1
- package/lib/redisUtils.js +10 -8
- package/lib/redisUtils.js.map +1 -1
- package/package.json +1 -1
- package/src/metricsRedisClient.js +26 -14
- package/src/redisUtils.js +10 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsRedisClient.d.ts","sourceRoot":"","sources":["../src/metricsRedisClient.js"],"names":[],"mappings":"AAEA;;;;;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;OAwC9C;IAzBC,oCAAoC;IACpC,iBAA8B;IAE9B,yCAAyC;IACzC,2DAIE;IAEF,mCAAmC;IACnC,sDAIE;IAEF,sCAAsC;IACtC,qDAIE;IAKJ,
|
|
1
|
+
{"version":3,"file":"metricsRedisClient.d.ts","sourceRoot":"","sources":["../src/metricsRedisClient.js"],"names":[],"mappings":"AAEA;;;;;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;OAwC9C;IAzBC,oCAAoC;IACpC,iBAA8B;IAE9B,yCAAyC;IACzC,2DAIE;IAEF,mCAAmC;IACnC,sDAIE;IAEF,sCAAsC;IACtC,qDAIE;IAKJ,6CA6BE;IAEF;;;OAGG;IACH,2BAFa,QAAQ,IAAI,CAAC,CAyDzB;IAED;;;OAGG;IACH,wBAFa,QAAQ,IAAI,CAAC,CAoBzB;IAED;;;OAGG;IACH,sDAMC;CA2BF"}
|
|
@@ -65,18 +65,28 @@ class RedisMetricsClient extends MetricsClient {
|
|
|
65
65
|
}
|
|
66
66
|
getRedisInfo = async section => {
|
|
67
67
|
if (!this.redisClient) throw new Error('Redis client not provided');
|
|
68
|
-
console.log('constructor name:
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
68
|
+
console.log('Redis client constructor name:', this.redisClient.constructor.name);
|
|
69
|
+
console.log('Redis client info function:', this.redisClient.info);
|
|
70
|
+
|
|
71
|
+
// node-redis v3 (uses callback)
|
|
72
|
+
if (typeof this.redisClient.send_command === 'function') {
|
|
73
|
+
console.log('Detected node-redis v3');
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
this.redisClient.info(section, (err, result) => {
|
|
76
|
+
if (err) reject(err);else resolve(result);
|
|
76
77
|
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// node-redis v4 or ioredis (info returns Promise)
|
|
82
|
+
if (typeof this.redisClient.info === 'function') {
|
|
83
|
+
console.log('Detected node-redis v4 or ioredis');
|
|
84
|
+
try {
|
|
85
|
+
const result = await this.redisClient.info(section);
|
|
86
|
+
return result;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
throw new Error(`Failed to get Redis INFO: ${err.message}`);
|
|
77
89
|
}
|
|
78
|
-
// node-redis v4 or ioredis: returns Promise
|
|
79
|
-
return this.redisClient.info(section);
|
|
80
90
|
}
|
|
81
91
|
throw new Error('Unsupported Redis client type');
|
|
82
92
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsRedisClient.js","names":["MetricsClient","require","RedisMetricsClient","constructor","redisClient","metricsConfig","intervalSec","parseInt","process","env","METRICS_QUEUE_INTERVAL_SEC","scripDefaultMetrics","processType","redisConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabels","redisMemoryGauge","redisStatsGauge","_setCleanupHandlers","getRedisInfo","section","Error","console","log","info","length","Promise","resolve","reject","err","result","collectRedisMetrics","clientsInfo","memoryInfo","statsInfo","all","labels","getDefaultLabels","parseRedisInfo","infoStr","Object","fromEntries","split","filter","line","startsWith","map","parts","clients","memory","stats","connected_clients","set","connection_type","used_memory","memory_type","maxmemory","instantaneous_ops_per_sec","operation","error","warn","message","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('.')\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 || 'redis-metrics',\n intervalSec,\n })\n\n /** Redis client used for metrics */\n this.redisClient = redisClient\n\n /** Gauge for Redis client connections */\n this.redisConnectionsGauge = this.createGauge({\n name: 'app_redis_connections_count',\n help: 'Number of Redis client connections',\n labelNames: this.withDefaultLabels(['connection_type']),\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 getRedisInfo = async section => {\n if (!this.redisClient) throw new Error('Redis client not provided')\n console.log('constructor name: ', this.redisClient.constructor.name) // :todo delete\n if (typeof this.redisClient.info === 'function') {\n if (this.redisClient.info.length === 1) {\n // node-redis v3: uses callback\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 // node-redis v4 or ioredis: returns Promise\n return this.redisClient.info(section)\n }\n throw new Error('Unsupported Redis client type')\n }\n\n /**\n * Collect basic Redis INFO metrics: clients, memory, stats\n * @returns {Promise<void>}\n */\n collectRedisMetrics = async () => {\n try {\n const [clientsInfo, memoryInfo, statsInfo] = await Promise.all([\n this.getRedisInfo('clients'),\n this.getRedisInfo('memory'),\n this.getRedisInfo('stats'),\n ])\n\n const labels = this.getDefaultLabels()\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 clients = parseRedisInfo(clientsInfo)\n const memory = parseRedisInfo(memoryInfo)\n const stats = parseRedisInfo(statsInfo)\n\n if (clients.connected_clients) {\n this.redisConnectionsGauge.set(\n { ...labels, connection_type: 'connected' },\n parseInt(clients.connected_clients, 10) || 0\n )\n }\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 `[redis-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 `[redis-metrics] Collected metrics for Redis`,\n JSON.stringify(metricObjects, null, 2)\n )\n }\n } catch (error) {\n console.error(\n `[redis-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(`[redis-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 (typeof this.redisClient.quit === 'function') {\n // node-redis v3 or v4\n await this.redisClient.quit()\n } else if (typeof this.redisClient.disconnect === 'function') {\n // ioredis\n await this.redisClient.disconnect()\n }\n } catch (err) {\n console.error('[redis-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;;AAEtC;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,kBAAkB,SAASF,aAAa,CAAC;EAC7C;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEG,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;;IAE9B;IACA,IAAI,CAACS,qBAAqB,GAAG,IAAI,CAACC,WAAW,CAAC;MAC5CC,IAAI,EAAE,6BAA6B;MACnCC,IAAI,EAAE,oCAAoC;MAC1CC,UAAU,EAAE,IAAI,CAACC,iBAAiB,CAAC,CAAC,iBAAiB,CAAC;IACxD,CAAC,CAAC;;IAEF;IACA,IAAI,CAACC,gBAAgB,GAAG,IAAI,CAACL,WAAW,CAAC;MACvCC,IAAI,EAAE,wBAAwB;MAC9BC,IAAI,EAAE,6BAA6B;MACnCC,UAAU,EAAE,IAAI,CAACC,iBAAiB,CAAC,CAAC,aAAa,CAAC;IACpD,CAAC,CAAC;;IAEF;IACA,IAAI,CAACE,eAAe,GAAG,IAAI,CAACN,WAAW,CAAC;MAC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KAAK,CAAC,+CAA+C,EAAE9B,GAAG,CAAC;MACrE,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ,CAAC;;EAED;AACF;AACA;AACA;EACE4C,OAAO,GAAG,MAAAA,CAAA,KAAY;IACpB,IAAI;MACF,IAAI,CAAC,IAAI,CAACxE,WAAW,EAAE;MAEvB,IAAI,OAAO,IAAI,CAACA,WAAW,CAACyE,IAAI,KAAK,UAAU,EAAE;QAC/C;QACA,MAAM,IAAI,CAACzE,WAAW,CAACyE,IAAI,CAAC,CAAC;MAC/B,CAAC,MAAM,IAAI,OAAO,IAAI,CAACzE,WAAW,CAAC0E,UAAU,KAAK,UAAU,EAAE;QAC5D;QACA,MAAM,IAAI,CAAC1E,WAAW,CAAC0E,UAAU,CAAC,CAAC;MACrC;IACF,CAAC,CAAC,OAAO9C,GAAG,EAAE;MACZP,OAAO,CAACqC,KAAK,CAAC,6CAA6C,EAAE9B,GAAG,CAAC;IACnE;IACAxB,OAAO,CAACuE,IAAI,CAAC,CAAC,CAAC;EACjB,CAAC;EAED1D,mBAAmB,GAAGA,CAAA,KAAM;IAC1Bb,OAAO,CAACwE,EAAE,CAAC,QAAQ,EAAE,IAAI,CAACJ,OAAO,CAAC;IAClCpE,OAAO,CAACwE,EAAE,CAAC,SAAS,EAAE,IAAI,CAACJ,OAAO,CAAC;EACrC,CAAC;AACH;AAEAK,MAAM,CAACC,OAAO,GAAG;EAAEhF;AAAmB,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"metricsRedisClient.js","names":["MetricsClient","require","RedisMetricsClient","constructor","redisClient","metricsConfig","intervalSec","parseInt","process","env","METRICS_QUEUE_INTERVAL_SEC","scripDefaultMetrics","processType","redisConnectionsGauge","createGauge","name","help","labelNames","withDefaultLabels","redisMemoryGauge","redisStatsGauge","_setCleanupHandlers","getRedisInfo","section","Error","console","log","info","send_command","Promise","resolve","reject","err","result","message","collectRedisMetrics","clientsInfo","memoryInfo","statsInfo","all","labels","getDefaultLabels","parseRedisInfo","infoStr","Object","fromEntries","split","filter","line","startsWith","map","parts","length","clients","memory","stats","connected_clients","set","connection_type","used_memory","memory_type","maxmemory","instantaneous_ops_per_sec","operation","error","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('.')\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 || 'redis-metrics',\n intervalSec,\n })\n\n /** Redis client used for metrics */\n this.redisClient = redisClient\n\n /** Gauge for Redis client connections */\n this.redisConnectionsGauge = this.createGauge({\n name: 'app_redis_connections_count',\n help: 'Number of Redis client connections',\n labelNames: this.withDefaultLabels(['connection_type']),\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 getRedisInfo = async section => {\n if (!this.redisClient) throw new Error('Redis client not provided');\n\n console.log('Redis client constructor name:', this.redisClient.constructor.name);\n console.log('Redis client info function:', this.redisClient.info);\n\n // node-redis v3 (uses callback)\n if (typeof this.redisClient.send_command === 'function') {\n console.log('Detected node-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 (typeof this.redisClient.info === 'function') {\n console.log('Detected node-redis v4 or ioredis');\n try {\n const result = await this.redisClient.info(section);\n return result;\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 /**\n * Collect basic Redis INFO metrics: clients, memory, stats\n * @returns {Promise<void>}\n */\n collectRedisMetrics = async () => {\n try {\n const [clientsInfo, memoryInfo, statsInfo] = await Promise.all([\n this.getRedisInfo('clients'),\n this.getRedisInfo('memory'),\n this.getRedisInfo('stats'),\n ])\n\n const labels = this.getDefaultLabels()\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 clients = parseRedisInfo(clientsInfo)\n const memory = parseRedisInfo(memoryInfo)\n const stats = parseRedisInfo(statsInfo)\n\n if (clients.connected_clients) {\n this.redisConnectionsGauge.set(\n { ...labels, connection_type: 'connected' },\n parseInt(clients.connected_clients, 10) || 0\n )\n }\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 `[redis-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 `[redis-metrics] Collected metrics for Redis`,\n JSON.stringify(metricObjects, null, 2)\n )\n }\n } catch (error) {\n console.error(\n `[redis-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(`[redis-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 (typeof this.redisClient.quit === 'function') {\n // node-redis v3 or v4\n await this.redisClient.quit()\n } else if (typeof this.redisClient.disconnect === 'function') {\n // ioredis\n await this.redisClient.disconnect()\n }\n } catch (err) {\n console.error('[redis-metrics] Error closing Redis client:', err)\n }\n process.exit(0)\n }\n\n _setCleanupHandlers = () => {\n process.on('SIGINT', this.cleanup)\n process.on('SIGTER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
|
/**
|
|
2
|
-
* Creates a configured setClientName function.
|
|
2
|
+
* Creates a configured setClientName function for node-redis v3.
|
|
3
3
|
*
|
|
4
4
|
* @param {string} [defaultAppName='undefined-app'] - Fallback app name if METRICS_APP_NAME is not set.
|
|
5
|
-
* @returns {(client:
|
|
5
|
+
* @returns {(client: any, name: string) => void}
|
|
6
6
|
*/
|
|
7
|
-
export function createSetClientNameForRedisV3(defaultAppName?: string | undefined): (client:
|
|
7
|
+
export function createSetClientNameForRedisV3(defaultAppName?: string | undefined): (client: any, name: string) => void;
|
|
8
8
|
/**
|
|
9
|
-
* Creates a function to set Redis client name for node-redis v4
|
|
10
|
-
*
|
|
11
|
-
* @
|
|
9
|
+
* Creates a function to set Redis client name for node-redis v4.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} [defaultAppName='undefined-app']
|
|
12
|
+
* @returns {(client: any, name: string) => void}
|
|
12
13
|
*/
|
|
13
|
-
export function createSetClientNameForRedisV4(defaultAppName?: string): (client:
|
|
14
|
+
export function createSetClientNameForRedisV4(defaultAppName?: string | undefined): (client: any, name: string) => void;
|
|
14
15
|
/**
|
|
15
|
-
* Creates a function to set Redis client name for ioredis
|
|
16
|
-
*
|
|
17
|
-
* @
|
|
16
|
+
* Creates a function to set Redis client name for ioredis.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} [defaultAppName='undefined-app']
|
|
19
|
+
* @returns {(client: any, name: string) => void}
|
|
18
20
|
*/
|
|
19
|
-
export function createSetClientNameForIoredis(defaultAppName?: string): (client:
|
|
21
|
+
export function createSetClientNameForIoredis(defaultAppName?: string | undefined): (client: any, name: string) => void;
|
|
20
22
|
//# sourceMappingURL=redisUtils.d.ts.map
|
package/lib/redisUtils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redisUtils.d.ts","sourceRoot":"","sources":["../src/redisUtils.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,6FAFsB,
|
|
1
|
+
{"version":3,"file":"redisUtils.d.ts","sourceRoot":"","sources":["../src/redisUtils.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,6FAFsB,GAAG,QAAQ,MAAM,KAAK,IAAI,CAuB/C;AA4BD;;;;;GAKG;AACH,6FAFsB,GAAG,QAAQ,MAAM,KAAK,IAAI,CAsB/C;AApDD;;;;;GAKG;AACH,6FAFsB,GAAG,QAAQ,MAAM,KAAK,IAAI,CAoB/C"}
|
package/lib/redisUtils.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Creates a configured setClientName function.
|
|
4
|
+
* Creates a configured setClientName function for node-redis v3.
|
|
5
5
|
*
|
|
6
6
|
* @param {string} [defaultAppName='undefined-app'] - Fallback app name if METRICS_APP_NAME is not set.
|
|
7
|
-
* @returns {(client:
|
|
7
|
+
* @returns {(client: any, name: string) => void}
|
|
8
8
|
*/
|
|
9
9
|
const createSetClientNameForRedisV3 = (defaultAppName = 'undefined-app') => {
|
|
10
10
|
return (client, name) => {
|
|
@@ -25,9 +25,10 @@ const createSetClientNameForRedisV3 = (defaultAppName = 'undefined-app') => {
|
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* Creates a function to set Redis client name for ioredis
|
|
29
|
-
*
|
|
30
|
-
* @
|
|
28
|
+
* Creates a function to set Redis client name for ioredis.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} [defaultAppName='undefined-app']
|
|
31
|
+
* @returns {(client: any, name: string) => void}
|
|
31
32
|
*/
|
|
32
33
|
const createSetClientNameForIoredis = (defaultAppName = 'undefined-app') => {
|
|
33
34
|
return (client, name) => {
|
|
@@ -46,9 +47,10 @@ const createSetClientNameForIoredis = (defaultAppName = 'undefined-app') => {
|
|
|
46
47
|
};
|
|
47
48
|
|
|
48
49
|
/**
|
|
49
|
-
* Creates a function to set Redis client name for node-redis v4
|
|
50
|
-
*
|
|
51
|
-
* @
|
|
50
|
+
* Creates a function to set Redis client name for node-redis v4.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} [defaultAppName='undefined-app']
|
|
53
|
+
* @returns {(client: any, name: string) => void}
|
|
52
54
|
*/
|
|
53
55
|
const createSetClientNameForRedisV4 = (defaultAppName = 'undefined-app') => {
|
|
54
56
|
return (client, name) => {
|
package/lib/redisUtils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redisUtils.js","names":["createSetClientNameForRedisV3","defaultAppName","client","name","appName","process","env","METRICS_APP_NAME","dyno","BUILD_DYNO_PROCESS_TYPE","NODE_ENV","on","send_command","err","console","error","log","pid","createSetClientNameForIoredis","once","call","then","catch","createSetClientNameForRedisV4","sendCommand","module","exports"],"sources":["../src/redisUtils.js"],"sourcesContent":["/**\n * Creates a configured setClientName function.\n *\n * @param {string} [defaultAppName='undefined-app'] - Fallback app name if METRICS_APP_NAME is not set.\n * @returns {(client:
|
|
1
|
+
{"version":3,"file":"redisUtils.js","names":["createSetClientNameForRedisV3","defaultAppName","client","name","appName","process","env","METRICS_APP_NAME","dyno","BUILD_DYNO_PROCESS_TYPE","NODE_ENV","on","send_command","err","console","error","log","pid","createSetClientNameForIoredis","once","call","then","catch","createSetClientNameForRedisV4","sendCommand","module","exports"],"sources":["../src/redisUtils.js"],"sourcesContent":["/**\n * Creates a configured setClientName function for node-redis v3.\n *\n * @param {string} [defaultAppName='undefined-app'] - Fallback app name if METRICS_APP_NAME is not set.\n * @returns {(client: any, name: string) => void}\n */\nconst createSetClientNameForRedisV3 = (defaultAppName = 'undefined-app') => {\n return (client, name) => {\n const appName = process.env.METRICS_APP_NAME || defaultAppName\n const dyno = process.env.BUILD_DYNO_PROCESS_TYPE || 'undefined-dyno'\n\n if (process.env.NODE_ENV !== 'test') {\n client.on('connect', () => {\n client.send_command(\n 'CLIENT',\n ['SETNAME', `${appName}:${dyno}:${name}`],\n err => {\n if (err) {\n console.error(`Failed to set client name for ${name}:`, err)\n } else {\n console.log(`Connected to Redis for pid:${process.pid} (${name})`)\n }\n }\n )\n })\n }\n }\n}\n\n/**\n * Creates a function to set Redis client name for ioredis.\n *\n * @param {string} [defaultAppName='undefined-app']\n * @returns {(client: any, name: string) => void}\n */\nconst createSetClientNameForIoredis = (defaultAppName = 'undefined-app') => {\n return (client, name) => {\n const appName = process.env.METRICS_APP_NAME || defaultAppName\n const dyno = process.env.BUILD_DYNO_PROCESS_TYPE || 'undefined-dyno'\n\n if (process.env.NODE_ENV !== 'test') {\n client.once('connect', () => {\n client\n .call('CLIENT', ['SETNAME', `${appName}:${dyno}:${name}`])\n .then(() => {\n console.log(`Connected to Redis for pid:${process.pid} (${name})`)\n })\n .catch(err => {\n console.error(`Failed to set client name for ${name}:`, err)\n })\n })\n }\n }\n}\n\n/**\n * Creates a function to set Redis client name for node-redis v4.\n *\n * @param {string} [defaultAppName='undefined-app']\n * @returns {(client: any, name: string) => void}\n */\nconst createSetClientNameForRedisV4 = (defaultAppName = 'undefined-app') => {\n return (client, name) => {\n const appName = process.env.METRICS_APP_NAME || defaultAppName\n const dyno = process.env.BUILD_DYNO_PROCESS_TYPE || 'undefined-dyno'\n\n if (process.env.NODE_ENV !== 'test') {\n client.on('ready', async () => {\n try {\n await client.sendCommand([\n 'CLIENT',\n 'SETNAME',\n `${appName}:${dyno}:${name}`,\n ])\n console.log(`Connected to Redis for pid:${process.pid} (${name})`)\n } catch (err) {\n console.error(`Failed to set client name for ${name}:`, err)\n }\n })\n }\n }\n}\n\nmodule.exports = {\n createSetClientNameForRedisV3,\n createSetClientNameForRedisV4,\n createSetClientNameForIoredis,\n}\n"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,6BAA6B,GAAGA,CAACC,cAAc,GAAG,eAAe,KAAK;EAC1E,OAAO,CAACC,MAAM,EAAEC,IAAI,KAAK;IACvB,MAAMC,OAAO,GAAGC,OAAO,CAACC,GAAG,CAACC,gBAAgB,IAAIN,cAAc;IAC9D,MAAMO,I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ACT,GAAG,IAAI;UACZC,OAAO,CAACC,KAAK,CAAC,iCAAiCZ,IAAI,GAAG,EAAEU,GAAG,CAAC;QAC9D,CAAC,CAAC;MACN,CAAC,CAAC;IACJ;EACF,CAAC;AACH,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA,MAAMU,6BAA6B,GAAGA,CAACtB,cAAc,GAAG,eAAe,KAAK;EAC1E,OAAO,CAACC,MAAM,EAAEC,IAAI,KAAK;IACvB,MAAMC,OAAO,GAAGC,OAAO,CAACC,GAAG,CAACC,gBAAgB,IAAIN,cAAc;IAC9D,MAAMO,IAAI,GAAGH,OAAO,CAACC,GAAG,CAACG,uBAAuB,IAAI,gBAAgB;IAEpE,IAAIJ,OAAO,CAACC,GAAG,CAACI,QAAQ,KAAK,MAAM,EAAE;MACnCR,MAAM,CAACS,EAAE,CAAC,OAAO,EAAE,YAAY;QAC7B,IAAI;UACF,MAAMT,MAAM,CAACsB,WAAW,CAAC,CACvB,QAAQ,EACR,SAAS,EACT,GAAGpB,OAAO,IAAII,IAAI,IAAIL,IAAI,EAAE,CAC7B,CAAC;UACFW,OAAO,CAACE,GAAG,CAAC,8BAA8BX,OAAO,CAACY,GAAG,KAAKd,IAAI,GAAG,CAAC;QACpE,CAAC,CAAC,OAAOU,GAAG,EAAE;UACZC,OAAO,CAACC,KAAK,CAAC,iCAAiCZ,IAAI,GAAG,EAAEU,GAAG,CAAC;QAC9D;MACF,CAAC,CAAC;IACJ;EACF,CAAC;AACH,CAAC;AAEDY,MAAM,CAACC,OAAO,GAAG;EACf1B,6BAA6B;EAC7BuB,6BAA6B;EAC7BL;AACF,CAAC","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -63,23 +63,35 @@ class RedisMetricsClient extends MetricsClient {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
getRedisInfo = async section => {
|
|
66
|
-
if (!this.redisClient) throw new Error('Redis client not provided')
|
|
67
|
-
|
|
66
|
+
if (!this.redisClient) throw new Error('Redis client not provided');
|
|
67
|
+
|
|
68
|
+
console.log('Redis client constructor name:', this.redisClient.constructor.name);
|
|
69
|
+
console.log('Redis client info function:', this.redisClient.info);
|
|
70
|
+
|
|
71
|
+
// node-redis v3 (uses callback)
|
|
72
|
+
if (typeof this.redisClient.send_command === 'function') {
|
|
73
|
+
console.log('Detected node-redis v3');
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
this.redisClient.info(section, (err, result) => {
|
|
76
|
+
if (err) reject(err);
|
|
77
|
+
else resolve(result);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// node-redis v4 or ioredis (info returns Promise)
|
|
68
83
|
if (typeof this.redisClient.info === 'function') {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})
|
|
76
|
-
})
|
|
84
|
+
console.log('Detected node-redis v4 or ioredis');
|
|
85
|
+
try {
|
|
86
|
+
const result = await this.redisClient.info(section);
|
|
87
|
+
return result;
|
|
88
|
+
} catch (err) {
|
|
89
|
+
throw new Error(`Failed to get Redis INFO: ${err.message}`);
|
|
77
90
|
}
|
|
78
|
-
// node-redis v4 or ioredis: returns Promise
|
|
79
|
-
return this.redisClient.info(section)
|
|
80
91
|
}
|
|
81
|
-
|
|
82
|
-
|
|
92
|
+
|
|
93
|
+
throw new Error('Unsupported Redis client type');
|
|
94
|
+
};
|
|
83
95
|
|
|
84
96
|
/**
|
|
85
97
|
* Collect basic Redis INFO metrics: clients, memory, stats
|
package/src/redisUtils.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Creates a configured setClientName function.
|
|
2
|
+
* Creates a configured setClientName function for node-redis v3.
|
|
3
3
|
*
|
|
4
4
|
* @param {string} [defaultAppName='undefined-app'] - Fallback app name if METRICS_APP_NAME is not set.
|
|
5
|
-
* @returns {(client:
|
|
5
|
+
* @returns {(client: any, name: string) => void}
|
|
6
6
|
*/
|
|
7
7
|
const createSetClientNameForRedisV3 = (defaultAppName = 'undefined-app') => {
|
|
8
8
|
return (client, name) => {
|
|
@@ -28,9 +28,10 @@ const createSetClientNameForRedisV3 = (defaultAppName = 'undefined-app') => {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
* Creates a function to set Redis client name for ioredis
|
|
32
|
-
*
|
|
33
|
-
* @
|
|
31
|
+
* Creates a function to set Redis client name for ioredis.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} [defaultAppName='undefined-app']
|
|
34
|
+
* @returns {(client: any, name: string) => void}
|
|
34
35
|
*/
|
|
35
36
|
const createSetClientNameForIoredis = (defaultAppName = 'undefined-app') => {
|
|
36
37
|
return (client, name) => {
|
|
@@ -53,9 +54,10 @@ const createSetClientNameForIoredis = (defaultAppName = 'undefined-app') => {
|
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
/**
|
|
56
|
-
* Creates a function to set Redis client name for node-redis v4
|
|
57
|
-
*
|
|
58
|
-
* @
|
|
57
|
+
* Creates a function to set Redis client name for node-redis v4.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} [defaultAppName='undefined-app']
|
|
60
|
+
* @returns {(client: any, name: string) => void}
|
|
59
61
|
*/
|
|
60
62
|
const createSetClientNameForRedisV4 = (defaultAppName = 'undefined-app') => {
|
|
61
63
|
return (client, name) => {
|