@platformatic/watt-extra 0.1.7 → 0.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/watt-extra",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "The Platformatic runtime manager",
5
5
  "type": "module",
6
6
  "scripts": {
package/plugins/alerts.js CHANGED
@@ -5,9 +5,10 @@ async function alerts (app, _opts) {
5
5
  const podHealthWindow =
6
6
  app.instanceConfig?.config?.scaler?.podHealthWindow || 60 * 1000
7
7
  const alertRetentionWindow =
8
- app.instanceConfig?.config?.scaler?.alertRetentionWindow || 30 * 1000
8
+ app.instanceConfig?.config?.scaler?.alertRetentionWindow || 10 * 1000
9
+
10
+ const lastServicesAlertTime = {}
9
11
 
10
- let lastAlertTime = 0
11
12
  async function setupAlerts () {
12
13
  // Skip alerts setup if ICC is not configured
13
14
  if (!app.env.PLT_ICC_URL) {
@@ -66,12 +67,16 @@ async function alerts (app, _opts) {
66
67
 
67
68
  if (healthInfo.unhealthy) {
68
69
  const currentTime = Date.now()
69
- if (currentTime - lastAlertTime < alertRetentionWindow) {
70
+
71
+ const serviceId = healthInfo.id
72
+ const lastAlertTime = lastServicesAlertTime[serviceId]
73
+
74
+ if (lastAlertTime && currentTime - lastAlertTime < alertRetentionWindow) {
70
75
  app.log.debug('Skipping alert, within retention window')
71
76
  return
72
77
  }
73
78
 
74
- lastAlertTime = currentTime
79
+ lastServicesAlertTime[serviceId] = currentTime
75
80
  delete healthInfo.healthConfig
76
81
 
77
82
  const authHeaders = await app.getAuthorizationHeader()
@@ -96,7 +101,6 @@ async function alerts (app, _opts) {
96
101
  }
97
102
 
98
103
  const alert = await body.json()
99
- const serviceId = healthInfo.id
100
104
 
101
105
  try {
102
106
  await app.sendFlamegraphs({
@@ -372,9 +372,9 @@ test('should respect alert retention window', async (t) => {
372
372
  })
373
373
 
374
374
  // Create a health info template
375
- const createHealthInfo = (unhealthy = true) => ({
376
- id: 'service-1',
377
- service: 'service-1',
375
+ const createHealthInfo = (serviceId, unhealthy = true) => ({
376
+ id: serviceId,
377
+ service: serviceId,
378
378
  currentHealth: {
379
379
  elu: unhealthy ? 0.95 : 0.5,
380
380
  heapUsed: 76798040,
@@ -393,22 +393,26 @@ test('should respect alert retention window', async (t) => {
393
393
  })
394
394
 
395
395
  // Send first unhealthy event - should trigger alert
396
- app.watt.runtime.emit('application:worker:health', createHealthInfo(true))
397
- await sleep(100)
396
+ app.watt.runtime.emit('application:worker:health', createHealthInfo('service-1', true))
397
+ await sleep(50)
398
+
399
+ // Send second unhealthy event immediately - should trigger alert
400
+ app.watt.runtime.emit('application:worker:health', createHealthInfo('service-2', true))
401
+ await sleep(50)
398
402
 
399
403
  // Send second unhealthy event immediately - should be ignored due to retention window
400
- app.watt.runtime.emit('application:worker:health', createHealthInfo(true))
404
+ app.watt.runtime.emit('application:worker:health', createHealthInfo('service-1', true))
401
405
  await sleep(100)
402
406
 
403
- assert.strictEqual(alertsReceived.length, 1, 'Only one alert should be sent within retention window')
407
+ assert.strictEqual(alertsReceived.length, 2, 'Only one alert should be sent within retention window')
404
408
 
405
409
  await sleep(500)
406
410
 
407
411
  // Send third unhealthy event - should trigger second alert
408
- app.watt.runtime.emit('application:worker:health', createHealthInfo(true))
412
+ app.watt.runtime.emit('application:worker:health', createHealthInfo('service-1', true))
409
413
  await sleep(100)
410
414
 
411
- assert.strictEqual(alertsReceived.length, 2, 'Second alert should be sent after retention window expires')
415
+ assert.strictEqual(alertsReceived.length, 3, 'Second alert should be sent after retention window expires')
412
416
  })
413
417
 
414
418
  test('should not set up alerts when scaler URL is missing', async (t) => {