@deeptracer/core 0.2.0 → 0.3.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.
- package/dist/chunk-XVDM75HZ.js +601 -0
- package/dist/index.cjs +333 -112
- package/dist/index.d.cts +5 -219
- package/dist/index.d.ts +5 -219
- package/dist/index.js +5 -1
- package/dist/internal-DdKQRgCs.d.cts +375 -0
- package/dist/internal-DdKQRgCs.d.ts +375 -0
- package/dist/internal.cjs +329 -112
- package/dist/internal.d.cts +1 -1
- package/dist/internal.d.ts +1 -1
- package/dist/internal.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-LSMMIS4A.js +0 -382
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for creating a DeepTracer logger instance.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const config: LoggerConfig = {
|
|
7
|
+
* product: "my-app",
|
|
8
|
+
* service: "api-server",
|
|
9
|
+
* environment: "production",
|
|
10
|
+
* endpoint: "https://deeptracer.example.com",
|
|
11
|
+
* apiKey: "dt_live_xxx",
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
interface LoggerConfig {
|
|
16
|
+
/** Product name (e.g., "spotbeam", "macro") */
|
|
17
|
+
product: string;
|
|
18
|
+
/** Service name (e.g., "api", "worker", "web") */
|
|
19
|
+
service: string;
|
|
20
|
+
/** Deployment environment */
|
|
21
|
+
environment: "production" | "staging";
|
|
22
|
+
/** DeepTracer ingestion endpoint URL */
|
|
23
|
+
endpoint: string;
|
|
24
|
+
/** DeepTracer API key for authentication */
|
|
25
|
+
apiKey: string;
|
|
26
|
+
/** Number of log entries to batch before sending. Default: 50 */
|
|
27
|
+
batchSize?: number;
|
|
28
|
+
/** Milliseconds between automatic batch flushes. Default: 5000 */
|
|
29
|
+
flushIntervalMs?: number;
|
|
30
|
+
/** Enable console output for all log calls (useful for local development) */
|
|
31
|
+
debug?: boolean;
|
|
32
|
+
/** Maximum breadcrumbs to retain for error reports. Default: 20 */
|
|
33
|
+
maxBreadcrumbs?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Hook to inspect, modify, or drop events before they are sent to DeepTracer.
|
|
36
|
+
* Return the event (possibly modified) to send it, or return `null` to drop it.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* beforeSend: (event) => {
|
|
41
|
+
* // Scrub PII
|
|
42
|
+
* if (event.type === "error" && event.data.context?.password) {
|
|
43
|
+
* delete event.data.context.password
|
|
44
|
+
* }
|
|
45
|
+
* // Drop health-check logs
|
|
46
|
+
* if (event.type === "log" && event.data.message.includes("/health")) {
|
|
47
|
+
* return null
|
|
48
|
+
* }
|
|
49
|
+
* return event
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
beforeSend?: (event: BeforeSendEvent) => BeforeSendEvent | null;
|
|
54
|
+
}
|
|
55
|
+
/** Log severity level */
|
|
56
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
57
|
+
/**
|
|
58
|
+
* A single log entry sent to the DeepTracer backend.
|
|
59
|
+
* Created internally by the Logger — you don't construct these manually.
|
|
60
|
+
*/
|
|
61
|
+
interface LogEntry {
|
|
62
|
+
timestamp: string;
|
|
63
|
+
level: LogLevel;
|
|
64
|
+
message: string;
|
|
65
|
+
metadata?: Record<string, unknown>;
|
|
66
|
+
trace_id?: string;
|
|
67
|
+
span_id?: string;
|
|
68
|
+
request_id?: string;
|
|
69
|
+
vercel_id?: string;
|
|
70
|
+
context?: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A breadcrumb entry — a trail of events leading up to an error.
|
|
74
|
+
* Automatically captured by the SDK; also settable manually via `addBreadcrumb()`.
|
|
75
|
+
*
|
|
76
|
+
* Dashboard icon mapping: `http`, `db`, `function`, `user`, `error` → specific icons.
|
|
77
|
+
*/
|
|
78
|
+
interface Breadcrumb {
|
|
79
|
+
/** Activity category (http, db, function, user, error, log, or custom) */
|
|
80
|
+
type: string;
|
|
81
|
+
/** Human-readable description (e.g., "POST /api/cart", "User loaded dashboard") */
|
|
82
|
+
message: string;
|
|
83
|
+
/** ISO 8601 timestamp */
|
|
84
|
+
timestamp: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Error report sent to DeepTracer for tracking and alerting.
|
|
88
|
+
*/
|
|
89
|
+
interface ErrorReport {
|
|
90
|
+
error_message: string;
|
|
91
|
+
stack_trace: string;
|
|
92
|
+
severity: "low" | "medium" | "high" | "critical";
|
|
93
|
+
context?: Record<string, unknown>;
|
|
94
|
+
trace_id?: string;
|
|
95
|
+
user_id?: string;
|
|
96
|
+
breadcrumbs?: Breadcrumb[];
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* LLM usage report for tracking AI costs and performance.
|
|
100
|
+
*/
|
|
101
|
+
interface LLMUsageReport {
|
|
102
|
+
model: string;
|
|
103
|
+
provider: string;
|
|
104
|
+
operation: string;
|
|
105
|
+
inputTokens: number;
|
|
106
|
+
outputTokens: number;
|
|
107
|
+
latencyMs: number;
|
|
108
|
+
costUsd?: number;
|
|
109
|
+
metadata?: Record<string, unknown>;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Raw span data sent to the DeepTracer backend.
|
|
113
|
+
*/
|
|
114
|
+
interface SpanData {
|
|
115
|
+
trace_id: string;
|
|
116
|
+
span_id: string;
|
|
117
|
+
parent_span_id: string;
|
|
118
|
+
operation: string;
|
|
119
|
+
start_time: string;
|
|
120
|
+
duration_ms: number;
|
|
121
|
+
status: "ok" | "error";
|
|
122
|
+
metadata?: Record<string, unknown>;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* A span representing a unit of work within a trace.
|
|
126
|
+
* Returned by the callback-based `startSpan()` — lifecycle is managed automatically.
|
|
127
|
+
*/
|
|
128
|
+
interface Span {
|
|
129
|
+
/** Trace ID linking all spans in a distributed trace */
|
|
130
|
+
traceId: string;
|
|
131
|
+
/** Unique ID for this span */
|
|
132
|
+
spanId: string;
|
|
133
|
+
/** ID of the parent span (empty string if root span) */
|
|
134
|
+
parentSpanId: string;
|
|
135
|
+
/** Operation name (e.g., "db-query", "fetch-user") */
|
|
136
|
+
operation: string;
|
|
137
|
+
/** Returns headers for propagating trace context to downstream services */
|
|
138
|
+
getHeaders(): Record<string, string>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* A span with manual lifecycle control. You must call `.end()` when done.
|
|
142
|
+
*/
|
|
143
|
+
interface InactiveSpan extends Span {
|
|
144
|
+
/** End the span and send timing data to DeepTracer */
|
|
145
|
+
end(options?: {
|
|
146
|
+
status?: "ok" | "error";
|
|
147
|
+
metadata?: Record<string, unknown>;
|
|
148
|
+
}): void;
|
|
149
|
+
/** Create a child span with callback-based lifecycle (auto-ends) */
|
|
150
|
+
startSpan<T>(operation: string, fn: (span: Span) => T): T;
|
|
151
|
+
/** Create a child span with manual lifecycle (you must call .end()) */
|
|
152
|
+
startInactiveSpan(operation: string): InactiveSpan;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Options for HTTP middleware (Hono, Express).
|
|
156
|
+
*/
|
|
157
|
+
interface MiddlewareOptions {
|
|
158
|
+
/** Custom function to generate the span operation name. Default: "{METHOD} {path}" */
|
|
159
|
+
operationName?: (method: string, path: string) => string;
|
|
160
|
+
/** Paths to exclude from tracing (e.g., ["/health", "/ready"]) */
|
|
161
|
+
ignorePaths?: string[];
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* User context attached to all outgoing events.
|
|
165
|
+
* Set via `logger.setUser()`, cleared via `logger.clearUser()`.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* logger.setUser({ id: "u_123", email: "user@example.com" })
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
interface User {
|
|
173
|
+
/** Unique user identifier (required) */
|
|
174
|
+
id: string;
|
|
175
|
+
/** User's email address */
|
|
176
|
+
email?: string;
|
|
177
|
+
/** Display name or username */
|
|
178
|
+
username?: string;
|
|
179
|
+
/** Any additional user attributes */
|
|
180
|
+
[key: string]: unknown;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Discriminated union for the `beforeSend` hook. Wraps every event type
|
|
184
|
+
* so the hook can inspect and modify events before they are sent.
|
|
185
|
+
*/
|
|
186
|
+
type BeforeSendEvent = {
|
|
187
|
+
type: "log";
|
|
188
|
+
data: LogEntry;
|
|
189
|
+
} | {
|
|
190
|
+
type: "error";
|
|
191
|
+
data: ErrorReport;
|
|
192
|
+
} | {
|
|
193
|
+
type: "trace";
|
|
194
|
+
data: SpanData;
|
|
195
|
+
} | {
|
|
196
|
+
type: "llm";
|
|
197
|
+
data: LLMUsageReport;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Shared mutable state across a Logger and all its child loggers.
|
|
202
|
+
* The root Logger creates this; `withContext()` and `forRequest()` pass
|
|
203
|
+
* the same reference so mutations (setUser, addBreadcrumb, etc.) are
|
|
204
|
+
* visible everywhere.
|
|
205
|
+
*/
|
|
206
|
+
interface LoggerState {
|
|
207
|
+
/** Current user context, set via setUser() */
|
|
208
|
+
user: User | null;
|
|
209
|
+
/** Global tags (flat string key-values), merged as metadata._tags */
|
|
210
|
+
tags: Record<string, string>;
|
|
211
|
+
/** Named context blocks, merged as metadata._contexts.{name} */
|
|
212
|
+
contexts: Record<string, Record<string, unknown>>;
|
|
213
|
+
/** Ring buffer of breadcrumbs for error reports */
|
|
214
|
+
breadcrumbs: Breadcrumb[];
|
|
215
|
+
/** Max breadcrumbs to retain */
|
|
216
|
+
maxBreadcrumbs: number;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Original console methods, preserved before any interception.
|
|
221
|
+
* Used internally by the Logger's debug output to avoid infinite loops
|
|
222
|
+
* when captureConsole() is active.
|
|
223
|
+
*
|
|
224
|
+
* @internal Exported for use by @deeptracer/node and @deeptracer/browser.
|
|
225
|
+
*/
|
|
226
|
+
declare const _originalConsole: {
|
|
227
|
+
log: (...data: any[]) => void;
|
|
228
|
+
info: (...data: any[]) => void;
|
|
229
|
+
warn: (...data: any[]) => void;
|
|
230
|
+
error: (...data: any[]) => void;
|
|
231
|
+
debug: (...data: any[]) => void;
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* DeepTracer Logger — lightweight observability SDK for logging, errors, tracing, and LLM usage.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* import { createLogger } from "@deeptracer/core"
|
|
239
|
+
*
|
|
240
|
+
* const logger = createLogger({
|
|
241
|
+
* product: "my-app",
|
|
242
|
+
* service: "api",
|
|
243
|
+
* environment: "production",
|
|
244
|
+
* endpoint: "https://deeptracer.example.com",
|
|
245
|
+
* apiKey: "dt_live_xxx",
|
|
246
|
+
* })
|
|
247
|
+
*
|
|
248
|
+
* logger.setUser({ id: "u_123", email: "user@example.com" })
|
|
249
|
+
* logger.setTags({ release: "1.2.3" })
|
|
250
|
+
* logger.info("Server started", { port: 3000 })
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
declare class Logger {
|
|
254
|
+
private batcher;
|
|
255
|
+
private transport;
|
|
256
|
+
protected contextName?: string;
|
|
257
|
+
protected config: LoggerConfig;
|
|
258
|
+
protected state: LoggerState;
|
|
259
|
+
protected requestMeta?: {
|
|
260
|
+
trace_id?: string;
|
|
261
|
+
span_id?: string;
|
|
262
|
+
request_id?: string;
|
|
263
|
+
vercel_id?: string;
|
|
264
|
+
};
|
|
265
|
+
constructor(config: LoggerConfig, contextName?: string, requestMeta?: {
|
|
266
|
+
trace_id?: string;
|
|
267
|
+
span_id?: string;
|
|
268
|
+
request_id?: string;
|
|
269
|
+
vercel_id?: string;
|
|
270
|
+
}, state?: LoggerState);
|
|
271
|
+
/**
|
|
272
|
+
* Set the current user context. Attached to all subsequent logs, errors, spans, and LLM reports.
|
|
273
|
+
* Shared across all child loggers (withContext, forRequest).
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```ts
|
|
277
|
+
* logger.setUser({ id: "u_123", email: "user@example.com", plan: "pro" })
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
280
|
+
setUser(user: User): void;
|
|
281
|
+
/** Clear the current user context. */
|
|
282
|
+
clearUser(): void;
|
|
283
|
+
/**
|
|
284
|
+
* Set global tags (flat string key-values). Merged into all events' metadata as `_tags`.
|
|
285
|
+
* Tags are indexed and searchable on the dashboard.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* logger.setTags({ release: "1.2.3", region: "us-east-1" })
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
setTags(tags: Record<string, string>): void;
|
|
293
|
+
/** Clear all global tags. */
|
|
294
|
+
clearTags(): void;
|
|
295
|
+
/**
|
|
296
|
+
* Set a named context block. Merged into metadata as `_contexts.{name}`.
|
|
297
|
+
* Contexts are structured objects attached for reference (not necessarily indexed).
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* ```ts
|
|
301
|
+
* logger.setContext("server", { hostname: "web-3", memory: "4gb" })
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
setContext(name: string, data: Record<string, unknown>): void;
|
|
305
|
+
/** Clear a specific context block, or all contexts if no name is given. */
|
|
306
|
+
clearContext(name?: string): void;
|
|
307
|
+
/**
|
|
308
|
+
* Manually add a breadcrumb to the trail.
|
|
309
|
+
* Breadcrumbs are also recorded automatically for every log, span, and error.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```ts
|
|
313
|
+
* logger.addBreadcrumb({ type: "http", message: "POST /api/checkout" })
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
addBreadcrumb(breadcrumb: {
|
|
317
|
+
type: string;
|
|
318
|
+
message: string;
|
|
319
|
+
timestamp?: string;
|
|
320
|
+
}): void;
|
|
321
|
+
/** Merge user, tags, and contexts from shared state into event metadata. */
|
|
322
|
+
private mergeStateMetadata;
|
|
323
|
+
/** Run the beforeSend hook. If the hook throws, pass the event through. */
|
|
324
|
+
private applyBeforeSend;
|
|
325
|
+
private log;
|
|
326
|
+
/** Log a debug message. */
|
|
327
|
+
debug(message: string, dataOrError?: Record<string, unknown> | unknown, error?: unknown): void;
|
|
328
|
+
/** Log an informational message. */
|
|
329
|
+
info(message: string, dataOrError?: Record<string, unknown> | unknown, error?: unknown): void;
|
|
330
|
+
/** Log a warning. */
|
|
331
|
+
warn(message: string, dataOrError?: Record<string, unknown> | unknown, error?: unknown): void;
|
|
332
|
+
/** Log an error. */
|
|
333
|
+
error(message: string, dataOrError?: Record<string, unknown> | unknown, error?: unknown): void;
|
|
334
|
+
/** Create a context-scoped logger. All logs include the context name. Shares state with parent. */
|
|
335
|
+
withContext(name: string): Logger;
|
|
336
|
+
/** Create a request-scoped logger that extracts trace context from headers. Shares state with parent. */
|
|
337
|
+
forRequest(request: Request): Logger;
|
|
338
|
+
/**
|
|
339
|
+
* Capture and report an error immediately (not batched).
|
|
340
|
+
* Automatically attaches breadcrumbs from the buffer and user context.
|
|
341
|
+
*/
|
|
342
|
+
captureError(error: Error | unknown, context?: {
|
|
343
|
+
severity?: "low" | "medium" | "high" | "critical";
|
|
344
|
+
userId?: string;
|
|
345
|
+
context?: Record<string, unknown>;
|
|
346
|
+
breadcrumbs?: Breadcrumb[];
|
|
347
|
+
}): void;
|
|
348
|
+
/** Track LLM usage. Sends to /ingest/llm and logs for visibility. */
|
|
349
|
+
llmUsage(report: LLMUsageReport): void;
|
|
350
|
+
/** Start a span with automatic lifecycle (callback-based, recommended). */
|
|
351
|
+
startSpan<T>(operation: string, fn: (span: Span) => T): T;
|
|
352
|
+
/** Start a span with manual lifecycle. You must call span.end(). */
|
|
353
|
+
startInactiveSpan(operation: string): InactiveSpan;
|
|
354
|
+
/** Wrap a function with automatic tracing and error capture. */
|
|
355
|
+
wrap<TArgs extends unknown[], TReturn>(operation: string, fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
356
|
+
/** Immediately flush all batched log entries. */
|
|
357
|
+
flush(): void;
|
|
358
|
+
/**
|
|
359
|
+
* Stop the batch timer, flush remaining logs, and wait for in-flight requests.
|
|
360
|
+
*
|
|
361
|
+
* @param timeoutMs - Max time to wait for in-flight requests (default: 2000ms)
|
|
362
|
+
* @returns Promise that resolves when all data is sent or timeout is reached
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```ts
|
|
366
|
+
* await logger.destroy()
|
|
367
|
+
* process.exit(0) // safe — data is confirmed sent
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
destroy(timeoutMs?: number): Promise<void>;
|
|
371
|
+
}
|
|
372
|
+
/** Create a new DeepTracer logger instance. */
|
|
373
|
+
declare function createLogger(config: LoggerConfig): Logger;
|
|
374
|
+
|
|
375
|
+
export { type BeforeSendEvent as B, type ErrorReport as E, type InactiveSpan as I, type LLMUsageReport as L, type MiddlewareOptions as M, type Span as S, type User as U, _originalConsole as _, type Breadcrumb as a, type LogEntry as b, type LogLevel as c, Logger as d, type LoggerConfig as e, type SpanData as f, createLogger as g, type LoggerState as h };
|