File size: 6,877 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**

 * Advanced Observability System

 * Distributed tracing, metrics, and logging

 */

export interface Trace {
    traceId: string;
    spanId: string;
    parentSpanId?: string;
    operation: string;
    startTime: Date;
    endTime?: Date;
    duration?: number;
    status: 'success' | 'error' | 'pending';
    tags: Record<string, any>;
    logs: Array<{ timestamp: Date; message: string; level: string }>;
}

export interface Metric {
    name: string;
    value: number;
    type: 'counter' | 'gauge' | 'histogram';
    timestamp: Date;
    tags: Record<string, string>;
}

export class ObservabilitySystem {
    private traces: Map<string, Trace[]> = new Map();
    private metrics: Metric[] = [];
    private activeSpans: Map<string, Trace> = new Map();

    /**

     * Start a new trace span

     */
    startSpan(operation: string, parentSpanId?: string): string {
        const spanId = this.generateId();
        const traceId = parentSpanId
            ? this.findTraceId(parentSpanId)
            : this.generateId();

        const span: Trace = {
            traceId,
            spanId,
            parentSpanId,
            operation,
            startTime: new Date(),
            status: 'pending',
            tags: {},
            logs: [],
        };

        this.activeSpans.set(spanId, span);

        if (!this.traces.has(traceId)) {
            this.traces.set(traceId, []);
        }
        this.traces.get(traceId)!.push(span);

        return spanId;
    }

    /**

     * End a trace span

     */
    endSpan(spanId: string, status: 'success' | 'error' = 'success'): void {
        const span = this.activeSpans.get(spanId);
        if (!span) return;

        span.endTime = new Date();
        span.duration = span.endTime.getTime() - span.startTime.getTime();
        span.status = status;

        this.activeSpans.delete(spanId);
    }

    /**

     * Add tags to a span

     */
    addTags(spanId: string, tags: Record<string, any>): void {
        const span = this.activeSpans.get(spanId);
        if (span) {
            span.tags = { ...span.tags, ...tags };
        }
    }

    /**

     * Add log to a span

     */
    addLog(spanId: string, message: string, level: string = 'info'): void {
        const span = this.activeSpans.get(spanId);
        if (span) {
            span.logs.push({
                timestamp: new Date(),
                message,
                level,
            });
        }
    }

    /**

     * Record a metric

     */
    recordMetric(
        name: string,
        value: number,
        type: Metric['type'] = 'gauge',
        tags: Record<string, string> = {}
    ): void {
        this.metrics.push({
            name,
            value,
            type,
            timestamp: new Date(),
            tags,
        });

        // Keep only last 10000 metrics
        if (this.metrics.length > 10000) {
            this.metrics.shift();
        }
    }

    /**

     * Get trace by ID

     */
    getTrace(traceId: string): Trace[] | undefined {
        return this.traces.get(traceId);
    }

    /**

     * Get metrics by name

     */
    getMetrics(name: string, since?: Date): Metric[] {
        let filtered = this.metrics.filter(m => m.name === name);

        if (since) {
            filtered = filtered.filter(m => m.timestamp >= since);
        }

        return filtered;
    }

    /**

     * Get aggregated metrics

     */
    getAggregatedMetrics(
        name: string,
        aggregation: 'sum' | 'avg' | 'min' | 'max' | 'count',
        since?: Date
    ): number {
        const metrics = this.getMetrics(name, since);

        if (metrics.length === 0) return 0;

        switch (aggregation) {
            case 'sum':
                return metrics.reduce((sum, m) => sum + m.value, 0);
            case 'avg':
                return metrics.reduce((sum, m) => sum + m.value, 0) / metrics.length;
            case 'min':
                return Math.min(...metrics.map(m => m.value));
            case 'max':
                return Math.max(...metrics.map(m => m.value));
            case 'count':
                return metrics.length;
            default:
                return 0;
        }
    }

    /**

     * Get slow traces (duration > threshold)

     */
    getSlowTraces(thresholdMs: number = 1000): Trace[] {
        const allTraces = Array.from(this.traces.values()).flat();
        return allTraces.filter(trace =>
            trace.duration && trace.duration > thresholdMs
        );
    }

    /**

     * Get error traces

     */
    getErrorTraces(): Trace[] {
        const allTraces = Array.from(this.traces.values()).flat();
        return allTraces.filter(trace => trace.status === 'error');
    }

    /**

     * Generate observability dashboard data

     */
    getDashboardData(): {
        totalTraces: number;
        activeSpans: number;
        errorRate: number;
        avgDuration: number;
        slowTraces: number;
        topOperations: Array<{ operation: string; count: number }>;
    } {
        const allTraces = Array.from(this.traces.values()).flat();
        const completedTraces = allTraces.filter(t => t.endTime);
        const errorTraces = allTraces.filter(t => t.status === 'error');

        const avgDuration = completedTraces.length > 0
            ? completedTraces.reduce((sum, t) => sum + (t.duration || 0), 0) / completedTraces.length
            : 0;

        // Count operations
        const operationCounts = new Map<string, number>();
        allTraces.forEach(trace => {
            operationCounts.set(trace.operation, (operationCounts.get(trace.operation) || 0) + 1);
        });

        const topOperations = Array.from(operationCounts.entries())
            .map(([operation, count]) => ({ operation, count }))
            .sort((a, b) => b.count - a.count)
            .slice(0, 10);

        return {
            totalTraces: allTraces.length,
            activeSpans: this.activeSpans.size,
            errorRate: completedTraces.length > 0 ? errorTraces.length / completedTraces.length : 0,
            avgDuration,
            slowTraces: this.getSlowTraces().length,
            topOperations,
        };
    }

    private generateId(): string {
        return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
    }

    private findTraceId(spanId: string): string {
        for (const [traceId, spans] of this.traces.entries()) {
            if (spans.some(s => s.spanId === spanId)) {
                return traceId;
            }
        }
        return this.generateId();
    }
}

export const observabilitySystem = new ObservabilitySystem();