@adalo/metrics 0.1.164 → 0.1.166
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/.env.example +0 -1
- package/__tests__/metricsRedisClient.test.js +138 -0
- package/lib/metrics/baseMetricsClient.d.ts +1 -15
- package/lib/metrics/baseMetricsClient.d.ts.map +1 -1
- package/lib/metrics/baseMetricsClient.js +10 -40
- package/lib/metrics/baseMetricsClient.js.map +1 -1
- package/lib/metrics/metricsQueueRedisClient.d.ts.map +1 -1
- package/lib/metrics/metricsQueueRedisClient.js +1 -3
- package/lib/metrics/metricsQueueRedisClient.js.map +1 -1
- package/lib/metrics/metricsRedisClient.d.ts +7 -41
- package/lib/metrics/metricsRedisClient.d.ts.map +1 -1
- package/lib/metrics/metricsRedisClient.js +20 -231
- package/lib/metrics/metricsRedisClient.js.map +1 -1
- package/package.json +5 -5
- package/src/metrics/baseMetricsClient.js +10 -51
- package/src/metrics/metricsQueueRedisClient.js +1 -3
- package/src/metrics/metricsRedisClient.js +21 -249
|
@@ -20,7 +20,6 @@ class BaseMetricsClient {
|
|
|
20
20
|
* @param {string} [config.pushgatewaySecret] Basic auth secret (Base64 of user:password)
|
|
21
21
|
* @param {number} [config.intervalSec] Interval in seconds for pushing metrics
|
|
22
22
|
* @param {boolean} [config.removeOldMetrics] Enable to clear metrics by service name
|
|
23
|
-
* @param {boolean} [config.skipFirstPush] Skip the first push (first push after interval); set METRICS_SKIP_FIRST_PUSH=true to give old instance time to exit
|
|
24
23
|
* @param {function} [config.startupValidation] Add to validate on start push.
|
|
25
24
|
* @param {boolean} [config.disablePushgateway] Disable pushing to Pushgateway (use HTTP scraping instead)
|
|
26
25
|
*/
|
|
@@ -49,14 +48,6 @@ class BaseMetricsClient {
|
|
|
49
48
|
this.removeOldMetrics =
|
|
50
49
|
config.removeOldMetrics ??
|
|
51
50
|
process.env.METRICS_REMOVE_OLD_METRICS === 'true'
|
|
52
|
-
/** When true, skip the immediate first push; first push runs after the first interval. Set METRICS_SKIP_FIRST_PUSH=true. */
|
|
53
|
-
this.skipFirstPush =
|
|
54
|
-
config.skipFirstPush ?? process.env.METRICS_SKIP_FIRST_PUSH === 'true'
|
|
55
|
-
/** If true (default), cleanup() calls process.exit(0). Set to false when the app handles SIGTERM itself (e.g. graceful HTTP shutdown). */
|
|
56
|
-
this.cleanupExitsProcess = config.cleanupExitsProcess ?? true
|
|
57
|
-
|
|
58
|
-
/** @type {NodeJS.Timeout | null} Push interval handle so it can be cleared on shutdown */
|
|
59
|
-
this._pushIntervalId = null
|
|
60
51
|
|
|
61
52
|
this.prefixLogs = `[${this.processType}] [${this.appName}] [${this.dynoId}] [Monitoring]`
|
|
62
53
|
|
|
@@ -223,27 +214,19 @@ class BaseMetricsClient {
|
|
|
223
214
|
}
|
|
224
215
|
|
|
225
216
|
if (customPushMetics && typeof customPushMetics === 'function') {
|
|
226
|
-
|
|
227
|
-
() => customPushMetics(),
|
|
228
|
-
interval * 1000
|
|
229
|
-
)
|
|
217
|
+
setInterval(() => customPushMetics(), interval * 1000)
|
|
230
218
|
} else {
|
|
231
|
-
|
|
219
|
+
setInterval(() => {
|
|
232
220
|
runPush().catch(err => {
|
|
233
221
|
console.error(`${this.prefixLogs} Failed to push metrics:`, err)
|
|
234
222
|
})
|
|
235
223
|
}, interval * 1000)
|
|
236
224
|
}
|
|
237
225
|
|
|
238
|
-
// First push
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
`${this.prefixLogs} Failed to push metrics (initial):`,
|
|
243
|
-
err
|
|
244
|
-
)
|
|
245
|
-
})
|
|
246
|
-
}
|
|
226
|
+
// First push immediately so metrics appear without waiting for the first interval
|
|
227
|
+
runPush().catch(err => {
|
|
228
|
+
console.error(`${this.prefixLogs} Failed to push metrics (initial):`, err)
|
|
229
|
+
})
|
|
247
230
|
|
|
248
231
|
let pushOrigin = 'none'
|
|
249
232
|
try {
|
|
@@ -258,20 +241,6 @@ class BaseMetricsClient {
|
|
|
258
241
|
)
|
|
259
242
|
}
|
|
260
243
|
|
|
261
|
-
/**
|
|
262
|
-
* Stop periodic metrics push (clears the interval).
|
|
263
|
-
* Call this before process exit to avoid pushing during shutdown and to reduce connection overlap on redeploy.
|
|
264
|
-
*/
|
|
265
|
-
stopPush = () => {
|
|
266
|
-
if (this._pushIntervalId) {
|
|
267
|
-
clearInterval(this._pushIntervalId)
|
|
268
|
-
this._pushIntervalId = null
|
|
269
|
-
console.warn(
|
|
270
|
-
`${this.prefixLogs} Metrics collection stopped (push interval cleared).`
|
|
271
|
-
)
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
244
|
pushMetrics = async () => {
|
|
276
245
|
return this._pushMetrics()
|
|
277
246
|
}
|
|
@@ -291,19 +260,14 @@ class BaseMetricsClient {
|
|
|
291
260
|
}
|
|
292
261
|
|
|
293
262
|
/**
|
|
294
|
-
* Cleanup metrics and
|
|
295
|
-
* Stops the push interval immediately (to avoid overlap on redeploy), then deletes this instance's metrics from the gateway, then exits if cleanupExitsProcess is true.
|
|
263
|
+
* Cleanup metrics and exit process.
|
|
296
264
|
* @returns {Promise<void>}
|
|
297
265
|
*/
|
|
298
266
|
cleanup = async () => {
|
|
299
|
-
console.warn(`${this.prefixLogs} Metrics cleanup started (shutdown).`)
|
|
300
|
-
this.stopPush()
|
|
301
267
|
if (this.enabled) {
|
|
302
268
|
await this.gatewayDelete()
|
|
303
269
|
}
|
|
304
|
-
|
|
305
|
-
process.exit(0)
|
|
306
|
-
}
|
|
270
|
+
process.exit(0)
|
|
307
271
|
}
|
|
308
272
|
|
|
309
273
|
/**
|
|
@@ -330,9 +294,6 @@ class BaseMetricsClient {
|
|
|
330
294
|
this.pushgatewayUrl &&
|
|
331
295
|
this.pushgatewayUrl.trim()
|
|
332
296
|
) {
|
|
333
|
-
console.warn(
|
|
334
|
-
`${this.prefixLogs} Deleting this instance's metrics from VM (app=${this.appName}, dyno_id=${this.dynoId}, process_type=${this.processType}).`
|
|
335
|
-
)
|
|
336
297
|
await this._deleteFromVMByLabels().catch(err => {
|
|
337
298
|
console.warn(
|
|
338
299
|
`${this.prefixLogs} Deletion from VM on shutdown failed:`,
|
|
@@ -494,10 +455,8 @@ class BaseMetricsClient {
|
|
|
494
455
|
}
|
|
495
456
|
|
|
496
457
|
_setCleanupHandlers = () => {
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
process.on('SIGTERM', this.cleanup)
|
|
500
|
-
}
|
|
458
|
+
process.on('SIGINT', this.cleanup)
|
|
459
|
+
process.on('SIGTERM', this.cleanup)
|
|
501
460
|
}
|
|
502
461
|
|
|
503
462
|
// GETTERS
|
|
@@ -11,7 +11,6 @@ class QueueRedisMetricsClient extends RedisMetricsClient {
|
|
|
11
11
|
/**
|
|
12
12
|
* @param {Object} options
|
|
13
13
|
* @param {any} options.redisClient - Redis client instance (required)
|
|
14
|
-
* @param {boolean} [options.gracefulShutdownRedis] - Passed to RedisMetricsClient
|
|
15
14
|
* @param {string} [options.appName] - Application name (from BaseMetricsClient)
|
|
16
15
|
* @param {string} [options.dynoId] - Dyno/instance ID (from BaseMetricsClient)
|
|
17
16
|
* @param {string} [options.processType] - Process type (from BaseMetricsClient)
|
|
@@ -336,7 +335,6 @@ class QueueRedisMetricsClient extends RedisMetricsClient {
|
|
|
336
335
|
|
|
337
336
|
/**
|
|
338
337
|
* Cleanup queues and exit process.
|
|
339
|
-
* Closes queues then runs Redis metrics cleanup (stop push, delete from VM, close Redis).
|
|
340
338
|
* @returns {Promise<void>}
|
|
341
339
|
*/
|
|
342
340
|
cleanup = async () => {
|
|
@@ -347,7 +345,7 @@ class QueueRedisMetricsClient extends RedisMetricsClient {
|
|
|
347
345
|
console.error(`[queue-metrics] Error closing queue ${queueName}:`, err)
|
|
348
346
|
}
|
|
349
347
|
}
|
|
350
|
-
|
|
348
|
+
process.exit(0)
|
|
351
349
|
}
|
|
352
350
|
}
|
|
353
351
|
|
|
@@ -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)
|
|
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
|
-
/**
|
|
59
|
-
this.
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
408
|
+
process.exit(0)
|
|
637
409
|
}
|
|
638
410
|
|
|
639
411
|
_setCleanupHandlers = () => {
|