@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
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/intent/aggregator.ts
|
|
2
|
+
var DEFAULT_WINDOW_MS = 15 * 60 * 1000;
|
|
3
|
+
|
|
4
|
+
class IntentAggregator {
|
|
5
|
+
windowMs;
|
|
6
|
+
sequenceSampleSize;
|
|
7
|
+
samples = [];
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.windowMs = options.windowMs ?? DEFAULT_WINDOW_MS;
|
|
10
|
+
this.sequenceSampleSize = options.sequenceSampleSize ?? 1000;
|
|
11
|
+
}
|
|
12
|
+
add(sample) {
|
|
13
|
+
this.samples.push(sample);
|
|
14
|
+
}
|
|
15
|
+
flush(now = new Date) {
|
|
16
|
+
const minTimestamp = now.getTime() - this.windowMs;
|
|
17
|
+
const windowSamples = this.samples.filter((sample) => sample.timestamp.getTime() >= minTimestamp);
|
|
18
|
+
this.samples.length = 0;
|
|
19
|
+
const metrics = this.aggregateMetrics(windowSamples);
|
|
20
|
+
const sequences = this.buildSequences(windowSamples);
|
|
21
|
+
const timestamps = windowSamples.map((sample) => sample.timestamp.getTime());
|
|
22
|
+
return {
|
|
23
|
+
metrics,
|
|
24
|
+
sequences,
|
|
25
|
+
sampleCount: windowSamples.length,
|
|
26
|
+
windowStart: timestamps.length ? new Date(Math.min(...timestamps)) : undefined,
|
|
27
|
+
windowEnd: timestamps.length ? new Date(Math.max(...timestamps)) : undefined
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
aggregateMetrics(samples) {
|
|
31
|
+
if (!samples.length)
|
|
32
|
+
return [];
|
|
33
|
+
const groups = new Map;
|
|
34
|
+
for (const sample of samples) {
|
|
35
|
+
const key = `${sample.operation.name}.v${sample.operation.version}`;
|
|
36
|
+
const arr = groups.get(key) ?? [];
|
|
37
|
+
arr.push(sample);
|
|
38
|
+
groups.set(key, arr);
|
|
39
|
+
}
|
|
40
|
+
return [...groups.values()].map((group) => {
|
|
41
|
+
const first = group[0];
|
|
42
|
+
if (!first)
|
|
43
|
+
throw new Error("Empty group in aggregation");
|
|
44
|
+
const durations = group.map((s) => s.durationMs).sort((a, b) => a - b);
|
|
45
|
+
const errors = group.filter((s) => !s.success);
|
|
46
|
+
const totalCalls = group.length;
|
|
47
|
+
const topErrors = errors.reduce((acc, sample) => {
|
|
48
|
+
if (!sample.errorCode)
|
|
49
|
+
return acc;
|
|
50
|
+
acc[sample.errorCode] = (acc[sample.errorCode] ?? 0) + 1;
|
|
51
|
+
return acc;
|
|
52
|
+
}, {});
|
|
53
|
+
const timestamps = group.map((s) => s.timestamp.getTime());
|
|
54
|
+
return {
|
|
55
|
+
operation: first.operation,
|
|
56
|
+
totalCalls,
|
|
57
|
+
successRate: (totalCalls - errors.length) / totalCalls,
|
|
58
|
+
errorRate: errors.length / totalCalls,
|
|
59
|
+
averageLatencyMs: durations.reduce((sum, value) => sum + value, 0) / totalCalls,
|
|
60
|
+
p95LatencyMs: percentile(durations, 0.95),
|
|
61
|
+
p99LatencyMs: percentile(durations, 0.99),
|
|
62
|
+
maxLatencyMs: Math.max(...durations),
|
|
63
|
+
windowStart: new Date(Math.min(...timestamps)),
|
|
64
|
+
windowEnd: new Date(Math.max(...timestamps)),
|
|
65
|
+
topErrors
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
buildSequences(samples) {
|
|
70
|
+
const byTrace = new Map;
|
|
71
|
+
for (const sample of samples.slice(-this.sequenceSampleSize)) {
|
|
72
|
+
if (!sample.traceId)
|
|
73
|
+
continue;
|
|
74
|
+
const arr = byTrace.get(sample.traceId) ?? [];
|
|
75
|
+
arr.push(sample);
|
|
76
|
+
byTrace.set(sample.traceId, arr);
|
|
77
|
+
}
|
|
78
|
+
const sequences = {};
|
|
79
|
+
for (const events of byTrace.values()) {
|
|
80
|
+
const ordered = events.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());
|
|
81
|
+
const steps = ordered.map((event) => event.operation.name);
|
|
82
|
+
if (steps.length < 2)
|
|
83
|
+
continue;
|
|
84
|
+
const key = `${steps.join(">")}@${ordered[0]?.tenantId ?? "global"}`;
|
|
85
|
+
const existing = sequences[key];
|
|
86
|
+
if (existing) {
|
|
87
|
+
existing.count += 1;
|
|
88
|
+
} else {
|
|
89
|
+
sequences[key] = {
|
|
90
|
+
steps,
|
|
91
|
+
tenantId: ordered[0]?.tenantId,
|
|
92
|
+
count: 1
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return Object.values(sequences).sort((a, b) => b.count - a.count);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function percentile(values, ratio) {
|
|
100
|
+
if (!values.length)
|
|
101
|
+
return 0;
|
|
102
|
+
if (values.length === 1)
|
|
103
|
+
return values[0] ?? 0;
|
|
104
|
+
const index = Math.min(values.length - 1, Math.floor(ratio * values.length));
|
|
105
|
+
return values[index] ?? 0;
|
|
106
|
+
}
|
|
107
|
+
export {
|
|
108
|
+
IntentAggregator
|
|
109
|
+
};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// src/intent/detector.ts
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
var DEFAULTS = {
|
|
4
|
+
errorRateThreshold: 0.05,
|
|
5
|
+
latencyP99ThresholdMs: 750,
|
|
6
|
+
throughputDropThreshold: 0.3,
|
|
7
|
+
minSequenceLength: 3
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
class IntentDetector {
|
|
11
|
+
options;
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
this.options = {
|
|
14
|
+
errorRateThreshold: options.errorRateThreshold ?? DEFAULTS.errorRateThreshold,
|
|
15
|
+
latencyP99ThresholdMs: options.latencyP99ThresholdMs ?? DEFAULTS.latencyP99ThresholdMs,
|
|
16
|
+
throughputDropThreshold: options.throughputDropThreshold ?? DEFAULTS.throughputDropThreshold,
|
|
17
|
+
minSequenceLength: options.minSequenceLength ?? DEFAULTS.minSequenceLength
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
detectFromMetrics(current, previous) {
|
|
21
|
+
const signals = [];
|
|
22
|
+
const baseline = new Map((previous ?? []).map((metric) => [
|
|
23
|
+
`${metric.operation.name}.v${metric.operation.version}`,
|
|
24
|
+
metric
|
|
25
|
+
]));
|
|
26
|
+
for (const metric of current) {
|
|
27
|
+
if (metric.errorRate >= this.options.errorRateThreshold) {
|
|
28
|
+
signals.push({
|
|
29
|
+
id: randomUUID(),
|
|
30
|
+
type: "error-spike",
|
|
31
|
+
operation: metric.operation,
|
|
32
|
+
confidence: Math.min(1, metric.errorRate / this.options.errorRateThreshold),
|
|
33
|
+
description: `Error rate ${metric.errorRate.toFixed(2)} exceeded threshold`,
|
|
34
|
+
metadata: {
|
|
35
|
+
errorRate: metric.errorRate,
|
|
36
|
+
topErrors: metric.topErrors
|
|
37
|
+
},
|
|
38
|
+
evidence: [
|
|
39
|
+
{
|
|
40
|
+
type: "metric",
|
|
41
|
+
description: "error-rate",
|
|
42
|
+
data: {
|
|
43
|
+
errorRate: metric.errorRate,
|
|
44
|
+
threshold: this.options.errorRateThreshold
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
});
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (metric.p99LatencyMs >= this.options.latencyP99ThresholdMs) {
|
|
52
|
+
signals.push({
|
|
53
|
+
id: randomUUID(),
|
|
54
|
+
type: "latency-regression",
|
|
55
|
+
operation: metric.operation,
|
|
56
|
+
confidence: Math.min(1, metric.p99LatencyMs / this.options.latencyP99ThresholdMs),
|
|
57
|
+
description: `P99 latency ${metric.p99LatencyMs}ms exceeded threshold`,
|
|
58
|
+
metadata: { p99LatencyMs: metric.p99LatencyMs },
|
|
59
|
+
evidence: [
|
|
60
|
+
{
|
|
61
|
+
type: "metric",
|
|
62
|
+
description: "p99-latency",
|
|
63
|
+
data: {
|
|
64
|
+
p99LatencyMs: metric.p99LatencyMs,
|
|
65
|
+
threshold: this.options.latencyP99ThresholdMs
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
});
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const base = baseline.get(`${metric.operation.name}.v${metric.operation.version}`);
|
|
73
|
+
if (base) {
|
|
74
|
+
const drop = (base.totalCalls - metric.totalCalls) / Math.max(base.totalCalls, 1);
|
|
75
|
+
if (drop >= this.options.throughputDropThreshold) {
|
|
76
|
+
signals.push({
|
|
77
|
+
id: randomUUID(),
|
|
78
|
+
type: "throughput-drop",
|
|
79
|
+
operation: metric.operation,
|
|
80
|
+
confidence: Math.min(1, drop / this.options.throughputDropThreshold),
|
|
81
|
+
description: `Throughput dropped ${(drop * 100).toFixed(1)}% vs baseline`,
|
|
82
|
+
metadata: {
|
|
83
|
+
baselineCalls: base.totalCalls,
|
|
84
|
+
currentCalls: metric.totalCalls
|
|
85
|
+
},
|
|
86
|
+
evidence: [
|
|
87
|
+
{
|
|
88
|
+
type: "metric",
|
|
89
|
+
description: "throughput-drop",
|
|
90
|
+
data: {
|
|
91
|
+
baselineCalls: base.totalCalls,
|
|
92
|
+
currentCalls: metric.totalCalls
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
]
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return signals;
|
|
101
|
+
}
|
|
102
|
+
detectSequentialIntents(sequences) {
|
|
103
|
+
const signals = [];
|
|
104
|
+
for (const sequence of sequences) {
|
|
105
|
+
if (sequence.steps.length < this.options.minSequenceLength)
|
|
106
|
+
continue;
|
|
107
|
+
const description = sequence.steps.join(" → ");
|
|
108
|
+
signals.push({
|
|
109
|
+
id: randomUUID(),
|
|
110
|
+
type: "missing-workflow-step",
|
|
111
|
+
confidence: 0.6,
|
|
112
|
+
description: `Repeated workflow detected: ${description}`,
|
|
113
|
+
metadata: {
|
|
114
|
+
steps: sequence.steps,
|
|
115
|
+
tenantId: sequence.tenantId,
|
|
116
|
+
occurrences: sequence.count
|
|
117
|
+
},
|
|
118
|
+
evidence: [
|
|
119
|
+
{
|
|
120
|
+
type: "sequence",
|
|
121
|
+
description: "sequential-calls",
|
|
122
|
+
data: { steps: sequence.steps, count: sequence.count }
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return signals;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
export {
|
|
131
|
+
IntentDetector
|
|
132
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/logging/index.ts
|
|
2
|
+
import { trace, context } from "@opentelemetry/api";
|
|
3
|
+
|
|
4
|
+
class Logger {
|
|
5
|
+
serviceName;
|
|
6
|
+
constructor(serviceName) {
|
|
7
|
+
this.serviceName = serviceName;
|
|
8
|
+
}
|
|
9
|
+
log(level, message, meta = {}) {
|
|
10
|
+
const span = trace.getSpan(context.active());
|
|
11
|
+
const traceId = span?.spanContext().traceId;
|
|
12
|
+
const spanId = span?.spanContext().spanId;
|
|
13
|
+
const entry = {
|
|
14
|
+
timestamp: new Date().toISOString(),
|
|
15
|
+
service: this.serviceName,
|
|
16
|
+
level,
|
|
17
|
+
message,
|
|
18
|
+
traceId,
|
|
19
|
+
spanId,
|
|
20
|
+
...meta
|
|
21
|
+
};
|
|
22
|
+
console.log(JSON.stringify(entry));
|
|
23
|
+
}
|
|
24
|
+
debug(message, meta) {
|
|
25
|
+
this.log("debug", message, meta);
|
|
26
|
+
}
|
|
27
|
+
info(message, meta) {
|
|
28
|
+
this.log("info", message, meta);
|
|
29
|
+
}
|
|
30
|
+
warn(message, meta) {
|
|
31
|
+
this.log("warn", message, meta);
|
|
32
|
+
}
|
|
33
|
+
error(message, meta) {
|
|
34
|
+
this.log("error", message, meta);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
var logger = new Logger(process.env.OTEL_SERVICE_NAME || "unknown-service");
|
|
38
|
+
export {
|
|
39
|
+
logger,
|
|
40
|
+
Logger
|
|
41
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/metrics/index.ts
|
|
2
|
+
import {
|
|
3
|
+
metrics
|
|
4
|
+
} from "@opentelemetry/api";
|
|
5
|
+
var DEFAULT_METER_NAME = "@contractspec/lib.observability";
|
|
6
|
+
function getMeter(name = DEFAULT_METER_NAME) {
|
|
7
|
+
return metrics.getMeter(name);
|
|
8
|
+
}
|
|
9
|
+
function createCounter(name, description, meterName) {
|
|
10
|
+
return getMeter(meterName).createCounter(name, { description });
|
|
11
|
+
}
|
|
12
|
+
function createUpDownCounter(name, description, meterName) {
|
|
13
|
+
return getMeter(meterName).createUpDownCounter(name, { description });
|
|
14
|
+
}
|
|
15
|
+
function createHistogram(name, description, meterName) {
|
|
16
|
+
return getMeter(meterName).createHistogram(name, { description });
|
|
17
|
+
}
|
|
18
|
+
var standardMetrics = {
|
|
19
|
+
httpRequests: createCounter("http_requests_total", "Total HTTP requests"),
|
|
20
|
+
httpDuration: createHistogram("http_request_duration_seconds", "HTTP request duration"),
|
|
21
|
+
operationErrors: createCounter("operation_errors_total", "Total operation errors"),
|
|
22
|
+
workflowDuration: createHistogram("workflow_duration_seconds", "Workflow execution duration")
|
|
23
|
+
};
|
|
24
|
+
export {
|
|
25
|
+
standardMetrics,
|
|
26
|
+
getMeter,
|
|
27
|
+
createUpDownCounter,
|
|
28
|
+
createHistogram,
|
|
29
|
+
createCounter
|
|
30
|
+
};
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// src/intent/aggregator.ts
|
|
2
|
+
var DEFAULT_WINDOW_MS = 15 * 60 * 1000;
|
|
3
|
+
|
|
4
|
+
class IntentAggregator {
|
|
5
|
+
windowMs;
|
|
6
|
+
sequenceSampleSize;
|
|
7
|
+
samples = [];
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.windowMs = options.windowMs ?? DEFAULT_WINDOW_MS;
|
|
10
|
+
this.sequenceSampleSize = options.sequenceSampleSize ?? 1000;
|
|
11
|
+
}
|
|
12
|
+
add(sample) {
|
|
13
|
+
this.samples.push(sample);
|
|
14
|
+
}
|
|
15
|
+
flush(now = new Date) {
|
|
16
|
+
const minTimestamp = now.getTime() - this.windowMs;
|
|
17
|
+
const windowSamples = this.samples.filter((sample) => sample.timestamp.getTime() >= minTimestamp);
|
|
18
|
+
this.samples.length = 0;
|
|
19
|
+
const metrics = this.aggregateMetrics(windowSamples);
|
|
20
|
+
const sequences = this.buildSequences(windowSamples);
|
|
21
|
+
const timestamps = windowSamples.map((sample) => sample.timestamp.getTime());
|
|
22
|
+
return {
|
|
23
|
+
metrics,
|
|
24
|
+
sequences,
|
|
25
|
+
sampleCount: windowSamples.length,
|
|
26
|
+
windowStart: timestamps.length ? new Date(Math.min(...timestamps)) : undefined,
|
|
27
|
+
windowEnd: timestamps.length ? new Date(Math.max(...timestamps)) : undefined
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
aggregateMetrics(samples) {
|
|
31
|
+
if (!samples.length)
|
|
32
|
+
return [];
|
|
33
|
+
const groups = new Map;
|
|
34
|
+
for (const sample of samples) {
|
|
35
|
+
const key = `${sample.operation.name}.v${sample.operation.version}`;
|
|
36
|
+
const arr = groups.get(key) ?? [];
|
|
37
|
+
arr.push(sample);
|
|
38
|
+
groups.set(key, arr);
|
|
39
|
+
}
|
|
40
|
+
return [...groups.values()].map((group) => {
|
|
41
|
+
const first = group[0];
|
|
42
|
+
if (!first)
|
|
43
|
+
throw new Error("Empty group in aggregation");
|
|
44
|
+
const durations = group.map((s) => s.durationMs).sort((a, b) => a - b);
|
|
45
|
+
const errors = group.filter((s) => !s.success);
|
|
46
|
+
const totalCalls = group.length;
|
|
47
|
+
const topErrors = errors.reduce((acc, sample) => {
|
|
48
|
+
if (!sample.errorCode)
|
|
49
|
+
return acc;
|
|
50
|
+
acc[sample.errorCode] = (acc[sample.errorCode] ?? 0) + 1;
|
|
51
|
+
return acc;
|
|
52
|
+
}, {});
|
|
53
|
+
const timestamps = group.map((s) => s.timestamp.getTime());
|
|
54
|
+
return {
|
|
55
|
+
operation: first.operation,
|
|
56
|
+
totalCalls,
|
|
57
|
+
successRate: (totalCalls - errors.length) / totalCalls,
|
|
58
|
+
errorRate: errors.length / totalCalls,
|
|
59
|
+
averageLatencyMs: durations.reduce((sum, value) => sum + value, 0) / totalCalls,
|
|
60
|
+
p95LatencyMs: percentile(durations, 0.95),
|
|
61
|
+
p99LatencyMs: percentile(durations, 0.99),
|
|
62
|
+
maxLatencyMs: Math.max(...durations),
|
|
63
|
+
windowStart: new Date(Math.min(...timestamps)),
|
|
64
|
+
windowEnd: new Date(Math.max(...timestamps)),
|
|
65
|
+
topErrors
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
buildSequences(samples) {
|
|
70
|
+
const byTrace = new Map;
|
|
71
|
+
for (const sample of samples.slice(-this.sequenceSampleSize)) {
|
|
72
|
+
if (!sample.traceId)
|
|
73
|
+
continue;
|
|
74
|
+
const arr = byTrace.get(sample.traceId) ?? [];
|
|
75
|
+
arr.push(sample);
|
|
76
|
+
byTrace.set(sample.traceId, arr);
|
|
77
|
+
}
|
|
78
|
+
const sequences = {};
|
|
79
|
+
for (const events of byTrace.values()) {
|
|
80
|
+
const ordered = events.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());
|
|
81
|
+
const steps = ordered.map((event) => event.operation.name);
|
|
82
|
+
if (steps.length < 2)
|
|
83
|
+
continue;
|
|
84
|
+
const key = `${steps.join(">")}@${ordered[0]?.tenantId ?? "global"}`;
|
|
85
|
+
const existing = sequences[key];
|
|
86
|
+
if (existing) {
|
|
87
|
+
existing.count += 1;
|
|
88
|
+
} else {
|
|
89
|
+
sequences[key] = {
|
|
90
|
+
steps,
|
|
91
|
+
tenantId: ordered[0]?.tenantId,
|
|
92
|
+
count: 1
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return Object.values(sequences).sort((a, b) => b.count - a.count);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function percentile(values, ratio) {
|
|
100
|
+
if (!values.length)
|
|
101
|
+
return 0;
|
|
102
|
+
if (values.length === 1)
|
|
103
|
+
return values[0] ?? 0;
|
|
104
|
+
const index = Math.min(values.length - 1, Math.floor(ratio * values.length));
|
|
105
|
+
return values[index] ?? 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/intent/detector.ts
|
|
109
|
+
import { randomUUID } from "node:crypto";
|
|
110
|
+
var DEFAULTS = {
|
|
111
|
+
errorRateThreshold: 0.05,
|
|
112
|
+
latencyP99ThresholdMs: 750,
|
|
113
|
+
throughputDropThreshold: 0.3,
|
|
114
|
+
minSequenceLength: 3
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
class IntentDetector {
|
|
118
|
+
options;
|
|
119
|
+
constructor(options = {}) {
|
|
120
|
+
this.options = {
|
|
121
|
+
errorRateThreshold: options.errorRateThreshold ?? DEFAULTS.errorRateThreshold,
|
|
122
|
+
latencyP99ThresholdMs: options.latencyP99ThresholdMs ?? DEFAULTS.latencyP99ThresholdMs,
|
|
123
|
+
throughputDropThreshold: options.throughputDropThreshold ?? DEFAULTS.throughputDropThreshold,
|
|
124
|
+
minSequenceLength: options.minSequenceLength ?? DEFAULTS.minSequenceLength
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
detectFromMetrics(current, previous) {
|
|
128
|
+
const signals = [];
|
|
129
|
+
const baseline = new Map((previous ?? []).map((metric) => [
|
|
130
|
+
`${metric.operation.name}.v${metric.operation.version}`,
|
|
131
|
+
metric
|
|
132
|
+
]));
|
|
133
|
+
for (const metric of current) {
|
|
134
|
+
if (metric.errorRate >= this.options.errorRateThreshold) {
|
|
135
|
+
signals.push({
|
|
136
|
+
id: randomUUID(),
|
|
137
|
+
type: "error-spike",
|
|
138
|
+
operation: metric.operation,
|
|
139
|
+
confidence: Math.min(1, metric.errorRate / this.options.errorRateThreshold),
|
|
140
|
+
description: `Error rate ${metric.errorRate.toFixed(2)} exceeded threshold`,
|
|
141
|
+
metadata: {
|
|
142
|
+
errorRate: metric.errorRate,
|
|
143
|
+
topErrors: metric.topErrors
|
|
144
|
+
},
|
|
145
|
+
evidence: [
|
|
146
|
+
{
|
|
147
|
+
type: "metric",
|
|
148
|
+
description: "error-rate",
|
|
149
|
+
data: {
|
|
150
|
+
errorRate: metric.errorRate,
|
|
151
|
+
threshold: this.options.errorRateThreshold
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
});
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (metric.p99LatencyMs >= this.options.latencyP99ThresholdMs) {
|
|
159
|
+
signals.push({
|
|
160
|
+
id: randomUUID(),
|
|
161
|
+
type: "latency-regression",
|
|
162
|
+
operation: metric.operation,
|
|
163
|
+
confidence: Math.min(1, metric.p99LatencyMs / this.options.latencyP99ThresholdMs),
|
|
164
|
+
description: `P99 latency ${metric.p99LatencyMs}ms exceeded threshold`,
|
|
165
|
+
metadata: { p99LatencyMs: metric.p99LatencyMs },
|
|
166
|
+
evidence: [
|
|
167
|
+
{
|
|
168
|
+
type: "metric",
|
|
169
|
+
description: "p99-latency",
|
|
170
|
+
data: {
|
|
171
|
+
p99LatencyMs: metric.p99LatencyMs,
|
|
172
|
+
threshold: this.options.latencyP99ThresholdMs
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
]
|
|
176
|
+
});
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const base = baseline.get(`${metric.operation.name}.v${metric.operation.version}`);
|
|
180
|
+
if (base) {
|
|
181
|
+
const drop = (base.totalCalls - metric.totalCalls) / Math.max(base.totalCalls, 1);
|
|
182
|
+
if (drop >= this.options.throughputDropThreshold) {
|
|
183
|
+
signals.push({
|
|
184
|
+
id: randomUUID(),
|
|
185
|
+
type: "throughput-drop",
|
|
186
|
+
operation: metric.operation,
|
|
187
|
+
confidence: Math.min(1, drop / this.options.throughputDropThreshold),
|
|
188
|
+
description: `Throughput dropped ${(drop * 100).toFixed(1)}% vs baseline`,
|
|
189
|
+
metadata: {
|
|
190
|
+
baselineCalls: base.totalCalls,
|
|
191
|
+
currentCalls: metric.totalCalls
|
|
192
|
+
},
|
|
193
|
+
evidence: [
|
|
194
|
+
{
|
|
195
|
+
type: "metric",
|
|
196
|
+
description: "throughput-drop",
|
|
197
|
+
data: {
|
|
198
|
+
baselineCalls: base.totalCalls,
|
|
199
|
+
currentCalls: metric.totalCalls
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
]
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return signals;
|
|
208
|
+
}
|
|
209
|
+
detectSequentialIntents(sequences) {
|
|
210
|
+
const signals = [];
|
|
211
|
+
for (const sequence of sequences) {
|
|
212
|
+
if (sequence.steps.length < this.options.minSequenceLength)
|
|
213
|
+
continue;
|
|
214
|
+
const description = sequence.steps.join(" → ");
|
|
215
|
+
signals.push({
|
|
216
|
+
id: randomUUID(),
|
|
217
|
+
type: "missing-workflow-step",
|
|
218
|
+
confidence: 0.6,
|
|
219
|
+
description: `Repeated workflow detected: ${description}`,
|
|
220
|
+
metadata: {
|
|
221
|
+
steps: sequence.steps,
|
|
222
|
+
tenantId: sequence.tenantId,
|
|
223
|
+
occurrences: sequence.count
|
|
224
|
+
},
|
|
225
|
+
evidence: [
|
|
226
|
+
{
|
|
227
|
+
type: "sequence",
|
|
228
|
+
description: "sequential-calls",
|
|
229
|
+
data: { steps: sequence.steps, count: sequence.count }
|
|
230
|
+
}
|
|
231
|
+
]
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return signals;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/pipeline/evolution-pipeline.ts
|
|
239
|
+
import { EventEmitter } from "node:events";
|
|
240
|
+
class EvolutionPipeline {
|
|
241
|
+
detector;
|
|
242
|
+
aggregator;
|
|
243
|
+
emitter;
|
|
244
|
+
onIntent;
|
|
245
|
+
onSnapshot;
|
|
246
|
+
timer;
|
|
247
|
+
previousMetrics;
|
|
248
|
+
constructor(options = {}) {
|
|
249
|
+
this.detector = options.detector ?? new IntentDetector;
|
|
250
|
+
this.aggregator = options.aggregator ?? new IntentAggregator;
|
|
251
|
+
this.emitter = options.emitter ?? new EventEmitter;
|
|
252
|
+
this.onIntent = options.onIntent;
|
|
253
|
+
this.onSnapshot = options.onSnapshot;
|
|
254
|
+
}
|
|
255
|
+
ingest(sample) {
|
|
256
|
+
this.aggregator.add(sample);
|
|
257
|
+
}
|
|
258
|
+
on(listener) {
|
|
259
|
+
this.emitter.on("event", listener);
|
|
260
|
+
}
|
|
261
|
+
start(intervalMs = 5 * 60 * 1000) {
|
|
262
|
+
this.stop();
|
|
263
|
+
this.timer = setInterval(() => {
|
|
264
|
+
this.run();
|
|
265
|
+
}, intervalMs);
|
|
266
|
+
}
|
|
267
|
+
stop() {
|
|
268
|
+
if (this.timer) {
|
|
269
|
+
clearInterval(this.timer);
|
|
270
|
+
this.timer = undefined;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
async run() {
|
|
274
|
+
const snapshot = this.aggregator.flush();
|
|
275
|
+
this.emit({
|
|
276
|
+
type: "telemetry.window",
|
|
277
|
+
payload: { sampleCount: snapshot.sampleCount }
|
|
278
|
+
});
|
|
279
|
+
if (this.onSnapshot)
|
|
280
|
+
await this.onSnapshot(snapshot);
|
|
281
|
+
if (!snapshot.sampleCount)
|
|
282
|
+
return;
|
|
283
|
+
const metricSignals = this.detector.detectFromMetrics(snapshot.metrics, this.previousMetrics);
|
|
284
|
+
const sequenceSignals = this.detector.detectSequentialIntents(snapshot.sequences);
|
|
285
|
+
this.previousMetrics = snapshot.metrics;
|
|
286
|
+
const signals = [...metricSignals, ...sequenceSignals];
|
|
287
|
+
for (const signal of signals) {
|
|
288
|
+
if (this.onIntent)
|
|
289
|
+
await this.onIntent(signal);
|
|
290
|
+
this.emit({ type: "intent.detected", payload: signal });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
emit(event) {
|
|
294
|
+
this.emitter.emit("event", event);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
export {
|
|
298
|
+
EvolutionPipeline
|
|
299
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// src/metrics/index.ts
|
|
2
|
+
import {
|
|
3
|
+
metrics
|
|
4
|
+
} from "@opentelemetry/api";
|
|
5
|
+
var DEFAULT_METER_NAME = "@contractspec/lib.observability";
|
|
6
|
+
function getMeter(name = DEFAULT_METER_NAME) {
|
|
7
|
+
return metrics.getMeter(name);
|
|
8
|
+
}
|
|
9
|
+
function createCounter(name, description, meterName) {
|
|
10
|
+
return getMeter(meterName).createCounter(name, { description });
|
|
11
|
+
}
|
|
12
|
+
function createUpDownCounter(name, description, meterName) {
|
|
13
|
+
return getMeter(meterName).createUpDownCounter(name, { description });
|
|
14
|
+
}
|
|
15
|
+
function createHistogram(name, description, meterName) {
|
|
16
|
+
return getMeter(meterName).createHistogram(name, { description });
|
|
17
|
+
}
|
|
18
|
+
var standardMetrics = {
|
|
19
|
+
httpRequests: createCounter("http_requests_total", "Total HTTP requests"),
|
|
20
|
+
httpDuration: createHistogram("http_request_duration_seconds", "HTTP request duration"),
|
|
21
|
+
operationErrors: createCounter("operation_errors_total", "Total operation errors"),
|
|
22
|
+
workflowDuration: createHistogram("workflow_duration_seconds", "Workflow execution duration")
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/pipeline/lifecycle-pipeline.ts
|
|
26
|
+
import { EventEmitter } from "node:events";
|
|
27
|
+
import { getStageLabel } from "@contractspec/lib.lifecycle";
|
|
28
|
+
class LifecycleKpiPipeline {
|
|
29
|
+
assessmentCounter;
|
|
30
|
+
confidenceHistogram;
|
|
31
|
+
stageUpDownCounter;
|
|
32
|
+
emitter;
|
|
33
|
+
lowConfidenceThreshold;
|
|
34
|
+
currentStageByTenant = new Map;
|
|
35
|
+
constructor(options = {}) {
|
|
36
|
+
const meterName = options.meterName ?? "@contractspec/lib.lifecycle-kpi";
|
|
37
|
+
this.assessmentCounter = createCounter("lifecycle_assessments_total", "Total lifecycle assessments", meterName);
|
|
38
|
+
this.confidenceHistogram = createHistogram("lifecycle_assessment_confidence", "Lifecycle assessment confidence distribution", meterName);
|
|
39
|
+
this.stageUpDownCounter = createUpDownCounter("lifecycle_stage_tenants", "Current tenants per lifecycle stage", meterName);
|
|
40
|
+
this.emitter = options.emitter ?? new EventEmitter;
|
|
41
|
+
this.lowConfidenceThreshold = options.lowConfidenceThreshold ?? 0.4;
|
|
42
|
+
}
|
|
43
|
+
recordAssessment(assessment, tenantId) {
|
|
44
|
+
const stageLabel = getStageLabel(assessment.stage);
|
|
45
|
+
const attributes = { stage: stageLabel, tenantId };
|
|
46
|
+
this.assessmentCounter.add(1, attributes);
|
|
47
|
+
this.confidenceHistogram.record(assessment.confidence, attributes);
|
|
48
|
+
this.ensureStageCounters(assessment.stage, tenantId);
|
|
49
|
+
this.emitter.emit("event", {
|
|
50
|
+
type: "assessment.recorded",
|
|
51
|
+
payload: { tenantId, stage: assessment.stage }
|
|
52
|
+
});
|
|
53
|
+
if (assessment.confidence < this.lowConfidenceThreshold) {
|
|
54
|
+
this.emitter.emit("event", {
|
|
55
|
+
type: "confidence.low",
|
|
56
|
+
payload: { tenantId, confidence: assessment.confidence }
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
on(listener) {
|
|
61
|
+
this.emitter.on("event", listener);
|
|
62
|
+
}
|
|
63
|
+
ensureStageCounters(stage, tenantId) {
|
|
64
|
+
if (!tenantId)
|
|
65
|
+
return;
|
|
66
|
+
const previous = this.currentStageByTenant.get(tenantId);
|
|
67
|
+
if (previous === stage)
|
|
68
|
+
return;
|
|
69
|
+
if (previous !== undefined) {
|
|
70
|
+
this.stageUpDownCounter.add(-1, {
|
|
71
|
+
stage: getStageLabel(previous),
|
|
72
|
+
tenantId
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
this.stageUpDownCounter.add(1, { stage: getStageLabel(stage), tenantId });
|
|
76
|
+
this.currentStageByTenant.set(tenantId, stage);
|
|
77
|
+
this.emitter.emit("event", {
|
|
78
|
+
type: "stage.changed",
|
|
79
|
+
payload: { tenantId, previousStage: previous, nextStage: stage }
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
LifecycleKpiPipeline
|
|
85
|
+
};
|