flow-debugger 1.9.7 → 1.9.9
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/ENHANCED_OBSERVABILITY.md +835 -0
- package/IMPLEMENTATION_SUMMARY.md +466 -0
- package/README.md +147 -8
- package/bin/cli.js +1 -1
- package/dist/cjs/core/Alerting.js +310 -0
- package/dist/cjs/core/Alerting.js.map +1 -0
- package/dist/cjs/core/AnomalyDetection.js +223 -0
- package/dist/cjs/core/AnomalyDetection.js.map +1 -0
- package/dist/cjs/core/DependencyGraph.js +251 -0
- package/dist/cjs/core/DependencyGraph.js.map +1 -0
- package/dist/cjs/core/DistributedTracing.js +245 -0
- package/dist/cjs/core/DistributedTracing.js.map +1 -0
- package/dist/cjs/core/ErrorClustering.js +257 -0
- package/dist/cjs/core/ErrorClustering.js.map +1 -0
- package/dist/cjs/core/LogCorrelation.js +242 -0
- package/dist/cjs/core/LogCorrelation.js.map +1 -0
- package/dist/cjs/core/Metrics.js +301 -0
- package/dist/cjs/core/Metrics.js.map +1 -0
- package/dist/cjs/core/RootCause.js +107 -9
- package/dist/cjs/core/RootCause.js.map +1 -1
- package/dist/cjs/core/TrendAnalysis.js +254 -0
- package/dist/cjs/core/TrendAnalysis.js.map +1 -0
- package/dist/cjs/core/types.js +14 -0
- package/dist/cjs/core/types.js.map +1 -1
- package/dist/cjs/index.js +27 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/middleware/express.js +105 -4
- package/dist/cjs/middleware/express.js.map +1 -1
- package/dist/esm/core/Alerting.js +305 -0
- package/dist/esm/core/Alerting.js.map +1 -0
- package/dist/esm/core/AnomalyDetection.js +218 -0
- package/dist/esm/core/AnomalyDetection.js.map +1 -0
- package/dist/esm/core/DependencyGraph.js +246 -0
- package/dist/esm/core/DependencyGraph.js.map +1 -0
- package/dist/esm/core/DistributedTracing.js +240 -0
- package/dist/esm/core/DistributedTracing.js.map +1 -0
- package/dist/esm/core/ErrorClustering.js +252 -0
- package/dist/esm/core/ErrorClustering.js.map +1 -0
- package/dist/esm/core/LogCorrelation.js +236 -0
- package/dist/esm/core/LogCorrelation.js.map +1 -0
- package/dist/esm/core/Metrics.js +297 -0
- package/dist/esm/core/Metrics.js.map +1 -0
- package/dist/esm/core/RootCause.js +107 -9
- package/dist/esm/core/RootCause.js.map +1 -1
- package/dist/esm/core/TrendAnalysis.js +250 -0
- package/dist/esm/core/TrendAnalysis.js.map +1 -0
- package/dist/esm/core/types.js +14 -0
- package/dist/esm/core/types.js.map +1 -1
- package/dist/esm/index.js +10 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/middleware/express.js +105 -4
- package/dist/esm/middleware/express.js.map +1 -1
- package/dist/types/core/Alerting.d.ts +82 -0
- package/dist/types/core/Alerting.d.ts.map +1 -0
- package/dist/types/core/AnomalyDetection.d.ts +93 -0
- package/dist/types/core/AnomalyDetection.d.ts.map +1 -0
- package/dist/types/core/DependencyGraph.d.ts +65 -0
- package/dist/types/core/DependencyGraph.d.ts.map +1 -0
- package/dist/types/core/DistributedTracing.d.ts +92 -0
- package/dist/types/core/DistributedTracing.d.ts.map +1 -0
- package/dist/types/core/ErrorClustering.d.ts +70 -0
- package/dist/types/core/ErrorClustering.d.ts.map +1 -0
- package/dist/types/core/LogCorrelation.d.ts +73 -0
- package/dist/types/core/LogCorrelation.d.ts.map +1 -0
- package/dist/types/core/Metrics.d.ts +73 -0
- package/dist/types/core/Metrics.d.ts.map +1 -0
- package/dist/types/core/RootCause.d.ts +9 -5
- package/dist/types/core/RootCause.d.ts.map +1 -1
- package/dist/types/core/TrendAnalysis.d.ts +63 -0
- package/dist/types/core/TrendAnalysis.d.ts.map +1 -0
- package/dist/types/core/types.d.ts +200 -0
- package/dist/types/core/types.d.ts.map +1 -1
- package/dist/types/index.d.ts +9 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/middleware/express.d.ts +12 -0
- package/dist/types/middleware/express.d.ts.map +1 -1
- package/package.json +3 -3
- package/test-results.json +1 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// ─────────────────────────────────────────────────────────────
|
|
2
|
+
// flow-debugger — Real-time Alerting System
|
|
3
|
+
// Webhooks, Slack, and intelligent alert delivery
|
|
4
|
+
// ─────────────────────────────────────────────────────────────
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
/**
|
|
7
|
+
* Alert Manager - Handles alert creation, deduplication, and delivery
|
|
8
|
+
*/
|
|
9
|
+
export class AlertManager extends EventEmitter {
|
|
10
|
+
constructor(webhooks = [], alertOnCritical = true, alertOnErrorSpike = true, errorSpikeThreshold = 5) {
|
|
11
|
+
super();
|
|
12
|
+
this.alerts = [];
|
|
13
|
+
this.rules = new Map();
|
|
14
|
+
this.webhooks = [];
|
|
15
|
+
this.maxAlerts = 1000;
|
|
16
|
+
this.dedupWindowMs = 5 * 60 * 1000; // 5 minutes
|
|
17
|
+
this.errorCounts = [];
|
|
18
|
+
this.webhooks = webhooks;
|
|
19
|
+
this.alertOnCritical = alertOnCritical;
|
|
20
|
+
this.alertOnErrorSpike = alertOnErrorSpike;
|
|
21
|
+
this.errorSpikeThreshold = errorSpikeThreshold;
|
|
22
|
+
this.setupDefaultRules();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Setup default alert rules
|
|
26
|
+
*/
|
|
27
|
+
setupDefaultRules() {
|
|
28
|
+
this.addRule({
|
|
29
|
+
id: 'critical_error',
|
|
30
|
+
name: 'Critical Error Detected',
|
|
31
|
+
type: 'critical_error',
|
|
32
|
+
condition: 'classification == CRITICAL',
|
|
33
|
+
threshold: 1,
|
|
34
|
+
windowMs: 0,
|
|
35
|
+
severity: 'critical',
|
|
36
|
+
enabled: this.alertOnCritical,
|
|
37
|
+
});
|
|
38
|
+
this.addRule({
|
|
39
|
+
id: 'error_spike',
|
|
40
|
+
name: 'Error Spike Detected',
|
|
41
|
+
type: 'error_spike',
|
|
42
|
+
condition: 'error_count > threshold',
|
|
43
|
+
threshold: this.errorSpikeThreshold,
|
|
44
|
+
windowMs: 5 * 60 * 1000,
|
|
45
|
+
severity: 'error',
|
|
46
|
+
enabled: this.alertOnErrorSpike,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Add alert rule
|
|
51
|
+
*/
|
|
52
|
+
addRule(rule) {
|
|
53
|
+
this.rules.set(rule.id, rule);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Remove alert rule
|
|
57
|
+
*/
|
|
58
|
+
removeRule(ruleId) {
|
|
59
|
+
this.rules.delete(ruleId);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Enable/disable rule
|
|
63
|
+
*/
|
|
64
|
+
toggleRule(ruleId, enabled) {
|
|
65
|
+
const rule = this.rules.get(ruleId);
|
|
66
|
+
if (rule) {
|
|
67
|
+
rule.enabled = enabled;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Add webhook endpoint
|
|
72
|
+
*/
|
|
73
|
+
addWebhook(url) {
|
|
74
|
+
if (!this.webhooks.includes(url)) {
|
|
75
|
+
this.webhooks.push(url);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Remove webhook endpoint
|
|
80
|
+
*/
|
|
81
|
+
removeWebhook(url) {
|
|
82
|
+
this.webhooks = this.webhooks.filter(w => w !== url);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Process trace and check for alerts
|
|
86
|
+
*/
|
|
87
|
+
processTrace(trace) {
|
|
88
|
+
// Track error counts for spike detection
|
|
89
|
+
if (trace.classification === 'ERROR' || trace.classification === 'CRITICAL') {
|
|
90
|
+
this.recordError();
|
|
91
|
+
}
|
|
92
|
+
// Check critical error alert
|
|
93
|
+
if (this.alertOnCritical && trace.classification === 'CRITICAL') {
|
|
94
|
+
this.createAlert({
|
|
95
|
+
type: 'critical_error',
|
|
96
|
+
severity: 'critical',
|
|
97
|
+
title: 'Critical Error Detected',
|
|
98
|
+
message: `Critical error on ${trace.method} ${trace.endpoint}${trace.rootCause ? `: ${trace.rootCause.cause}` : ''}`,
|
|
99
|
+
traceId: trace.traceId,
|
|
100
|
+
endpoint: trace.endpoint,
|
|
101
|
+
metadata: {
|
|
102
|
+
classification: trace.classification,
|
|
103
|
+
rootCause: trace.rootCause,
|
|
104
|
+
duration: trace.totalDuration,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
// Check error spike
|
|
109
|
+
if (this.alertOnErrorSpike) {
|
|
110
|
+
this.checkErrorSpike();
|
|
111
|
+
}
|
|
112
|
+
// Check custom rules
|
|
113
|
+
this.checkCustomRules(trace);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Create and deliver an alert
|
|
117
|
+
*/
|
|
118
|
+
createAlert(alert) {
|
|
119
|
+
// Deduplication check
|
|
120
|
+
if (this.isDuplicate(alert)) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
const fullAlert = {
|
|
124
|
+
id: this.generateAlertId(),
|
|
125
|
+
timestamp: new Date(),
|
|
126
|
+
delivered: false,
|
|
127
|
+
...alert,
|
|
128
|
+
};
|
|
129
|
+
this.alerts.unshift(fullAlert);
|
|
130
|
+
// Trim old alerts
|
|
131
|
+
if (this.alerts.length > this.maxAlerts) {
|
|
132
|
+
this.alerts = this.alerts.slice(0, this.maxAlerts);
|
|
133
|
+
}
|
|
134
|
+
// Emit event
|
|
135
|
+
this.emit('alert', fullAlert);
|
|
136
|
+
// Deliver to webhooks
|
|
137
|
+
this.deliverAlert(fullAlert);
|
|
138
|
+
return fullAlert;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get recent alerts
|
|
142
|
+
*/
|
|
143
|
+
getAlerts(limit = 50) {
|
|
144
|
+
return this.alerts.slice(0, limit);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get alerts by type
|
|
148
|
+
*/
|
|
149
|
+
getAlertsByType(type) {
|
|
150
|
+
return this.alerts.filter(a => a.type === type);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get alerts by severity
|
|
154
|
+
*/
|
|
155
|
+
getAlertsBySeverity(severity) {
|
|
156
|
+
return this.alerts.filter(a => a.severity === severity);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Clear all alerts
|
|
160
|
+
*/
|
|
161
|
+
clear() {
|
|
162
|
+
this.alerts = [];
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get all rules
|
|
166
|
+
*/
|
|
167
|
+
getRules() {
|
|
168
|
+
return Array.from(this.rules.values());
|
|
169
|
+
}
|
|
170
|
+
recordError() {
|
|
171
|
+
const now = Date.now();
|
|
172
|
+
const windowStart = now - 5 * 60 * 1000;
|
|
173
|
+
// Remove old entries
|
|
174
|
+
this.errorCounts = this.errorCounts.filter(e => e.timestamp > windowStart);
|
|
175
|
+
// Add current error
|
|
176
|
+
const lastEntry = this.errorCounts[this.errorCounts.length - 1];
|
|
177
|
+
if (lastEntry && lastEntry.timestamp > now - 60 * 1000) {
|
|
178
|
+
lastEntry.count++;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
this.errorCounts.push({ timestamp: now, count: 1 });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
checkErrorSpike() {
|
|
185
|
+
const now = Date.now();
|
|
186
|
+
const windowStart = now - 5 * 60 * 1000;
|
|
187
|
+
const recentErrors = this.errorCounts
|
|
188
|
+
.filter(e => e.timestamp > windowStart)
|
|
189
|
+
.reduce((sum, e) => sum + e.count, 0);
|
|
190
|
+
if (recentErrors >= this.errorSpikeThreshold) {
|
|
191
|
+
this.createAlert({
|
|
192
|
+
type: 'error_spike',
|
|
193
|
+
severity: 'error',
|
|
194
|
+
title: 'Error Spike Detected',
|
|
195
|
+
message: `${recentErrors} errors detected in the last 5 minutes (threshold: ${this.errorSpikeThreshold})`,
|
|
196
|
+
metadata: {
|
|
197
|
+
errorCount: recentErrors,
|
|
198
|
+
threshold: this.errorSpikeThreshold,
|
|
199
|
+
windowMs: 5 * 60 * 1000,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
checkCustomRules(trace) {
|
|
205
|
+
this.rules.forEach(rule => {
|
|
206
|
+
if (!rule.enabled)
|
|
207
|
+
return;
|
|
208
|
+
// Evaluate rule condition (simplified evaluation)
|
|
209
|
+
if (this.evaluateRule(rule, trace)) {
|
|
210
|
+
this.createAlert({
|
|
211
|
+
type: rule.type,
|
|
212
|
+
severity: rule.severity,
|
|
213
|
+
title: rule.name,
|
|
214
|
+
message: `Rule "${rule.name}" triggered on ${trace.method} ${trace.endpoint}`,
|
|
215
|
+
traceId: trace.traceId,
|
|
216
|
+
endpoint: trace.endpoint,
|
|
217
|
+
metadata: {
|
|
218
|
+
ruleId: rule.id,
|
|
219
|
+
condition: rule.condition,
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
evaluateRule(rule, trace) {
|
|
226
|
+
// Simplified rule evaluation
|
|
227
|
+
// In production, use a proper expression parser
|
|
228
|
+
const condition = rule.condition;
|
|
229
|
+
if (condition.includes('classification == CRITICAL') && trace.classification === 'CRITICAL') {
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
if (condition.includes('duration >') && trace.totalDuration > rule.threshold) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
isDuplicate(alert) {
|
|
238
|
+
const now = Date.now();
|
|
239
|
+
return this.alerts.some(a => a.type === alert.type &&
|
|
240
|
+
a.endpoint === alert.endpoint &&
|
|
241
|
+
now - a.timestamp.getTime() < this.dedupWindowMs);
|
|
242
|
+
}
|
|
243
|
+
async deliverAlert(alert) {
|
|
244
|
+
const deliveryPromises = this.webhooks.map(url => this.sendWebhook(url, alert));
|
|
245
|
+
try {
|
|
246
|
+
await Promise.all(deliveryPromises);
|
|
247
|
+
alert.delivered = true;
|
|
248
|
+
alert.deliveredAt = new Date();
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
console.error('[AlertManager] Failed to deliver alert:', error);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async sendWebhook(url, alert) {
|
|
255
|
+
try {
|
|
256
|
+
const response = await fetch(url, {
|
|
257
|
+
method: 'POST',
|
|
258
|
+
headers: { 'Content-Type': 'application/json' },
|
|
259
|
+
body: JSON.stringify({
|
|
260
|
+
alert_type: 'flow_debugger',
|
|
261
|
+
...alert,
|
|
262
|
+
}),
|
|
263
|
+
});
|
|
264
|
+
if (!response.ok) {
|
|
265
|
+
throw new Error(`Webhook responded with ${response.status}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
// Log but don't throw - don't fail the app
|
|
270
|
+
console.error(`[AlertManager] Webhook ${url} failed:`, error);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
generateAlertId() {
|
|
274
|
+
return `alert_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Slack-specific alert formatter
|
|
279
|
+
*/
|
|
280
|
+
export function formatSlackMessage(alert) {
|
|
281
|
+
const colorMap = {
|
|
282
|
+
info: '#36a64f',
|
|
283
|
+
warning: '#ffcc00',
|
|
284
|
+
error: '#ff6600',
|
|
285
|
+
critical: '#ff0000',
|
|
286
|
+
};
|
|
287
|
+
return {
|
|
288
|
+
attachments: [
|
|
289
|
+
{
|
|
290
|
+
color: colorMap[alert.severity],
|
|
291
|
+
title: alert.title,
|
|
292
|
+
text: alert.message,
|
|
293
|
+
fields: [
|
|
294
|
+
{ title: 'Type', value: alert.type, short: true },
|
|
295
|
+
{ title: 'Severity', value: alert.severity, short: true },
|
|
296
|
+
...(alert.endpoint ? [{ title: 'Endpoint', value: alert.endpoint, short: true }] : []),
|
|
297
|
+
...(alert.traceId ? [{ title: 'Trace ID', value: alert.traceId, short: true }] : []),
|
|
298
|
+
],
|
|
299
|
+
ts: Math.floor(alert.timestamp.getTime() / 1000),
|
|
300
|
+
footer: 'Flow Debugger',
|
|
301
|
+
},
|
|
302
|
+
],
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
//# sourceMappingURL=Alerting.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Alerting.js","sourceRoot":"","sources":["../../../src/core/Alerting.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,4CAA4C;AAC5C,kDAAkD;AAClD,gEAAgE;AAEhE,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,YAAY;IAW1C,YACI,WAAqB,EAAE,EACvB,kBAA2B,IAAI,EAC/B,oBAA6B,IAAI,EACjC,sBAA8B,CAAC;QAE/B,KAAK,EAAE,CAAC;QAhBJ,WAAM,GAAY,EAAE,CAAC;QACrB,UAAK,GAA2B,IAAI,GAAG,EAAE,CAAC;QAC1C,aAAQ,GAAa,EAAE,CAAC;QACxB,cAAS,GAAW,IAAI,CAAC;QACzB,kBAAa,GAAW,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,YAAY;QAInD,gBAAW,GAA2C,EAAE,CAAC;QAS7D,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,IAAI,CAAC,mBAAmB,GAAG,mBAAmB,CAAC;QAC/C,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IAED;;OAEG;IACK,iBAAiB;QACrB,IAAI,CAAC,OAAO,CAAC;YACT,EAAE,EAAE,gBAAgB;YAC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AAC,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;oBACtF,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;iBACvF;gBACD,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;gBAChD,MAAM,EAAE,eAAe;aAC1B;SACJ;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// ─────────────────────────────────────────────────────────────
|
|
2
|
+
// flow-debugger — Anomaly Detection System
|
|
3
|
+
// Statistical and ML-based anomaly detection for metrics
|
|
4
|
+
// ─────────────────────────────────────────────────────────────
|
|
5
|
+
/**
|
|
6
|
+
* Anomaly Detector - Uses statistical methods to detect anomalies
|
|
7
|
+
*/
|
|
8
|
+
export class AnomalyDetector {
|
|
9
|
+
constructor(sensitivity = 2) {
|
|
10
|
+
this.baseline = new Map();
|
|
11
|
+
this.minSamples = 30;
|
|
12
|
+
this.decayFactor = 0.1; // For exponential moving average
|
|
13
|
+
this.sensitivity = sensitivity;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Record a data point for a metric
|
|
17
|
+
*/
|
|
18
|
+
record(metric, value, timestamp = new Date()) {
|
|
19
|
+
const stats = this.baseline.get(metric);
|
|
20
|
+
if (!stats) {
|
|
21
|
+
this.baseline.set(metric, {
|
|
22
|
+
count: 1,
|
|
23
|
+
mean: value,
|
|
24
|
+
m2: 0,
|
|
25
|
+
min: value,
|
|
26
|
+
max: value,
|
|
27
|
+
lastUpdated: timestamp,
|
|
28
|
+
values: [value],
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
// Welford's online algorithm for variance
|
|
33
|
+
stats.count++;
|
|
34
|
+
const delta = value - stats.mean;
|
|
35
|
+
stats.mean += delta / stats.count;
|
|
36
|
+
const delta2 = value - stats.mean;
|
|
37
|
+
stats.m2 += delta * delta2;
|
|
38
|
+
stats.min = Math.min(stats.min, value);
|
|
39
|
+
stats.max = Math.max(stats.max, value);
|
|
40
|
+
stats.lastUpdated = timestamp;
|
|
41
|
+
// Keep recent values for percentile calculations
|
|
42
|
+
stats.values.push(value);
|
|
43
|
+
if (stats.values.length > 1000) {
|
|
44
|
+
stats.values.shift();
|
|
45
|
+
}
|
|
46
|
+
// Exponential moving average for recent behavior
|
|
47
|
+
stats.ema = stats.ema ? stats.ema * (1 - this.decayFactor) + value * this.decayFactor : value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Check if a value is anomalous
|
|
52
|
+
*/
|
|
53
|
+
detect(metric, value, timestamp = new Date()) {
|
|
54
|
+
const stats = this.baseline.get(metric);
|
|
55
|
+
if (!stats || stats.count < this.minSamples) {
|
|
56
|
+
// Not enough data for detection
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
const variance = stats.m2 / (stats.count - 1);
|
|
60
|
+
const stdDev = Math.sqrt(variance);
|
|
61
|
+
if (stdDev === 0) {
|
|
62
|
+
// No variance - any difference is anomalous
|
|
63
|
+
return {
|
|
64
|
+
isAnomaly: value !== stats.mean,
|
|
65
|
+
score: value !== stats.mean ? 1 : 0,
|
|
66
|
+
expectedValue: stats.mean,
|
|
67
|
+
actualValue: value,
|
|
68
|
+
deviation: value !== stats.mean ? Infinity : 0,
|
|
69
|
+
metric,
|
|
70
|
+
timestamp,
|
|
71
|
+
confidence: 50,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const deviation = Math.abs(value - stats.mean) / stdDev;
|
|
75
|
+
const isAnomaly = deviation > this.sensitivity;
|
|
76
|
+
const score = Math.min(1, deviation / (this.sensitivity * 2));
|
|
77
|
+
// Calculate confidence based on sample size
|
|
78
|
+
const confidence = Math.min(100, (stats.count / this.minSamples) * 100);
|
|
79
|
+
return {
|
|
80
|
+
isAnomaly,
|
|
81
|
+
score,
|
|
82
|
+
expectedValue: stats.mean,
|
|
83
|
+
actualValue: value,
|
|
84
|
+
deviation,
|
|
85
|
+
metric,
|
|
86
|
+
timestamp,
|
|
87
|
+
confidence,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Analyze a trace for anomalies
|
|
92
|
+
*/
|
|
93
|
+
analyzeTrace(trace) {
|
|
94
|
+
const results = [];
|
|
95
|
+
// Check total duration
|
|
96
|
+
const durationResult = this.detect('request_duration', trace.totalDuration, trace.timestamp);
|
|
97
|
+
if (durationResult) {
|
|
98
|
+
results.push(durationResult);
|
|
99
|
+
}
|
|
100
|
+
// Check each step duration
|
|
101
|
+
trace.steps.forEach((step, index) => {
|
|
102
|
+
const stepMetric = `step_duration_${step.service}_${step.name.replace(/\s+/g, '_')}`;
|
|
103
|
+
const stepResult = this.detect(stepMetric, step.duration, trace.timestamp);
|
|
104
|
+
if (stepResult && stepResult.isAnomaly) {
|
|
105
|
+
results.push(stepResult);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return results;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get baseline statistics for a metric
|
|
112
|
+
*/
|
|
113
|
+
getBaseline(metric) {
|
|
114
|
+
return this.baseline.get(metric) || null;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get all baselines
|
|
118
|
+
*/
|
|
119
|
+
getAllBaselines() {
|
|
120
|
+
return new Map(this.baseline);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Reset baseline for a metric
|
|
124
|
+
*/
|
|
125
|
+
reset(metric) {
|
|
126
|
+
this.baseline.delete(metric);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Clear all baselines
|
|
130
|
+
*/
|
|
131
|
+
clear() {
|
|
132
|
+
this.baseline.clear();
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get percentiles for a metric
|
|
136
|
+
*/
|
|
137
|
+
getPercentiles(metric) {
|
|
138
|
+
const stats = this.baseline.get(metric);
|
|
139
|
+
if (!stats || stats.values.length < 10)
|
|
140
|
+
return null;
|
|
141
|
+
const sorted = [...stats.values].sort((a, b) => a - b);
|
|
142
|
+
const length = sorted.length;
|
|
143
|
+
return {
|
|
144
|
+
p50: sorted[Math.floor(length * 0.5)],
|
|
145
|
+
p90: sorted[Math.floor(length * 0.9)],
|
|
146
|
+
p95: sorted[Math.floor(length * 0.95)],
|
|
147
|
+
p99: sorted[Math.floor(length * 0.99)],
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Detect seasonal patterns (simplified)
|
|
152
|
+
*/
|
|
153
|
+
detectSeasonality(metric, points, periodMs) {
|
|
154
|
+
if (points.length < periodMs * 2)
|
|
155
|
+
return false;
|
|
156
|
+
// Autocorrelation-based seasonality detection
|
|
157
|
+
const values = points.map(p => p.value);
|
|
158
|
+
const mean = values.reduce((a, b) => a + b, 0) / values.length;
|
|
159
|
+
const variance = values.reduce((sum, v) => sum + Math.pow(v - mean, 2), 0) / values.length;
|
|
160
|
+
if (variance === 0)
|
|
161
|
+
return false;
|
|
162
|
+
// Calculate autocorrelation at lag = period
|
|
163
|
+
const lag = Math.min(periodMs, values.length / 2);
|
|
164
|
+
let autocorr = 0;
|
|
165
|
+
for (let i = 0; i < values.length - lag; i++) {
|
|
166
|
+
autocorr += (values[i] - mean) * (values[i + lag] - mean);
|
|
167
|
+
}
|
|
168
|
+
autocorr /= (values.length - lag) * variance;
|
|
169
|
+
// Strong autocorrelation indicates seasonality
|
|
170
|
+
return Math.abs(autocorr) > 0.5;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* CUSUM algorithm for change point detection
|
|
175
|
+
*/
|
|
176
|
+
export class CUSUMDetector {
|
|
177
|
+
constructor(threshold = 5, drift = 0.5) {
|
|
178
|
+
this.sum = 0;
|
|
179
|
+
this.target = 0;
|
|
180
|
+
this.changePoints = [];
|
|
181
|
+
this.threshold = threshold;
|
|
182
|
+
this.drift = drift;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Process a new value and detect change points
|
|
186
|
+
*/
|
|
187
|
+
update(value, timestamp = new Date()) {
|
|
188
|
+
if (this.target === 0) {
|
|
189
|
+
this.target = value;
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
// Update target with moving average
|
|
193
|
+
this.target = this.target * 0.9 + value * 0.1;
|
|
194
|
+
// CUSUM calculation
|
|
195
|
+
this.sum = Math.max(0, this.sum + (value - this.target) - this.drift);
|
|
196
|
+
if (this.sum > this.threshold) {
|
|
197
|
+
this.changePoints.push({ timestamp, value });
|
|
198
|
+
this.sum = 0;
|
|
199
|
+
return true; // Change point detected
|
|
200
|
+
}
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Get detected change points
|
|
205
|
+
*/
|
|
206
|
+
getChangePoints() {
|
|
207
|
+
return [...this.changePoints];
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Reset detector
|
|
211
|
+
*/
|
|
212
|
+
reset() {
|
|
213
|
+
this.sum = 0;
|
|
214
|
+
this.target = 0;
|
|
215
|
+
this.changePoints = [];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
//# sourceMappingURL=AnomalyDetection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AnomalyDetection.js","sourceRoot":"","sources":["../../../src/core/AnomalyDetection.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,2CAA2C;AAC3C,yDAAyD;AACzD,gEAAgE;AAIhE;;GAEG;AAC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