@lssm/lib.observability 0.0.0-canary-20251120170226 → 0.2.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 (64) hide show
  1. package/.turbo/turbo-build.log +92 -29
  2. package/.turbo/turbo-lint.log +20 -0
  3. package/CHANGELOG.md +13 -2
  4. package/README.md +7 -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/pipeline/evolution-pipeline.d.mts +40 -0
  41. package/dist/pipeline/evolution-pipeline.d.mts.map +1 -0
  42. package/dist/pipeline/evolution-pipeline.mjs +2 -0
  43. package/dist/pipeline/evolution-pipeline.mjs.map +1 -0
  44. package/dist/pipeline/lifecycle-pipeline.d.mts +44 -0
  45. package/dist/pipeline/lifecycle-pipeline.d.mts.map +1 -0
  46. package/dist/pipeline/lifecycle-pipeline.mjs +2 -0
  47. package/dist/pipeline/lifecycle-pipeline.mjs.map +1 -0
  48. package/dist/tracing/middleware.d.mts +16 -2
  49. package/dist/tracing/middleware.d.mts.map +1 -1
  50. package/dist/tracing/middleware.mjs +1 -1
  51. package/dist/tracing/middleware.mjs.map +1 -1
  52. package/package.json +12 -1
  53. package/src/anomaly/alert-manager.ts +31 -0
  54. package/src/anomaly/anomaly-detector.ts +94 -0
  55. package/src/anomaly/baseline-calculator.ts +54 -0
  56. package/src/anomaly/root-cause-analyzer.ts +55 -0
  57. package/src/index.ts +36 -1
  58. package/src/intent/aggregator.ts +161 -0
  59. package/src/intent/detector.ts +187 -0
  60. package/src/pipeline/evolution-pipeline.ts +90 -0
  61. package/src/pipeline/lifecycle-pipeline.ts +105 -0
  62. package/src/tracing/middleware.ts +77 -1
  63. package/tsconfig.json +7 -0
  64. package/tsconfig.tsbuildinfo +1 -1
@@ -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
+ }
@@ -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
+ }
@@ -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,11 +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
  }
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
@@ -7,3 +7,10 @@
7
7
  "outDir": "dist"
8
8
  }
9
9
  }
10
+
11
+
12
+
13
+
14
+
15
+
16
+