@contractspec/lib.observability 1.57.0 → 1.58.0
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/CHANGELOG.md +13 -0
- package/dist/anomaly/alert-manager.d.ts +17 -0
- package/dist/anomaly/alert-manager.js +24 -0
- package/dist/anomaly/anomaly-detector.d.ts +22 -0
- package/dist/anomaly/anomaly-detector.js +102 -0
- package/dist/anomaly/baseline-calculator.d.ts +23 -0
- package/dist/anomaly/baseline-calculator.js +40 -0
- package/dist/anomaly/root-cause-analyzer.d.ts +19 -0
- package/dist/anomaly/root-cause-analyzer.js +32 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +1078 -0
- package/dist/intent/aggregator.d.ts +57 -0
- package/dist/intent/aggregator.js +110 -0
- package/dist/intent/detector.d.ts +28 -0
- package/dist/intent/detector.js +133 -0
- package/dist/logging/index.d.ts +17 -0
- package/dist/logging/index.js +42 -0
- package/dist/metrics/index.d.ts +12 -0
- package/dist/metrics/index.js +31 -0
- package/dist/node/anomaly/alert-manager.js +23 -0
- package/dist/node/anomaly/anomaly-detector.js +101 -0
- package/dist/node/anomaly/baseline-calculator.js +39 -0
- package/dist/node/anomaly/root-cause-analyzer.js +31 -0
- package/dist/node/index.js +1077 -0
- package/dist/node/intent/aggregator.js +109 -0
- package/dist/node/intent/detector.js +132 -0
- package/dist/node/logging/index.js +41 -0
- package/dist/node/metrics/index.js +30 -0
- package/dist/node/pipeline/evolution-pipeline.js +299 -0
- package/dist/node/pipeline/lifecycle-pipeline.js +85 -0
- package/dist/node/telemetry/posthog-baseline-reader.js +308 -0
- package/dist/node/telemetry/posthog-telemetry.js +60 -0
- package/dist/node/tracing/index.js +52 -0
- package/dist/node/tracing/middleware.js +150 -0
- package/dist/pipeline/evolution-pipeline.d.ts +36 -0
- package/dist/pipeline/evolution-pipeline.js +300 -0
- package/dist/pipeline/lifecycle-pipeline.d.ts +40 -0
- package/dist/pipeline/lifecycle-pipeline.js +86 -0
- package/dist/telemetry/posthog-baseline-reader.d.ts +27 -0
- package/dist/telemetry/posthog-baseline-reader.js +309 -0
- package/dist/telemetry/posthog-telemetry.d.ts +15 -0
- package/dist/telemetry/posthog-telemetry.js +61 -0
- package/dist/tracing/index.d.ts +5 -0
- package/dist/tracing/index.js +53 -0
- package/dist/tracing/middleware.d.ts +15 -0
- package/dist/tracing/middleware.js +151 -0
- package/package.json +140 -43
- package/dist/anomaly/alert-manager.d.mts +0 -21
- package/dist/anomaly/alert-manager.mjs +0 -23
- package/dist/anomaly/anomaly-detector.d.mts +0 -26
- package/dist/anomaly/anomaly-detector.mjs +0 -58
- package/dist/anomaly/baseline-calculator.d.mts +0 -26
- package/dist/anomaly/baseline-calculator.mjs +0 -37
- package/dist/anomaly/root-cause-analyzer.d.mts +0 -23
- package/dist/anomaly/root-cause-analyzer.mjs +0 -27
- package/dist/index.d.mts +0 -15
- package/dist/index.mjs +0 -16
- package/dist/intent/aggregator.d.mts +0 -60
- package/dist/intent/aggregator.mjs +0 -98
- package/dist/intent/detector.d.mts +0 -32
- package/dist/intent/detector.mjs +0 -122
- package/dist/logging/index.d.mts +0 -20
- package/dist/logging/index.mjs +0 -40
- package/dist/metrics/index.d.mts +0 -17
- package/dist/metrics/index.mjs +0 -26
- package/dist/pipeline/evolution-pipeline.d.mts +0 -40
- package/dist/pipeline/evolution-pipeline.mjs +0 -66
- package/dist/pipeline/lifecycle-pipeline.d.mts +0 -44
- package/dist/pipeline/lifecycle-pipeline.mjs +0 -73
- package/dist/telemetry/posthog-baseline-reader.d.mts +0 -31
- package/dist/telemetry/posthog-baseline-reader.mjs +0 -266
- package/dist/telemetry/posthog-telemetry.d.mts +0 -19
- package/dist/telemetry/posthog-telemetry.mjs +0 -61
- package/dist/tracing/index.d.mts +0 -9
- package/dist/tracing/index.mjs +0 -47
- package/dist/tracing/middleware.d.mts +0 -19
- package/dist/tracing/middleware.mjs +0 -80
package/dist/index.js
ADDED
|
@@ -0,0 +1,1078 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/anomaly/alert-manager.ts
|
|
3
|
+
class AlertManager {
|
|
4
|
+
options;
|
|
5
|
+
cooldownMs;
|
|
6
|
+
lastAlert = new Map;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
this.cooldownMs = options.cooldownMs ?? 60000;
|
|
10
|
+
}
|
|
11
|
+
async notify(signal, analysis) {
|
|
12
|
+
const key = `${signal.type}:${analysis.culprit?.id ?? "none"}`;
|
|
13
|
+
const now = Date.now();
|
|
14
|
+
const last = this.lastAlert.get(key) ?? 0;
|
|
15
|
+
if (now - last < this.cooldownMs) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
await this.options.transport({ signal, analysis });
|
|
19
|
+
this.lastAlert.set(key, now);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/anomaly/baseline-calculator.ts
|
|
24
|
+
class BaselineCalculator {
|
|
25
|
+
alpha;
|
|
26
|
+
snapshot = {
|
|
27
|
+
latencyP99: 0,
|
|
28
|
+
latencyP95: 0,
|
|
29
|
+
errorRate: 0,
|
|
30
|
+
throughput: 0,
|
|
31
|
+
sampleCount: 0
|
|
32
|
+
};
|
|
33
|
+
constructor(alpha = 0.2) {
|
|
34
|
+
this.alpha = alpha;
|
|
35
|
+
}
|
|
36
|
+
update(point) {
|
|
37
|
+
const { sampleCount } = this.snapshot;
|
|
38
|
+
const nextCount = sampleCount + 1;
|
|
39
|
+
const weight = sampleCount === 0 ? 1 : this.alpha;
|
|
40
|
+
this.snapshot = {
|
|
41
|
+
latencyP99: this.mix(this.snapshot.latencyP99, point.latencyP99, weight),
|
|
42
|
+
latencyP95: this.mix(this.snapshot.latencyP95, point.latencyP95, weight),
|
|
43
|
+
errorRate: this.mix(this.snapshot.errorRate, point.errorRate, weight),
|
|
44
|
+
throughput: this.mix(this.snapshot.throughput, point.throughput, weight),
|
|
45
|
+
sampleCount: nextCount
|
|
46
|
+
};
|
|
47
|
+
return this.snapshot;
|
|
48
|
+
}
|
|
49
|
+
getSnapshot() {
|
|
50
|
+
return this.snapshot;
|
|
51
|
+
}
|
|
52
|
+
mix(current, next, weight) {
|
|
53
|
+
if (this.snapshot.sampleCount === 0) {
|
|
54
|
+
return next;
|
|
55
|
+
}
|
|
56
|
+
return current * (1 - weight) + next * weight;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/anomaly/anomaly-detector.ts
|
|
61
|
+
class AnomalyDetector {
|
|
62
|
+
baseline;
|
|
63
|
+
thresholds = {
|
|
64
|
+
errorRateDelta: 0.5,
|
|
65
|
+
latencyDelta: 0.35,
|
|
66
|
+
throughputDrop: 0.4,
|
|
67
|
+
minSamples: 10
|
|
68
|
+
};
|
|
69
|
+
constructor(options = {}) {
|
|
70
|
+
this.baseline = new BaselineCalculator;
|
|
71
|
+
this.thresholds = { ...this.thresholds, ...options };
|
|
72
|
+
}
|
|
73
|
+
evaluate(point) {
|
|
74
|
+
const baselineSnapshot = this.baseline.update(point);
|
|
75
|
+
if (baselineSnapshot.sampleCount < this.thresholds.minSamples) {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
const signals = [];
|
|
79
|
+
const errorDelta = this.relativeDelta(point.errorRate, baselineSnapshot.errorRate);
|
|
80
|
+
if (errorDelta > this.thresholds.errorRateDelta) {
|
|
81
|
+
signals.push({
|
|
82
|
+
type: "error_rate_spike",
|
|
83
|
+
delta: errorDelta,
|
|
84
|
+
point,
|
|
85
|
+
baseline: baselineSnapshot
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const latencyDelta = this.relativeDelta(point.latencyP99, baselineSnapshot.latencyP99);
|
|
89
|
+
if (latencyDelta > this.thresholds.latencyDelta) {
|
|
90
|
+
signals.push({
|
|
91
|
+
type: "latency_regression",
|
|
92
|
+
delta: latencyDelta,
|
|
93
|
+
point,
|
|
94
|
+
baseline: baselineSnapshot
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const throughputDelta = this.relativeDrop(point.throughput, baselineSnapshot.throughput);
|
|
98
|
+
if (throughputDelta > this.thresholds.throughputDrop) {
|
|
99
|
+
signals.push({
|
|
100
|
+
type: "throughput_drop",
|
|
101
|
+
delta: throughputDelta,
|
|
102
|
+
point,
|
|
103
|
+
baseline: baselineSnapshot
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return signals;
|
|
107
|
+
}
|
|
108
|
+
relativeDelta(value, baseline) {
|
|
109
|
+
if (baseline === 0) {
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
return (value - baseline) / baseline;
|
|
113
|
+
}
|
|
114
|
+
relativeDrop(value, baseline) {
|
|
115
|
+
if (baseline === 0) {
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
return (baseline - value) / baseline;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/anomaly/root-cause-analyzer.ts
|
|
123
|
+
class RootCauseAnalyzer {
|
|
124
|
+
lookbackMs;
|
|
125
|
+
constructor(lookbackMs = 15 * 60 * 1000) {
|
|
126
|
+
this.lookbackMs = lookbackMs;
|
|
127
|
+
}
|
|
128
|
+
analyze(signal, deployments) {
|
|
129
|
+
const windowStart = new Date(signal.point.timestamp.getTime() - this.lookbackMs);
|
|
130
|
+
const candidates = deployments.filter((deployment) => deployment.deployedAt >= windowStart).sort((a, b) => b.deployedAt.getTime() - a.deployedAt.getTime());
|
|
131
|
+
const notes = [];
|
|
132
|
+
let culprit;
|
|
133
|
+
if (candidates.length > 0) {
|
|
134
|
+
culprit = candidates[0];
|
|
135
|
+
if (culprit) {
|
|
136
|
+
notes.push(`Closest deployment ${culprit.id} (${culprit.operation}) at ${culprit.deployedAt.toISOString()}`);
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
notes.push("No deployments found within lookback window.");
|
|
140
|
+
}
|
|
141
|
+
if (signal.type === "latency_regression") {
|
|
142
|
+
notes.push("Verify recent schema changes and external dependency latency.");
|
|
143
|
+
}
|
|
144
|
+
if (signal.type === "error_rate_spike") {
|
|
145
|
+
notes.push("Check SLO monitor for correlated incidents.");
|
|
146
|
+
}
|
|
147
|
+
return { signal, culprit, notes };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/tracing/index.ts
|
|
152
|
+
import {
|
|
153
|
+
SpanStatusCode,
|
|
154
|
+
trace
|
|
155
|
+
} from "@opentelemetry/api";
|
|
156
|
+
var DEFAULT_TRACER_NAME = "@contractspec/lib.observability";
|
|
157
|
+
function getTracer(name = DEFAULT_TRACER_NAME) {
|
|
158
|
+
return trace.getTracer(name);
|
|
159
|
+
}
|
|
160
|
+
async function traceAsync(name, fn, tracerName) {
|
|
161
|
+
const tracer = getTracer(tracerName);
|
|
162
|
+
return tracer.startActiveSpan(name, async (span) => {
|
|
163
|
+
try {
|
|
164
|
+
const result = await fn(span);
|
|
165
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
166
|
+
return result;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
span.recordException(error);
|
|
169
|
+
span.setStatus({
|
|
170
|
+
code: SpanStatusCode.ERROR,
|
|
171
|
+
message: error instanceof Error ? error.message : String(error)
|
|
172
|
+
});
|
|
173
|
+
throw error;
|
|
174
|
+
} finally {
|
|
175
|
+
span.end();
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
function traceSync(name, fn, tracerName) {
|
|
180
|
+
const tracer = getTracer(tracerName);
|
|
181
|
+
return tracer.startActiveSpan(name, (span) => {
|
|
182
|
+
try {
|
|
183
|
+
const result = fn(span);
|
|
184
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
185
|
+
return result;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
span.recordException(error);
|
|
188
|
+
span.setStatus({
|
|
189
|
+
code: SpanStatusCode.ERROR,
|
|
190
|
+
message: error instanceof Error ? error.message : String(error)
|
|
191
|
+
});
|
|
192
|
+
throw error;
|
|
193
|
+
} finally {
|
|
194
|
+
span.end();
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// src/metrics/index.ts
|
|
200
|
+
import {
|
|
201
|
+
metrics
|
|
202
|
+
} from "@opentelemetry/api";
|
|
203
|
+
var DEFAULT_METER_NAME = "@contractspec/lib.observability";
|
|
204
|
+
function getMeter(name = DEFAULT_METER_NAME) {
|
|
205
|
+
return metrics.getMeter(name);
|
|
206
|
+
}
|
|
207
|
+
function createCounter(name, description, meterName) {
|
|
208
|
+
return getMeter(meterName).createCounter(name, { description });
|
|
209
|
+
}
|
|
210
|
+
function createUpDownCounter(name, description, meterName) {
|
|
211
|
+
return getMeter(meterName).createUpDownCounter(name, { description });
|
|
212
|
+
}
|
|
213
|
+
function createHistogram(name, description, meterName) {
|
|
214
|
+
return getMeter(meterName).createHistogram(name, { description });
|
|
215
|
+
}
|
|
216
|
+
var standardMetrics = {
|
|
217
|
+
httpRequests: createCounter("http_requests_total", "Total HTTP requests"),
|
|
218
|
+
httpDuration: createHistogram("http_request_duration_seconds", "HTTP request duration"),
|
|
219
|
+
operationErrors: createCounter("operation_errors_total", "Total operation errors"),
|
|
220
|
+
workflowDuration: createHistogram("workflow_duration_seconds", "Workflow execution duration")
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// src/logging/index.ts
|
|
224
|
+
import { trace as trace2, context } from "@opentelemetry/api";
|
|
225
|
+
|
|
226
|
+
class Logger {
|
|
227
|
+
serviceName;
|
|
228
|
+
constructor(serviceName) {
|
|
229
|
+
this.serviceName = serviceName;
|
|
230
|
+
}
|
|
231
|
+
log(level, message, meta = {}) {
|
|
232
|
+
const span = trace2.getSpan(context.active());
|
|
233
|
+
const traceId = span?.spanContext().traceId;
|
|
234
|
+
const spanId = span?.spanContext().spanId;
|
|
235
|
+
const entry = {
|
|
236
|
+
timestamp: new Date().toISOString(),
|
|
237
|
+
service: this.serviceName,
|
|
238
|
+
level,
|
|
239
|
+
message,
|
|
240
|
+
traceId,
|
|
241
|
+
spanId,
|
|
242
|
+
...meta
|
|
243
|
+
};
|
|
244
|
+
console.log(JSON.stringify(entry));
|
|
245
|
+
}
|
|
246
|
+
debug(message, meta) {
|
|
247
|
+
this.log("debug", message, meta);
|
|
248
|
+
}
|
|
249
|
+
info(message, meta) {
|
|
250
|
+
this.log("info", message, meta);
|
|
251
|
+
}
|
|
252
|
+
warn(message, meta) {
|
|
253
|
+
this.log("warn", message, meta);
|
|
254
|
+
}
|
|
255
|
+
error(message, meta) {
|
|
256
|
+
this.log("error", message, meta);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
var logger = new Logger(process.env.OTEL_SERVICE_NAME || "unknown-service");
|
|
260
|
+
|
|
261
|
+
// src/tracing/middleware.ts
|
|
262
|
+
function createTracingMiddleware(options = {}) {
|
|
263
|
+
return async (req, next) => {
|
|
264
|
+
const method = req.method;
|
|
265
|
+
const url = new URL(req.url);
|
|
266
|
+
const path = url.pathname;
|
|
267
|
+
standardMetrics.httpRequests.add(1, { method, path });
|
|
268
|
+
const startTime = performance.now();
|
|
269
|
+
return traceAsync(`HTTP ${method} ${path}`, async (span) => {
|
|
270
|
+
span.setAttribute("http.method", method);
|
|
271
|
+
span.setAttribute("http.url", req.url);
|
|
272
|
+
try {
|
|
273
|
+
const response = await next();
|
|
274
|
+
span.setAttribute("http.status_code", response.status);
|
|
275
|
+
const duration = (performance.now() - startTime) / 1000;
|
|
276
|
+
standardMetrics.httpDuration.record(duration, {
|
|
277
|
+
method,
|
|
278
|
+
path,
|
|
279
|
+
status: response.status.toString()
|
|
280
|
+
});
|
|
281
|
+
emitTelemetrySample({
|
|
282
|
+
req,
|
|
283
|
+
res: response,
|
|
284
|
+
span,
|
|
285
|
+
success: true,
|
|
286
|
+
durationMs: duration * 1000,
|
|
287
|
+
options
|
|
288
|
+
});
|
|
289
|
+
return response;
|
|
290
|
+
} catch (error) {
|
|
291
|
+
standardMetrics.operationErrors.add(1, { method, path });
|
|
292
|
+
emitTelemetrySample({
|
|
293
|
+
req,
|
|
294
|
+
span,
|
|
295
|
+
success: false,
|
|
296
|
+
durationMs: performance.now() - startTime,
|
|
297
|
+
error,
|
|
298
|
+
options
|
|
299
|
+
});
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
function emitTelemetrySample({
|
|
306
|
+
req,
|
|
307
|
+
res,
|
|
308
|
+
span,
|
|
309
|
+
success,
|
|
310
|
+
durationMs,
|
|
311
|
+
error,
|
|
312
|
+
options
|
|
313
|
+
}) {
|
|
314
|
+
if (!options.onSample || !options.resolveOperation)
|
|
315
|
+
return;
|
|
316
|
+
const operation = options.resolveOperation({ req, res });
|
|
317
|
+
if (!operation)
|
|
318
|
+
return;
|
|
319
|
+
const sample = {
|
|
320
|
+
operation,
|
|
321
|
+
durationMs,
|
|
322
|
+
success,
|
|
323
|
+
timestamp: new Date,
|
|
324
|
+
errorCode: !success && error instanceof Error ? error.name : success ? undefined : "unknown",
|
|
325
|
+
tenantId: options.tenantResolver?.(req),
|
|
326
|
+
actorId: options.actorResolver?.(req),
|
|
327
|
+
traceId: span.spanContext().traceId,
|
|
328
|
+
metadata: {
|
|
329
|
+
method: req.method,
|
|
330
|
+
path: new URL(req.url).pathname,
|
|
331
|
+
status: res?.status
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
options.onSample(sample);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// src/intent/aggregator.ts
|
|
338
|
+
var DEFAULT_WINDOW_MS = 15 * 60 * 1000;
|
|
339
|
+
|
|
340
|
+
class IntentAggregator {
|
|
341
|
+
windowMs;
|
|
342
|
+
sequenceSampleSize;
|
|
343
|
+
samples = [];
|
|
344
|
+
constructor(options = {}) {
|
|
345
|
+
this.windowMs = options.windowMs ?? DEFAULT_WINDOW_MS;
|
|
346
|
+
this.sequenceSampleSize = options.sequenceSampleSize ?? 1000;
|
|
347
|
+
}
|
|
348
|
+
add(sample) {
|
|
349
|
+
this.samples.push(sample);
|
|
350
|
+
}
|
|
351
|
+
flush(now = new Date) {
|
|
352
|
+
const minTimestamp = now.getTime() - this.windowMs;
|
|
353
|
+
const windowSamples = this.samples.filter((sample) => sample.timestamp.getTime() >= minTimestamp);
|
|
354
|
+
this.samples.length = 0;
|
|
355
|
+
const metrics2 = this.aggregateMetrics(windowSamples);
|
|
356
|
+
const sequences = this.buildSequences(windowSamples);
|
|
357
|
+
const timestamps = windowSamples.map((sample) => sample.timestamp.getTime());
|
|
358
|
+
return {
|
|
359
|
+
metrics: metrics2,
|
|
360
|
+
sequences,
|
|
361
|
+
sampleCount: windowSamples.length,
|
|
362
|
+
windowStart: timestamps.length ? new Date(Math.min(...timestamps)) : undefined,
|
|
363
|
+
windowEnd: timestamps.length ? new Date(Math.max(...timestamps)) : undefined
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
aggregateMetrics(samples) {
|
|
367
|
+
if (!samples.length)
|
|
368
|
+
return [];
|
|
369
|
+
const groups = new Map;
|
|
370
|
+
for (const sample of samples) {
|
|
371
|
+
const key = `${sample.operation.name}.v${sample.operation.version}`;
|
|
372
|
+
const arr = groups.get(key) ?? [];
|
|
373
|
+
arr.push(sample);
|
|
374
|
+
groups.set(key, arr);
|
|
375
|
+
}
|
|
376
|
+
return [...groups.values()].map((group) => {
|
|
377
|
+
const first = group[0];
|
|
378
|
+
if (!first)
|
|
379
|
+
throw new Error("Empty group in aggregation");
|
|
380
|
+
const durations = group.map((s) => s.durationMs).sort((a, b) => a - b);
|
|
381
|
+
const errors = group.filter((s) => !s.success);
|
|
382
|
+
const totalCalls = group.length;
|
|
383
|
+
const topErrors = errors.reduce((acc, sample) => {
|
|
384
|
+
if (!sample.errorCode)
|
|
385
|
+
return acc;
|
|
386
|
+
acc[sample.errorCode] = (acc[sample.errorCode] ?? 0) + 1;
|
|
387
|
+
return acc;
|
|
388
|
+
}, {});
|
|
389
|
+
const timestamps = group.map((s) => s.timestamp.getTime());
|
|
390
|
+
return {
|
|
391
|
+
operation: first.operation,
|
|
392
|
+
totalCalls,
|
|
393
|
+
successRate: (totalCalls - errors.length) / totalCalls,
|
|
394
|
+
errorRate: errors.length / totalCalls,
|
|
395
|
+
averageLatencyMs: durations.reduce((sum, value) => sum + value, 0) / totalCalls,
|
|
396
|
+
p95LatencyMs: percentile(durations, 0.95),
|
|
397
|
+
p99LatencyMs: percentile(durations, 0.99),
|
|
398
|
+
maxLatencyMs: Math.max(...durations),
|
|
399
|
+
windowStart: new Date(Math.min(...timestamps)),
|
|
400
|
+
windowEnd: new Date(Math.max(...timestamps)),
|
|
401
|
+
topErrors
|
|
402
|
+
};
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
buildSequences(samples) {
|
|
406
|
+
const byTrace = new Map;
|
|
407
|
+
for (const sample of samples.slice(-this.sequenceSampleSize)) {
|
|
408
|
+
if (!sample.traceId)
|
|
409
|
+
continue;
|
|
410
|
+
const arr = byTrace.get(sample.traceId) ?? [];
|
|
411
|
+
arr.push(sample);
|
|
412
|
+
byTrace.set(sample.traceId, arr);
|
|
413
|
+
}
|
|
414
|
+
const sequences = {};
|
|
415
|
+
for (const events of byTrace.values()) {
|
|
416
|
+
const ordered = events.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());
|
|
417
|
+
const steps = ordered.map((event) => event.operation.name);
|
|
418
|
+
if (steps.length < 2)
|
|
419
|
+
continue;
|
|
420
|
+
const key = `${steps.join(">")}@${ordered[0]?.tenantId ?? "global"}`;
|
|
421
|
+
const existing = sequences[key];
|
|
422
|
+
if (existing) {
|
|
423
|
+
existing.count += 1;
|
|
424
|
+
} else {
|
|
425
|
+
sequences[key] = {
|
|
426
|
+
steps,
|
|
427
|
+
tenantId: ordered[0]?.tenantId,
|
|
428
|
+
count: 1
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return Object.values(sequences).sort((a, b) => b.count - a.count);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
function percentile(values, ratio) {
|
|
436
|
+
if (!values.length)
|
|
437
|
+
return 0;
|
|
438
|
+
if (values.length === 1)
|
|
439
|
+
return values[0] ?? 0;
|
|
440
|
+
const index = Math.min(values.length - 1, Math.floor(ratio * values.length));
|
|
441
|
+
return values[index] ?? 0;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// src/intent/detector.ts
|
|
445
|
+
import { randomUUID } from "crypto";
|
|
446
|
+
var DEFAULTS = {
|
|
447
|
+
errorRateThreshold: 0.05,
|
|
448
|
+
latencyP99ThresholdMs: 750,
|
|
449
|
+
throughputDropThreshold: 0.3,
|
|
450
|
+
minSequenceLength: 3
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
class IntentDetector {
|
|
454
|
+
options;
|
|
455
|
+
constructor(options = {}) {
|
|
456
|
+
this.options = {
|
|
457
|
+
errorRateThreshold: options.errorRateThreshold ?? DEFAULTS.errorRateThreshold,
|
|
458
|
+
latencyP99ThresholdMs: options.latencyP99ThresholdMs ?? DEFAULTS.latencyP99ThresholdMs,
|
|
459
|
+
throughputDropThreshold: options.throughputDropThreshold ?? DEFAULTS.throughputDropThreshold,
|
|
460
|
+
minSequenceLength: options.minSequenceLength ?? DEFAULTS.minSequenceLength
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
detectFromMetrics(current, previous) {
|
|
464
|
+
const signals = [];
|
|
465
|
+
const baseline = new Map((previous ?? []).map((metric) => [
|
|
466
|
+
`${metric.operation.name}.v${metric.operation.version}`,
|
|
467
|
+
metric
|
|
468
|
+
]));
|
|
469
|
+
for (const metric of current) {
|
|
470
|
+
if (metric.errorRate >= this.options.errorRateThreshold) {
|
|
471
|
+
signals.push({
|
|
472
|
+
id: randomUUID(),
|
|
473
|
+
type: "error-spike",
|
|
474
|
+
operation: metric.operation,
|
|
475
|
+
confidence: Math.min(1, metric.errorRate / this.options.errorRateThreshold),
|
|
476
|
+
description: `Error rate ${metric.errorRate.toFixed(2)} exceeded threshold`,
|
|
477
|
+
metadata: {
|
|
478
|
+
errorRate: metric.errorRate,
|
|
479
|
+
topErrors: metric.topErrors
|
|
480
|
+
},
|
|
481
|
+
evidence: [
|
|
482
|
+
{
|
|
483
|
+
type: "metric",
|
|
484
|
+
description: "error-rate",
|
|
485
|
+
data: {
|
|
486
|
+
errorRate: metric.errorRate,
|
|
487
|
+
threshold: this.options.errorRateThreshold
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
]
|
|
491
|
+
});
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (metric.p99LatencyMs >= this.options.latencyP99ThresholdMs) {
|
|
495
|
+
signals.push({
|
|
496
|
+
id: randomUUID(),
|
|
497
|
+
type: "latency-regression",
|
|
498
|
+
operation: metric.operation,
|
|
499
|
+
confidence: Math.min(1, metric.p99LatencyMs / this.options.latencyP99ThresholdMs),
|
|
500
|
+
description: `P99 latency ${metric.p99LatencyMs}ms exceeded threshold`,
|
|
501
|
+
metadata: { p99LatencyMs: metric.p99LatencyMs },
|
|
502
|
+
evidence: [
|
|
503
|
+
{
|
|
504
|
+
type: "metric",
|
|
505
|
+
description: "p99-latency",
|
|
506
|
+
data: {
|
|
507
|
+
p99LatencyMs: metric.p99LatencyMs,
|
|
508
|
+
threshold: this.options.latencyP99ThresholdMs
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
]
|
|
512
|
+
});
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
const base = baseline.get(`${metric.operation.name}.v${metric.operation.version}`);
|
|
516
|
+
if (base) {
|
|
517
|
+
const drop = (base.totalCalls - metric.totalCalls) / Math.max(base.totalCalls, 1);
|
|
518
|
+
if (drop >= this.options.throughputDropThreshold) {
|
|
519
|
+
signals.push({
|
|
520
|
+
id: randomUUID(),
|
|
521
|
+
type: "throughput-drop",
|
|
522
|
+
operation: metric.operation,
|
|
523
|
+
confidence: Math.min(1, drop / this.options.throughputDropThreshold),
|
|
524
|
+
description: `Throughput dropped ${(drop * 100).toFixed(1)}% vs baseline`,
|
|
525
|
+
metadata: {
|
|
526
|
+
baselineCalls: base.totalCalls,
|
|
527
|
+
currentCalls: metric.totalCalls
|
|
528
|
+
},
|
|
529
|
+
evidence: [
|
|
530
|
+
{
|
|
531
|
+
type: "metric",
|
|
532
|
+
description: "throughput-drop",
|
|
533
|
+
data: {
|
|
534
|
+
baselineCalls: base.totalCalls,
|
|
535
|
+
currentCalls: metric.totalCalls
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
]
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return signals;
|
|
544
|
+
}
|
|
545
|
+
detectSequentialIntents(sequences) {
|
|
546
|
+
const signals = [];
|
|
547
|
+
for (const sequence of sequences) {
|
|
548
|
+
if (sequence.steps.length < this.options.minSequenceLength)
|
|
549
|
+
continue;
|
|
550
|
+
const description = sequence.steps.join(" \u2192 ");
|
|
551
|
+
signals.push({
|
|
552
|
+
id: randomUUID(),
|
|
553
|
+
type: "missing-workflow-step",
|
|
554
|
+
confidence: 0.6,
|
|
555
|
+
description: `Repeated workflow detected: ${description}`,
|
|
556
|
+
metadata: {
|
|
557
|
+
steps: sequence.steps,
|
|
558
|
+
tenantId: sequence.tenantId,
|
|
559
|
+
occurrences: sequence.count
|
|
560
|
+
},
|
|
561
|
+
evidence: [
|
|
562
|
+
{
|
|
563
|
+
type: "sequence",
|
|
564
|
+
description: "sequential-calls",
|
|
565
|
+
data: { steps: sequence.steps, count: sequence.count }
|
|
566
|
+
}
|
|
567
|
+
]
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
return signals;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// src/pipeline/evolution-pipeline.ts
|
|
575
|
+
import { EventEmitter } from "events";
|
|
576
|
+
class EvolutionPipeline {
|
|
577
|
+
detector;
|
|
578
|
+
aggregator;
|
|
579
|
+
emitter;
|
|
580
|
+
onIntent;
|
|
581
|
+
onSnapshot;
|
|
582
|
+
timer;
|
|
583
|
+
previousMetrics;
|
|
584
|
+
constructor(options = {}) {
|
|
585
|
+
this.detector = options.detector ?? new IntentDetector;
|
|
586
|
+
this.aggregator = options.aggregator ?? new IntentAggregator;
|
|
587
|
+
this.emitter = options.emitter ?? new EventEmitter;
|
|
588
|
+
this.onIntent = options.onIntent;
|
|
589
|
+
this.onSnapshot = options.onSnapshot;
|
|
590
|
+
}
|
|
591
|
+
ingest(sample) {
|
|
592
|
+
this.aggregator.add(sample);
|
|
593
|
+
}
|
|
594
|
+
on(listener) {
|
|
595
|
+
this.emitter.on("event", listener);
|
|
596
|
+
}
|
|
597
|
+
start(intervalMs = 5 * 60 * 1000) {
|
|
598
|
+
this.stop();
|
|
599
|
+
this.timer = setInterval(() => {
|
|
600
|
+
this.run();
|
|
601
|
+
}, intervalMs);
|
|
602
|
+
}
|
|
603
|
+
stop() {
|
|
604
|
+
if (this.timer) {
|
|
605
|
+
clearInterval(this.timer);
|
|
606
|
+
this.timer = undefined;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
async run() {
|
|
610
|
+
const snapshot = this.aggregator.flush();
|
|
611
|
+
this.emit({
|
|
612
|
+
type: "telemetry.window",
|
|
613
|
+
payload: { sampleCount: snapshot.sampleCount }
|
|
614
|
+
});
|
|
615
|
+
if (this.onSnapshot)
|
|
616
|
+
await this.onSnapshot(snapshot);
|
|
617
|
+
if (!snapshot.sampleCount)
|
|
618
|
+
return;
|
|
619
|
+
const metricSignals = this.detector.detectFromMetrics(snapshot.metrics, this.previousMetrics);
|
|
620
|
+
const sequenceSignals = this.detector.detectSequentialIntents(snapshot.sequences);
|
|
621
|
+
this.previousMetrics = snapshot.metrics;
|
|
622
|
+
const signals = [...metricSignals, ...sequenceSignals];
|
|
623
|
+
for (const signal of signals) {
|
|
624
|
+
if (this.onIntent)
|
|
625
|
+
await this.onIntent(signal);
|
|
626
|
+
this.emit({ type: "intent.detected", payload: signal });
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
emit(event) {
|
|
630
|
+
this.emitter.emit("event", event);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// src/pipeline/lifecycle-pipeline.ts
|
|
635
|
+
import { EventEmitter as EventEmitter2 } from "events";
|
|
636
|
+
import { getStageLabel } from "@contractspec/lib.lifecycle";
|
|
637
|
+
class LifecycleKpiPipeline {
|
|
638
|
+
assessmentCounter;
|
|
639
|
+
confidenceHistogram;
|
|
640
|
+
stageUpDownCounter;
|
|
641
|
+
emitter;
|
|
642
|
+
lowConfidenceThreshold;
|
|
643
|
+
currentStageByTenant = new Map;
|
|
644
|
+
constructor(options = {}) {
|
|
645
|
+
const meterName = options.meterName ?? "@contractspec/lib.lifecycle-kpi";
|
|
646
|
+
this.assessmentCounter = createCounter("lifecycle_assessments_total", "Total lifecycle assessments", meterName);
|
|
647
|
+
this.confidenceHistogram = createHistogram("lifecycle_assessment_confidence", "Lifecycle assessment confidence distribution", meterName);
|
|
648
|
+
this.stageUpDownCounter = createUpDownCounter("lifecycle_stage_tenants", "Current tenants per lifecycle stage", meterName);
|
|
649
|
+
this.emitter = options.emitter ?? new EventEmitter2;
|
|
650
|
+
this.lowConfidenceThreshold = options.lowConfidenceThreshold ?? 0.4;
|
|
651
|
+
}
|
|
652
|
+
recordAssessment(assessment, tenantId) {
|
|
653
|
+
const stageLabel = getStageLabel(assessment.stage);
|
|
654
|
+
const attributes = { stage: stageLabel, tenantId };
|
|
655
|
+
this.assessmentCounter.add(1, attributes);
|
|
656
|
+
this.confidenceHistogram.record(assessment.confidence, attributes);
|
|
657
|
+
this.ensureStageCounters(assessment.stage, tenantId);
|
|
658
|
+
this.emitter.emit("event", {
|
|
659
|
+
type: "assessment.recorded",
|
|
660
|
+
payload: { tenantId, stage: assessment.stage }
|
|
661
|
+
});
|
|
662
|
+
if (assessment.confidence < this.lowConfidenceThreshold) {
|
|
663
|
+
this.emitter.emit("event", {
|
|
664
|
+
type: "confidence.low",
|
|
665
|
+
payload: { tenantId, confidence: assessment.confidence }
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
on(listener) {
|
|
670
|
+
this.emitter.on("event", listener);
|
|
671
|
+
}
|
|
672
|
+
ensureStageCounters(stage, tenantId) {
|
|
673
|
+
if (!tenantId)
|
|
674
|
+
return;
|
|
675
|
+
const previous = this.currentStageByTenant.get(tenantId);
|
|
676
|
+
if (previous === stage)
|
|
677
|
+
return;
|
|
678
|
+
if (previous !== undefined) {
|
|
679
|
+
this.stageUpDownCounter.add(-1, {
|
|
680
|
+
stage: getStageLabel(previous),
|
|
681
|
+
tenantId
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
this.stageUpDownCounter.add(1, { stage: getStageLabel(stage), tenantId });
|
|
685
|
+
this.currentStageByTenant.set(tenantId, stage);
|
|
686
|
+
this.emitter.emit("event", {
|
|
687
|
+
type: "stage.changed",
|
|
688
|
+
payload: { tenantId, previousStage: previous, nextStage: stage }
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// src/telemetry/posthog-telemetry.ts
|
|
694
|
+
class PosthogTelemetryProvider {
|
|
695
|
+
provider;
|
|
696
|
+
eventPrefix;
|
|
697
|
+
includeMetadata;
|
|
698
|
+
constructor(provider, options = {}) {
|
|
699
|
+
this.provider = provider;
|
|
700
|
+
this.eventPrefix = options.eventPrefix ?? "observability";
|
|
701
|
+
this.includeMetadata = options.includeMetadata ?? false;
|
|
702
|
+
}
|
|
703
|
+
async captureSample(sample) {
|
|
704
|
+
await this.provider.capture({
|
|
705
|
+
distinctId: sample.actorId ?? sample.tenantId ?? "unknown",
|
|
706
|
+
event: `${this.eventPrefix}.operation`,
|
|
707
|
+
timestamp: sample.timestamp,
|
|
708
|
+
properties: {
|
|
709
|
+
operation: sample.operation.name,
|
|
710
|
+
version: sample.operation.version,
|
|
711
|
+
durationMs: sample.durationMs,
|
|
712
|
+
success: sample.success,
|
|
713
|
+
errorCode: sample.errorCode ?? null,
|
|
714
|
+
tenantId: sample.tenantId ?? null,
|
|
715
|
+
traceId: sample.traceId ?? null,
|
|
716
|
+
...this.includeMetadata && sample.metadata ? { metadata: sample.metadata } : {}
|
|
717
|
+
}
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
async captureSnapshot(snapshot) {
|
|
721
|
+
await this.provider.capture({
|
|
722
|
+
distinctId: "system",
|
|
723
|
+
event: `${this.eventPrefix}.window`,
|
|
724
|
+
timestamp: snapshot.windowEnd ?? new Date,
|
|
725
|
+
properties: {
|
|
726
|
+
sampleCount: snapshot.sampleCount,
|
|
727
|
+
metricsCount: snapshot.metrics.length,
|
|
728
|
+
sequencesCount: snapshot.sequences.length,
|
|
729
|
+
windowStart: snapshot.windowStart?.toISOString() ?? null,
|
|
730
|
+
windowEnd: snapshot.windowEnd?.toISOString() ?? null,
|
|
731
|
+
...this.includeMetadata ? {
|
|
732
|
+
metrics: snapshot.metrics.map((metric) => ({
|
|
733
|
+
operation: metric.operation.name,
|
|
734
|
+
version: metric.operation.version,
|
|
735
|
+
totalCalls: metric.totalCalls,
|
|
736
|
+
successRate: metric.successRate,
|
|
737
|
+
errorRate: metric.errorRate,
|
|
738
|
+
averageLatencyMs: metric.averageLatencyMs,
|
|
739
|
+
p95LatencyMs: metric.p95LatencyMs,
|
|
740
|
+
p99LatencyMs: metric.p99LatencyMs,
|
|
741
|
+
maxLatencyMs: metric.maxLatencyMs,
|
|
742
|
+
topErrors: metric.topErrors
|
|
743
|
+
})),
|
|
744
|
+
sequences: snapshot.sequences
|
|
745
|
+
} : {}
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
// src/telemetry/posthog-baseline-reader.ts
|
|
752
|
+
class PosthogBaselineReader {
|
|
753
|
+
reader;
|
|
754
|
+
eventPrefix;
|
|
755
|
+
constructor(reader, options = {}) {
|
|
756
|
+
this.reader = reader;
|
|
757
|
+
this.eventPrefix = options.eventPrefix ?? "observability";
|
|
758
|
+
}
|
|
759
|
+
async readSamples(input) {
|
|
760
|
+
const result = await this.queryHogQL({
|
|
761
|
+
query: [
|
|
762
|
+
"select",
|
|
763
|
+
" properties.operation as operationName,",
|
|
764
|
+
" properties.version as version,",
|
|
765
|
+
" properties.durationMs as durationMs,",
|
|
766
|
+
" properties.success as success,",
|
|
767
|
+
" properties.errorCode as errorCode,",
|
|
768
|
+
" properties.tenantId as tenantId,",
|
|
769
|
+
" properties.traceId as traceId,",
|
|
770
|
+
" properties.metadata as metadata,",
|
|
771
|
+
" distinct_id as actorId,",
|
|
772
|
+
" timestamp as timestamp",
|
|
773
|
+
"from events",
|
|
774
|
+
`where ${buildOperationWhereClause(this.eventPrefix, input)}`,
|
|
775
|
+
"order by timestamp desc",
|
|
776
|
+
`limit ${input.limit ?? 1000}`
|
|
777
|
+
].join(`
|
|
778
|
+
`),
|
|
779
|
+
values: buildOperationValues(input)
|
|
780
|
+
});
|
|
781
|
+
return mapTelemetrySamples(result);
|
|
782
|
+
}
|
|
783
|
+
async readAggregatedMetrics(operation, windowDays = 7) {
|
|
784
|
+
const dateRange = buildWindowRange(windowDays);
|
|
785
|
+
const result = await this.queryHogQL({
|
|
786
|
+
query: [
|
|
787
|
+
"select",
|
|
788
|
+
" count() as totalCalls,",
|
|
789
|
+
" avg(properties.durationMs) as averageLatencyMs,",
|
|
790
|
+
" quantile(0.95)(properties.durationMs) as p95LatencyMs,",
|
|
791
|
+
" quantile(0.99)(properties.durationMs) as p99LatencyMs,",
|
|
792
|
+
" max(properties.durationMs) as maxLatencyMs,",
|
|
793
|
+
" sum(if(properties.success = 1, 1, 0)) as successCount,",
|
|
794
|
+
" sum(if(properties.success = 0, 1, 0)) as errorCount",
|
|
795
|
+
"from events",
|
|
796
|
+
`where ${buildOperationWhereClause(this.eventPrefix, {
|
|
797
|
+
operations: [operation],
|
|
798
|
+
dateRange
|
|
799
|
+
})}`
|
|
800
|
+
].join(`
|
|
801
|
+
`),
|
|
802
|
+
values: buildOperationValues({
|
|
803
|
+
operations: [operation],
|
|
804
|
+
dateRange
|
|
805
|
+
})
|
|
806
|
+
});
|
|
807
|
+
const stats = mapAggregatedMetrics(result, operation, dateRange);
|
|
808
|
+
if (!stats)
|
|
809
|
+
return null;
|
|
810
|
+
const topErrors = await this.readTopErrors(operation, dateRange);
|
|
811
|
+
return {
|
|
812
|
+
...stats,
|
|
813
|
+
topErrors
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
async readOperationSequences(dateRange) {
|
|
817
|
+
const result = await this.queryHogQL({
|
|
818
|
+
query: [
|
|
819
|
+
"select",
|
|
820
|
+
" properties.sequences as sequences",
|
|
821
|
+
"from events",
|
|
822
|
+
`where event = {eventName}`,
|
|
823
|
+
dateRange?.from ? "and timestamp >= {dateFrom}" : "",
|
|
824
|
+
dateRange?.to ? "and timestamp < {dateTo}" : "",
|
|
825
|
+
"order by timestamp desc",
|
|
826
|
+
"limit 50"
|
|
827
|
+
].filter(Boolean).join(`
|
|
828
|
+
`),
|
|
829
|
+
values: {
|
|
830
|
+
eventName: `${this.eventPrefix}.window`,
|
|
831
|
+
dateFrom: toIsoString(dateRange?.from),
|
|
832
|
+
dateTo: toIsoString(dateRange?.to)
|
|
833
|
+
}
|
|
834
|
+
});
|
|
835
|
+
return mergeSequences(result);
|
|
836
|
+
}
|
|
837
|
+
async readTopErrors(operation, dateRange) {
|
|
838
|
+
const result = await this.queryHogQL({
|
|
839
|
+
query: [
|
|
840
|
+
"select",
|
|
841
|
+
" properties.errorCode as errorCode,",
|
|
842
|
+
" count() as errorCount",
|
|
843
|
+
"from events",
|
|
844
|
+
`where ${buildOperationWhereClause(this.eventPrefix, {
|
|
845
|
+
operations: [operation],
|
|
846
|
+
dateRange
|
|
847
|
+
})} and properties.success = 0`,
|
|
848
|
+
"group by errorCode",
|
|
849
|
+
"order by errorCount desc",
|
|
850
|
+
"limit 5"
|
|
851
|
+
].join(`
|
|
852
|
+
`),
|
|
853
|
+
values: buildOperationValues({
|
|
854
|
+
operations: [operation],
|
|
855
|
+
dateRange
|
|
856
|
+
})
|
|
857
|
+
});
|
|
858
|
+
const rows = mapRows(result);
|
|
859
|
+
return rows.reduce((acc, row) => {
|
|
860
|
+
const code = asString(row.errorCode);
|
|
861
|
+
if (!code)
|
|
862
|
+
return acc;
|
|
863
|
+
acc[code] = asNumber(row.errorCount);
|
|
864
|
+
return acc;
|
|
865
|
+
}, {});
|
|
866
|
+
}
|
|
867
|
+
async queryHogQL(input) {
|
|
868
|
+
if (!this.reader.queryHogQL) {
|
|
869
|
+
throw new Error("Analytics reader does not support HogQL queries.");
|
|
870
|
+
}
|
|
871
|
+
return this.reader.queryHogQL(input);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
function buildOperationWhereClause(eventPrefix, input) {
|
|
875
|
+
const clauses = [`event = '${eventPrefix}.operation'`];
|
|
876
|
+
if (input.operations?.length) {
|
|
877
|
+
clauses.push(`(${buildOperationFilters(input.operations)})`);
|
|
878
|
+
}
|
|
879
|
+
if (input.dateRange?.from) {
|
|
880
|
+
clauses.push("timestamp >= {dateFrom}");
|
|
881
|
+
}
|
|
882
|
+
if (input.dateRange?.to) {
|
|
883
|
+
clauses.push("timestamp < {dateTo}");
|
|
884
|
+
}
|
|
885
|
+
return clauses.join(" and ");
|
|
886
|
+
}
|
|
887
|
+
function buildOperationValues(input) {
|
|
888
|
+
const values = {
|
|
889
|
+
dateFrom: toIsoString(input.dateRange?.from),
|
|
890
|
+
dateTo: toIsoString(input.dateRange?.to)
|
|
891
|
+
};
|
|
892
|
+
input.operations?.forEach((op, index) => {
|
|
893
|
+
values[`operationName${index}`] = op.name;
|
|
894
|
+
values[`operationVersion${index}`] = op.version;
|
|
895
|
+
});
|
|
896
|
+
return values;
|
|
897
|
+
}
|
|
898
|
+
function buildOperationFilters(operations) {
|
|
899
|
+
return operations.map((_op, index) => `(properties.operation = {operationName${index}} and properties.version = {operationVersion${index}})`).join(" or ");
|
|
900
|
+
}
|
|
901
|
+
function mapTelemetrySamples(result) {
|
|
902
|
+
const rows = mapRows(result);
|
|
903
|
+
return rows.flatMap((row) => {
|
|
904
|
+
const operationName = asString(row.operationName);
|
|
905
|
+
const version = asString(row.version);
|
|
906
|
+
const timestamp = asDate(row.timestamp);
|
|
907
|
+
if (!operationName || !version || !timestamp) {
|
|
908
|
+
return [];
|
|
909
|
+
}
|
|
910
|
+
return [
|
|
911
|
+
{
|
|
912
|
+
operation: { name: operationName, version },
|
|
913
|
+
durationMs: asNumber(row.durationMs),
|
|
914
|
+
success: asBoolean(row.success),
|
|
915
|
+
timestamp,
|
|
916
|
+
errorCode: asOptionalString(row.errorCode) ?? undefined,
|
|
917
|
+
tenantId: asOptionalString(row.tenantId) ?? undefined,
|
|
918
|
+
traceId: asOptionalString(row.traceId) ?? undefined,
|
|
919
|
+
actorId: asOptionalString(row.actorId) ?? undefined,
|
|
920
|
+
metadata: isRecord(row.metadata) ? row.metadata : undefined
|
|
921
|
+
}
|
|
922
|
+
];
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
function mapAggregatedMetrics(result, operation, dateRange) {
|
|
926
|
+
const rows = mapRows(result);
|
|
927
|
+
const row = rows[0];
|
|
928
|
+
if (!row)
|
|
929
|
+
return null;
|
|
930
|
+
const totalCalls = asNumber(row.totalCalls);
|
|
931
|
+
if (!totalCalls)
|
|
932
|
+
return null;
|
|
933
|
+
const successCount = asNumber(row.successCount);
|
|
934
|
+
const errorCount = asNumber(row.errorCount);
|
|
935
|
+
const windowStart = toDate(dateRange.from) ?? new Date;
|
|
936
|
+
const windowEnd = toDate(dateRange.to) ?? new Date;
|
|
937
|
+
return {
|
|
938
|
+
operation,
|
|
939
|
+
totalCalls,
|
|
940
|
+
successRate: totalCalls ? successCount / totalCalls : 0,
|
|
941
|
+
errorRate: totalCalls ? errorCount / totalCalls : 0,
|
|
942
|
+
averageLatencyMs: asNumber(row.averageLatencyMs),
|
|
943
|
+
p95LatencyMs: asNumber(row.p95LatencyMs),
|
|
944
|
+
p99LatencyMs: asNumber(row.p99LatencyMs),
|
|
945
|
+
maxLatencyMs: asNumber(row.maxLatencyMs),
|
|
946
|
+
windowStart,
|
|
947
|
+
windowEnd,
|
|
948
|
+
topErrors: {}
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
function mergeSequences(result) {
|
|
952
|
+
const rows = mapRows(result);
|
|
953
|
+
const merged = new Map;
|
|
954
|
+
rows.forEach((row) => {
|
|
955
|
+
const sequences = row.sequences;
|
|
956
|
+
if (!Array.isArray(sequences))
|
|
957
|
+
return;
|
|
958
|
+
sequences.forEach((sequence) => {
|
|
959
|
+
if (!isRecord(sequence))
|
|
960
|
+
return;
|
|
961
|
+
const steps = Array.isArray(sequence.steps) ? sequence.steps.filter((step) => typeof step === "string") : [];
|
|
962
|
+
if (steps.length === 0)
|
|
963
|
+
return;
|
|
964
|
+
const tenantId = typeof sequence.tenantId === "string" ? sequence.tenantId : undefined;
|
|
965
|
+
const count = typeof sequence.count === "number" && Number.isFinite(sequence.count) ? sequence.count : 0;
|
|
966
|
+
const key = `${tenantId ?? "global"}:${steps.join(">")}`;
|
|
967
|
+
const existing = merged.get(key);
|
|
968
|
+
if (existing) {
|
|
969
|
+
existing.count += count;
|
|
970
|
+
} else {
|
|
971
|
+
merged.set(key, { steps, tenantId, count });
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
});
|
|
975
|
+
return [...merged.values()];
|
|
976
|
+
}
|
|
977
|
+
function mapRows(result) {
|
|
978
|
+
if (!Array.isArray(result.results) || !Array.isArray(result.columns)) {
|
|
979
|
+
return [];
|
|
980
|
+
}
|
|
981
|
+
const columns = result.columns;
|
|
982
|
+
return result.results.flatMap((row) => {
|
|
983
|
+
if (!Array.isArray(row))
|
|
984
|
+
return [];
|
|
985
|
+
const record = {};
|
|
986
|
+
columns.forEach((column, index) => {
|
|
987
|
+
record[column] = row[index];
|
|
988
|
+
});
|
|
989
|
+
return [record];
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
function buildWindowRange(windowDays) {
|
|
993
|
+
const windowEnd = new Date;
|
|
994
|
+
const windowStart = new Date(windowEnd.getTime() - windowDays * 24 * 60 * 60 * 1000);
|
|
995
|
+
return {
|
|
996
|
+
from: windowStart,
|
|
997
|
+
to: windowEnd
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
function asString(value) {
|
|
1001
|
+
if (typeof value === "string" && value.trim())
|
|
1002
|
+
return value;
|
|
1003
|
+
if (typeof value === "number")
|
|
1004
|
+
return String(value);
|
|
1005
|
+
return null;
|
|
1006
|
+
}
|
|
1007
|
+
function asOptionalString(value) {
|
|
1008
|
+
if (typeof value === "string")
|
|
1009
|
+
return value;
|
|
1010
|
+
if (typeof value === "number")
|
|
1011
|
+
return String(value);
|
|
1012
|
+
return null;
|
|
1013
|
+
}
|
|
1014
|
+
function asNumber(value) {
|
|
1015
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
1016
|
+
return value;
|
|
1017
|
+
if (typeof value === "string" && value.trim()) {
|
|
1018
|
+
const parsed = Number(value);
|
|
1019
|
+
if (Number.isFinite(parsed))
|
|
1020
|
+
return parsed;
|
|
1021
|
+
}
|
|
1022
|
+
return 0;
|
|
1023
|
+
}
|
|
1024
|
+
function asBoolean(value) {
|
|
1025
|
+
if (typeof value === "boolean")
|
|
1026
|
+
return value;
|
|
1027
|
+
if (typeof value === "number")
|
|
1028
|
+
return value !== 0;
|
|
1029
|
+
if (typeof value === "string")
|
|
1030
|
+
return value.toLowerCase() === "true";
|
|
1031
|
+
return false;
|
|
1032
|
+
}
|
|
1033
|
+
function asDate(value) {
|
|
1034
|
+
if (value instanceof Date)
|
|
1035
|
+
return value;
|
|
1036
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
1037
|
+
const date = new Date(value);
|
|
1038
|
+
if (!Number.isNaN(date.getTime()))
|
|
1039
|
+
return date;
|
|
1040
|
+
}
|
|
1041
|
+
return null;
|
|
1042
|
+
}
|
|
1043
|
+
function toIsoString(value) {
|
|
1044
|
+
if (!value)
|
|
1045
|
+
return;
|
|
1046
|
+
return typeof value === "string" ? value : value.toISOString();
|
|
1047
|
+
}
|
|
1048
|
+
function toDate(value) {
|
|
1049
|
+
if (!value)
|
|
1050
|
+
return null;
|
|
1051
|
+
return value instanceof Date ? value : new Date(value);
|
|
1052
|
+
}
|
|
1053
|
+
function isRecord(value) {
|
|
1054
|
+
return typeof value === "object" && value !== null;
|
|
1055
|
+
}
|
|
1056
|
+
export {
|
|
1057
|
+
traceSync,
|
|
1058
|
+
traceAsync,
|
|
1059
|
+
standardMetrics,
|
|
1060
|
+
logger,
|
|
1061
|
+
getTracer,
|
|
1062
|
+
getMeter,
|
|
1063
|
+
createUpDownCounter,
|
|
1064
|
+
createTracingMiddleware,
|
|
1065
|
+
createHistogram,
|
|
1066
|
+
createCounter,
|
|
1067
|
+
RootCauseAnalyzer,
|
|
1068
|
+
PosthogTelemetryProvider,
|
|
1069
|
+
PosthogBaselineReader,
|
|
1070
|
+
Logger,
|
|
1071
|
+
LifecycleKpiPipeline,
|
|
1072
|
+
IntentDetector,
|
|
1073
|
+
IntentAggregator,
|
|
1074
|
+
EvolutionPipeline,
|
|
1075
|
+
BaselineCalculator,
|
|
1076
|
+
AnomalyDetector,
|
|
1077
|
+
AlertManager
|
|
1078
|
+
};
|