forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalertsEngine.js
More file actions
234 lines (218 loc) · 7.23 KB
/
Copy pathalertsEngine.js
File metadata and controls
234 lines (218 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Alerts engine for Nova Rewards.
*
* Evaluates a set of configurable rules against the current metrics snapshot
* on each call. Fires an event when a rule transitions from OK → FIRING and
* again when it resolves.
*
* Alert states: 'ok' | 'firing' | 'resolved'
*
* Built-in rules (all thresholds configurable via env vars):
* high_error_rate - HTTP error rate > ALERT_ERROR_RATE_PCT (default 10 %)
* slow_response_p95 - p95 response time > ALERT_P95_MS (default 2000 ms)
* low_distribution_rate - distributions in last window < ALERT_MIN_DISTRIBUTIONS (default 0, alerts only when metric exists)
* blockchain_errors - blockchain error events > ALERT_BLOCKCHAIN_ERRORS (default 5)
*/
const metrics = require('./metricsCollector');
const { logEvent } = require('./eventsLogger');
// ── Thresholds (env-configurable) ────────────────────────────────────────────
const THRESHOLDS = {
errorRatePct: parseFloat(process.env.ALERT_ERROR_RATE_PCT ?? '10'),
p95Ms: parseFloat(process.env.ALERT_P95_MS ?? '2000'),
blockchainErrors: parseInt(process.env.ALERT_BLOCKCHAIN_ERRORS ?? '5', 10),
};
// ── Persistent alert state ────────────────────────────────────────────────────
/**
* @type {Map<string, { id: string, state: string, firedAt: string|null, resolvedAt: string|null, message: string, severity: string }>}
*/
const alertState = new Map();
/**
* History of alert transitions (ring buffer, last 200).
* @type {object[]}
*/
let alertHistory = [];
const MAX_HISTORY = 200;
// ── Rule definitions ──────────────────────────────────────────────────────────
/**
* Each rule returns { firing: boolean, message: string, severity: 'warn'|'error' }.
* @type {Array<{ id: string, name: string, evaluate: (snap: object) => { firing: boolean, message: string, severity: string } }>}
*/
const rules = [
{
id: 'high_error_rate',
name: 'High HTTP Error Rate',
evaluate(snap) {
const firing = snap.errorRate > THRESHOLDS.errorRatePct;
return {
firing,
message: firing
? `HTTP error rate is ${snap.errorRate}% (threshold: ${THRESHOLDS.errorRatePct}%)`
: `HTTP error rate is ${snap.errorRate}% — normal`,
severity: 'error',
};
},
},
{
id: 'slow_response_p95',
name: 'Slow P95 Response Time',
evaluate(snap) {
// Find the worst p95 across all routes
let worstP95 = 0;
let worstRoute = '';
for (const [route, stats] of Object.entries(snap.responseTimes ?? {})) {
if (stats.p95 > worstP95) {
worstP95 = stats.p95;
worstRoute = route;
}
}
const firing = worstP95 > THRESHOLDS.p95Ms;
return {
firing,
message: firing
? `P95 response time for ${worstRoute} is ${worstP95}ms (threshold: ${THRESHOLDS.p95Ms}ms)`
: `All route P95 response times are within threshold`,
severity: 'warn',
};
},
},
{
id: 'blockchain_errors',
name: 'Elevated Blockchain Errors',
evaluate(snap) {
const count = snap.counters['events.error.blockchain'] ?? 0;
const firing = count > THRESHOLDS.blockchainErrors;
return {
firing,
message: firing
? `Blockchain errors: ${count} (threshold: ${THRESHOLDS.blockchainErrors})`
: `Blockchain errors: ${count} — normal`,
severity: 'error',
};
},
},
{
id: 'no_distributions',
name: 'No Reward Distributions',
evaluate(snap) {
const total = snap.counters['http.requests.total'] ?? 0;
const dists = snap.counters['events.reward.distributed'] ?? 0;
// Only fire when the server has served at least 20 requests but 0 distributions
const firing = total >= 20 && dists === 0;
return {
firing,
message: firing
? `No reward distributions recorded after ${total} requests`
: `Distributions: ${dists}`,
severity: 'warn',
};
},
},
];
// ── Evaluation ────────────────────────────────────────────────────────────────
/**
* Evaluate all rules against the current metrics snapshot.
* Updates alert state and appends to history on transitions.
*
* @returns {object[]} current alert state for all rules
*/
function evaluate() {
const snap = metrics.snapshot();
const now = new Date().toISOString();
for (const rule of rules) {
const { firing, message, severity } = rule.evaluate(snap);
const prev = alertState.get(rule.id);
if (firing) {
if (!prev || prev.state !== 'firing') {
// Transition → FIRING
const alert = {
id: rule.id,
name: rule.name,
state: 'firing',
severity,
message,
firedAt: now,
resolvedAt: null,
};
alertState.set(rule.id, alert);
_appendHistory({ ...alert, transitionAt: now });
logEvent('alert.fired', { ruleId: rule.id, name: rule.name, message }, severity);
} else {
// Still firing — update message in case the value changed
alertState.set(rule.id, { ...prev, message });
}
} else {
if (prev && prev.state === 'firing') {
// Transition → RESOLVED
const alert = {
id: rule.id,
name: rule.name,
state: 'resolved',
severity: 'info',
message,
firedAt: prev.firedAt,
resolvedAt: now,
};
alertState.set(rule.id, alert);
_appendHistory({ ...alert, transitionAt: now });
logEvent('alert.resolved', { ruleId: rule.id, name: rule.name }, 'info');
} else if (!prev) {
// First evaluation, rule is OK
alertState.set(rule.id, {
id: rule.id,
name: rule.name,
state: 'ok',
severity: 'info',
message,
firedAt: null,
resolvedAt: null,
});
}
}
}
return getAlerts();
}
/**
* Return current alert states for all rules.
* @returns {object[]}
*/
function getAlerts() {
evaluate._lastEval = Date.now();
return rules.map((r) => alertState.get(r.id) ?? {
id: r.id,
name: r.name,
state: 'ok',
severity: 'info',
message: 'Not yet evaluated',
firedAt: null,
resolvedAt: null,
});
}
/**
* Return the last N alert transition records.
* @param {number} [limit=50]
* @returns {object[]}
*/
function getAlertHistory(limit = 50) {
return [...alertHistory].reverse().slice(0, limit);
}
function _appendHistory(entry) {
alertHistory.push(entry);
if (alertHistory.length > MAX_HISTORY) {
alertHistory = alertHistory.slice(alertHistory.length - MAX_HISTORY);
}
}
/**
* Reset all alert state (useful in tests).
*/
function reset() {
alertState.clear();
alertHistory = [];
}
module.exports = {
evaluate,
getAlerts,
getAlertHistory,
rules,
THRESHOLDS,
reset,
};