@adalo/metrics 0.1.165 → 0.1.167

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.
@@ -9,9 +9,6 @@ const {
9
9
  const redisConnectionStableFields = ['name', 'flags', 'cmd']
10
10
  const redisConnectionFields = ['name', 'flags', 'tot-mem', 'cmd']
11
11
 
12
- /** Stream entries older than this (ms) are trimmed so messages are not kept in Redis. Fixed 60s. */
13
- const GRACEFUL_SHUTDOWN_STREAM_MAXAGE_MS = 60000
14
-
15
12
  /**
16
13
  * RedisMetricsClient extends BaseMetricsClient to collect
17
14
  * Redis metrics periodically and push them to Prometheus Pushgateway.
@@ -21,8 +18,7 @@ const GRACEFUL_SHUTDOWN_STREAM_MAXAGE_MS = 60000
21
18
  class RedisMetricsClient extends BaseMetricsClient {
22
19
  /**
23
20
  * @param {Object} options
24
- * @param {any} options.redisClient - Redis client instance (required). Used for metrics and publish. Subscriber is created internally (duplicate() or REDIS_URL).
25
- * @param {boolean} [options.gracefulShutdownRedis] - Default true. When true, new instance publishes on start and old instances exit on message. Set false or METRICS_GRACEFUL_SHUTDOWN_REDIS=false to disable.
21
+ * @param {any} options.redisClient - Redis client instance (required)
26
22
  * @param {string} [options.appName] - Application name (from BaseMetricsClient)
27
23
  * @param {string} [options.dynoId] - Dyno/instance ID (from BaseMetricsClient)
28
24
  * @param {string} [options.processType] - Process type (from BaseMetricsClient)
@@ -55,36 +51,34 @@ class RedisMetricsClient extends BaseMetricsClient {
55
51
  this.redisClient = redisClient
56
52
  this.redisClientType = getRedisClientType(redisClient)
57
53
 
58
- /** Dedicated Redis connection for subscribe (subscriber mode); closed in cleanup. */
59
- this._subClient = null
60
-
61
- this._initGracefulShutdown(metricsConfig.gracefulShutdownRedis)
54
+ /** Label names for Redis metrics: app + process_type only (no dyno_id). */
55
+ this._redisLabelNames = ['app', 'process_type']
62
56
 
63
- /** Counter for Redis client connections */
57
+ /** Counter for Redis connection metrics */
64
58
  this.redisConnectionsGauge = this.createGauge({
65
59
  name: 'app_redis_connections_count',
66
60
  help: 'Redis client connections',
67
- labelNames: this.withDefaultLabels(redisConnectionStableFields),
61
+ labelNames: [...this._redisLabelNames, ...redisConnectionStableFields],
68
62
  })
69
63
 
70
64
  this.redisConnectionsMemoryGauge = this.createGauge({
71
65
  name: 'app_redis_connections_memory_usage_count',
72
66
  help: 'Redis client connections',
73
- labelNames: this.withDefaultLabels(redisConnectionStableFields),
67
+ labelNames: [...this._redisLabelNames, ...redisConnectionStableFields],
74
68
  })
75
69
 
76
70
  /** Gauge for Redis memory usage */
77
71
  this.redisMemoryGauge = this.createGauge({
78
72
  name: 'app_redis_memory_bytes',
79
73
  help: 'Redis memory usage in bytes',
80
- labelNames: this.withDefaultLabels(['memory_type']),
74
+ labelNames: [...this._redisLabelNames, 'memory_type'],
81
75
  })
82
76
 
83
77
  /** Gauge for Redis operation stats */
84
78
  this.redisStatsGauge = this.createGauge({
85
79
  name: 'app_redis_stats_total',
86
80
  help: 'Redis operation statistics',
87
- labelNames: this.withDefaultLabels(['operation']),
81
+ labelNames: [...this._redisLabelNames, 'operation'],
88
82
  })
89
83
 
90
84
  // Track emitted connection label combinations so we can:
@@ -99,211 +93,6 @@ class RedisMetricsClient extends BaseMetricsClient {
99
93
  this._setCleanupHandlers()
100
94
  }
101
95
 
102
- /**
103
- * Initialize graceful-shutdown state and subscribe when enabled. Called from constructor.
104
- * @param {boolean} [gracefulShutdownRedis] - Explicit false to disable; otherwise enabled unless METRICS_GRACEFUL_SHUTDOWN_REDIS=false.
105
- */
106
- _initGracefulShutdown(gracefulShutdownRedis) {
107
- const disabledByParam = gracefulShutdownRedis === false
108
- const disabledByEnv =
109
- process.env.METRICS_GRACEFUL_SHUTDOWN_REDIS === 'false'
110
- this._gracefulShutdownRedis = !disabledByParam && !disabledByEnv
111
- this._gracefulShutdownStream = this._gracefulShutdownRedis
112
- ? `metrics:graceful-shutdown:${this.appName}:${this.processType}`
113
- : null
114
- this._gracefulShutdownAckChannel = this._gracefulShutdownRedis
115
- ? `metrics:graceful-shutdown-ack:${this.appName}:${this.processType}`
116
- : null
117
- this._gracefulShutdownLogPrefix = `[graceful-shutdown] [${this.processType}] [${this.appName}] [${this.dynoId}]`
118
- if (this._gracefulShutdownRedis && this._gracefulShutdownStream) {
119
- this._setupGracefulShutdownSubscribe()
120
- }
121
- }
122
-
123
- /**
124
- * Create a dedicated Redis client for subscribe (pub/sub). Uses duplicate() when available, else createClient from REDIS_URL. Branches by redisClientType (IOREDIS, REDIS_V3, REDIS_V4).
125
- * @returns {any|null} Subscriber client or null if not possible.
126
- */
127
- _createSubscriberClient() {
128
- if (this.redisClientType === IOREDIS && this.redisClient && typeof this.redisClient.duplicate === 'function') {
129
- return this.redisClient.duplicate()
130
- }
131
- if ((this.redisClientType === REDIS_V3 || this.redisClientType === REDIS_V4) && this.redisClient && typeof this.redisClient.duplicate === 'function') {
132
- return this.redisClient.duplicate()
133
- }
134
- if ((this.redisClientType === REDIS_V3 || this.redisClientType === REDIS_V4) && process.env.REDIS_URL) {
135
- try {
136
- const redis = require('redis')
137
- if (typeof redis.createClient !== 'function') return null
138
- try {
139
- return redis.createClient({ url: process.env.REDIS_URL })
140
- } catch {
141
- return redis.createClient(process.env.REDIS_URL)
142
- }
143
- } catch {
144
- return null
145
- }
146
- }
147
- return null
148
- }
149
-
150
- /**
151
- * Set up Redis subscribe for graceful shutdown. Uses _createSubscriberClient() so subscriber matches client type (ioredis vs node-redis v3/v4).
152
- */
153
- /**
154
- * Set up Redis stream read for graceful shutdown. Uses _createSubscriberClient() and XREAD BLOCK; stream is trimmed (MAXLEN and MINID) so messages are not kept in Redis forever.
155
- */
156
- _setupGracefulShutdownSubscribe() {
157
- const streamKey = this._gracefulShutdownStream
158
- if (!streamKey) return
159
-
160
- const subClient = this._createSubscriberClient()
161
- if (subClient && typeof subClient.on === 'function') {
162
- subClient.on('error', () => {})
163
- }
164
-
165
- if (!subClient) return
166
-
167
- this._subClient = subClient
168
- this._streamReadLoop(streamKey)
169
- }
170
-
171
- _streamReadLoop(streamKey) {
172
- const self = this
173
- const blockMs = 5000
174
-
175
- const runRead = () => {
176
- if (!self._subClient) return
177
- self._xreadBlock(streamKey, blockMs)
178
- .then((entries) => {
179
- if (!entries || entries.length === 0) {
180
- setImmediate(runRead)
181
- return
182
- }
183
- self._cleanupAndPublishAck()
184
- })
185
- .catch(() => {
186
- setImmediate(runRead)
187
- })
188
- }
189
- runRead()
190
- }
191
-
192
- _xreadBlock(streamKey, blockMs) {
193
- const client = this._subClient || this.redisClient
194
- if (!client) return Promise.resolve([])
195
-
196
- if (this.redisClientType === REDIS_V3) {
197
- return new Promise((resolve, reject) => {
198
- client.send_command('XREAD', ['BLOCK', String(blockMs), 'STREAMS', streamKey, '$'], (err, result) => {
199
- if (err) return reject(err)
200
- resolve(this._parseXreadReply(result, streamKey))
201
- })
202
- })
203
- }
204
- if (this.redisClientType === REDIS_V4) {
205
- const p = client.sendCommand(['XREAD', 'BLOCK', String(blockMs), 'STREAMS', streamKey, '$'])
206
- return Promise.resolve(p).then(result => this._parseXreadReply(result, streamKey)).catch(() => [])
207
- }
208
- if (this.redisClientType === IOREDIS) {
209
- const p = client.call('XREAD', 'BLOCK', blockMs, 'STREAMS', streamKey, '$')
210
- return Promise.resolve(p).then(result => this._parseXreadReply(result, streamKey)).catch(() => [])
211
- }
212
- return Promise.resolve([])
213
- }
214
-
215
- _parseXreadReply(reply, streamKey) {
216
- if (!reply || !Array.isArray(reply)) return []
217
- const streamReply = reply.find(r => r && r[0] === streamKey)
218
- if (!streamReply || !Array.isArray(streamReply[1])) return []
219
- return streamReply[1].map(entry => (Array.isArray(entry) ? [entry[0], entry[1] || []] : [entry, []]))
220
- }
221
-
222
- /**
223
- * Old instance: stop push, clear metrics from VM, then publish ack so new can start; then exit.
224
- */
225
- async _cleanupAndPublishAck() {
226
- this.stopPush()
227
- if (this.enabled) {
228
- await this.gatewayDelete()
229
- }
230
- await this._publishAck()
231
- if (this._subClient) {
232
- try {
233
- if (this.redisClientType === REDIS_V3) {
234
- await new Promise((resolve, reject) => {
235
- if (typeof this._subClient.quit === 'function') {
236
- this._subClient.quit(err => (err ? reject(err) : resolve()))
237
- } else resolve()
238
- })
239
- } else if (this.redisClientType === REDIS_V4) {
240
- if (this._subClient.quit) await this._subClient.quit()
241
- } else if (this.redisClientType === IOREDIS) {
242
- if (this._subClient.disconnect) await this._subClient.disconnect()
243
- }
244
- } catch {
245
- // ignore
246
- }
247
- this._subClient = null
248
- }
249
- try {
250
- if (this.redisClient) {
251
- if (this.redisClientType === REDIS_V3 || this.redisClientType === REDIS_V4) {
252
- await this.redisClient.quit()
253
- } else if (this.redisClientType === IOREDIS) {
254
- await this.redisClient.disconnect()
255
- }
256
- }
257
- } catch {
258
- // ignore
259
- }
260
- process.exit(0)
261
- }
262
-
263
- _publishAck() {
264
- const ackChannel = this._gracefulShutdownAckChannel
265
- if (!ackChannel || !this.redisClient) return Promise.resolve()
266
- console.warn(
267
- `${this._gracefulShutdownLogPrefix} OLD: clearing metrics and exiting.`
268
- )
269
- const msg = this.dynoId || 'stopped'
270
- if (this.redisClientType === REDIS_V3) {
271
- return new Promise((resolve) => {
272
- this.redisClient.send_command('PUBLISH', [ackChannel, msg], () => resolve())
273
- })
274
- }
275
- if (this.redisClientType === REDIS_V4) {
276
- return this.redisClient.sendCommand(['PUBLISH', ackChannel, msg]).catch(() => {})
277
- }
278
- if (this.redisClientType === IOREDIS) {
279
- return this.redisClient.publish(ackChannel, msg).catch(() => {})
280
- }
281
- return Promise.resolve()
282
- }
283
-
284
- /**
285
- * Publish "new instance started" to stream so old instances exit. Stream is trimmed (MAXLEN ~ 10 and MINID older than 1 min) so messages are not kept in Redis forever.
286
- */
287
- _publishNewInstanceStarted() {
288
- const streamKey = this._gracefulShutdownStream
289
- if (!streamKey || !this.redisClient) return
290
- const value = this.dynoId || '1'
291
- const noop = () => {}
292
- const maxAgeMs = GRACEFUL_SHUTDOWN_STREAM_MAXAGE_MS
293
- const minId = `${Date.now() - maxAgeMs}-0`
294
-
295
- if (this.redisClientType === REDIS_V3) {
296
- this.redisClient.send_command('XADD', [streamKey, 'MAXLEN', '~', '10', '*', 'dyno_id', value], noop)
297
- this.redisClient.send_command('XTRIM', [streamKey, 'MINID', minId], noop)
298
- } else if (this.redisClientType === REDIS_V4) {
299
- this.redisClient.sendCommand(['XADD', streamKey, 'MAXLEN', '~', '10', '*', 'dyno_id', value]).catch(noop)
300
- this.redisClient.sendCommand(['XTRIM', streamKey, 'MINID', minId]).catch(noop)
301
- } else if (this.redisClientType === IOREDIS) {
302
- this.redisClient.call('XADD', streamKey, 'MAXLEN', '~', 10, '*', 'dyno_id', value).catch(noop)
303
- this.redisClient.call('XTRIM', streamKey, 'MINID', minId).catch(noop)
304
- }
305
- }
306
-
307
96
  getRedisConnections = async () => {
308
97
  if (!this.redisClient) throw new Error('Redis client not provided')
309
98
 
@@ -396,7 +185,7 @@ class RedisMetricsClient extends BaseMetricsClient {
396
185
  this.getRedisConnections(),
397
186
  ])
398
187
 
399
- const labels = this.getDefaultLabels()
188
+ const labels = { app: this.appName, process_type: this.processType }
400
189
 
401
190
  const connections = this.parseRedisConnections(connectionsInfoStr)
402
191
 
@@ -553,7 +342,7 @@ class RedisMetricsClient extends BaseMetricsClient {
553
342
  )
554
343
  }
555
344
  } catch (error) {
556
- console.error(
345
+ console.warn(
557
346
  `[queue-metrics] Failed to collect Redis metrics:`,
558
347
  error.message
559
348
  )
@@ -569,6 +358,14 @@ class RedisMetricsClient extends BaseMetricsClient {
569
358
  await this.collectRedisMetrics()
570
359
  await this.gatewayPush()
571
360
  this.clearAllCounters()
361
+
362
+ if (this.metricsLogValues) {
363
+ const metricObjects = await this.registry.getMetricsAsJSON()
364
+ console.info(
365
+ `[queue-metrics] Collected metrics for Redis`,
366
+ JSON.stringify(metricObjects, null, 2)
367
+ )
368
+ }
572
369
  } catch (error) {
573
370
  console.error(
574
371
  `[queue-metrics] Failed to collect Redis metrics: ${error.message}`
@@ -578,7 +375,8 @@ class RedisMetricsClient extends BaseMetricsClient {
578
375
  }
579
376
 
580
377
  /**
581
- * Start periodic collection. When graceful shutdown is on: new publishes "new started" so old exits and clears; new does not push until after one interval (skipFirstPush), giving old time to stop.
378
+ * Start periodic collection.
379
+ * @param {number} [intervalSec=this.intervalSec] - Interval in seconds
582
380
  */
583
381
  startPush = (intervalSec = this.intervalSec) => {
584
382
  this._startPush(intervalSec, () => {
@@ -586,39 +384,13 @@ class RedisMetricsClient extends BaseMetricsClient {
586
384
  console.error(`[queue-metrics] Failed to push Redis metrics:`, err)
587
385
  })
588
386
  })
589
- if (this._gracefulShutdownRedis) {
590
- this._publishNewInstanceStarted()
591
- }
592
387
  }
593
388
 
594
389
  /**
595
390
  * Cleanup Redis client and exit process.
596
- * Stops push, deletes this instance's metrics from VM (if removeOldMetrics), closes subscriber and main Redis, then exits.
597
391
  * @returns {Promise<void>}
598
392
  */
599
393
  cleanup = async () => {
600
- this.stopPush()
601
- if (this.enabled) {
602
- await this.gatewayDelete()
603
- }
604
- if (this._subClient) {
605
- try {
606
- if (this.redisClientType === REDIS_V3) {
607
- await new Promise((resolve, reject) => {
608
- if (typeof this._subClient.quit === 'function') {
609
- this._subClient.quit(err => (err ? reject(err) : resolve()))
610
- } else resolve()
611
- })
612
- } else if (this.redisClientType === REDIS_V4) {
613
- if (this._subClient.quit) await this._subClient.quit()
614
- } else if (this.redisClientType === IOREDIS) {
615
- if (this._subClient.disconnect) await this._subClient.disconnect()
616
- }
617
- } catch (err) {
618
- console.error('[queue-metrics] Error closing subscriber client:', err)
619
- }
620
- this._subClient = null
621
- }
622
394
  try {
623
395
  if (!this.redisClient) return
624
396
 
@@ -633,7 +405,7 @@ class RedisMetricsClient extends BaseMetricsClient {
633
405
  } catch (err) {
634
406
  console.error('[queue-metrics] Error closing Redis client:', err)
635
407
  }
636
- await super.cleanup()
408
+ process.exit(0)
637
409
  }
638
410
 
639
411
  _setCleanupHandlers = () => {