@mandujs/core 0.20.7 → 0.20.9

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.
@@ -6,3 +6,13 @@ export {
6
6
  type EventHandler,
7
7
  } from "./event-bus";
8
8
  export { connectLoggerToEventBus } from "./logger-adapter";
9
+ // Phase 6: SQLite 영구 저장 + 시계열 쿼리
10
+ export {
11
+ startSqliteStore,
12
+ stopSqliteStore,
13
+ queryEvents,
14
+ queryStats,
15
+ exportJsonl,
16
+ exportOtlp,
17
+ type QueryOptions,
18
+ } from "./sqlite-store";
@@ -0,0 +1,254 @@
1
+ /**
2
+ * Phase 6-1: SQLite 영구 저장소
3
+ *
4
+ * EventBus 이벤트를 .mandu/observability.db에 저장한다.
5
+ * Bun 내장 bun:sqlite를 사용하여 추가 의존성 없음.
6
+ *
7
+ * - 시계열 쿼리 지원 (Phase 6-2)
8
+ * - JSONL/OTLP 내보내기 지원 (Phase 6-3)
9
+ */
10
+
11
+ import path from "path";
12
+ import fs from "fs";
13
+ import type { ObservabilityEvent, EventType, ObservabilitySeverity } from "./event-bus";
14
+ import { eventBus } from "./event-bus";
15
+
16
+ interface SqliteDatabase {
17
+ exec(sql: string): void;
18
+ prepare(sql: string): {
19
+ run(...params: unknown[]): { lastInsertRowid: number | bigint };
20
+ all(...params: unknown[]): unknown[];
21
+ get(...params: unknown[]): unknown;
22
+ };
23
+ close(): void;
24
+ }
25
+
26
+ let dbInstance: SqliteDatabase | null = null;
27
+ let unsubscribe: (() => void) | null = null;
28
+
29
+ const SCHEMA = `
30
+ CREATE TABLE IF NOT EXISTS events (
31
+ id TEXT PRIMARY KEY,
32
+ correlation_id TEXT,
33
+ type TEXT NOT NULL,
34
+ severity TEXT NOT NULL,
35
+ source TEXT NOT NULL,
36
+ message TEXT NOT NULL,
37
+ data TEXT,
38
+ duration_ms INTEGER,
39
+ timestamp INTEGER NOT NULL
40
+ );
41
+ CREATE INDEX IF NOT EXISTS idx_type ON events(type);
42
+ CREATE INDEX IF NOT EXISTS idx_correlation ON events(correlation_id);
43
+ CREATE INDEX IF NOT EXISTS idx_timestamp ON events(timestamp);
44
+ CREATE INDEX IF NOT EXISTS idx_severity ON events(severity);
45
+ `;
46
+
47
+ /**
48
+ * SQLite 저장소 초기화 및 EventBus 구독
49
+ */
50
+ export async function startSqliteStore(rootDir: string): Promise<void> {
51
+ if (dbInstance) return; // 이미 시작됨
52
+
53
+ const dbDir = path.join(rootDir, ".mandu");
54
+ const dbPath = path.join(dbDir, "observability.db");
55
+
56
+ try {
57
+ fs.mkdirSync(dbDir, { recursive: true });
58
+ } catch { /* exists */ }
59
+
60
+ // bun:sqlite 동적 import (Bun 환경에서만 동작)
61
+ let Database: new (path: string) => SqliteDatabase;
62
+ try {
63
+ const mod = await import("bun:sqlite");
64
+ Database = mod.Database as unknown as typeof Database;
65
+ } catch {
66
+ console.warn("[Mandu Observability] bun:sqlite unavailable — SQLite store disabled");
67
+ return;
68
+ }
69
+
70
+ const db = new Database(dbPath);
71
+ db.exec(SCHEMA);
72
+ dbInstance = db;
73
+
74
+ const insert = db.prepare(`
75
+ INSERT OR REPLACE INTO events (id, correlation_id, type, severity, source, message, data, duration_ms, timestamp)
76
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
77
+ `);
78
+
79
+ unsubscribe = eventBus.on("*", (event) => {
80
+ try {
81
+ insert.run(
82
+ event.id,
83
+ event.correlationId ?? null,
84
+ event.type,
85
+ event.severity,
86
+ event.source,
87
+ event.message,
88
+ event.data ? JSON.stringify(event.data) : null,
89
+ event.duration ?? null,
90
+ event.timestamp,
91
+ );
92
+ } catch {
93
+ // 저장 실패는 silent — 메모리 EventBus는 계속 동작
94
+ }
95
+ });
96
+ }
97
+
98
+ /**
99
+ * SQLite 저장소 중지
100
+ */
101
+ export function stopSqliteStore(): void {
102
+ unsubscribe?.();
103
+ unsubscribe = null;
104
+ dbInstance?.close();
105
+ dbInstance = null;
106
+ }
107
+
108
+ /**
109
+ * Phase 6-2: 시계열 쿼리
110
+ */
111
+ export interface QueryOptions {
112
+ type?: EventType;
113
+ severity?: ObservabilitySeverity;
114
+ source?: string;
115
+ correlationId?: string;
116
+ sinceMs?: number; // 절대 timestamp
117
+ untilMs?: number;
118
+ limit?: number;
119
+ }
120
+
121
+ export function queryEvents(options: QueryOptions = {}): ObservabilityEvent[] {
122
+ if (!dbInstance) return [];
123
+
124
+ const conditions: string[] = [];
125
+ const params: unknown[] = [];
126
+
127
+ if (options.type) { conditions.push("type = ?"); params.push(options.type); }
128
+ if (options.severity) { conditions.push("severity = ?"); params.push(options.severity); }
129
+ if (options.source) { conditions.push("source = ?"); params.push(options.source); }
130
+ if (options.correlationId) { conditions.push("correlation_id = ?"); params.push(options.correlationId); }
131
+ if (options.sinceMs) { conditions.push("timestamp >= ?"); params.push(options.sinceMs); }
132
+ if (options.untilMs) { conditions.push("timestamp <= ?"); params.push(options.untilMs); }
133
+
134
+ const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
135
+ const limit = options.limit ?? 100;
136
+
137
+ const sql = `SELECT * FROM events ${where} ORDER BY timestamp DESC LIMIT ${limit}`;
138
+ const rows = dbInstance.prepare(sql).all(...params) as Array<{
139
+ id: string;
140
+ correlation_id: string | null;
141
+ type: string;
142
+ severity: string;
143
+ source: string;
144
+ message: string;
145
+ data: string | null;
146
+ duration_ms: number | null;
147
+ timestamp: number;
148
+ }>;
149
+
150
+ return rows.map((r) => ({
151
+ id: r.id,
152
+ correlationId: r.correlation_id ?? undefined,
153
+ type: r.type as EventType,
154
+ severity: r.severity as ObservabilitySeverity,
155
+ source: r.source,
156
+ message: r.message,
157
+ data: r.data ? JSON.parse(r.data) : undefined,
158
+ duration: r.duration_ms ?? undefined,
159
+ timestamp: r.timestamp,
160
+ }));
161
+ }
162
+
163
+ /**
164
+ * Phase 6-2: 시계열 통계 (시간 window 기반)
165
+ */
166
+ export function queryStats(windowMs: number): Record<string, { count: number; errors: number; avgDuration: number }> {
167
+ if (!dbInstance) return {};
168
+
169
+ const since = Date.now() - windowMs;
170
+ const sql = `
171
+ SELECT
172
+ type,
173
+ COUNT(*) as count,
174
+ SUM(CASE WHEN severity = 'error' THEN 1 ELSE 0 END) as errors,
175
+ AVG(duration_ms) as avg_duration
176
+ FROM events
177
+ WHERE timestamp >= ?
178
+ GROUP BY type
179
+ `;
180
+ const rows = dbInstance.prepare(sql).all(since) as Array<{
181
+ type: string;
182
+ count: number;
183
+ errors: number;
184
+ avg_duration: number | null;
185
+ }>;
186
+
187
+ const result: Record<string, { count: number; errors: number; avgDuration: number }> = {};
188
+ for (const r of rows) {
189
+ result[r.type] = {
190
+ count: r.count,
191
+ errors: r.errors,
192
+ avgDuration: r.avg_duration ?? 0,
193
+ };
194
+ }
195
+ return result;
196
+ }
197
+
198
+ /**
199
+ * Phase 6-3: JSONL 내보내기
200
+ */
201
+ export function exportJsonl(options: QueryOptions = {}): string {
202
+ const events = queryEvents({ ...options, limit: options.limit ?? 10_000 });
203
+ return events.map((e) => JSON.stringify(e)).join("\n");
204
+ }
205
+
206
+ /**
207
+ * Phase 6-3: OpenTelemetry 호환 JSON 내보내기
208
+ * (단순화된 trace 형식)
209
+ */
210
+ export function exportOtlp(options: QueryOptions = {}): string {
211
+ const events = queryEvents({ ...options, limit: options.limit ?? 10_000 });
212
+
213
+ const spans = events.map((e) => ({
214
+ traceId: e.correlationId ?? e.id,
215
+ spanId: e.id,
216
+ name: e.message,
217
+ kind: "SPAN_KIND_INTERNAL",
218
+ startTimeUnixNano: BigInt(e.timestamp) * 1_000_000n,
219
+ endTimeUnixNano: BigInt(e.timestamp + (e.duration ?? 0)) * 1_000_000n,
220
+ attributes: [
221
+ { key: "type", value: { stringValue: e.type } },
222
+ { key: "severity", value: { stringValue: e.severity } },
223
+ { key: "source", value: { stringValue: e.source } },
224
+ ...(e.data
225
+ ? Object.entries(e.data).map(([k, v]) => ({
226
+ key: k,
227
+ value: { stringValue: typeof v === "string" ? v : JSON.stringify(v) },
228
+ }))
229
+ : []),
230
+ ],
231
+ status: { code: e.severity === "error" ? 2 : 1 },
232
+ }));
233
+
234
+ // BigInt → string for JSON serialization
235
+ return JSON.stringify(
236
+ {
237
+ resourceSpans: [
238
+ {
239
+ resource: { attributes: [{ key: "service.name", value: { stringValue: "mandu" } }] },
240
+ scopeSpans: [{ scope: { name: "mandu-observability" }, spans }],
241
+ },
242
+ ],
243
+ },
244
+ (_k, v) => (typeof v === "bigint" ? v.toString() : v),
245
+ 2,
246
+ );
247
+ }
248
+
249
+ /**
250
+ * 데이터베이스 인스턴스 (테스트용)
251
+ */
252
+ export function getDb(): SqliteDatabase | null {
253
+ return dbInstance;
254
+ }
@@ -40,6 +40,7 @@ import {
40
40
  } from "./cors";
41
41
  import { validateImportPath } from "./security";
42
42
  import { KITCHEN_PREFIX, KitchenHandler, recordRequest } from "../kitchen/kitchen-handler";
43
+ import { eventBus } from "../observability/event-bus";
43
44
  import {
44
45
  type MiddlewareFn,
45
46
  type MiddlewareConfig,
@@ -759,6 +760,103 @@ interface StaticFileResult {
759
760
  }
760
761
 
761
762
  const INTERNAL_CACHE_ENDPOINT = "/_mandu/cache";
763
+ const INTERNAL_EVENTS_ENDPOINT = "/__mandu/events";
764
+
765
+ function handleEventsStreamRequest(req: Request): Response {
766
+ const url = new URL(req.url);
767
+ const filterType = url.searchParams.get("type") || undefined;
768
+ const filterSeverity = url.searchParams.get("severity") || undefined;
769
+ const filterSource = url.searchParams.get("source") || undefined;
770
+ const filterTrace = url.searchParams.get("trace") || undefined;
771
+
772
+ const matches = (e: import("../observability/event-bus").ObservabilityEvent): boolean => {
773
+ if (filterType && e.type !== filterType) return false;
774
+ if (filterSeverity && e.severity !== filterSeverity) return false;
775
+ if (filterSource && e.source !== filterSource) return false;
776
+ if (filterTrace && e.correlationId !== filterTrace) return false;
777
+ return true;
778
+ };
779
+
780
+ let unsubscribe: (() => void) | null = null;
781
+ let heartbeat: ReturnType<typeof setInterval> | null = null;
782
+
783
+ const stream = new ReadableStream<Uint8Array>({
784
+ start(controller) {
785
+ const encoder = new TextEncoder();
786
+ const send = (data: string, eventName?: string) => {
787
+ try {
788
+ const prefix = eventName ? `event: ${eventName}\n` : "";
789
+ controller.enqueue(encoder.encode(`${prefix}data: ${data}\n\n`));
790
+ } catch {
791
+ // Stream closed
792
+ }
793
+ };
794
+
795
+ // Replay recent events that match filters
796
+ const recent = eventBus.getRecent();
797
+ for (const e of recent) {
798
+ if (matches(e)) send(JSON.stringify(e));
799
+ }
800
+
801
+ // Subscribe to live events
802
+ unsubscribe = eventBus.on("*", (event) => {
803
+ if (matches(event)) send(JSON.stringify(event));
804
+ });
805
+
806
+ // Heartbeat (comment line) every 15s to keep connection alive
807
+ heartbeat = setInterval(() => {
808
+ try {
809
+ controller.enqueue(encoder.encode(`: heartbeat\n\n`));
810
+ } catch {
811
+ // ignore
812
+ }
813
+ }, 15000);
814
+
815
+ // Tear down when client disconnects
816
+ const signal = req.signal;
817
+ if (signal) {
818
+ signal.addEventListener("abort", () => {
819
+ if (unsubscribe) { unsubscribe(); unsubscribe = null; }
820
+ if (heartbeat) { clearInterval(heartbeat); heartbeat = null; }
821
+ try { controller.close(); } catch { /* noop */ }
822
+ });
823
+ }
824
+ },
825
+ cancel() {
826
+ if (unsubscribe) { unsubscribe(); unsubscribe = null; }
827
+ if (heartbeat) { clearInterval(heartbeat); heartbeat = null; }
828
+ },
829
+ });
830
+
831
+ return new Response(stream, {
832
+ status: 200,
833
+ headers: {
834
+ "Content-Type": "text/event-stream",
835
+ "Cache-Control": "no-cache, no-store, must-revalidate",
836
+ "Connection": "keep-alive",
837
+ "X-Accel-Buffering": "no",
838
+ },
839
+ });
840
+ }
841
+
842
+ function handleEventsRecentRequest(req: Request): Response {
843
+ const url = new URL(req.url);
844
+ const count = url.searchParams.get("count");
845
+ const type = url.searchParams.get("type") || undefined;
846
+ const severity = url.searchParams.get("severity") || undefined;
847
+ const windowParam = url.searchParams.get("windowMs");
848
+ const windowMs = windowParam ? Number(windowParam) : undefined;
849
+
850
+ const events = eventBus.getRecent(
851
+ count ? Number(count) : undefined,
852
+ {
853
+ type: type as import("../observability/event-bus").EventType | undefined,
854
+ severity: severity as import("../observability/event-bus").ObservabilitySeverity | undefined,
855
+ },
856
+ );
857
+ const stats = eventBus.getStats(windowMs);
858
+ return Response.json({ events, stats });
859
+ }
762
860
 
763
861
  function createStaticErrorResponse(status: 400 | 403 | 404 | 500): Response {
764
862
  const body = {
@@ -1021,6 +1119,8 @@ async function handleInternalCacheControlRequest(
1021
1119
 
1022
1120
  async function handleRequest(req: Request, router: Router, registry: ServerRegistry): Promise<Response> {
1023
1121
  const requestStart = Date.now();
1122
+ // Phase 1-4: Correlation ID — 한 요청에서 발생하는 모든 이벤트를 추적
1123
+ const correlationId = req.headers.get("x-mandu-request-id") ?? crypto.randomUUID();
1024
1124
  const result = await handleRequestInternal(req, router, registry);
1025
1125
 
1026
1126
  if (!result.ok) {
@@ -1032,10 +1132,20 @@ async function handleRequest(req: Request, router: Router, registry: ServerRegis
1032
1132
  }
1033
1133
  const url = new URL(req.url);
1034
1134
  const p = url.pathname;
1035
- if (!p.startsWith("/.mandu/") && !p.startsWith("/__kitchen")) {
1135
+ if (!p.startsWith("/.mandu/") && !p.startsWith("/__kitchen") && !p.startsWith("/__mandu/")) {
1036
1136
  const elapsed = Date.now() - requestStart;
1037
1137
  console.log(`[${new Date().toLocaleTimeString()}] ${req.method} ${p} ${errorResponse.status} ${elapsed}ms`);
1038
- recordRequest({ id: crypto.randomUUID(), method: req.method, path: p, status: errorResponse.status, duration: elapsed, timestamp: Date.now() });
1138
+ recordRequest({ id: correlationId, method: req.method, path: p, status: errorResponse.status, duration: elapsed, timestamp: Date.now() });
1139
+ // Phase 1-2: HTTP 요청 → EventBus
1140
+ eventBus.emit({
1141
+ type: "http",
1142
+ severity: errorResponse.status >= 500 ? "error" : errorResponse.status >= 400 ? "warn" : "info",
1143
+ source: "server",
1144
+ correlationId,
1145
+ message: `${req.method} ${p} ${errorResponse.status}`,
1146
+ duration: elapsed,
1147
+ data: { method: req.method, path: p, status: errorResponse.status, error: true },
1148
+ });
1039
1149
  }
1040
1150
  }
1041
1151
  return errorResponse;
@@ -1051,13 +1161,23 @@ async function handleRequest(req: Request, router: Router, registry: ServerRegis
1051
1161
  result.value.headers.set("Cache-Control", "no-cache, no-store, must-revalidate");
1052
1162
  }
1053
1163
 
1054
- if (!p.startsWith("/.mandu/") && !p.startsWith("/__kitchen")) {
1164
+ if (!p.startsWith("/.mandu/") && !p.startsWith("/__kitchen") && !p.startsWith("/__mandu/")) {
1055
1165
  const elapsed = Date.now() - requestStart;
1056
1166
  const status = result.value.status;
1057
1167
  const cacheHdr = result.value.headers.get("X-Mandu-Cache") ?? "";
1058
1168
  const cacheTag = cacheHdr ? ` ${cacheHdr}` : "";
1059
1169
  console.log(`[${new Date().toLocaleTimeString()}] ${req.method} ${p} ${status} ${elapsed}ms${cacheTag}`);
1060
- recordRequest({ id: crypto.randomUUID(), method: req.method, path: p, status, duration: elapsed, timestamp: Date.now(), cacheStatus: cacheHdr || undefined });
1170
+ recordRequest({ id: correlationId, method: req.method, path: p, status, duration: elapsed, timestamp: Date.now(), cacheStatus: cacheHdr || undefined });
1171
+ // Phase 1-2: HTTP 요청 → EventBus
1172
+ eventBus.emit({
1173
+ type: "http",
1174
+ severity: status >= 500 ? "error" : status >= 400 ? "warn" : "info",
1175
+ source: "server",
1176
+ correlationId,
1177
+ message: `${req.method} ${p} ${status}${cacheTag}`,
1178
+ duration: elapsed,
1179
+ data: { method: req.method, path: p, status, cache: cacheHdr || undefined },
1180
+ });
1061
1181
  }
1062
1182
  }
1063
1183
 
@@ -1688,6 +1808,14 @@ async function handleRequestInternal(
1688
1808
  return ok(await handleInternalCacheControlRequest(req, settings));
1689
1809
  }
1690
1810
 
1811
+ // 1.7. Internal observability EventBus stream + recent snapshot
1812
+ if (pathname === INTERNAL_EVENTS_ENDPOINT) {
1813
+ return ok(handleEventsStreamRequest(req));
1814
+ }
1815
+ if (pathname === `${INTERNAL_EVENTS_ENDPOINT}/recent`) {
1816
+ return ok(handleEventsRecentRequest(req));
1817
+ }
1818
+
1691
1819
  // 2. Kitchen dev dashboard (dev mode only)
1692
1820
  if (settings.isDev && pathname.startsWith(KITCHEN_PREFIX) && registry.kitchen) {
1693
1821
  const kitchenResponse = await registry.kitchen.handle(req, pathname);