@lssm/lib.observability 0.0.0-canary-20251120192244 → 0.2.1

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 (70) hide show
  1. package/.turbo/turbo-build.log +92 -29
  2. package/.turbo/turbo-lint.log +20 -0
  3. package/CHANGELOG.md +20 -1
  4. package/README.md +6 -0
  5. package/dist/anomaly/alert-manager.d.mts +21 -0
  6. package/dist/anomaly/alert-manager.d.mts.map +1 -0
  7. package/dist/anomaly/alert-manager.mjs +2 -0
  8. package/dist/anomaly/alert-manager.mjs.map +1 -0
  9. package/dist/anomaly/anomaly-detector.d.mts +26 -0
  10. package/dist/anomaly/anomaly-detector.d.mts.map +1 -0
  11. package/dist/anomaly/anomaly-detector.mjs +2 -0
  12. package/dist/anomaly/anomaly-detector.mjs.map +1 -0
  13. package/dist/anomaly/baseline-calculator.d.mts +26 -0
  14. package/dist/anomaly/baseline-calculator.d.mts.map +1 -0
  15. package/dist/anomaly/baseline-calculator.mjs +2 -0
  16. package/dist/anomaly/baseline-calculator.mjs.map +1 -0
  17. package/dist/anomaly/root-cause-analyzer.d.mts +23 -0
  18. package/dist/anomaly/root-cause-analyzer.d.mts.map +1 -0
  19. package/dist/anomaly/root-cause-analyzer.mjs +2 -0
  20. package/dist/anomaly/root-cause-analyzer.mjs.map +1 -0
  21. package/dist/index.d.mts +10 -2
  22. package/dist/index.mjs +1 -1
  23. package/dist/intent/aggregator.d.mts +60 -0
  24. package/dist/intent/aggregator.d.mts.map +1 -0
  25. package/dist/intent/aggregator.mjs +2 -0
  26. package/dist/intent/aggregator.mjs.map +1 -0
  27. package/dist/intent/detector.d.mts +32 -0
  28. package/dist/intent/detector.d.mts.map +1 -0
  29. package/dist/intent/detector.mjs +2 -0
  30. package/dist/intent/detector.mjs.map +1 -0
  31. package/dist/lifecycle/dist/index.mjs +1 -0
  32. package/dist/lifecycle/dist/types/axes.mjs +2 -0
  33. package/dist/lifecycle/dist/types/axes.mjs.map +1 -0
  34. package/dist/lifecycle/dist/types/milestones.mjs +1 -0
  35. package/dist/lifecycle/dist/types/signals.mjs +1 -0
  36. package/dist/lifecycle/dist/types/stages.mjs +2 -0
  37. package/dist/lifecycle/dist/types/stages.mjs.map +1 -0
  38. package/dist/lifecycle/dist/utils/formatters.mjs +2 -0
  39. package/dist/lifecycle/dist/utils/formatters.mjs.map +1 -0
  40. package/dist/logging/index.mjs.map +1 -1
  41. package/dist/metrics/index.mjs.map +1 -1
  42. package/dist/pipeline/evolution-pipeline.d.mts +40 -0
  43. package/dist/pipeline/evolution-pipeline.d.mts.map +1 -0
  44. package/dist/pipeline/evolution-pipeline.mjs +2 -0
  45. package/dist/pipeline/evolution-pipeline.mjs.map +1 -0
  46. package/dist/pipeline/lifecycle-pipeline.d.mts +44 -0
  47. package/dist/pipeline/lifecycle-pipeline.d.mts.map +1 -0
  48. package/dist/pipeline/lifecycle-pipeline.mjs +2 -0
  49. package/dist/pipeline/lifecycle-pipeline.mjs.map +1 -0
  50. package/dist/tracing/index.mjs.map +1 -1
  51. package/dist/tracing/middleware.d.mts +16 -2
  52. package/dist/tracing/middleware.d.mts.map +1 -1
  53. package/dist/tracing/middleware.mjs +1 -1
  54. package/dist/tracing/middleware.mjs.map +1 -1
  55. package/package.json +12 -1
  56. package/src/anomaly/alert-manager.ts +31 -0
  57. package/src/anomaly/anomaly-detector.ts +94 -0
  58. package/src/anomaly/baseline-calculator.ts +54 -0
  59. package/src/anomaly/root-cause-analyzer.ts +55 -0
  60. package/src/index.ts +35 -1
  61. package/src/intent/aggregator.ts +161 -0
  62. package/src/intent/detector.ts +187 -0
  63. package/src/logging/index.ts +0 -1
  64. package/src/metrics/index.ts +0 -1
  65. package/src/pipeline/evolution-pipeline.ts +90 -0
  66. package/src/pipeline/lifecycle-pipeline.ts +105 -0
  67. package/src/tracing/index.ts +0 -1
  68. package/src/tracing/middleware.ts +76 -1
  69. package/tsconfig.json +6 -0
  70. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,161 @@
1
+ export interface TelemetrySample {
2
+ operation: { name: string; version: number };
3
+ durationMs: number;
4
+ success: boolean;
5
+ timestamp: Date;
6
+ errorCode?: string;
7
+ tenantId?: string;
8
+ traceId?: string;
9
+ actorId?: string;
10
+ metadata?: Record<string, unknown>;
11
+ }
12
+
13
+ export interface AggregatedOperationMetrics {
14
+ operation: { name: string; version: number };
15
+ totalCalls: number;
16
+ successRate: number;
17
+ errorRate: number;
18
+ averageLatencyMs: number;
19
+ p95LatencyMs: number;
20
+ p99LatencyMs: number;
21
+ maxLatencyMs: number;
22
+ windowStart: Date;
23
+ windowEnd: Date;
24
+ topErrors: Record<string, number>;
25
+ }
26
+
27
+ export interface OperationSequence {
28
+ steps: string[];
29
+ tenantId?: string;
30
+ count: number;
31
+ }
32
+
33
+ export interface IntentAggregatorSnapshot {
34
+ metrics: AggregatedOperationMetrics[];
35
+ sequences: OperationSequence[];
36
+ sampleCount: number;
37
+ windowStart?: Date;
38
+ windowEnd?: Date;
39
+ }
40
+
41
+ export interface IntentAggregatorOptions {
42
+ windowMs?: number;
43
+ sequenceSampleSize?: number;
44
+ }
45
+
46
+ const DEFAULT_WINDOW_MS = 15 * 60 * 1000; // 15 minutes
47
+
48
+ export class IntentAggregator {
49
+ private readonly windowMs: number;
50
+ private readonly sequenceSampleSize: number;
51
+ private readonly samples: TelemetrySample[] = [];
52
+
53
+ constructor(options: IntentAggregatorOptions = {}) {
54
+ this.windowMs = options.windowMs ?? DEFAULT_WINDOW_MS;
55
+ this.sequenceSampleSize = options.sequenceSampleSize ?? 1000;
56
+ }
57
+
58
+ add(sample: TelemetrySample) {
59
+ this.samples.push(sample);
60
+ }
61
+
62
+ flush(now = new Date()): IntentAggregatorSnapshot {
63
+ const minTimestamp = now.getTime() - this.windowMs;
64
+ const windowSamples = this.samples.filter(
65
+ (sample) => sample.timestamp.getTime() >= minTimestamp
66
+ );
67
+ this.samples.length = 0;
68
+ const metrics = this.aggregateMetrics(windowSamples);
69
+ const sequences = this.buildSequences(windowSamples);
70
+ const timestamps = windowSamples.map((sample) =>
71
+ sample.timestamp.getTime()
72
+ );
73
+ return {
74
+ metrics,
75
+ sequences,
76
+ sampleCount: windowSamples.length,
77
+ windowStart: timestamps.length
78
+ ? new Date(Math.min(...timestamps))
79
+ : undefined,
80
+ windowEnd: timestamps.length
81
+ ? new Date(Math.max(...timestamps))
82
+ : undefined,
83
+ };
84
+ }
85
+
86
+ private aggregateMetrics(samples: TelemetrySample[]) {
87
+ if (!samples.length) return [] as AggregatedOperationMetrics[];
88
+ const groups = new Map<string, TelemetrySample[]>();
89
+ for (const sample of samples) {
90
+ const key = `${sample.operation.name}.v${sample.operation.version}`;
91
+ const arr = groups.get(key) ?? [];
92
+ arr.push(sample);
93
+ groups.set(key, arr);
94
+ }
95
+
96
+ return [...groups.values()].map((group) => {
97
+ const durations = group.map((s) => s.durationMs).sort((a, b) => a - b);
98
+ const errors = group.filter((s) => !s.success);
99
+ const totalCalls = group.length;
100
+ const topErrors = errors.reduce<Record<string, number>>((acc, sample) => {
101
+ if (!sample.errorCode) return acc;
102
+ acc[sample.errorCode] = (acc[sample.errorCode] ?? 0) + 1;
103
+ return acc;
104
+ }, {});
105
+ const timestamps = group.map((s) => s.timestamp.getTime());
106
+ return {
107
+ operation: group[0]!.operation,
108
+ totalCalls,
109
+ successRate: (totalCalls - errors.length) / totalCalls,
110
+ errorRate: errors.length / totalCalls,
111
+ averageLatencyMs:
112
+ durations.reduce((sum, value) => sum + value, 0) / totalCalls,
113
+ p95LatencyMs: percentile(durations, 0.95),
114
+ p99LatencyMs: percentile(durations, 0.99),
115
+ maxLatencyMs: Math.max(...durations),
116
+ windowStart: new Date(Math.min(...timestamps)),
117
+ windowEnd: new Date(Math.max(...timestamps)),
118
+ topErrors,
119
+ } satisfies AggregatedOperationMetrics;
120
+ });
121
+ }
122
+
123
+ private buildSequences(samples: TelemetrySample[]): OperationSequence[] {
124
+ const byTrace = new Map<string, TelemetrySample[]>();
125
+ for (const sample of samples.slice(-this.sequenceSampleSize)) {
126
+ if (!sample.traceId) continue;
127
+ const arr = byTrace.get(sample.traceId) ?? [];
128
+ arr.push(sample);
129
+ byTrace.set(sample.traceId, arr);
130
+ }
131
+
132
+ const sequences: Record<string, OperationSequence> = {};
133
+ for (const [traceId, events] of byTrace.entries()) {
134
+ const ordered = events.sort(
135
+ (a, b) => a.timestamp.getTime() - b.timestamp.getTime()
136
+ );
137
+ const steps = ordered.map((event) => event.operation.name);
138
+ if (steps.length < 2) continue;
139
+ const key = `${steps.join('>')}@${ordered[0]?.tenantId ?? 'global'}`;
140
+ const existing = sequences[key];
141
+ if (existing) {
142
+ existing.count += 1;
143
+ } else {
144
+ sequences[key] = {
145
+ steps,
146
+ tenantId: ordered[0]?.tenantId,
147
+ count: 1,
148
+ };
149
+ }
150
+ }
151
+
152
+ return Object.values(sequences).sort((a, b) => b.count - a.count);
153
+ }
154
+ }
155
+
156
+ function percentile(values: number[], ratio: number) {
157
+ if (!values.length) return 0;
158
+ if (values.length === 1) return values[0]!;
159
+ const index = Math.min(values.length - 1, Math.floor(ratio * values.length));
160
+ return values[index]!;
161
+ }
@@ -0,0 +1,187 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import type {
3
+ AggregatedOperationMetrics,
4
+ OperationSequence,
5
+ } from './aggregator';
6
+
7
+ export type IntentSignalType =
8
+ | 'latency-regression'
9
+ | 'error-spike'
10
+ | 'throughput-drop'
11
+ | 'missing-workflow-step';
12
+
13
+ export interface IntentSignal {
14
+ id: string;
15
+ type: IntentSignalType;
16
+ operation?: AggregatedOperationMetrics['operation'];
17
+ confidence: number;
18
+ description: string;
19
+ metadata?: Record<string, unknown>;
20
+ evidence: {
21
+ type: 'metric' | 'sequence' | 'anomaly';
22
+ description: string;
23
+ data?: Record<string, unknown>;
24
+ }[];
25
+ }
26
+
27
+ export interface IntentDetectorOptions {
28
+ errorRateThreshold?: number;
29
+ latencyP99ThresholdMs?: number;
30
+ throughputDropThreshold?: number;
31
+ minSequenceLength?: number;
32
+ }
33
+
34
+ const DEFAULTS = {
35
+ errorRateThreshold: 0.05,
36
+ latencyP99ThresholdMs: 750,
37
+ throughputDropThreshold: 0.3,
38
+ minSequenceLength: 3,
39
+ };
40
+
41
+ export class IntentDetector {
42
+ private readonly options: Required<IntentDetectorOptions>;
43
+
44
+ constructor(options: IntentDetectorOptions = {}) {
45
+ this.options = {
46
+ errorRateThreshold:
47
+ options.errorRateThreshold ?? DEFAULTS.errorRateThreshold,
48
+ latencyP99ThresholdMs:
49
+ options.latencyP99ThresholdMs ?? DEFAULTS.latencyP99ThresholdMs,
50
+ throughputDropThreshold:
51
+ options.throughputDropThreshold ?? DEFAULTS.throughputDropThreshold,
52
+ minSequenceLength:
53
+ options.minSequenceLength ?? DEFAULTS.minSequenceLength,
54
+ };
55
+ }
56
+
57
+ detectFromMetrics(
58
+ current: AggregatedOperationMetrics[],
59
+ previous?: AggregatedOperationMetrics[]
60
+ ): IntentSignal[] {
61
+ const signals: IntentSignal[] = [];
62
+ const baseline = new Map(
63
+ (previous ?? []).map((metric) => [
64
+ `${metric.operation.name}.v${metric.operation.version}`,
65
+ metric,
66
+ ])
67
+ );
68
+
69
+ for (const metric of current) {
70
+ if (metric.errorRate >= this.options.errorRateThreshold) {
71
+ signals.push({
72
+ id: randomUUID(),
73
+ type: 'error-spike',
74
+ operation: metric.operation,
75
+ confidence: Math.min(
76
+ 1,
77
+ metric.errorRate / this.options.errorRateThreshold
78
+ ),
79
+ description: `Error rate ${metric.errorRate.toFixed(2)} exceeded threshold`,
80
+ metadata: {
81
+ errorRate: metric.errorRate,
82
+ topErrors: metric.topErrors,
83
+ },
84
+ evidence: [
85
+ {
86
+ type: 'metric',
87
+ description: 'error-rate',
88
+ data: {
89
+ errorRate: metric.errorRate,
90
+ threshold: this.options.errorRateThreshold,
91
+ },
92
+ },
93
+ ],
94
+ });
95
+ continue;
96
+ }
97
+
98
+ if (metric.p99LatencyMs >= this.options.latencyP99ThresholdMs) {
99
+ signals.push({
100
+ id: randomUUID(),
101
+ type: 'latency-regression',
102
+ operation: metric.operation,
103
+ confidence: Math.min(
104
+ 1,
105
+ metric.p99LatencyMs / this.options.latencyP99ThresholdMs
106
+ ),
107
+ description: `P99 latency ${metric.p99LatencyMs}ms exceeded threshold`,
108
+ metadata: { p99LatencyMs: metric.p99LatencyMs },
109
+ evidence: [
110
+ {
111
+ type: 'metric',
112
+ description: 'p99-latency',
113
+ data: {
114
+ p99LatencyMs: metric.p99LatencyMs,
115
+ threshold: this.options.latencyP99ThresholdMs,
116
+ },
117
+ },
118
+ ],
119
+ });
120
+ continue;
121
+ }
122
+
123
+ const base = baseline.get(
124
+ `${metric.operation.name}.v${metric.operation.version}`
125
+ );
126
+ if (base) {
127
+ const drop =
128
+ (base.totalCalls - metric.totalCalls) / Math.max(base.totalCalls, 1);
129
+ if (drop >= this.options.throughputDropThreshold) {
130
+ signals.push({
131
+ id: randomUUID(),
132
+ type: 'throughput-drop',
133
+ operation: metric.operation,
134
+ confidence: Math.min(
135
+ 1,
136
+ drop / this.options.throughputDropThreshold
137
+ ),
138
+ description: `Throughput dropped ${(drop * 100).toFixed(1)}% vs baseline`,
139
+ metadata: {
140
+ baselineCalls: base.totalCalls,
141
+ currentCalls: metric.totalCalls,
142
+ },
143
+ evidence: [
144
+ {
145
+ type: 'metric',
146
+ description: 'throughput-drop',
147
+ data: {
148
+ baselineCalls: base.totalCalls,
149
+ currentCalls: metric.totalCalls,
150
+ },
151
+ },
152
+ ],
153
+ });
154
+ }
155
+ }
156
+ }
157
+
158
+ return signals;
159
+ }
160
+
161
+ detectSequentialIntents(sequences: OperationSequence[]): IntentSignal[] {
162
+ const signals: IntentSignal[] = [];
163
+ for (const sequence of sequences) {
164
+ if (sequence.steps.length < this.options.minSequenceLength) continue;
165
+ const description = sequence.steps.join(' → ');
166
+ signals.push({
167
+ id: randomUUID(),
168
+ type: 'missing-workflow-step',
169
+ confidence: 0.6,
170
+ description: `Repeated workflow detected: ${description}`,
171
+ metadata: {
172
+ steps: sequence.steps,
173
+ tenantId: sequence.tenantId,
174
+ occurrences: sequence.count,
175
+ },
176
+ evidence: [
177
+ {
178
+ type: 'sequence',
179
+ description: 'sequential-calls',
180
+ data: { steps: sequence.steps, count: sequence.count },
181
+ },
182
+ ],
183
+ });
184
+ }
185
+ return signals;
186
+ }
187
+ }
@@ -54,4 +54,3 @@ export class Logger {
54
54
  export const logger = new Logger(
55
55
  process.env.OTEL_SERVICE_NAME || 'unknown-service'
56
56
  );
57
-
@@ -51,4 +51,3 @@ export const standardMetrics = {
51
51
  'Workflow execution duration'
52
52
  ),
53
53
  };
54
-
@@ -0,0 +1,90 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import {
3
+ IntentAggregator,
4
+ type IntentAggregatorSnapshot,
5
+ type TelemetrySample,
6
+ } from '../intent/aggregator';
7
+ import { IntentDetector, type IntentSignal } from '../intent/detector';
8
+
9
+ export type EvolutionPipelineEvent =
10
+ | { type: 'intent.detected'; payload: IntentSignal }
11
+ | { type: 'telemetry.window'; payload: { sampleCount: number } };
12
+
13
+ export interface EvolutionPipelineOptions {
14
+ detector?: IntentDetector;
15
+ aggregator?: IntentAggregator;
16
+ emitter?: EventEmitter;
17
+ onIntent?: (intent: IntentSignal) => Promise<void> | void;
18
+ onSnapshot?: (snapshot: IntentAggregatorSnapshot) => Promise<void> | void;
19
+ }
20
+
21
+ export class EvolutionPipeline {
22
+ private readonly detector: IntentDetector;
23
+ private readonly aggregator: IntentAggregator;
24
+ private readonly emitter: EventEmitter;
25
+ private readonly onIntent?: (intent: IntentSignal) => Promise<void> | void;
26
+ private readonly onSnapshot?: (
27
+ snapshot: IntentAggregatorSnapshot
28
+ ) => Promise<void> | void;
29
+ private timer?: NodeJS.Timeout;
30
+ private previousMetrics?: ReturnType<IntentAggregator['flush']>['metrics'];
31
+
32
+ constructor(options: EvolutionPipelineOptions = {}) {
33
+ this.detector = options.detector ?? new IntentDetector();
34
+ this.aggregator = options.aggregator ?? new IntentAggregator();
35
+ this.emitter = options.emitter ?? new EventEmitter();
36
+ this.onIntent = options.onIntent;
37
+ this.onSnapshot = options.onSnapshot;
38
+ }
39
+
40
+ ingest(sample: TelemetrySample) {
41
+ this.aggregator.add(sample);
42
+ }
43
+
44
+ on(listener: (event: EvolutionPipelineEvent) => void) {
45
+ this.emitter.on('event', listener);
46
+ }
47
+
48
+ start(intervalMs = 5 * 60 * 1000) {
49
+ this.stop();
50
+ this.timer = setInterval(() => {
51
+ void this.run();
52
+ }, intervalMs);
53
+ }
54
+
55
+ stop() {
56
+ if (this.timer) {
57
+ clearInterval(this.timer);
58
+ this.timer = undefined;
59
+ }
60
+ }
61
+
62
+ async run() {
63
+ const snapshot = this.aggregator.flush();
64
+ this.emit({
65
+ type: 'telemetry.window',
66
+ payload: { sampleCount: snapshot.sampleCount },
67
+ });
68
+ if (this.onSnapshot) await this.onSnapshot(snapshot);
69
+ if (!snapshot.sampleCount) return;
70
+
71
+ const metricSignals = this.detector.detectFromMetrics(
72
+ snapshot.metrics,
73
+ this.previousMetrics
74
+ );
75
+ const sequenceSignals = this.detector.detectSequentialIntents(
76
+ snapshot.sequences
77
+ );
78
+ this.previousMetrics = snapshot.metrics;
79
+
80
+ const signals = [...metricSignals, ...sequenceSignals];
81
+ for (const signal of signals) {
82
+ if (this.onIntent) await this.onIntent(signal);
83
+ this.emit({ type: 'intent.detected', payload: signal });
84
+ }
85
+ }
86
+
87
+ private emit(event: EvolutionPipelineEvent) {
88
+ this.emitter.emit('event', event);
89
+ }
90
+ }
@@ -0,0 +1,105 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import type { LifecycleAssessment, LifecycleStage } from '@lssm/lib.lifecycle';
3
+ import { getStageLabel } from '@lssm/lib.lifecycle';
4
+ import {
5
+ createCounter,
6
+ createHistogram,
7
+ createUpDownCounter,
8
+ } from '../metrics';
9
+
10
+ export type LifecyclePipelineEvent =
11
+ | {
12
+ type: 'assessment.recorded';
13
+ payload: { tenantId?: string; stage: LifecycleStage };
14
+ }
15
+ | {
16
+ type: 'stage.changed';
17
+ payload: {
18
+ tenantId?: string;
19
+ previousStage?: LifecycleStage;
20
+ nextStage: LifecycleStage;
21
+ };
22
+ }
23
+ | {
24
+ type: 'confidence.low';
25
+ payload: { tenantId?: string; confidence: number };
26
+ };
27
+
28
+ export interface LifecycleKpiPipelineOptions {
29
+ meterName?: string;
30
+ emitter?: EventEmitter;
31
+ lowConfidenceThreshold?: number;
32
+ }
33
+
34
+ export class LifecycleKpiPipeline {
35
+ private readonly assessmentCounter;
36
+ private readonly confidenceHistogram;
37
+ private readonly stageUpDownCounter;
38
+ private readonly emitter: EventEmitter;
39
+ private readonly lowConfidenceThreshold: number;
40
+ private readonly currentStageByTenant = new Map<string, LifecycleStage>();
41
+
42
+ constructor(options: LifecycleKpiPipelineOptions = {}) {
43
+ const meterName = options.meterName ?? '@lssm/lib.lifecycle-kpi';
44
+ this.assessmentCounter = createCounter(
45
+ 'lifecycle_assessments_total',
46
+ 'Total lifecycle assessments',
47
+ meterName
48
+ );
49
+ this.confidenceHistogram = createHistogram(
50
+ 'lifecycle_assessment_confidence',
51
+ 'Lifecycle assessment confidence distribution',
52
+ meterName
53
+ );
54
+ this.stageUpDownCounter = createUpDownCounter(
55
+ 'lifecycle_stage_tenants',
56
+ 'Current tenants per lifecycle stage',
57
+ meterName
58
+ );
59
+ this.emitter = options.emitter ?? new EventEmitter();
60
+ this.lowConfidenceThreshold = options.lowConfidenceThreshold ?? 0.4;
61
+ }
62
+
63
+ recordAssessment(assessment: LifecycleAssessment, tenantId?: string) {
64
+ const stageLabel = getStageLabel(assessment.stage);
65
+ const attributes = { stage: stageLabel, tenantId };
66
+ this.assessmentCounter.add(1, attributes);
67
+ this.confidenceHistogram.record(assessment.confidence, attributes);
68
+
69
+ this.ensureStageCounters(assessment.stage, tenantId);
70
+ this.emitter.emit('event', {
71
+ type: 'assessment.recorded',
72
+ payload: { tenantId, stage: assessment.stage },
73
+ } satisfies LifecyclePipelineEvent);
74
+
75
+ if (assessment.confidence < this.lowConfidenceThreshold) {
76
+ this.emitter.emit('event', {
77
+ type: 'confidence.low',
78
+ payload: { tenantId, confidence: assessment.confidence },
79
+ } satisfies LifecyclePipelineEvent);
80
+ }
81
+ }
82
+
83
+ on(listener: (event: LifecyclePipelineEvent) => void) {
84
+ this.emitter.on('event', listener);
85
+ }
86
+
87
+ private ensureStageCounters(stage: LifecycleStage, tenantId?: string) {
88
+ if (!tenantId) return;
89
+ const previous = this.currentStageByTenant.get(tenantId);
90
+ if (previous === stage) return;
91
+
92
+ if (previous !== undefined) {
93
+ this.stageUpDownCounter.add(-1, {
94
+ stage: getStageLabel(previous),
95
+ tenantId,
96
+ });
97
+ }
98
+ this.stageUpDownCounter.add(1, { stage: getStageLabel(stage), tenantId });
99
+ this.currentStageByTenant.set(tenantId, stage);
100
+ this.emitter.emit('event', {
101
+ type: 'stage.changed',
102
+ payload: { tenantId, previousStage: previous, nextStage: stage },
103
+ } satisfies LifecyclePipelineEvent);
104
+ }
105
+ }
@@ -59,4 +59,3 @@ export function traceSync<T>(
59
59
  }
60
60
  });
61
61
  }
62
-
@@ -1,7 +1,21 @@
1
+ import type { Span } from '@opentelemetry/api';
1
2
  import { traceAsync } from './index';
2
3
  import { standardMetrics } from '../metrics';
4
+ import type { TelemetrySample } from '../intent/aggregator';
3
5
 
4
- export function createTracingMiddleware() {
6
+ export interface TracingMiddlewareOptions {
7
+ resolveOperation?: (input: {
8
+ req: Request;
9
+ res?: Response;
10
+ }) => { name: string; version: number } | undefined;
11
+ onSample?: (sample: TelemetrySample) => void;
12
+ tenantResolver?: (req: Request) => string | undefined;
13
+ actorResolver?: (req: Request) => string | undefined;
14
+ }
15
+
16
+ export function createTracingMiddleware(
17
+ options: TracingMiddlewareOptions = {}
18
+ ) {
5
19
  return async (req: Request, next: () => Promise<Response>) => {
6
20
  const method = req.method;
7
21
  const url = new URL(req.url);
@@ -25,12 +39,73 @@ export function createTracingMiddleware() {
25
39
  status: response.status.toString(),
26
40
  });
27
41
 
42
+ emitTelemetrySample({
43
+ req,
44
+ res: response,
45
+ span,
46
+ success: true,
47
+ durationMs: duration * 1000,
48
+ options,
49
+ });
50
+
28
51
  return response;
29
52
  } catch (error) {
30
53
  standardMetrics.operationErrors.add(1, { method, path });
54
+ emitTelemetrySample({
55
+ req,
56
+ span,
57
+ success: false,
58
+ durationMs: performance.now() - startTime,
59
+ error,
60
+ options,
61
+ });
31
62
  throw error;
32
63
  }
33
64
  });
34
65
  };
35
66
  }
36
67
 
68
+ interface EmitTelemetryArgs {
69
+ req: Request;
70
+ res?: Response;
71
+ span: Span;
72
+ success: boolean;
73
+ durationMs: number;
74
+ error?: unknown;
75
+ options: TracingMiddlewareOptions;
76
+ }
77
+
78
+ function emitTelemetrySample({
79
+ req,
80
+ res,
81
+ span,
82
+ success,
83
+ durationMs,
84
+ error,
85
+ options,
86
+ }: EmitTelemetryArgs) {
87
+ if (!options.onSample || !options.resolveOperation) return;
88
+ const operation = options.resolveOperation({ req, res });
89
+ if (!operation) return;
90
+ const sample: TelemetrySample = {
91
+ operation,
92
+ durationMs,
93
+ success,
94
+ timestamp: new Date(),
95
+ errorCode:
96
+ !success && error instanceof Error
97
+ ? error.name
98
+ : success
99
+ ? undefined
100
+ : 'unknown',
101
+ tenantId: options.tenantResolver?.(req),
102
+ actorId: options.actorResolver?.(req),
103
+ traceId: span.spanContext().traceId,
104
+ metadata: {
105
+ method: req.method,
106
+ path: new URL(req.url).pathname,
107
+ status: res?.status,
108
+ },
109
+ };
110
+ options.onSample(sample);
111
+ }
package/tsconfig.json CHANGED
@@ -8,3 +8,9 @@
8
8
  }
9
9
  }
10
10
 
11
+
12
+
13
+
14
+
15
+
16
+