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