@tracewayapp/backend 0.2.0 → 1.0.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.
package/README.md CHANGED
@@ -1,225 +1,67 @@
1
1
  # @tracewayapp/backend
2
2
 
3
- Traceway SDK for Node.js backends. Core telemetry collection library.
3
+ Traceway SDK for Node.js backends. Provides error tracking, distributed tracing, span management, and metrics collection.
4
4
 
5
- ## Quick Start
6
-
7
- ```ts
8
- import * as traceway from "@tracewayapp/backend";
9
-
10
- traceway.init("your-token@https://your-traceway-server.com/api/report", {
11
- version: "1.0.0",
12
- debug: true,
13
- });
14
-
15
- // Capture an error
16
- traceway.captureException(new Error("something broke"));
17
-
18
- // Capture a message
19
- traceway.captureMessage("Deployment completed");
20
-
21
- // Capture a custom metric
22
- traceway.captureMetric("queue.length", 42);
23
-
24
- // Graceful shutdown
25
- await traceway.shutdown();
26
- ```
27
-
28
- ## Architecture
29
-
30
- ### Two Modes of Operation
5
+ ## Installation
31
6
 
32
- The SDK supports both **manual** and **automatic** context propagation:
33
-
34
- ```
35
- ┌─────────────────────────────────────────────────────────────────────┐
36
- │ Your Application │
37
- ├─────────────────────────────────────────────────────────────────────┤
38
- │ │
39
- │ ┌─────────────────────────────────────────────────────────────┐ │
40
- │ │ AsyncLocalStorage │ │
41
- │ │ ┌─────────────────────────────────────────────────────┐ │ │
42
- │ │ │ TraceContext (per request/task) │ │ │
43
- │ │ │ - traceId: string │ │ │
44
- │ │ │ - spans: Span[] │ │ │
45
- │ │ │ - attributes: Record<string, string> │ │ │
46
- │ │ │ - startedAt: Date │ │ │
47
- │ │ │ - statusCode, bodySize, clientIP, endpoint │ │ │
48
- │ │ └─────────────────────────────────────────────────────┘ │ │
49
- │ └─────────────────────────────────────────────────────────────┘ │
50
- │ │ │
51
- │ withTraceContext() ───────┘ │
52
- │ │
53
- │ captureException() ──────► auto-detects context ──────┐ │
54
- │ captureMessage() ────────► auto-detects context ──────┤ │
55
- │ endSpan() ───────────────► auto-adds to context ──────┤ │
56
- │ captureCurrentTrace() ───► reads from context ────────┤ │
57
- │ ▼ │
58
- │ ┌────────────────────┐ │
59
- │ │ CollectionFrameStore│ │
60
- │ │ (global singleton) │ │
61
- │ └────────────────────┘ │
62
- │ │ │
63
- │ gzip + POST │
64
- │ ▼ │
65
- │ Traceway API │
66
- └─────────────────────────────────────────────────────────────────────┘
7
+ ```bash
8
+ npm install @tracewayapp/backend
67
9
  ```
68
10
 
69
- ### Automatic Context Propagation (Recommended)
70
-
71
- The SDK uses Node.js `AsyncLocalStorage` to automatically propagate trace context through async operations:
72
-
73
- ```ts
74
- import * as traceway from "@tracewayapp/backend";
75
-
76
- traceway.init("token@https://api.traceway.io/api/report");
11
+ ## Quick Start
77
12
 
78
- // Wrap your request handler
79
- app.get("/api/users", (req, res) => {
80
- traceway.withTraceContext(
81
- { endpoint: `${req.method} ${req.path}`, clientIP: req.ip },
13
+ ```typescript
14
+ import {
15
+ init,
16
+ captureException,
17
+ withTraceContext,
18
+ startSpan,
19
+ endSpan,
20
+ captureCurrentTrace,
21
+ shutdown,
22
+ } from "@tracewayapp/backend";
23
+
24
+ // Initialize once at startup
25
+ init("your-token@https://traceway.example.com/api/report");
26
+
27
+ // Use trace context for HTTP requests
28
+ async function handleRequest(req, res) {
29
+ await withTraceContext(
30
+ {
31
+ endpoint: `${req.method} ${req.path}`,
32
+ clientIP: req.ip,
33
+ },
82
34
  async () => {
83
- // All operations inside automatically belong to this trace
84
-
85
- const span = traceway.startSpan("db-query");
86
- const users = await db.query("SELECT * FROM users");
87
- traceway.endSpan(span); // Auto-added to trace context
88
-
89
- // Exceptions auto-link to trace
90
- if (!users.length) {
91
- traceway.captureMessage("No users found");
35
+ try {
36
+ const dbSpan = startSpan("database-query");
37
+ const users = await db.query("SELECT * FROM users");
38
+ endSpan(dbSpan);
39
+
40
+ res.json(users);
41
+ } catch (error) {
42
+ captureException(error);
43
+ res.status(500).json({ error: "Internal error" });
44
+ } finally {
45
+ captureCurrentTrace();
92
46
  }
93
-
94
- res.json(users);
95
-
96
- // Capture the trace at the end
97
- traceway.setTraceResponseInfo(200, JSON.stringify(users).length);
98
- traceway.captureCurrentTrace();
99
47
  }
100
48
  );
101
- });
102
- ```
103
-
104
- ### Context API
105
-
106
- | Function | Description |
107
- |----------|-------------|
108
- | `withTraceContext(options, fn)` | Run function within a new trace context |
109
- | `getTraceContext()` | Get current context (or undefined) |
110
- | `getTraceId()` | Get current trace ID (or undefined) |
111
- | `hasTraceContext()` | Check if inside a trace context |
112
- | `setTraceAttribute(key, value)` | Set attribute on current trace |
113
- | `setTraceAttributes(attrs)` | Set multiple attributes |
114
- | `setTraceResponseInfo(status, size?)` | Set HTTP response info |
115
- | `addSpanToContext(span)` | Manually add a span |
116
- | `getTraceSpans()` | Get all spans in current trace |
117
- | `getTraceDuration()` | Get elapsed time in ms |
118
- | `forkTraceContext(fn)` | Fork context for parallel ops |
119
- | `captureCurrentTrace()` | Capture trace from context |
120
-
121
- ### Building Framework Middleware
122
-
123
- The context API makes it easy to build framework-specific middleware:
124
-
125
- ```ts
126
- // Express middleware
127
- import * as traceway from "@tracewayapp/backend";
128
-
129
- export function tracewayMiddleware() {
130
- return (req, res, next) => {
131
- traceway.withTraceContext(
132
- {
133
- endpoint: `${req.method} ${req.path}`,
134
- clientIP: req.ip,
135
- attributes: { userAgent: req.get("User-Agent") || "" },
136
- },
137
- () => {
138
- // Intercept response to capture trace
139
- const originalEnd = res.end;
140
- res.end = function (...args) {
141
- traceway.setTraceResponseInfo(
142
- res.statusCode,
143
- res.get("Content-Length") ? parseInt(res.get("Content-Length")) : 0
144
- );
145
- traceway.captureCurrentTrace();
146
- return originalEnd.apply(this, args);
147
- };
148
-
149
- next();
150
- }
151
- );
152
- };
153
49
  }
154
50
 
155
- // Usage
156
- app.use(tracewayMiddleware());
157
- app.get("/api/users", async (req, res) => {
158
- // Context is already set up - just use the SDK
159
- const span = traceway.startSpan("fetch-users");
160
- try {
161
- const users = await getUsers();
162
- traceway.endSpan(span);
163
- res.json(users);
164
- } catch (err) {
165
- traceway.endSpan(span);
166
- traceway.captureException(err); // Auto-linked to request trace
167
- res.status(500).json({ error: "Internal error" });
168
- }
51
+ // Graceful shutdown
52
+ process.on("SIGTERM", async () => {
53
+ await shutdown();
54
+ process.exit(0);
169
55
  });
170
56
  ```
171
57
 
172
- ```ts
173
- // Fastify plugin
174
- import * as traceway from "@tracewayapp/backend";
175
-
176
- export const tracewayPlugin = (fastify, opts, done) => {
177
- fastify.addHook("onRequest", (request, reply, done) => {
178
- traceway.withTraceContext(
179
- {
180
- endpoint: `${request.method} ${request.url}`,
181
- clientIP: request.ip,
182
- },
183
- () => done()
184
- );
185
- });
186
-
187
- fastify.addHook("onResponse", (request, reply, done) => {
188
- traceway.setTraceResponseInfo(reply.statusCode);
189
- traceway.captureCurrentTrace();
190
- done();
191
- });
192
-
193
- done();
194
- };
195
- ```
196
-
197
- ### Manual Mode (Legacy)
198
-
199
- You can still use explicit parameter passing if preferred:
200
-
201
- ```ts
202
- import * as traceway from "@tracewayapp/backend";
203
- import { generateUUID } from "@tracewayapp/core";
204
-
205
- const traceId = generateUUID();
206
- const spans: Span[] = [];
207
-
208
- const span = traceway.startSpan("operation");
209
- // ... do work ...
210
- spans.push(traceway.endSpan(span, false)); // false = don't auto-add to context
211
-
212
- traceway.captureTrace(traceId, "GET /api", 150, new Date(), 200, 1024, "127.0.0.1", {}, spans);
213
- traceway.captureExceptionWithAttributes(err, {}, traceId);
214
- ```
215
-
216
- ## Public API
58
+ ## API
217
59
 
218
60
  ### Core Functions
219
61
 
220
62
  | Function | Description |
221
63
  |----------|-------------|
222
- | `init(connectionString, options?)` | Initialize the SDK (throws if already initialized) |
64
+ | `init(connectionString, options?)` | Initialize the SDK |
223
65
  | `shutdown()` | Stop timers, flush remaining data, and await final upload |
224
66
 
225
67
  ### Capture Functions
@@ -241,6 +83,23 @@ traceway.captureExceptionWithAttributes(err, {}, traceId);
241
83
  | `startSpan(name)` | Start a span (returns `SpanHandle`) |
242
84
  | `endSpan(span, addToContext?)` | End span (auto-adds to context by default) |
243
85
 
86
+ ### Context API
87
+
88
+ | Function | Description |
89
+ |----------|-------------|
90
+ | `withTraceContext(options, fn)` | Run function within a new trace context |
91
+ | `runWithTraceContext(options, fn)` | Run function within a new trace context (alternative) |
92
+ | `getTraceContext()` | Get current context (or `undefined`) |
93
+ | `getTraceId()` | Get current trace ID (or `undefined`) |
94
+ | `hasTraceContext()` | Check if inside a trace context |
95
+ | `setTraceAttribute(key, value)` | Set attribute on current trace |
96
+ | `setTraceAttributes(attrs)` | Set multiple attributes |
97
+ | `setTraceResponseInfo(status, size?)` | Set HTTP response info |
98
+ | `addSpanToContext(span)` | Manually add a span |
99
+ | `getTraceSpans()` | Get all spans in current trace |
100
+ | `getTraceDuration()` | Get elapsed time in ms |
101
+ | `forkTraceContext(fn)` | Fork context for parallel ops |
102
+
244
103
  ### Utility Functions
245
104
 
246
105
  | Function | Description |
@@ -248,28 +107,6 @@ traceway.captureExceptionWithAttributes(err, {}, traceId);
248
107
  | `shouldSample(isError)` | Check if trace should be recorded |
249
108
  | `measureTask(title, fn)` | Execute function as auto-captured task |
250
109
 
251
- ### Span Handle vs Span Object
252
-
253
- `startSpan()` returns a **span handle** for tracking:
254
- ```ts
255
- interface SpanHandle {
256
- id: string; // UUID
257
- name: string; // Operation name
258
- startTime: string; // ISO timestamp
259
- startedAt: number; // Unix ms (for duration calc)
260
- }
261
- ```
262
-
263
- `endSpan()` returns a **Span object** for the trace:
264
- ```ts
265
- interface Span {
266
- id: string; // Same UUID
267
- name: string; // Operation name
268
- startTime: string; // ISO timestamp
269
- duration: number; // Nanoseconds
270
- }
271
- ```
272
-
273
110
  ## Configuration Options
274
111
 
275
112
  | Option | Type | Default | Description |
@@ -284,85 +121,6 @@ interface Span {
284
121
  | `sampleRate` | `number` | `1.0` | Normal trace sampling rate (0.0-1.0) |
285
122
  | `errorSampleRate` | `number` | `1.0` | Error trace sampling rate (0.0-1.0) |
286
123
 
287
- ## Collection Frame Lifecycle
288
-
289
- ```
290
- ┌──────────────────────────────────────────────────────────────────┐
291
- │ 1. COLLECT │
292
- │ captureException/Trace/Metric ──► Current CollectionFrame │
293
- │ │
294
- │ 2. ROTATE (every collectionInterval ms) │
295
- │ Current frame ──► Send Queue (ring buffer, max 12 frames) │
296
- │ │
297
- │ 3. UPLOAD (respects uploadThrottle) │
298
- │ Send Queue ──► gzip ──► POST /api/report │
299
- │ Headers: Authorization: Bearer {token} │
300
- │ Content-Type: application/json │
301
- │ Content-Encoding: gzip │
302
- │ │
303
- │ 4. CLEANUP │
304
- │ 200 OK ──► Remove frames from queue │
305
- │ Failure ──► Retain frames for retry │
306
- │ │
307
- │ 5. SHUTDOWN │
308
- │ shutdown() ──► Rotate current ──► Upload all ──► Stop timers │
309
- └──────────────────────────────────────────────────────────────────┘
310
- ```
311
-
312
- ## Auto-Collected Metrics
313
-
314
- Every `metricsInterval` ms (default: 30s), these metrics are automatically captured:
315
-
316
- | Name | Description | Unit |
317
- |------|-------------|------|
318
- | `mem.used` | Process RSS memory | MB |
319
- | `mem.total` | Total system memory | MB |
320
- | `cpu.used_pcnt` | CPU usage (delta-based) | % (0-100) |
321
-
322
- ## Sampling
323
-
324
- Use sampling to reduce data volume in high-traffic applications:
325
-
326
- ```ts
327
- traceway.init(connectionString, {
328
- sampleRate: 0.1, // Record 10% of normal traces
329
- errorSampleRate: 1.0, // Record 100% of error traces
330
- });
331
-
332
- // In your code, check before capturing:
333
- if (traceway.shouldSample(isError)) {
334
- traceway.captureTrace(...);
335
- }
336
- ```
337
-
338
- The `measureTask()` function automatically respects sampling rates.
339
-
340
- ## Comparison with Go Client
341
-
342
- | Go Client Feature | JS Backend Equivalent |
343
- |-------------------|----------------------|
344
- | `Init(connectionString, opts...)` | `init(connectionString, options)` |
345
- | `StartTrace(ctx) context.Context` | `withTraceContext(options, fn)` |
346
- | `GetTraceFromContext(ctx)` | `getTraceContext()` |
347
- | `GetTraceIdFromContext(ctx)` | `getTraceId()` |
348
- | `GetAttributesFromContext(ctx)` | `getTraceContext()?.attributes` |
349
- | `WithAttributes(ctx, fn)` | `setTraceAttribute()` / `setTraceAttributes()` |
350
- | `StartSpan(ctx, name)` | `startSpan(name)` (auto-adds on `endSpan()`) |
351
- | `CaptureException(err)` | `captureException(err)` (auto-detects context) |
352
- | `CaptureExceptionWithContext(ctx, err)` | `captureException(err)` inside `withTraceContext()` |
353
- | `CaptureTrace(tc, ...)` | `captureCurrentTrace()` or `captureTrace(...)` |
354
- | `CaptureTask(tc, ...)` | `captureCurrentTrace()` with `isTask: true` |
355
- | `MeasureTask(title, fn)` | `measureTask(title, fn)` |
356
- | `Recover()` / `RecoverWithContext(ctx)` | Use try/catch with `captureException()` |
357
- | `tracewayhttp.New(...)` | Build with `withTraceContext()` (see examples) |
358
- | `tracewaygin.New(...)` | Build with `withTraceContext()` (see examples) |
359
- | `tracewaydb.NewTwDB(...)` | Wrap DB calls with `startSpan()`/`endSpan()` |
360
-
361
- ## Thread Safety
124
+ ## Requirements
362
125
 
363
- The SDK is designed for concurrent use in Node.js:
364
- - `AsyncLocalStorage` provides request isolation automatically
365
- - `CollectionFrameStore` uses synchronous operations (Node.js event loop)
366
- - Timer callbacks are non-blocking via `unref()`
367
- - `shutdown()` is async and awaits the final upload
368
- - Parallel requests each get their own isolated trace context
126
+ - Node.js >= 18
package/dist/index.d.mts CHANGED
@@ -45,7 +45,7 @@ declare function endSpan(span: SpanHandle, addToContext?: boolean): Span;
45
45
  declare function captureCurrentTrace(): void;
46
46
  declare function shouldSample(isError: boolean): boolean;
47
47
  declare function measureTask(title: string, fn: () => void | Promise<void>): void;
48
- declare function shutdown(): Promise<void>;
48
+ declare function shutdown(timeoutMs?: number): Promise<void>;
49
49
 
50
50
  /**
51
51
  * Trace context stored in AsyncLocalStorage.
@@ -226,7 +226,7 @@ declare class CollectionFrameStore {
226
226
  private processSendQueue;
227
227
  private triggerUpload;
228
228
  private collectSystemMetrics;
229
- shutdown(): Promise<void>;
229
+ shutdown(timeoutMs?: number): Promise<void>;
230
230
  }
231
231
 
232
232
  export { CollectionFrameStore, type CollectionFrameStoreOptions, type SpanHandle, type TraceContext, type TraceContextOptions, addSpanToContext, captureCurrentTrace, captureException, captureExceptionWithAttributes, captureMessage, captureMetric, captureTask, captureTrace, collectMetrics, endSpan, forkTraceContext, formatErrorStackTrace, getTraceContext, getTraceDuration, getTraceId, getTraceSpans, hasTraceContext, init, measureTask, resetCpuTracking, runWithTraceContext, setTraceAttribute, setTraceAttributes, setTraceResponseInfo, shouldSample, shutdown, startSpan, asyncLocalStorage as traceContextStorage, withTraceContext };
package/dist/index.d.ts CHANGED
@@ -45,7 +45,7 @@ declare function endSpan(span: SpanHandle, addToContext?: boolean): Span;
45
45
  declare function captureCurrentTrace(): void;
46
46
  declare function shouldSample(isError: boolean): boolean;
47
47
  declare function measureTask(title: string, fn: () => void | Promise<void>): void;
48
- declare function shutdown(): Promise<void>;
48
+ declare function shutdown(timeoutMs?: number): Promise<void>;
49
49
 
50
50
  /**
51
51
  * Trace context stored in AsyncLocalStorage.
@@ -226,7 +226,7 @@ declare class CollectionFrameStore {
226
226
  private processSendQueue;
227
227
  private triggerUpload;
228
228
  private collectSystemMetrics;
229
- shutdown(): Promise<void>;
229
+ shutdown(timeoutMs?: number): Promise<void>;
230
230
  }
231
231
 
232
232
  export { CollectionFrameStore, type CollectionFrameStoreOptions, type SpanHandle, type TraceContext, type TraceContextOptions, addSpanToContext, captureCurrentTrace, captureException, captureExceptionWithAttributes, captureMessage, captureMetric, captureTask, captureTrace, collectMetrics, endSpan, forkTraceContext, formatErrorStackTrace, getTraceContext, getTraceDuration, getTraceId, getTraceSpans, hasTraceContext, init, measureTask, resetCpuTracking, runWithTraceContext, setTraceAttribute, setTraceAttributes, setTraceResponseInfo, shouldSample, shutdown, startSpan, asyncLocalStorage as traceContextStorage, withTraceContext };
package/dist/index.js CHANGED
@@ -323,7 +323,7 @@ var CollectionFrameStore = class {
323
323
  }
324
324
  }
325
325
  }
326
- async shutdown() {
326
+ async shutdown(timeoutMs) {
327
327
  if (this.collectionTimer !== null) {
328
328
  clearInterval(this.collectionTimer);
329
329
  this.collectionTimer = null;
@@ -335,30 +335,40 @@ var CollectionFrameStore = class {
335
335
  this.rotateCurrentCollectionFrame();
336
336
  const frames = this.sendQueue.readAll();
337
337
  if (frames.length === 0) return;
338
- const payload = {
339
- collectionFrames: frames,
340
- appVersion: this.version,
341
- serverName: this.serverName
342
- };
343
- try {
344
- const jsonData = JSON.stringify(payload);
345
- const gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));
346
- const resp = await fetch(this.apiUrl, {
347
- method: "POST",
348
- headers: {
349
- "Content-Type": "application/json",
350
- "Content-Encoding": "gzip",
351
- Authorization: `Bearer ${this.token}`
352
- },
353
- body: gzipped
354
- });
355
- if (resp.status === 200) {
356
- this.sendQueue.remove(frames);
357
- }
358
- } catch (err) {
359
- if (this.debug) {
360
- console.error("Traceway: shutdown upload failed:", err);
338
+ const uploadPromise = (async () => {
339
+ const payload = {
340
+ collectionFrames: frames,
341
+ appVersion: this.version,
342
+ serverName: this.serverName
343
+ };
344
+ try {
345
+ const jsonData = JSON.stringify(payload);
346
+ const gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));
347
+ const resp = await fetch(this.apiUrl, {
348
+ method: "POST",
349
+ headers: {
350
+ "Content-Type": "application/json",
351
+ "Content-Encoding": "gzip",
352
+ Authorization: `Bearer ${this.token}`
353
+ },
354
+ body: gzipped
355
+ });
356
+ if (resp.status === 200) {
357
+ this.sendQueue.remove(frames);
358
+ }
359
+ } catch (err) {
360
+ if (this.debug) {
361
+ console.error("Traceway: shutdown upload failed:", err);
362
+ }
361
363
  }
364
+ })();
365
+ if (timeoutMs !== void 0) {
366
+ await Promise.race([
367
+ uploadPromise,
368
+ new Promise((resolve) => setTimeout(resolve, timeoutMs))
369
+ ]);
370
+ } else {
371
+ await uploadPromise;
362
372
  }
363
373
  }
364
374
  };
@@ -700,11 +710,11 @@ function measureTask(title, fn) {
700
710
  throw err;
701
711
  }
702
712
  }
703
- async function shutdown() {
713
+ async function shutdown(timeoutMs) {
704
714
  if (!store) return;
705
715
  const s = store;
706
716
  store = null;
707
- await s.shutdown();
717
+ await s.shutdown(timeoutMs);
708
718
  }
709
719
  // Annotate the CommonJS export names for ESM import in node:
710
720
  0 && (module.exports = {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/traceway.ts","../src/collection-frame-store.ts","../src/typed-ring.ts","../src/metrics.ts","../src/stack-trace.ts","../src/context.ts"],"sourcesContent":["export {\n init,\n captureException,\n captureExceptionWithAttributes,\n captureMessage,\n captureMetric,\n captureTrace,\n captureTask,\n captureCurrentTrace,\n startSpan,\n endSpan,\n shouldSample,\n measureTask,\n shutdown,\n} from \"./traceway.js\";\nexport type { SpanHandle } from \"./traceway.js\";\n\n// Context propagation (AsyncLocalStorage-based)\nexport {\n withTraceContext,\n runWithTraceContext,\n getTraceContext,\n getTraceId,\n hasTraceContext,\n addSpanToContext,\n setTraceAttribute,\n setTraceAttributes,\n setTraceResponseInfo,\n getTraceSpans,\n getTraceDuration,\n forkTraceContext,\n traceContextStorage,\n} from \"./context.js\";\nexport type { TraceContext, TraceContextOptions } from \"./context.js\";\n\nexport { formatErrorStackTrace } from \"./stack-trace.js\";\nexport { collectMetrics, resetCpuTracking } from \"./metrics.js\";\nexport { CollectionFrameStore } from \"./collection-frame-store.js\";\nexport type { CollectionFrameStoreOptions } from \"./collection-frame-store.js\";\n\nexport type {\n ExceptionStackTrace,\n MetricRecord,\n Span,\n Trace,\n CollectionFrame,\n ReportRequest,\n TracewayOptions,\n} from \"@tracewayapp/core\";\n","import {\n generateUUID,\n parseConnectionString,\n nowISO,\n msToNanoseconds,\n} from \"@tracewayapp/core\";\nimport type {\n TracewayOptions,\n ExceptionStackTrace,\n Trace,\n Span,\n} from \"@tracewayapp/core\";\nimport { CollectionFrameStore } from \"./collection-frame-store.js\";\nimport { formatErrorStackTrace } from \"./stack-trace.js\";\nimport {\n getTraceContext,\n getTraceId,\n addSpanToContext,\n type TraceContext,\n} from \"./context.js\";\nimport * as os from \"os\";\n\nlet store: CollectionFrameStore | null = null;\n\nfunction getHostname(): string {\n try {\n const hostname = os.hostname();\n const dotIdx = hostname.indexOf(\".\");\n return dotIdx >= 0 ? hostname.slice(0, dotIdx) : hostname;\n } catch {\n return \"unknown\";\n }\n}\n\nexport function init(\n connectionString: string,\n options: TracewayOptions = {},\n): void {\n if (store !== null) {\n throw new Error(\"Traceway: already initialized. Call shutdown() first.\");\n }\n\n const { token, apiUrl } = parseConnectionString(connectionString);\n\n store = new CollectionFrameStore({\n apiUrl,\n token,\n debug: options.debug ?? false,\n maxCollectionFrames: options.maxCollectionFrames ?? 12,\n collectionInterval: options.collectionInterval ?? 5000,\n uploadThrottle: options.uploadThrottle ?? 2000,\n metricsInterval: options.metricsInterval ?? 30000,\n version: options.version ?? \"\",\n serverName: options.serverName ?? getHostname(),\n sampleRate: options.sampleRate ?? 1,\n errorSampleRate: options.errorSampleRate ?? 1,\n });\n}\n\nexport function captureException(error: Error): void {\n if (!store) return;\n // Auto-detect trace context\n const ctx = getTraceContext();\n captureExceptionWithAttributes(\n error,\n ctx?.attributes,\n ctx?.traceId,\n );\n}\n\nexport function captureExceptionWithAttributes(\n error: Error,\n attributes?: Record<string, string>,\n traceId?: string,\n): void {\n if (!store) return;\n // Auto-detect trace context if not explicitly provided\n const ctx = getTraceContext();\n const resolvedTraceId = traceId ?? ctx?.traceId ?? null;\n const resolvedAttrs = attributes ?? ctx?.attributes;\n const isTask = ctx?.isTask ?? false;\n\n store.addException({\n traceId: resolvedTraceId,\n isTask: isTask || undefined,\n stackTrace: formatErrorStackTrace(error),\n recordedAt: nowISO(),\n attributes: resolvedAttrs,\n isMessage: false,\n });\n}\n\nexport function captureMessage(\n msg: string,\n attributes?: Record<string, string>,\n): void {\n if (!store) return;\n // Auto-detect trace context\n const ctx = getTraceContext();\n const resolvedTraceId = ctx?.traceId ?? null;\n const resolvedAttrs = attributes ?? ctx?.attributes;\n const isTask = ctx?.isTask ?? false;\n\n store.addException({\n traceId: resolvedTraceId,\n isTask: isTask || undefined,\n stackTrace: msg,\n recordedAt: nowISO(),\n attributes: resolvedAttrs,\n isMessage: true,\n });\n}\n\nexport function captureMetric(name: string, value: number): void {\n if (!store) return;\n store.addMetric({\n name,\n value,\n recordedAt: nowISO(),\n });\n}\n\nexport function captureTrace(\n traceId: string,\n endpoint: string,\n durationMs: number,\n startedAt: Date,\n statusCode: number,\n bodySize: number,\n clientIP: string,\n attributes?: Record<string, string>,\n spans?: Span[],\n): void {\n if (!store) return;\n store.addTrace({\n id: traceId,\n endpoint,\n duration: msToNanoseconds(durationMs),\n recordedAt: startedAt.toISOString(),\n statusCode,\n bodySize,\n clientIP,\n attributes,\n spans,\n });\n}\n\nexport function captureTask(\n traceId: string,\n taskName: string,\n durationMs: number,\n startedAt: Date,\n attributes?: Record<string, string>,\n spans?: Span[],\n): void {\n if (!store) return;\n store.addTrace({\n id: traceId,\n endpoint: taskName,\n duration: msToNanoseconds(durationMs),\n recordedAt: startedAt.toISOString(),\n statusCode: 0,\n bodySize: 0,\n clientIP: \"\",\n attributes,\n spans,\n isTask: true,\n });\n}\n\n/** Handle returned by startSpan for tracking span timing */\nexport interface SpanHandle {\n id: string;\n name: string;\n startTime: string;\n startedAt: number;\n}\n\n/**\n * Start a new span. If within a trace context, the span will be\n * automatically added to the trace when ended.\n */\nexport function startSpan(name: string): SpanHandle {\n const now = Date.now();\n return {\n id: generateUUID(),\n name,\n startTime: new Date(now).toISOString(),\n startedAt: now,\n };\n}\n\n/**\n * End a span and get the completed Span object.\n * If within a trace context, automatically adds the span to the trace.\n *\n * @param addToContext - If true (default), adds span to current trace context\n */\nexport function endSpan(span: SpanHandle, addToContext: boolean = true): Span {\n const durationMs = Date.now() - span.startedAt;\n const completedSpan: Span = {\n id: span.id,\n name: span.name,\n startTime: span.startTime,\n duration: msToNanoseconds(durationMs),\n };\n\n // Auto-add to trace context if available\n if (addToContext) {\n addSpanToContext(completedSpan);\n }\n\n return completedSpan;\n}\n\n/**\n * Capture the current trace context as a trace.\n * Call this at the end of a request/task to record the trace.\n *\n * @example\n * ```ts\n * // In Express middleware (after response)\n * withTraceContext({ endpoint: `${req.method} ${req.path}` }, async () => {\n * await handleRequest(req, res);\n * setTraceResponseInfo(res.statusCode, contentLength);\n * captureCurrentTrace(); // Records the trace with all spans\n * });\n * ```\n */\nexport function captureCurrentTrace(): void {\n if (!store) return;\n\n const ctx = getTraceContext();\n if (!ctx) return;\n\n const durationMs = Date.now() - ctx.startedAt.getTime();\n const isError = (ctx.statusCode ?? 0) >= 500;\n\n if (!shouldSample(isError)) return;\n\n if (ctx.isTask) {\n store.addTrace({\n id: ctx.traceId,\n endpoint: ctx.endpoint ?? \"unknown-task\",\n duration: msToNanoseconds(durationMs),\n recordedAt: ctx.startedAt.toISOString(),\n statusCode: 0,\n bodySize: 0,\n clientIP: \"\",\n attributes: Object.keys(ctx.attributes).length > 0 ? ctx.attributes : undefined,\n spans: ctx.spans.length > 0 ? ctx.spans : undefined,\n isTask: true,\n });\n } else {\n store.addTrace({\n id: ctx.traceId,\n endpoint: ctx.endpoint ?? \"unknown\",\n duration: msToNanoseconds(durationMs),\n recordedAt: ctx.startedAt.toISOString(),\n statusCode: ctx.statusCode ?? 0,\n bodySize: ctx.bodySize ?? 0,\n clientIP: ctx.clientIP ?? \"\",\n attributes: Object.keys(ctx.attributes).length > 0 ? ctx.attributes : undefined,\n spans: ctx.spans.length > 0 ? ctx.spans : undefined,\n });\n }\n}\n\nexport function shouldSample(isError: boolean): boolean {\n if (!store) return false;\n const rate = isError ? store.errorSampleRate : store.sampleRate;\n if (rate >= 1) return true;\n if (rate <= 0) return false;\n return Math.random() < rate;\n}\n\nexport function measureTask(\n title: string,\n fn: () => void | Promise<void>,\n): void {\n const traceId = generateUUID();\n const start = Date.now();\n const startDate = new Date(start);\n\n try {\n const result = fn();\n if (result && typeof (result as Promise<void>).then === \"function\") {\n (result as Promise<void>)\n .then(() => {\n const durationMs = Date.now() - start;\n if (shouldSample(false)) {\n captureTask(traceId, title, durationMs, startDate);\n }\n })\n .catch((err: unknown) => {\n const durationMs = Date.now() - start;\n if (shouldSample(true)) {\n captureTask(traceId, title, durationMs, startDate);\n if (err instanceof Error) {\n captureExceptionWithAttributes(err, undefined, traceId);\n }\n }\n throw err;\n });\n } else {\n const durationMs = Date.now() - start;\n if (shouldSample(false)) {\n captureTask(traceId, title, durationMs, startDate);\n }\n }\n } catch (err) {\n const durationMs = Date.now() - start;\n if (shouldSample(true)) {\n captureTask(traceId, title, durationMs, startDate);\n if (err instanceof Error) {\n captureExceptionWithAttributes(err, undefined, traceId);\n }\n }\n throw err;\n }\n}\n\nexport async function shutdown(): Promise<void> {\n if (!store) return;\n const s = store;\n store = null;\n await s.shutdown();\n}\n","import * as zlib from \"zlib\";\nimport {\n nowISO,\n generateUUID,\n msToNanoseconds,\n} from \"@tracewayapp/core\";\nimport { TypedRing } from \"./typed-ring.js\";\nimport type {\n CollectionFrame,\n ExceptionStackTrace,\n MetricRecord,\n Trace,\n Span,\n ReportRequest,\n} from \"@tracewayapp/core\";\nimport { collectMetrics } from \"./metrics.js\";\n\nexport interface CollectionFrameStoreOptions {\n apiUrl: string;\n token: string;\n debug: boolean;\n maxCollectionFrames: number;\n collectionInterval: number;\n uploadThrottle: number;\n metricsInterval: number;\n version: string;\n serverName: string;\n sampleRate: number;\n errorSampleRate: number;\n}\n\nexport class CollectionFrameStore {\n private current: CollectionFrame | null = null;\n private currentSetAt: number = Date.now();\n private sendQueue: TypedRing<CollectionFrame>;\n private lastUploadStarted: number | null = null;\n\n private collectionTimer: ReturnType<typeof setInterval> | null = null;\n private metricsTimer: ReturnType<typeof setInterval> | null = null;\n\n private readonly apiUrl: string;\n private readonly token: string;\n private readonly debug: boolean;\n private readonly collectionInterval: number;\n private readonly uploadThrottle: number;\n private readonly metricsInterval: number;\n private readonly version: string;\n private readonly serverName: string;\n\n readonly sampleRate: number;\n readonly errorSampleRate: number;\n\n constructor(options: CollectionFrameStoreOptions) {\n this.apiUrl = options.apiUrl;\n this.token = options.token;\n this.debug = options.debug;\n this.collectionInterval = options.collectionInterval;\n this.uploadThrottle = options.uploadThrottle;\n this.metricsInterval = options.metricsInterval;\n this.version = options.version;\n this.serverName = options.serverName;\n this.sampleRate = options.sampleRate;\n this.errorSampleRate = options.errorSampleRate;\n this.sendQueue = new TypedRing<CollectionFrame>(\n options.maxCollectionFrames,\n );\n\n this.collectionTimer = setInterval(() => {\n this.tick();\n }, this.collectionInterval);\n if (this.collectionTimer && typeof this.collectionTimer.unref === \"function\") {\n this.collectionTimer.unref();\n }\n\n this.metricsTimer = setInterval(() => {\n this.collectSystemMetrics();\n }, this.metricsInterval);\n if (this.metricsTimer && typeof this.metricsTimer.unref === \"function\") {\n this.metricsTimer.unref();\n }\n }\n\n private tick(): void {\n if (this.current !== null) {\n if (this.currentSetAt < Date.now() - this.collectionInterval) {\n this.rotateCurrentCollectionFrame();\n this.processSendQueue();\n }\n } else if (this.sendQueue.length > 0) {\n this.processSendQueue();\n }\n }\n\n private ensureCurrent(): CollectionFrame {\n if (this.current === null) {\n this.current = { stackTraces: [], metrics: [], traces: [] };\n this.currentSetAt = Date.now();\n }\n return this.current;\n }\n\n addException(exception: ExceptionStackTrace): void {\n this.ensureCurrent().stackTraces.push(exception);\n }\n\n addMetric(metric: MetricRecord): void {\n this.ensureCurrent().metrics.push(metric);\n }\n\n addTrace(trace: Trace): void {\n this.ensureCurrent().traces.push(trace);\n }\n\n private rotateCurrentCollectionFrame(): void {\n if (this.current !== null) {\n this.sendQueue.push(this.current);\n this.current = null;\n }\n }\n\n private processSendQueue(): void {\n if (\n this.lastUploadStarted === null ||\n this.lastUploadStarted < Date.now() - this.uploadThrottle\n ) {\n this.lastUploadStarted = Date.now();\n const frames = this.sendQueue.readAll();\n if (frames.length > 0) {\n this.triggerUpload(frames);\n }\n }\n }\n\n private triggerUpload(framesToSend: CollectionFrame[]): void {\n const payload: ReportRequest = {\n collectionFrames: framesToSend,\n appVersion: this.version,\n serverName: this.serverName,\n };\n\n let jsonData: string;\n try {\n jsonData = JSON.stringify(payload);\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to serialize frames:\", err);\n }\n return;\n }\n\n let gzipped: Uint8Array;\n try {\n gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to gzip:\", err);\n }\n return;\n }\n\n fetch(this.apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${this.token}`,\n },\n body: gzipped as unknown as BodyInit,\n })\n .then((resp) => {\n if (resp.status === 200) {\n this.sendQueue.remove(framesToSend);\n } else if (this.debug) {\n console.error(`Traceway: upload returned status ${resp.status}`);\n }\n })\n .catch((err) => {\n if (this.debug) {\n console.error(\"Traceway: upload failed:\", err);\n }\n });\n }\n\n private collectSystemMetrics(): void {\n try {\n const metrics = collectMetrics();\n for (const metric of metrics) {\n this.addMetric(metric);\n }\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to collect metrics:\", err);\n }\n }\n }\n\n async shutdown(): Promise<void> {\n if (this.collectionTimer !== null) {\n clearInterval(this.collectionTimer);\n this.collectionTimer = null;\n }\n if (this.metricsTimer !== null) {\n clearInterval(this.metricsTimer);\n this.metricsTimer = null;\n }\n\n this.rotateCurrentCollectionFrame();\n\n const frames = this.sendQueue.readAll();\n if (frames.length === 0) return;\n\n const payload: ReportRequest = {\n collectionFrames: frames,\n appVersion: this.version,\n serverName: this.serverName,\n };\n\n try {\n const jsonData = JSON.stringify(payload);\n const gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));\n\n const resp = await fetch(this.apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${this.token}`,\n },\n body: gzipped as unknown as BodyInit,\n });\n\n if (resp.status === 200) {\n this.sendQueue.remove(frames);\n }\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: shutdown upload failed:\", err);\n }\n }\n }\n}\n","export class TypedRing<T> {\n private arr: (T | null)[];\n private head: number = 0;\n private _capacity: number;\n private _len: number = 0;\n\n constructor(capacity: number) {\n this._capacity = capacity;\n this.arr = new Array<T | null>(capacity).fill(null);\n }\n\n get length(): number {\n return this._len;\n }\n\n get capacity(): number {\n return this._capacity;\n }\n\n push(val: T): void {\n this.arr[this.head] = val;\n this.head = (this.head + 1) % this._capacity;\n if (this._len < this._capacity) {\n this._len += 1;\n }\n }\n\n readAll(): T[] {\n const result: T[] = [];\n for (let i = 0; i < this._len; i++) {\n const idx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n result.push(this.arr[idx] as T);\n }\n return result;\n }\n\n clear(): void {\n for (let i = 0; i < this.arr.length; i++) {\n this.arr[i] = null;\n }\n this.head = 0;\n this._len = 0;\n }\n\n remove(vals: T[]): number {\n if (vals.length === 0) return 0;\n\n const toRemove = new Set<T>(vals);\n let writeIdx = 0;\n let removed = 0;\n\n for (let i = 0; i < this._len; i++) {\n const readIdx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n if (toRemove.has(this.arr[readIdx] as T)) {\n removed++;\n } else {\n if (writeIdx !== i) {\n const destIdx =\n (this.head - this._len + writeIdx + this._capacity) %\n this._capacity;\n this.arr[destIdx] = this.arr[readIdx];\n }\n writeIdx++;\n }\n }\n\n for (let i = writeIdx; i < this._len; i++) {\n const idx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n this.arr[idx] = null;\n }\n\n this._len = writeIdx;\n this.head = (this.head - removed + this._capacity) % this._capacity;\n\n return removed;\n }\n}\n","import * as os from \"os\";\nimport { METRIC_MEM_USED, METRIC_MEM_TOTAL, METRIC_CPU_USED_PCNT } from \"@tracewayapp/core\";\nimport type { MetricRecord } from \"@tracewayapp/core\";\nimport { nowISO } from \"@tracewayapp/core\";\n\nlet prevCpuUsage: NodeJS.CpuUsage | null = null;\nlet prevCpuTime: number | null = null;\n\nexport function collectMetrics(): MetricRecord[] {\n const metrics: MetricRecord[] = [];\n const recordedAt = nowISO();\n\n const mem = process.memoryUsage();\n metrics.push({\n name: METRIC_MEM_USED,\n value: Math.round((mem.rss / 1024 / 1024) * 100) / 100,\n recordedAt,\n });\n\n const totalMem = os.totalmem();\n metrics.push({\n name: METRIC_MEM_TOTAL,\n value: Math.round((totalMem / 1024 / 1024) * 100) / 100,\n recordedAt,\n });\n\n const currentCpuUsage = process.cpuUsage();\n const currentTime = Date.now();\n if (prevCpuUsage !== null && prevCpuTime !== null) {\n const elapsedMs = currentTime - prevCpuTime;\n if (elapsedMs > 0) {\n const userDelta = currentCpuUsage.user - prevCpuUsage.user;\n const systemDelta = currentCpuUsage.system - prevCpuUsage.system;\n const totalDeltaMs = (userDelta + systemDelta) / 1000;\n const cpuCount = os.cpus().length || 1;\n const cpuPercent = (totalDeltaMs / elapsedMs / cpuCount) * 100;\n metrics.push({\n name: METRIC_CPU_USED_PCNT,\n value: Math.round(cpuPercent * 100) / 100,\n recordedAt,\n });\n }\n }\n prevCpuUsage = currentCpuUsage;\n prevCpuTime = currentTime;\n\n return metrics;\n}\n\nexport function resetCpuTracking(): void {\n prevCpuUsage = null;\n prevCpuTime = null;\n}\n","export function formatErrorStackTrace(error: Error): string {\n const lines: string[] = [];\n\n const typeName = error.constructor.name || \"Error\";\n lines.push(`${typeName}: ${error.message}`);\n\n if (error.stack) {\n const stackLines = error.stack.split(\"\\n\");\n for (const line of stackLines) {\n const match = line.match(/^\\s+at\\s+(.+?)\\s+\\((.+):(\\d+):\\d+\\)$/);\n if (match) {\n const funcName = shortenFunctionName(match[1]);\n const file = shortenFilePath(match[2]);\n const lineNum = match[3];\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${lineNum}`);\n continue;\n }\n\n const matchNoParens = line.match(/^\\s+at\\s+(.+):(\\d+):\\d+$/);\n if (matchNoParens) {\n const file = shortenFilePath(matchNoParens[1]);\n const lineNum = matchNoParens[2];\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${lineNum}`);\n continue;\n }\n\n const matchFnOnly = line.match(/^\\s+at\\s+(.+)$/);\n if (matchFnOnly) {\n const funcName = shortenFunctionName(matchFnOnly[1]);\n lines.push(`${funcName}()`);\n lines.push(` <unknown>:0`);\n }\n }\n }\n\n return lines.join(\"\\n\") + \"\\n\";\n}\n\nfunction shortenFunctionName(fn: string): string {\n const slashIdx = fn.lastIndexOf(\"/\");\n if (slashIdx >= 0) {\n fn = fn.slice(slashIdx + 1);\n }\n const dotIdx = fn.indexOf(\".\");\n if (dotIdx >= 0) {\n fn = fn.slice(dotIdx + 1);\n }\n return fn;\n}\n\nfunction shortenFilePath(filePath: string): string {\n const parts = filePath.split(\"/\");\n return parts[parts.length - 1];\n}\n","import { AsyncLocalStorage } from \"async_hooks\";\nimport { generateUUID } from \"@tracewayapp/core\";\nimport type { Span } from \"@tracewayapp/core\";\n\n/**\n * Trace context stored in AsyncLocalStorage.\n * Automatically propagates through async operations.\n */\nexport interface TraceContext {\n /** Unique trace identifier (UUID v4) */\n traceId: string;\n /** Whether this is a background task (vs HTTP request) */\n isTask: boolean;\n /** When the trace started */\n startedAt: Date;\n /** Collected spans within this trace */\n spans: Span[];\n /** Key-value attributes for this trace */\n attributes: Record<string, string>;\n /** For HTTP traces: endpoint like \"GET /api/users\" */\n endpoint?: string;\n /** For HTTP traces: response status code */\n statusCode?: number;\n /** For HTTP traces: response body size */\n bodySize?: number;\n /** For HTTP traces: client IP address */\n clientIP?: string;\n}\n\n/**\n * Options for creating a new trace context.\n */\nexport interface TraceContextOptions {\n /** Custom trace ID (auto-generated if not provided) */\n traceId?: string;\n /** Mark as background task instead of HTTP trace */\n isTask?: boolean;\n /** Initial attributes */\n attributes?: Record<string, string>;\n /** HTTP endpoint (e.g., \"GET /api/users\") */\n endpoint?: string;\n /** Client IP address */\n clientIP?: string;\n}\n\n// The AsyncLocalStorage instance for trace context\nconst asyncLocalStorage = new AsyncLocalStorage<TraceContext>();\n\n/**\n * Get the current trace context, if any.\n * Returns undefined if not within a trace context.\n */\nexport function getTraceContext(): TraceContext | undefined {\n return asyncLocalStorage.getStore();\n}\n\n/**\n * Get the current trace ID, if within a trace context.\n */\nexport function getTraceId(): string | undefined {\n return asyncLocalStorage.getStore()?.traceId;\n}\n\n/**\n * Check if currently within a trace context.\n */\nexport function hasTraceContext(): boolean {\n return asyncLocalStorage.getStore() !== undefined;\n}\n\n/**\n * Run a function within a new trace context.\n * All async operations within will have access to this context.\n *\n * @example\n * ```ts\n * // HTTP request handler\n * withTraceContext({ endpoint: \"GET /api/users\", clientIP: req.ip }, async () => {\n * const users = await db.query(\"SELECT * FROM users\");\n * captureException(new Error(\"oops\")); // Auto-linked to trace\n * return users;\n * });\n *\n * // Background task\n * withTraceContext({ isTask: true, endpoint: \"process-emails\" }, async () => {\n * await processEmails();\n * });\n * ```\n */\nexport function withTraceContext<T>(\n options: TraceContextOptions,\n fn: () => T,\n): T {\n const context: TraceContext = {\n traceId: options.traceId ?? generateUUID(),\n isTask: options.isTask ?? false,\n startedAt: new Date(),\n spans: [],\n attributes: options.attributes ?? {},\n endpoint: options.endpoint,\n clientIP: options.clientIP,\n };\n return asyncLocalStorage.run(context, fn);\n}\n\n/**\n * Run a function within a trace context, automatically capturing the trace on completion.\n * This is a convenience wrapper that handles timing and capture automatically.\n *\n * @example\n * ```ts\n * // In Express middleware\n * app.use((req, res, next) => {\n * runWithTraceContext(\n * {\n * endpoint: `${req.method} ${req.path}`,\n * clientIP: req.ip,\n * attributes: { userId: req.user?.id },\n * },\n * async () => {\n * await next();\n * // Status code and body size set via setTraceResponseInfo()\n * },\n * { captureOnEnd: true }\n * );\n * });\n * ```\n */\nexport function runWithTraceContext<T>(\n options: TraceContextOptions,\n fn: () => T | Promise<T>,\n): T | Promise<T> {\n return withTraceContext(options, fn);\n}\n\n/**\n * Add a completed span to the current trace context.\n * No-op if not within a trace context.\n */\nexport function addSpanToContext(span: Span): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.spans.push(span);\n }\n}\n\n/**\n * Set an attribute on the current trace context.\n * No-op if not within a trace context.\n */\nexport function setTraceAttribute(key: string, value: string): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.attributes[key] = value;\n }\n}\n\n/**\n * Set multiple attributes on the current trace context.\n * No-op if not within a trace context.\n */\nexport function setTraceAttributes(attributes: Record<string, string>): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n Object.assign(ctx.attributes, attributes);\n }\n}\n\n/**\n * Set HTTP response info on the current trace context.\n * Used by framework adapters after the response is sent.\n */\nexport function setTraceResponseInfo(\n statusCode: number,\n bodySize?: number,\n): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.statusCode = statusCode;\n ctx.bodySize = bodySize;\n }\n}\n\n/**\n * Get all spans from the current trace context.\n * Returns empty array if not within a trace context.\n */\nexport function getTraceSpans(): Span[] {\n return asyncLocalStorage.getStore()?.spans ?? [];\n}\n\n/**\n * Get the duration in milliseconds since the trace started.\n * Returns 0 if not within a trace context.\n */\nexport function getTraceDuration(): number {\n const ctx = asyncLocalStorage.getStore();\n if (!ctx) return 0;\n return Date.now() - ctx.startedAt.getTime();\n}\n\n/**\n * Fork the current trace context for a sub-operation.\n * Useful for parallel operations that should have isolated spans.\n * Returns undefined if not within a trace context.\n */\nexport function forkTraceContext<T>(fn: () => T): T | undefined {\n const parentCtx = asyncLocalStorage.getStore();\n if (!parentCtx) return undefined;\n\n const forkedContext: TraceContext = {\n ...parentCtx,\n spans: [], // Forked context gets its own spans\n attributes: { ...parentCtx.attributes },\n };\n\n return asyncLocalStorage.run(forkedContext, () => {\n const result = fn();\n // Merge spans back to parent after completion\n parentCtx.spans.push(...forkedContext.spans);\n return result;\n });\n}\n\n// Export the AsyncLocalStorage instance for advanced use cases\nexport { asyncLocalStorage as traceContextStorage };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,eAKO;;;ACLP,WAAsB;;;ACAf,IAAM,YAAN,MAAmB;AAAA,EAChB;AAAA,EACA,OAAe;AAAA,EACf;AAAA,EACA,OAAe;AAAA,EAEvB,YAAY,UAAkB;AAC5B,SAAK,YAAY;AACjB,SAAK,MAAM,IAAI,MAAgB,QAAQ,EAAE,KAAK,IAAI;AAAA,EACpD;AAAA,EAEA,IAAI,SAAiB;AACnB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,WAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,KAAK,KAAc;AACjB,SAAK,IAAI,KAAK,IAAI,IAAI;AACtB,SAAK,QAAQ,KAAK,OAAO,KAAK,KAAK;AACnC,QAAI,KAAK,OAAO,KAAK,WAAW;AAC9B,WAAK,QAAQ;AAAA,IACf;AAAA,EACF;AAAA,EAEA,UAAe;AACb,UAAM,SAAc,CAAC;AACrB,aAAS,IAAI,GAAG,IAAI,KAAK,MAAM,KAAK;AAClC,YAAM,OACH,KAAK,OAAO,KAAK,OAAO,IAAI,KAAK,aAAa,KAAK;AACtD,aAAO,KAAK,KAAK,IAAI,GAAG,CAAM;AAAA,IAChC;AACA,WAAO;AAAA,EACT;AAAA,EAEA,QAAc;AACZ,aAAS,IAAI,GAAG,IAAI,KAAK,IAAI,QAAQ,KAAK;AACxC,WAAK,IAAI,CAAC,IAAI;AAAA,IAChB;AACA,SAAK,OAAO;AACZ,SAAK,OAA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AChC,YAAI,aAAa,IAAI,GAAG;AACtB,sBAAY,SAAS,OAAO,YAAY,SAAS;AACjD,cAAI,eAAe,OAAO;AACxB,2CAA+B,KAAK,QAAW,OAAO;AAAA,UACxD;AAAA,QACF;AACA,cAAM;AAAA,MACR,CAAC;AAAA,IACL,OAAO;AACL,YAAM,aAAa,KAAK,IAAI,IAAI;AAChC,UAAI,aAAa,KAAK,GAAG;AACvB,oBAAY,SAAS,OAAO,YAAY,SAAS;AAAA,MACnD;AAAA,IACF;AAAA,EACF,SAAS,KAAK;AACZ,UAAM,aAAa,KAAK,IAAI,IAAI;AAChC,QAAI,aAAa,IAAI,GAAG;AACtB,kBAAY,SAAS,OAAO,YAAY,SAAS;AACjD,UAAI,eAAe,OAAO;AACxB,uCAA+B,KAAK,QAAW,OAAO;AAAA,MACxD;AAAA,IACF;AACA,UAAM;AAAA,EACR;AACF;AAEA,eAAsB,WAA0B;AAC9C,MAAI,CAAC,MAAO;AACZ,QAAM,IAAI;AACV,UAAQ;AACR,QAAM,EAAE,SAAS;AACnB;","names":["import_core","import_core","import_core","os","hostname"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/traceway.ts","../src/collection-frame-store.ts","../src/typed-ring.ts","../src/metrics.ts","../src/stack-trace.ts","../src/context.ts"],"sourcesContent":["export {\n init,\n captureException,\n captureExceptionWithAttributes,\n captureMessage,\n captureMetric,\n captureTrace,\n captureTask,\n captureCurrentTrace,\n startSpan,\n endSpan,\n shouldSample,\n measureTask,\n shutdown,\n} from \"./traceway.js\";\nexport type { SpanHandle } from \"./traceway.js\";\n\n// Context propagation (AsyncLocalStorage-based)\nexport {\n withTraceContext,\n runWithTraceContext,\n getTraceContext,\n getTraceId,\n hasTraceContext,\n addSpanToContext,\n setTraceAttribute,\n setTraceAttributes,\n setTraceResponseInfo,\n getTraceSpans,\n getTraceDuration,\n forkTraceContext,\n traceContextStorage,\n} from \"./context.js\";\nexport type { TraceContext, TraceContextOptions } from \"./context.js\";\n\nexport { formatErrorStackTrace } from \"./stack-trace.js\";\nexport { collectMetrics, resetCpuTracking } from \"./metrics.js\";\nexport { CollectionFrameStore } from \"./collection-frame-store.js\";\nexport type { CollectionFrameStoreOptions } from \"./collection-frame-store.js\";\n\nexport type {\n ExceptionStackTrace,\n MetricRecord,\n Span,\n Trace,\n CollectionFrame,\n ReportRequest,\n TracewayOptions,\n} from \"@tracewayapp/core\";\n","import {\n generateUUID,\n parseConnectionString,\n nowISO,\n msToNanoseconds,\n} from \"@tracewayapp/core\";\nimport type {\n TracewayOptions,\n ExceptionStackTrace,\n Trace,\n Span,\n} from \"@tracewayapp/core\";\nimport { CollectionFrameStore } from \"./collection-frame-store.js\";\nimport { formatErrorStackTrace } from \"./stack-trace.js\";\nimport {\n getTraceContext,\n getTraceId,\n addSpanToContext,\n type TraceContext,\n} from \"./context.js\";\nimport * as os from \"os\";\n\nlet store: CollectionFrameStore | null = null;\n\nfunction getHostname(): string {\n try {\n const hostname = os.hostname();\n const dotIdx = hostname.indexOf(\".\");\n return dotIdx >= 0 ? hostname.slice(0, dotIdx) : hostname;\n } catch {\n return \"unknown\";\n }\n}\n\nexport function init(\n connectionString: string,\n options: TracewayOptions = {},\n): void {\n if (store !== null) {\n throw new Error(\"Traceway: already initialized. Call shutdown() first.\");\n }\n\n const { token, apiUrl } = parseConnectionString(connectionString);\n\n store = new CollectionFrameStore({\n apiUrl,\n token,\n debug: options.debug ?? false,\n maxCollectionFrames: options.maxCollectionFrames ?? 12,\n collectionInterval: options.collectionInterval ?? 5000,\n uploadThrottle: options.uploadThrottle ?? 2000,\n metricsInterval: options.metricsInterval ?? 30000,\n version: options.version ?? \"\",\n serverName: options.serverName ?? getHostname(),\n sampleRate: options.sampleRate ?? 1,\n errorSampleRate: options.errorSampleRate ?? 1,\n });\n}\n\nexport function captureException(error: Error): void {\n if (!store) return;\n // Auto-detect trace context\n const ctx = getTraceContext();\n captureExceptionWithAttributes(\n error,\n ctx?.attributes,\n ctx?.traceId,\n );\n}\n\nexport function captureExceptionWithAttributes(\n error: Error,\n attributes?: Record<string, string>,\n traceId?: string,\n): void {\n if (!store) return;\n // Auto-detect trace context if not explicitly provided\n const ctx = getTraceContext();\n const resolvedTraceId = traceId ?? ctx?.traceId ?? null;\n const resolvedAttrs = attributes ?? ctx?.attributes;\n const isTask = ctx?.isTask ?? false;\n\n store.addException({\n traceId: resolvedTraceId,\n isTask: isTask || undefined,\n stackTrace: formatErrorStackTrace(error),\n recordedAt: nowISO(),\n attributes: resolvedAttrs,\n isMessage: false,\n });\n}\n\nexport function captureMessage(\n msg: string,\n attributes?: Record<string, string>,\n): void {\n if (!store) return;\n // Auto-detect trace context\n const ctx = getTraceContext();\n const resolvedTraceId = ctx?.traceId ?? null;\n const resolvedAttrs = attributes ?? ctx?.attributes;\n const isTask = ctx?.isTask ?? false;\n\n store.addException({\n traceId: resolvedTraceId,\n isTask: isTask || undefined,\n stackTrace: msg,\n recordedAt: nowISO(),\n attributes: resolvedAttrs,\n isMessage: true,\n });\n}\n\nexport function captureMetric(name: string, value: number): void {\n if (!store) return;\n store.addMetric({\n name,\n value,\n recordedAt: nowISO(),\n });\n}\n\nexport function captureTrace(\n traceId: string,\n endpoint: string,\n durationMs: number,\n startedAt: Date,\n statusCode: number,\n bodySize: number,\n clientIP: string,\n attributes?: Record<string, string>,\n spans?: Span[],\n): void {\n if (!store) return;\n store.addTrace({\n id: traceId,\n endpoint,\n duration: msToNanoseconds(durationMs),\n recordedAt: startedAt.toISOString(),\n statusCode,\n bodySize,\n clientIP,\n attributes,\n spans,\n });\n}\n\nexport function captureTask(\n traceId: string,\n taskName: string,\n durationMs: number,\n startedAt: Date,\n attributes?: Record<string, string>,\n spans?: Span[],\n): void {\n if (!store) return;\n store.addTrace({\n id: traceId,\n endpoint: taskName,\n duration: msToNanoseconds(durationMs),\n recordedAt: startedAt.toISOString(),\n statusCode: 0,\n bodySize: 0,\n clientIP: \"\",\n attributes,\n spans,\n isTask: true,\n });\n}\n\n/** Handle returned by startSpan for tracking span timing */\nexport interface SpanHandle {\n id: string;\n name: string;\n startTime: string;\n startedAt: number;\n}\n\n/**\n * Start a new span. If within a trace context, the span will be\n * automatically added to the trace when ended.\n */\nexport function startSpan(name: string): SpanHandle {\n const now = Date.now();\n return {\n id: generateUUID(),\n name,\n startTime: new Date(now).toISOString(),\n startedAt: now,\n };\n}\n\n/**\n * End a span and get the completed Span object.\n * If within a trace context, automatically adds the span to the trace.\n *\n * @param addToContext - If true (default), adds span to current trace context\n */\nexport function endSpan(span: SpanHandle, addToContext: boolean = true): Span {\n const durationMs = Date.now() - span.startedAt;\n const completedSpan: Span = {\n id: span.id,\n name: span.name,\n startTime: span.startTime,\n duration: msToNanoseconds(durationMs),\n };\n\n // Auto-add to trace context if available\n if (addToContext) {\n addSpanToContext(completedSpan);\n }\n\n return completedSpan;\n}\n\n/**\n * Capture the current trace context as a trace.\n * Call this at the end of a request/task to record the trace.\n *\n * @example\n * ```ts\n * // In Express middleware (after response)\n * withTraceContext({ endpoint: `${req.method} ${req.path}` }, async () => {\n * await handleRequest(req, res);\n * setTraceResponseInfo(res.statusCode, contentLength);\n * captureCurrentTrace(); // Records the trace with all spans\n * });\n * ```\n */\nexport function captureCurrentTrace(): void {\n if (!store) return;\n\n const ctx = getTraceContext();\n if (!ctx) return;\n\n const durationMs = Date.now() - ctx.startedAt.getTime();\n const isError = (ctx.statusCode ?? 0) >= 500;\n\n if (!shouldSample(isError)) return;\n\n if (ctx.isTask) {\n store.addTrace({\n id: ctx.traceId,\n endpoint: ctx.endpoint ?? \"unknown-task\",\n duration: msToNanoseconds(durationMs),\n recordedAt: ctx.startedAt.toISOString(),\n statusCode: 0,\n bodySize: 0,\n clientIP: \"\",\n attributes: Object.keys(ctx.attributes).length > 0 ? ctx.attributes : undefined,\n spans: ctx.spans.length > 0 ? ctx.spans : undefined,\n isTask: true,\n });\n } else {\n store.addTrace({\n id: ctx.traceId,\n endpoint: ctx.endpoint ?? \"unknown\",\n duration: msToNanoseconds(durationMs),\n recordedAt: ctx.startedAt.toISOString(),\n statusCode: ctx.statusCode ?? 0,\n bodySize: ctx.bodySize ?? 0,\n clientIP: ctx.clientIP ?? \"\",\n attributes: Object.keys(ctx.attributes).length > 0 ? ctx.attributes : undefined,\n spans: ctx.spans.length > 0 ? ctx.spans : undefined,\n });\n }\n}\n\nexport function shouldSample(isError: boolean): boolean {\n if (!store) return false;\n const rate = isError ? store.errorSampleRate : store.sampleRate;\n if (rate >= 1) return true;\n if (rate <= 0) return false;\n return Math.random() < rate;\n}\n\nexport function measureTask(\n title: string,\n fn: () => void | Promise<void>,\n): void {\n const traceId = generateUUID();\n const start = Date.now();\n const startDate = new Date(start);\n\n try {\n const result = fn();\n if (result && typeof (result as Promise<void>).then === \"function\") {\n (result as Promise<void>)\n .then(() => {\n const durationMs = Date.now() - start;\n if (shouldSample(false)) {\n captureTask(traceId, title, durationMs, startDate);\n }\n })\n .catch((err: unknown) => {\n const durationMs = Date.now() - start;\n if (shouldSample(true)) {\n captureTask(traceId, title, durationMs, startDate);\n if (err instanceof Error) {\n captureExceptionWithAttributes(err, undefined, traceId);\n }\n }\n throw err;\n });\n } else {\n const durationMs = Date.now() - start;\n if (shouldSample(false)) {\n captureTask(traceId, title, durationMs, startDate);\n }\n }\n } catch (err) {\n const durationMs = Date.now() - start;\n if (shouldSample(true)) {\n captureTask(traceId, title, durationMs, startDate);\n if (err instanceof Error) {\n captureExceptionWithAttributes(err, undefined, traceId);\n }\n }\n throw err;\n }\n}\n\nexport async function shutdown(timeoutMs?: number): Promise<void> {\n if (!store) return;\n const s = store;\n store = null;\n await s.shutdown(timeoutMs);\n}\n","import * as zlib from \"zlib\";\nimport {\n nowISO,\n generateUUID,\n msToNanoseconds,\n} from \"@tracewayapp/core\";\nimport { TypedRing } from \"./typed-ring.js\";\nimport type {\n CollectionFrame,\n ExceptionStackTrace,\n MetricRecord,\n Trace,\n Span,\n ReportRequest,\n} from \"@tracewayapp/core\";\nimport { collectMetrics } from \"./metrics.js\";\n\nexport interface CollectionFrameStoreOptions {\n apiUrl: string;\n token: string;\n debug: boolean;\n maxCollectionFrames: number;\n collectionInterval: number;\n uploadThrottle: number;\n metricsInterval: number;\n version: string;\n serverName: string;\n sampleRate: number;\n errorSampleRate: number;\n}\n\nexport class CollectionFrameStore {\n private current: CollectionFrame | null = null;\n private currentSetAt: number = Date.now();\n private sendQueue: TypedRing<CollectionFrame>;\n private lastUploadStarted: number | null = null;\n\n private collectionTimer: ReturnType<typeof setInterval> | null = null;\n private metricsTimer: ReturnType<typeof setInterval> | null = null;\n\n private readonly apiUrl: string;\n private readonly token: string;\n private readonly debug: boolean;\n private readonly collectionInterval: number;\n private readonly uploadThrottle: number;\n private readonly metricsInterval: number;\n private readonly version: string;\n private readonly serverName: string;\n\n readonly sampleRate: number;\n readonly errorSampleRate: number;\n\n constructor(options: CollectionFrameStoreOptions) {\n this.apiUrl = options.apiUrl;\n this.token = options.token;\n this.debug = options.debug;\n this.collectionInterval = options.collectionInterval;\n this.uploadThrottle = options.uploadThrottle;\n this.metricsInterval = options.metricsInterval;\n this.version = options.version;\n this.serverName = options.serverName;\n this.sampleRate = options.sampleRate;\n this.errorSampleRate = options.errorSampleRate;\n this.sendQueue = new TypedRing<CollectionFrame>(\n options.maxCollectionFrames,\n );\n\n this.collectionTimer = setInterval(() => {\n this.tick();\n }, this.collectionInterval);\n if (this.collectionTimer && typeof this.collectionTimer.unref === \"function\") {\n this.collectionTimer.unref();\n }\n\n this.metricsTimer = setInterval(() => {\n this.collectSystemMetrics();\n }, this.metricsInterval);\n if (this.metricsTimer && typeof this.metricsTimer.unref === \"function\") {\n this.metricsTimer.unref();\n }\n }\n\n private tick(): void {\n if (this.current !== null) {\n if (this.currentSetAt < Date.now() - this.collectionInterval) {\n this.rotateCurrentCollectionFrame();\n this.processSendQueue();\n }\n } else if (this.sendQueue.length > 0) {\n this.processSendQueue();\n }\n }\n\n private ensureCurrent(): CollectionFrame {\n if (this.current === null) {\n this.current = { stackTraces: [], metrics: [], traces: [] };\n this.currentSetAt = Date.now();\n }\n return this.current;\n }\n\n addException(exception: ExceptionStackTrace): void {\n this.ensureCurrent().stackTraces.push(exception);\n }\n\n addMetric(metric: MetricRecord): void {\n this.ensureCurrent().metrics.push(metric);\n }\n\n addTrace(trace: Trace): void {\n this.ensureCurrent().traces.push(trace);\n }\n\n private rotateCurrentCollectionFrame(): void {\n if (this.current !== null) {\n this.sendQueue.push(this.current);\n this.current = null;\n }\n }\n\n private processSendQueue(): void {\n if (\n this.lastUploadStarted === null ||\n this.lastUploadStarted < Date.now() - this.uploadThrottle\n ) {\n this.lastUploadStarted = Date.now();\n const frames = this.sendQueue.readAll();\n if (frames.length > 0) {\n this.triggerUpload(frames);\n }\n }\n }\n\n private triggerUpload(framesToSend: CollectionFrame[]): void {\n const payload: ReportRequest = {\n collectionFrames: framesToSend,\n appVersion: this.version,\n serverName: this.serverName,\n };\n\n let jsonData: string;\n try {\n jsonData = JSON.stringify(payload);\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to serialize frames:\", err);\n }\n return;\n }\n\n let gzipped: Uint8Array;\n try {\n gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to gzip:\", err);\n }\n return;\n }\n\n fetch(this.apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${this.token}`,\n },\n body: gzipped as unknown as BodyInit,\n })\n .then((resp) => {\n if (resp.status === 200) {\n this.sendQueue.remove(framesToSend);\n } else if (this.debug) {\n console.error(`Traceway: upload returned status ${resp.status}`);\n }\n })\n .catch((err) => {\n if (this.debug) {\n console.error(\"Traceway: upload failed:\", err);\n }\n });\n }\n\n private collectSystemMetrics(): void {\n try {\n const metrics = collectMetrics();\n for (const metric of metrics) {\n this.addMetric(metric);\n }\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to collect metrics:\", err);\n }\n }\n }\n\n async shutdown(timeoutMs?: number): Promise<void> {\n if (this.collectionTimer !== null) {\n clearInterval(this.collectionTimer);\n this.collectionTimer = null;\n }\n if (this.metricsTimer !== null) {\n clearInterval(this.metricsTimer);\n this.metricsTimer = null;\n }\n\n this.rotateCurrentCollectionFrame();\n\n const frames = this.sendQueue.readAll();\n if (frames.length === 0) return;\n\n const uploadPromise = (async () => {\n const payload: ReportRequest = {\n collectionFrames: frames,\n appVersion: this.version,\n serverName: this.serverName,\n };\n\n try {\n const jsonData = JSON.stringify(payload);\n const gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));\n\n const resp = await fetch(this.apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${this.token}`,\n },\n body: gzipped as unknown as BodyInit,\n });\n\n if (resp.status === 200) {\n this.sendQueue.remove(frames);\n }\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: shutdown upload failed:\", err);\n }\n }\n })();\n\n if (timeoutMs !== undefined) {\n await Promise.race([\n uploadPromise,\n new Promise<void>((resolve) => setTimeout(resolve, timeoutMs)),\n ]);\n } else {\n await uploadPromise;\n }\n }\n}\n","export class TypedRing<T> {\n private arr: (T | null)[];\n private head: number = 0;\n private _capacity: number;\n private _len: number = 0;\n\n constructor(capacity: number) {\n this._capacity = capacity;\n this.arr = new Array<T | null>(capacity).fill(null);\n }\n\n get length(): number {\n return this._len;\n }\n\n get capacity(): number {\n return this._capacity;\n }\n\n push(val: T): void {\n this.arr[this.head] = val;\n this.head = (this.head + 1) % this._capacity;\n if (this._len < this._capacity) {\n this._len += 1;\n }\n }\n\n readAll(): T[] {\n const result: T[] = [];\n for (let i = 0; i < this._len; i++) {\n const idx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n result.push(this.arr[idx] as T);\n }\n return result;\n }\n\n clear(): void {\n for (let i = 0; i < this.arr.length; i++) {\n this.arr[i] = null;\n }\n this.head = 0;\n this._len = 0;\n }\n\n remove(vals: T[]): number {\n if (vals.length === 0) return 0;\n\n const toRemove = new Set<T>(vals);\n let writeIdx = 0;\n let removed = 0;\n\n for (let i = 0; i < this._len; i++) {\n const readIdx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n if (toRemove.has(this.arr[readIdx] as T)) {\n removed++;\n } else {\n if (writeIdx !== i) {\n const destIdx =\n (this.head - this._len + writeIdx + this._capacity) %\n this._capacity;\n this.arr[destIdx] = this.arr[readIdx];\n }\n writeIdx++;\n }\n }\n\n for (let i = writeIdx; i < this._len; i++) {\n const idx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n this.arr[idx] = null;\n }\n\n this._len = writeIdx;\n this.head = (this.head - removed + this._capacity) % this._capacity;\n\n return removed;\n }\n}\n","import * as os from \"os\";\nimport { METRIC_MEM_USED, METRIC_MEM_TOTAL, METRIC_CPU_USED_PCNT } from \"@tracewayapp/core\";\nimport type { MetricRecord } from \"@tracewayapp/core\";\nimport { nowISO } from \"@tracewayapp/core\";\n\nlet prevCpuUsage: NodeJS.CpuUsage | null = null;\nlet prevCpuTime: number | null = null;\n\nexport function collectMetrics(): MetricRecord[] {\n const metrics: MetricRecord[] = [];\n const recordedAt = nowISO();\n\n const mem = process.memoryUsage();\n metrics.push({\n name: METRIC_MEM_USED,\n value: Math.round((mem.rss / 1024 / 1024) * 100) / 100,\n recordedAt,\n });\n\n const totalMem = os.totalmem();\n metrics.push({\n name: METRIC_MEM_TOTAL,\n value: Math.round((totalMem / 1024 / 1024) * 100) / 100,\n recordedAt,\n });\n\n const currentCpuUsage = process.cpuUsage();\n const currentTime = Date.now();\n if (prevCpuUsage !== null && prevCpuTime !== null) {\n const elapsedMs = currentTime - prevCpuTime;\n if (elapsedMs > 0) {\n const userDelta = currentCpuUsage.user - prevCpuUsage.user;\n const systemDelta = currentCpuUsage.system - prevCpuUsage.system;\n const totalDeltaMs = (userDelta + systemDelta) / 1000;\n const cpuCount = os.cpus().length || 1;\n const cpuPercent = (totalDeltaMs / elapsedMs / cpuCount) * 100;\n metrics.push({\n name: METRIC_CPU_USED_PCNT,\n value: Math.round(cpuPercent * 100) / 100,\n recordedAt,\n });\n }\n }\n prevCpuUsage = currentCpuUsage;\n prevCpuTime = currentTime;\n\n return metrics;\n}\n\nexport function resetCpuTracking(): void {\n prevCpuUsage = null;\n prevCpuTime = null;\n}\n","export function formatErrorStackTrace(error: Error): string {\n const lines: string[] = [];\n\n const typeName = error.constructor.name || \"Error\";\n lines.push(`${typeName}: ${error.message}`);\n\n if (error.stack) {\n const stackLines = error.stack.split(\"\\n\");\n for (const line of stackLines) {\n const match = line.match(/^\\s+at\\s+(.+?)\\s+\\((.+):(\\d+):\\d+\\)$/);\n if (match) {\n const funcName = shortenFunctionName(match[1]);\n const file = shortenFilePath(match[2]);\n const lineNum = match[3];\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${lineNum}`);\n continue;\n }\n\n const matchNoParens = line.match(/^\\s+at\\s+(.+):(\\d+):\\d+$/);\n if (matchNoParens) {\n const file = shortenFilePath(matchNoParens[1]);\n const lineNum = matchNoParens[2];\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${lineNum}`);\n continue;\n }\n\n const matchFnOnly = line.match(/^\\s+at\\s+(.+)$/);\n if (matchFnOnly) {\n const funcName = shortenFunctionName(matchFnOnly[1]);\n lines.push(`${funcName}()`);\n lines.push(` <unknown>:0`);\n }\n }\n }\n\n return lines.join(\"\\n\") + \"\\n\";\n}\n\nfunction shortenFunctionName(fn: string): string {\n const slashIdx = fn.lastIndexOf(\"/\");\n if (slashIdx >= 0) {\n fn = fn.slice(slashIdx + 1);\n }\n const dotIdx = fn.indexOf(\".\");\n if (dotIdx >= 0) {\n fn = fn.slice(dotIdx + 1);\n }\n return fn;\n}\n\nfunction shortenFilePath(filePath: string): string {\n const parts = filePath.split(\"/\");\n return parts[parts.length - 1];\n}\n","import { AsyncLocalStorage } from \"async_hooks\";\nimport { generateUUID } from \"@tracewayapp/core\";\nimport type { Span } from \"@tracewayapp/core\";\n\n/**\n * Trace context stored in AsyncLocalStorage.\n * Automatically propagates through async operations.\n */\nexport interface TraceContext {\n /** Unique trace identifier (UUID v4) */\n traceId: string;\n /** Whether this is a background task (vs HTTP request) */\n isTask: boolean;\n /** When the trace started */\n startedAt: Date;\n /** Collected spans within this trace */\n spans: Span[];\n /** Key-value attributes for this trace */\n attributes: Record<string, string>;\n /** For HTTP traces: endpoint like \"GET /api/users\" */\n endpoint?: string;\n /** For HTTP traces: response status code */\n statusCode?: number;\n /** For HTTP traces: response body size */\n bodySize?: number;\n /** For HTTP traces: client IP address */\n clientIP?: string;\n}\n\n/**\n * Options for creating a new trace context.\n */\nexport interface TraceContextOptions {\n /** Custom trace ID (auto-generated if not provided) */\n traceId?: string;\n /** Mark as background task instead of HTTP trace */\n isTask?: boolean;\n /** Initial attributes */\n attributes?: Record<string, string>;\n /** HTTP endpoint (e.g., \"GET /api/users\") */\n endpoint?: string;\n /** Client IP address */\n clientIP?: string;\n}\n\n// The AsyncLocalStorage instance for trace context\nconst asyncLocalStorage = new AsyncLocalStorage<TraceContext>();\n\n/**\n * Get the current trace context, if any.\n * Returns undefined if not within a trace context.\n */\nexport function getTraceContext(): TraceContext | undefined {\n return asyncLocalStorage.getStore();\n}\n\n/**\n * Get the current trace ID, if within a trace context.\n */\nexport function getTraceId(): string | undefined {\n return asyncLocalStorage.getStore()?.traceId;\n}\n\n/**\n * Check if currently within a trace context.\n */\nexport function hasTraceContext(): boolean {\n return asyncLocalStorage.getStore() !== undefined;\n}\n\n/**\n * Run a function within a new trace context.\n * All async operations within will have access to this context.\n *\n * @example\n * ```ts\n * // HTTP request handler\n * withTraceContext({ endpoint: \"GET /api/users\", clientIP: req.ip }, async () => {\n * const users = await db.query(\"SELECT * FROM users\");\n * captureException(new Error(\"oops\")); // Auto-linked to trace\n * return users;\n * });\n *\n * // Background task\n * withTraceContext({ isTask: true, endpoint: \"process-emails\" }, async () => {\n * await processEmails();\n * });\n * ```\n */\nexport function withTraceContext<T>(\n options: TraceContextOptions,\n fn: () => T,\n): T {\n const context: TraceContext = {\n traceId: options.traceId ?? generateUUID(),\n isTask: options.isTask ?? false,\n startedAt: new Date(),\n spans: [],\n attributes: options.attributes ?? {},\n endpoint: options.endpoint,\n clientIP: options.clientIP,\n };\n return asyncLocalStorage.run(context, fn);\n}\n\n/**\n * Run a function within a trace context, automatically capturing the trace on completion.\n * This is a convenience wrapper that handles timing and capture automatically.\n *\n * @example\n * ```ts\n * // In Express middleware\n * app.use((req, res, next) => {\n * runWithTraceContext(\n * {\n * endpoint: `${req.method} ${req.path}`,\n * clientIP: req.ip,\n * attributes: { userId: req.user?.id },\n * },\n * async () => {\n * await next();\n * // Status code and body size set via setTraceResponseInfo()\n * },\n * { captureOnEnd: true }\n * );\n * });\n * ```\n */\nexport function runWithTraceContext<T>(\n options: TraceContextOptions,\n fn: () => T | Promise<T>,\n): T | Promise<T> {\n return withTraceContext(options, fn);\n}\n\n/**\n * Add a completed span to the current trace context.\n * No-op if not within a trace context.\n */\nexport function addSpanToContext(span: Span): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.spans.push(span);\n }\n}\n\n/**\n * Set an attribute on the current trace context.\n * No-op if not within a trace context.\n */\nexport function setTraceAttribute(key: string, value: string): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.attributes[key] = value;\n }\n}\n\n/**\n * Set multiple attributes on the current trace context.\n * No-op if not within a trace context.\n */\nexport function setTraceAttributes(attributes: Record<string, string>): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n Object.assign(ctx.attributes, attributes);\n }\n}\n\n/**\n * Set HTTP response info on the current trace context.\n * Used by framework adapters after the response is sent.\n */\nexport function setTraceResponseInfo(\n statusCode: number,\n bodySize?: number,\n): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.statusCode = statusCode;\n ctx.bodySize = bodySize;\n }\n}\n\n/**\n * Get all spans from the current trace context.\n * Returns empty array if not within a trace context.\n */\nexport function getTraceSpans(): Span[] {\n return asyncLocalStorage.getStore()?.spans ?? [];\n}\n\n/**\n * Get the duration in milliseconds since the trace started.\n * Returns 0 if not within a trace context.\n */\nexport function getTraceDuration(): number {\n const ctx = asyncLocalStorage.getStore();\n if (!ctx) return 0;\n return Date.now() - ctx.startedAt.getTime();\n}\n\n/**\n * Fork the current trace context for a sub-operation.\n * Useful for parallel operations that should have isolated spans.\n * Returns undefined if not within a trace context.\n */\nexport function forkTraceContext<T>(fn: () => T): T | undefined {\n const parentCtx = asyncLocalStorage.getStore();\n if (!parentCtx) return undefined;\n\n const forkedContext: TraceContext = {\n ...parentCtx,\n spans: [], // Forked context gets its own spans\n attributes: { ...parentCtx.attributes },\n };\n\n return asyncLocalStorage.run(forkedContext, () => {\n const result = fn();\n // Merge spans back to parent after completion\n parentCtx.spans.push(...forkedContext.spans);\n return result;\n });\n}\n\n// Export the AsyncLocalStorage instance for advanced use cases\nexport { asyncLocalStorage as traceContextStorage };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,eAKO;;;ACLP,WAAsB;;;ACAf,IAAM,YAAN,MAAmB;AAAA,EAChB;AAAA,EACA,OAAe;AAAA,EACf;AAAA,EACA,OAAe;AAAA,EAEvB,YAAY,UAAkB;AAC5B,SAAK,YAAY;AACjB,SAAK,MAAM,IAAI,MAAgB,QAAQ,EAAE,KAAK,IAAI;AAAA,EACpD;AAAA,EAEA,IAAI,SAAiB;AACnB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,WAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,E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,eAAe,OAAO;AACxB,uCAA+B,KAAK,QAAW,OAAO;AAAA,MACxD;AAAA,IACF;AACA,UAAM;AAAA,EACR;AACF;AAEA,eAAsB,SAAS,WAAmC;AAChE,MAAI,CAAC,MAAO;AACZ,QAAM,IAAI;AACV,UAAQ;AACR,QAAM,EAAE,SAAS,SAAS;AAC5B;","names":["import_core","import_core","import_core","os","hostname"]}
package/dist/index.mjs CHANGED
@@ -263,7 +263,7 @@ var CollectionFrameStore = class {
263
263
  }
264
264
  }
265
265
  }
266
- async shutdown() {
266
+ async shutdown(timeoutMs) {
267
267
  if (this.collectionTimer !== null) {
268
268
  clearInterval(this.collectionTimer);
269
269
  this.collectionTimer = null;
@@ -275,30 +275,40 @@ var CollectionFrameStore = class {
275
275
  this.rotateCurrentCollectionFrame();
276
276
  const frames = this.sendQueue.readAll();
277
277
  if (frames.length === 0) return;
278
- const payload = {
279
- collectionFrames: frames,
280
- appVersion: this.version,
281
- serverName: this.serverName
282
- };
283
- try {
284
- const jsonData = JSON.stringify(payload);
285
- const gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));
286
- const resp = await fetch(this.apiUrl, {
287
- method: "POST",
288
- headers: {
289
- "Content-Type": "application/json",
290
- "Content-Encoding": "gzip",
291
- Authorization: `Bearer ${this.token}`
292
- },
293
- body: gzipped
294
- });
295
- if (resp.status === 200) {
296
- this.sendQueue.remove(frames);
297
- }
298
- } catch (err) {
299
- if (this.debug) {
300
- console.error("Traceway: shutdown upload failed:", err);
278
+ const uploadPromise = (async () => {
279
+ const payload = {
280
+ collectionFrames: frames,
281
+ appVersion: this.version,
282
+ serverName: this.serverName
283
+ };
284
+ try {
285
+ const jsonData = JSON.stringify(payload);
286
+ const gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));
287
+ const resp = await fetch(this.apiUrl, {
288
+ method: "POST",
289
+ headers: {
290
+ "Content-Type": "application/json",
291
+ "Content-Encoding": "gzip",
292
+ Authorization: `Bearer ${this.token}`
293
+ },
294
+ body: gzipped
295
+ });
296
+ if (resp.status === 200) {
297
+ this.sendQueue.remove(frames);
298
+ }
299
+ } catch (err) {
300
+ if (this.debug) {
301
+ console.error("Traceway: shutdown upload failed:", err);
302
+ }
301
303
  }
304
+ })();
305
+ if (timeoutMs !== void 0) {
306
+ await Promise.race([
307
+ uploadPromise,
308
+ new Promise((resolve) => setTimeout(resolve, timeoutMs))
309
+ ]);
310
+ } else {
311
+ await uploadPromise;
302
312
  }
303
313
  }
304
314
  };
@@ -640,11 +650,11 @@ function measureTask(title, fn) {
640
650
  throw err;
641
651
  }
642
652
  }
643
- async function shutdown() {
653
+ async function shutdown(timeoutMs) {
644
654
  if (!store) return;
645
655
  const s = store;
646
656
  store = null;
647
- await s.shutdown();
657
+ await s.shutdown(timeoutMs);
648
658
  }
649
659
  export {
650
660
  CollectionFrameStore,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/traceway.ts","../src/collection-frame-store.ts","../src/typed-ring.ts","../src/metrics.ts","../src/stack-trace.ts","../src/context.ts"],"sourcesContent":["import {\n generateUUID,\n parseConnectionString,\n nowISO,\n msToNanoseconds,\n} from \"@tracewayapp/core\";\nimport type {\n TracewayOptions,\n ExceptionStackTrace,\n Trace,\n Span,\n} from \"@tracewayapp/core\";\nimport { CollectionFrameStore } from \"./collection-frame-store.js\";\nimport { formatErrorStackTrace } from \"./stack-trace.js\";\nimport {\n getTraceContext,\n getTraceId,\n addSpanToContext,\n type TraceContext,\n} from \"./context.js\";\nimport * as os from \"os\";\n\nlet store: CollectionFrameStore | null = null;\n\nfunction getHostname(): string {\n try {\n const hostname = os.hostname();\n const dotIdx = hostname.indexOf(\".\");\n return dotIdx >= 0 ? hostname.slice(0, dotIdx) : hostname;\n } catch {\n return \"unknown\";\n }\n}\n\nexport function init(\n connectionString: string,\n options: TracewayOptions = {},\n): void {\n if (store !== null) {\n throw new Error(\"Traceway: already initialized. Call shutdown() first.\");\n }\n\n const { token, apiUrl } = parseConnectionString(connectionString);\n\n store = new CollectionFrameStore({\n apiUrl,\n token,\n debug: options.debug ?? false,\n maxCollectionFrames: options.maxCollectionFrames ?? 12,\n collectionInterval: options.collectionInterval ?? 5000,\n uploadThrottle: options.uploadThrottle ?? 2000,\n metricsInterval: options.metricsInterval ?? 30000,\n version: options.version ?? \"\",\n serverName: options.serverName ?? getHostname(),\n sampleRate: options.sampleRate ?? 1,\n errorSampleRate: options.errorSampleRate ?? 1,\n });\n}\n\nexport function captureException(error: Error): void {\n if (!store) return;\n // Auto-detect trace context\n const ctx = getTraceContext();\n captureExceptionWithAttributes(\n error,\n ctx?.attributes,\n ctx?.traceId,\n );\n}\n\nexport function captureExceptionWithAttributes(\n error: Error,\n attributes?: Record<string, string>,\n traceId?: string,\n): void {\n if (!store) return;\n // Auto-detect trace context if not explicitly provided\n const ctx = getTraceContext();\n const resolvedTraceId = traceId ?? ctx?.traceId ?? null;\n const resolvedAttrs = attributes ?? ctx?.attributes;\n const isTask = ctx?.isTask ?? false;\n\n store.addException({\n traceId: resolvedTraceId,\n isTask: isTask || undefined,\n stackTrace: formatErrorStackTrace(error),\n recordedAt: nowISO(),\n attributes: resolvedAttrs,\n isMessage: false,\n });\n}\n\nexport function captureMessage(\n msg: string,\n attributes?: Record<string, string>,\n): void {\n if (!store) return;\n // Auto-detect trace context\n const ctx = getTraceContext();\n const resolvedTraceId = ctx?.traceId ?? null;\n const resolvedAttrs = attributes ?? ctx?.attributes;\n const isTask = ctx?.isTask ?? false;\n\n store.addException({\n traceId: resolvedTraceId,\n isTask: isTask || undefined,\n stackTrace: msg,\n recordedAt: nowISO(),\n attributes: resolvedAttrs,\n isMessage: true,\n });\n}\n\nexport function captureMetric(name: string, value: number): void {\n if (!store) return;\n store.addMetric({\n name,\n value,\n recordedAt: nowISO(),\n });\n}\n\nexport function captureTrace(\n traceId: string,\n endpoint: string,\n durationMs: number,\n startedAt: Date,\n statusCode: number,\n bodySize: number,\n clientIP: string,\n attributes?: Record<string, string>,\n spans?: Span[],\n): void {\n if (!store) return;\n store.addTrace({\n id: traceId,\n endpoint,\n duration: msToNanoseconds(durationMs),\n recordedAt: startedAt.toISOString(),\n statusCode,\n bodySize,\n clientIP,\n attributes,\n spans,\n });\n}\n\nexport function captureTask(\n traceId: string,\n taskName: string,\n durationMs: number,\n startedAt: Date,\n attributes?: Record<string, string>,\n spans?: Span[],\n): void {\n if (!store) return;\n store.addTrace({\n id: traceId,\n endpoint: taskName,\n duration: msToNanoseconds(durationMs),\n recordedAt: startedAt.toISOString(),\n statusCode: 0,\n bodySize: 0,\n clientIP: \"\",\n attributes,\n spans,\n isTask: true,\n });\n}\n\n/** Handle returned by startSpan for tracking span timing */\nexport interface SpanHandle {\n id: string;\n name: string;\n startTime: string;\n startedAt: number;\n}\n\n/**\n * Start a new span. If within a trace context, the span will be\n * automatically added to the trace when ended.\n */\nexport function startSpan(name: string): SpanHandle {\n const now = Date.now();\n return {\n id: generateUUID(),\n name,\n startTime: new Date(now).toISOString(),\n startedAt: now,\n };\n}\n\n/**\n * End a span and get the completed Span object.\n * If within a trace context, automatically adds the span to the trace.\n *\n * @param addToContext - If true (default), adds span to current trace context\n */\nexport function endSpan(span: SpanHandle, addToContext: boolean = true): Span {\n const durationMs = Date.now() - span.startedAt;\n const completedSpan: Span = {\n id: span.id,\n name: span.name,\n startTime: span.startTime,\n duration: msToNanoseconds(durationMs),\n };\n\n // Auto-add to trace context if available\n if (addToContext) {\n addSpanToContext(completedSpan);\n }\n\n return completedSpan;\n}\n\n/**\n * Capture the current trace context as a trace.\n * Call this at the end of a request/task to record the trace.\n *\n * @example\n * ```ts\n * // In Express middleware (after response)\n * withTraceContext({ endpoint: `${req.method} ${req.path}` }, async () => {\n * await handleRequest(req, res);\n * setTraceResponseInfo(res.statusCode, contentLength);\n * captureCurrentTrace(); // Records the trace with all spans\n * });\n * ```\n */\nexport function captureCurrentTrace(): void {\n if (!store) return;\n\n const ctx = getTraceContext();\n if (!ctx) return;\n\n const durationMs = Date.now() - ctx.startedAt.getTime();\n const isError = (ctx.statusCode ?? 0) >= 500;\n\n if (!shouldSample(isError)) return;\n\n if (ctx.isTask) {\n store.addTrace({\n id: ctx.traceId,\n endpoint: ctx.endpoint ?? \"unknown-task\",\n duration: msToNanoseconds(durationMs),\n recordedAt: ctx.startedAt.toISOString(),\n statusCode: 0,\n bodySize: 0,\n clientIP: \"\",\n attributes: Object.keys(ctx.attributes).length > 0 ? ctx.attributes : undefined,\n spans: ctx.spans.length > 0 ? ctx.spans : undefined,\n isTask: true,\n });\n } else {\n store.addTrace({\n id: ctx.traceId,\n endpoint: ctx.endpoint ?? \"unknown\",\n duration: msToNanoseconds(durationMs),\n recordedAt: ctx.startedAt.toISOString(),\n statusCode: ctx.statusCode ?? 0,\n bodySize: ctx.bodySize ?? 0,\n clientIP: ctx.clientIP ?? \"\",\n attributes: Object.keys(ctx.attributes).length > 0 ? ctx.attributes : undefined,\n spans: ctx.spans.length > 0 ? ctx.spans : undefined,\n });\n }\n}\n\nexport function shouldSample(isError: boolean): boolean {\n if (!store) return false;\n const rate = isError ? store.errorSampleRate : store.sampleRate;\n if (rate >= 1) return true;\n if (rate <= 0) return false;\n return Math.random() < rate;\n}\n\nexport function measureTask(\n title: string,\n fn: () => void | Promise<void>,\n): void {\n const traceId = generateUUID();\n const start = Date.now();\n const startDate = new Date(start);\n\n try {\n const result = fn();\n if (result && typeof (result as Promise<void>).then === \"function\") {\n (result as Promise<void>)\n .then(() => {\n const durationMs = Date.now() - start;\n if (shouldSample(false)) {\n captureTask(traceId, title, durationMs, startDate);\n }\n })\n .catch((err: unknown) => {\n const durationMs = Date.now() - start;\n if (shouldSample(true)) {\n captureTask(traceId, title, durationMs, startDate);\n if (err instanceof Error) {\n captureExceptionWithAttributes(err, undefined, traceId);\n }\n }\n throw err;\n });\n } else {\n const durationMs = Date.now() - start;\n if (shouldSample(false)) {\n captureTask(traceId, title, durationMs, startDate);\n }\n }\n } catch (err) {\n const durationMs = Date.now() - start;\n if (shouldSample(true)) {\n captureTask(traceId, title, durationMs, startDate);\n if (err instanceof Error) {\n captureExceptionWithAttributes(err, undefined, traceId);\n }\n }\n throw err;\n }\n}\n\nexport async function shutdown(): Promise<void> {\n if (!store) return;\n const s = store;\n store = null;\n await s.shutdown();\n}\n","import * as zlib from \"zlib\";\nimport {\n nowISO,\n generateUUID,\n msToNanoseconds,\n} from \"@tracewayapp/core\";\nimport { TypedRing } from \"./typed-ring.js\";\nimport type {\n CollectionFrame,\n ExceptionStackTrace,\n MetricRecord,\n Trace,\n Span,\n ReportRequest,\n} from \"@tracewayapp/core\";\nimport { collectMetrics } from \"./metrics.js\";\n\nexport interface CollectionFrameStoreOptions {\n apiUrl: string;\n token: string;\n debug: boolean;\n maxCollectionFrames: number;\n collectionInterval: number;\n uploadThrottle: number;\n metricsInterval: number;\n version: string;\n serverName: string;\n sampleRate: number;\n errorSampleRate: number;\n}\n\nexport class CollectionFrameStore {\n private current: CollectionFrame | null = null;\n private currentSetAt: number = Date.now();\n private sendQueue: TypedRing<CollectionFrame>;\n private lastUploadStarted: number | null = null;\n\n private collectionTimer: ReturnType<typeof setInterval> | null = null;\n private metricsTimer: ReturnType<typeof setInterval> | null = null;\n\n private readonly apiUrl: string;\n private readonly token: string;\n private readonly debug: boolean;\n private readonly collectionInterval: number;\n private readonly uploadThrottle: number;\n private readonly metricsInterval: number;\n private readonly version: string;\n private readonly serverName: string;\n\n readonly sampleRate: number;\n readonly errorSampleRate: number;\n\n constructor(options: CollectionFrameStoreOptions) {\n this.apiUrl = options.apiUrl;\n this.token = options.token;\n this.debug = options.debug;\n this.collectionInterval = options.collectionInterval;\n this.uploadThrottle = options.uploadThrottle;\n this.metricsInterval = options.metricsInterval;\n this.version = options.version;\n this.serverName = options.serverName;\n this.sampleRate = options.sampleRate;\n this.errorSampleRate = options.errorSampleRate;\n this.sendQueue = new TypedRing<CollectionFrame>(\n options.maxCollectionFrames,\n );\n\n this.collectionTimer = setInterval(() => {\n this.tick();\n }, this.collectionInterval);\n if (this.collectionTimer && typeof this.collectionTimer.unref === \"function\") {\n this.collectionTimer.unref();\n }\n\n this.metricsTimer = setInterval(() => {\n this.collectSystemMetrics();\n }, this.metricsInterval);\n if (this.metricsTimer && typeof this.metricsTimer.unref === \"function\") {\n this.metricsTimer.unref();\n }\n }\n\n private tick(): void {\n if (this.current !== null) {\n if (this.currentSetAt < Date.now() - this.collectionInterval) {\n this.rotateCurrentCollectionFrame();\n this.processSendQueue();\n }\n } else if (this.sendQueue.length > 0) {\n this.processSendQueue();\n }\n }\n\n private ensureCurrent(): CollectionFrame {\n if (this.current === null) {\n this.current = { stackTraces: [], metrics: [], traces: [] };\n this.currentSetAt = Date.now();\n }\n return this.current;\n }\n\n addException(exception: ExceptionStackTrace): void {\n this.ensureCurrent().stackTraces.push(exception);\n }\n\n addMetric(metric: MetricRecord): void {\n this.ensureCurrent().metrics.push(metric);\n }\n\n addTrace(trace: Trace): void {\n this.ensureCurrent().traces.push(trace);\n }\n\n private rotateCurrentCollectionFrame(): void {\n if (this.current !== null) {\n this.sendQueue.push(this.current);\n this.current = null;\n }\n }\n\n private processSendQueue(): void {\n if (\n this.lastUploadStarted === null ||\n this.lastUploadStarted < Date.now() - this.uploadThrottle\n ) {\n this.lastUploadStarted = Date.now();\n const frames = this.sendQueue.readAll();\n if (frames.length > 0) {\n this.triggerUpload(frames);\n }\n }\n }\n\n private triggerUpload(framesToSend: CollectionFrame[]): void {\n const payload: ReportRequest = {\n collectionFrames: framesToSend,\n appVersion: this.version,\n serverName: this.serverName,\n };\n\n let jsonData: string;\n try {\n jsonData = JSON.stringify(payload);\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to serialize frames:\", err);\n }\n return;\n }\n\n let gzipped: Uint8Array;\n try {\n gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to gzip:\", err);\n }\n return;\n }\n\n fetch(this.apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${this.token}`,\n },\n body: gzipped as unknown as BodyInit,\n })\n .then((resp) => {\n if (resp.status === 200) {\n this.sendQueue.remove(framesToSend);\n } else if (this.debug) {\n console.error(`Traceway: upload returned status ${resp.status}`);\n }\n })\n .catch((err) => {\n if (this.debug) {\n console.error(\"Traceway: upload failed:\", err);\n }\n });\n }\n\n private collectSystemMetrics(): void {\n try {\n const metrics = collectMetrics();\n for (const metric of metrics) {\n this.addMetric(metric);\n }\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to collect metrics:\", err);\n }\n }\n }\n\n async shutdown(): Promise<void> {\n if (this.collectionTimer !== null) {\n clearInterval(this.collectionTimer);\n this.collectionTimer = null;\n }\n if (this.metricsTimer !== null) {\n clearInterval(this.metricsTimer);\n this.metricsTimer = null;\n }\n\n this.rotateCurrentCollectionFrame();\n\n const frames = this.sendQueue.readAll();\n if (frames.length === 0) return;\n\n const payload: ReportRequest = {\n collectionFrames: frames,\n appVersion: this.version,\n serverName: this.serverName,\n };\n\n try {\n const jsonData = JSON.stringify(payload);\n const gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));\n\n const resp = await fetch(this.apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${this.token}`,\n },\n body: gzipped as unknown as BodyInit,\n });\n\n if (resp.status === 200) {\n this.sendQueue.remove(frames);\n }\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: shutdown upload failed:\", err);\n }\n }\n }\n}\n","export class TypedRing<T> {\n private arr: (T | null)[];\n private head: number = 0;\n private _capacity: number;\n private _len: number = 0;\n\n constructor(capacity: number) {\n this._capacity = capacity;\n this.arr = new Array<T | null>(capacity).fill(null);\n }\n\n get length(): number {\n return this._len;\n }\n\n get capacity(): number {\n return this._capacity;\n }\n\n push(val: T): void {\n this.arr[this.head] = val;\n this.head = (this.head + 1) % this._capacity;\n if (this._len < this._capacity) {\n this._len += 1;\n }\n }\n\n readAll(): T[] {\n const result: T[] = [];\n for (let i = 0; i < this._len; i++) {\n const idx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n result.push(this.arr[idx] as T);\n }\n return result;\n }\n\n clear(): void {\n for (let i = 0; i < this.arr.length; i++) {\n this.arr[i] = null;\n }\n this.head = 0;\n this._len = 0;\n }\n\n remove(vals: T[]): number {\n if (vals.length === 0) return 0;\n\n const toRemove = new Set<T>(vals);\n let writeIdx = 0;\n let removed = 0;\n\n for (let i = 0; i < this._len; i++) {\n const readIdx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n if (toRemove.has(this.arr[readIdx] as T)) {\n removed++;\n } else {\n if (writeIdx !== i) {\n const destIdx =\n (this.head - this._len + writeIdx + this._capacity) %\n this._capacity;\n this.arr[destIdx] = this.arr[readIdx];\n }\n writeIdx++;\n }\n }\n\n for (let i = writeIdx; i < this._len; i++) {\n const idx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n this.arr[idx] = null;\n }\n\n this._len = writeIdx;\n this.head = (this.head - removed + this._capacity) % this._capacity;\n\n return removed;\n }\n}\n","import * as os from \"os\";\nimport { METRIC_MEM_USED, METRIC_MEM_TOTAL, METRIC_CPU_USED_PCNT } from \"@tracewayapp/core\";\nimport type { MetricRecord } from \"@tracewayapp/core\";\nimport { nowISO } from \"@tracewayapp/core\";\n\nlet prevCpuUsage: NodeJS.CpuUsage | null = null;\nlet prevCpuTime: number | null = null;\n\nexport function collectMetrics(): MetricRecord[] {\n const metrics: MetricRecord[] = [];\n const recordedAt = nowISO();\n\n const mem = process.memoryUsage();\n metrics.push({\n name: METRIC_MEM_USED,\n value: Math.round((mem.rss / 1024 / 1024) * 100) / 100,\n recordedAt,\n });\n\n const totalMem = os.totalmem();\n metrics.push({\n name: METRIC_MEM_TOTAL,\n value: Math.round((totalMem / 1024 / 1024) * 100) / 100,\n recordedAt,\n });\n\n const currentCpuUsage = process.cpuUsage();\n const currentTime = Date.now();\n if (prevCpuUsage !== null && prevCpuTime !== null) {\n const elapsedMs = currentTime - prevCpuTime;\n if (elapsedMs > 0) {\n const userDelta = currentCpuUsage.user - prevCpuUsage.user;\n const systemDelta = currentCpuUsage.system - prevCpuUsage.system;\n const totalDeltaMs = (userDelta + systemDelta) / 1000;\n const cpuCount = os.cpus().length || 1;\n const cpuPercent = (totalDeltaMs / elapsedMs / cpuCount) * 100;\n metrics.push({\n name: METRIC_CPU_USED_PCNT,\n value: Math.round(cpuPercent * 100) / 100,\n recordedAt,\n });\n }\n }\n prevCpuUsage = currentCpuUsage;\n prevCpuTime = currentTime;\n\n return metrics;\n}\n\nexport function resetCpuTracking(): void {\n prevCpuUsage = null;\n prevCpuTime = null;\n}\n","export function formatErrorStackTrace(error: Error): string {\n const lines: string[] = [];\n\n const typeName = error.constructor.name || \"Error\";\n lines.push(`${typeName}: ${error.message}`);\n\n if (error.stack) {\n const stackLines = error.stack.split(\"\\n\");\n for (const line of stackLines) {\n const match = line.match(/^\\s+at\\s+(.+?)\\s+\\((.+):(\\d+):\\d+\\)$/);\n if (match) {\n const funcName = shortenFunctionName(match[1]);\n const file = shortenFilePath(match[2]);\n const lineNum = match[3];\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${lineNum}`);\n continue;\n }\n\n const matchNoParens = line.match(/^\\s+at\\s+(.+):(\\d+):\\d+$/);\n if (matchNoParens) {\n const file = shortenFilePath(matchNoParens[1]);\n const lineNum = matchNoParens[2];\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${lineNum}`);\n continue;\n }\n\n const matchFnOnly = line.match(/^\\s+at\\s+(.+)$/);\n if (matchFnOnly) {\n const funcName = shortenFunctionName(matchFnOnly[1]);\n lines.push(`${funcName}()`);\n lines.push(` <unknown>:0`);\n }\n }\n }\n\n return lines.join(\"\\n\") + \"\\n\";\n}\n\nfunction shortenFunctionName(fn: string): string {\n const slashIdx = fn.lastIndexOf(\"/\");\n if (slashIdx >= 0) {\n fn = fn.slice(slashIdx + 1);\n }\n const dotIdx = fn.indexOf(\".\");\n if (dotIdx >= 0) {\n fn = fn.slice(dotIdx + 1);\n }\n return fn;\n}\n\nfunction shortenFilePath(filePath: string): string {\n const parts = filePath.split(\"/\");\n return parts[parts.length - 1];\n}\n","import { AsyncLocalStorage } from \"async_hooks\";\nimport { generateUUID } from \"@tracewayapp/core\";\nimport type { Span } from \"@tracewayapp/core\";\n\n/**\n * Trace context stored in AsyncLocalStorage.\n * Automatically propagates through async operations.\n */\nexport interface TraceContext {\n /** Unique trace identifier (UUID v4) */\n traceId: string;\n /** Whether this is a background task (vs HTTP request) */\n isTask: boolean;\n /** When the trace started */\n startedAt: Date;\n /** Collected spans within this trace */\n spans: Span[];\n /** Key-value attributes for this trace */\n attributes: Record<string, string>;\n /** For HTTP traces: endpoint like \"GET /api/users\" */\n endpoint?: string;\n /** For HTTP traces: response status code */\n statusCode?: number;\n /** For HTTP traces: response body size */\n bodySize?: number;\n /** For HTTP traces: client IP address */\n clientIP?: string;\n}\n\n/**\n * Options for creating a new trace context.\n */\nexport interface TraceContextOptions {\n /** Custom trace ID (auto-generated if not provided) */\n traceId?: string;\n /** Mark as background task instead of HTTP trace */\n isTask?: boolean;\n /** Initial attributes */\n attributes?: Record<string, string>;\n /** HTTP endpoint (e.g., \"GET /api/users\") */\n endpoint?: string;\n /** Client IP address */\n clientIP?: string;\n}\n\n// The AsyncLocalStorage instance for trace context\nconst asyncLocalStorage = new AsyncLocalStorage<TraceContext>();\n\n/**\n * Get the current trace context, if any.\n * Returns undefined if not within a trace context.\n */\nexport function getTraceContext(): TraceContext | undefined {\n return asyncLocalStorage.getStore();\n}\n\n/**\n * Get the current trace ID, if within a trace context.\n */\nexport function getTraceId(): string | undefined {\n return asyncLocalStorage.getStore()?.traceId;\n}\n\n/**\n * Check if currently within a trace context.\n */\nexport function hasTraceContext(): boolean {\n return asyncLocalStorage.getStore() !== undefined;\n}\n\n/**\n * Run a function within a new trace context.\n * All async operations within will have access to this context.\n *\n * @example\n * ```ts\n * // HTTP request handler\n * withTraceContext({ endpoint: \"GET /api/users\", clientIP: req.ip }, async () => {\n * const users = await db.query(\"SELECT * FROM users\");\n * captureException(new Error(\"oops\")); // Auto-linked to trace\n * return users;\n * });\n *\n * // Background task\n * withTraceContext({ isTask: true, endpoint: \"process-emails\" }, async () => {\n * await processEmails();\n * });\n * ```\n */\nexport function withTraceContext<T>(\n options: TraceContextOptions,\n fn: () => T,\n): T {\n const context: TraceContext = {\n traceId: options.traceId ?? generateUUID(),\n isTask: options.isTask ?? false,\n startedAt: new Date(),\n spans: [],\n attributes: options.attributes ?? {},\n endpoint: options.endpoint,\n clientIP: options.clientIP,\n };\n return asyncLocalStorage.run(context, fn);\n}\n\n/**\n * Run a function within a trace context, automatically capturing the trace on completion.\n * This is a convenience wrapper that handles timing and capture automatically.\n *\n * @example\n * ```ts\n * // In Express middleware\n * app.use((req, res, next) => {\n * runWithTraceContext(\n * {\n * endpoint: `${req.method} ${req.path}`,\n * clientIP: req.ip,\n * attributes: { userId: req.user?.id },\n * },\n * async () => {\n * await next();\n * // Status code and body size set via setTraceResponseInfo()\n * },\n * { captureOnEnd: true }\n * );\n * });\n * ```\n */\nexport function runWithTraceContext<T>(\n options: TraceContextOptions,\n fn: () => T | Promise<T>,\n): T | Promise<T> {\n return withTraceContext(options, fn);\n}\n\n/**\n * Add a completed span to the current trace context.\n * No-op if not within a trace context.\n */\nexport function addSpanToContext(span: Span): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.spans.push(span);\n }\n}\n\n/**\n * Set an attribute on the current trace context.\n * No-op if not within a trace context.\n */\nexport function setTraceAttribute(key: string, value: string): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.attributes[key] = value;\n }\n}\n\n/**\n * Set multiple attributes on the current trace context.\n * No-op if not within a trace context.\n */\nexport function setTraceAttributes(attributes: Record<string, string>): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n Object.assign(ctx.attributes, attributes);\n }\n}\n\n/**\n * Set HTTP response info on the current trace context.\n * Used by framework adapters after the response is sent.\n */\nexport function setTraceResponseInfo(\n statusCode: number,\n bodySize?: number,\n): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.statusCode = statusCode;\n ctx.bodySize = bodySize;\n }\n}\n\n/**\n * Get all spans from the current trace context.\n * Returns empty array if not within a trace context.\n */\nexport function getTraceSpans(): Span[] {\n return asyncLocalStorage.getStore()?.spans ?? [];\n}\n\n/**\n * Get the duration in milliseconds since the trace started.\n * Returns 0 if not within a trace context.\n */\nexport function getTraceDuration(): number {\n const ctx = asyncLocalStorage.getStore();\n if (!ctx) return 0;\n return Date.now() - ctx.startedAt.getTime();\n}\n\n/**\n * Fork the current trace context for a sub-operation.\n * Useful for parallel operations that should have isolated spans.\n * Returns undefined if not within a trace context.\n */\nexport function forkTraceContext<T>(fn: () => T): T | undefined {\n const parentCtx = asyncLocalStorage.getStore();\n if (!parentCtx) return undefined;\n\n const forkedContext: TraceContext = {\n ...parentCtx,\n spans: [], // Forked context gets its own spans\n attributes: { ...parentCtx.attributes },\n };\n\n return asyncLocalStorage.run(forkedContext, () => {\n const result = fn();\n // Merge spans back to parent after completion\n parentCtx.spans.push(...forkedContext.spans);\n return result;\n });\n}\n\n// Export the AsyncLocalStorage instance for advanced use cases\nexport { asyncLocalStorage as traceContextStorage };\n"],"mappings":";AAAA;AAAA,EACE,gBAAAA;AAAA,EACA;AAAA,EACA,UAAAC;AAAA,EACA;AAAA,OACK;;;ACLP,YAAY,UAAU;;;ACAf,IAAM,YAAN,MAAmB;AAAA,EAChB;AAAA,EACA,OAAe;AAAA,EACf;AAAA,EACA,OAAe;AAAA,EAEvB,YAAY,UAAkB;AAC5B,SAAK,YAAY;AACjB,SAAK,MAAM,IAAI,MAAgB,QAAQ,EAAE,KAAK,IAAI;AAAA,EACpD;AAAA,EAEA,IAAI,SAAiB;AACnB,WAAO,KAAK;AAAA,EACd;AA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Z,QAAM,IAAI;AACV,UAAQ;AACR,QAAM,EAAE,SAAS;AACnB;","names":["generateUUID","nowISO","os","hostname","nowISO","generateUUID"]}
1
+ {"version":3,"sources":["../src/traceway.ts","../src/collection-frame-store.ts","../src/typed-ring.ts","../src/metrics.ts","../src/stack-trace.ts","../src/context.ts"],"sourcesContent":["import {\n generateUUID,\n parseConnectionString,\n nowISO,\n msToNanoseconds,\n} from \"@tracewayapp/core\";\nimport type {\n TracewayOptions,\n ExceptionStackTrace,\n Trace,\n Span,\n} from \"@tracewayapp/core\";\nimport { CollectionFrameStore } from \"./collection-frame-store.js\";\nimport { formatErrorStackTrace } from \"./stack-trace.js\";\nimport {\n getTraceContext,\n getTraceId,\n addSpanToContext,\n type TraceContext,\n} from \"./context.js\";\nimport * as os from \"os\";\n\nlet store: CollectionFrameStore | null = null;\n\nfunction getHostname(): string {\n try {\n const hostname = os.hostname();\n const dotIdx = hostname.indexOf(\".\");\n return dotIdx >= 0 ? hostname.slice(0, dotIdx) : hostname;\n } catch {\n return \"unknown\";\n }\n}\n\nexport function init(\n connectionString: string,\n options: TracewayOptions = {},\n): void {\n if (store !== null) {\n throw new Error(\"Traceway: already initialized. Call shutdown() first.\");\n }\n\n const { token, apiUrl } = parseConnectionString(connectionString);\n\n store = new CollectionFrameStore({\n apiUrl,\n token,\n debug: options.debug ?? false,\n maxCollectionFrames: options.maxCollectionFrames ?? 12,\n collectionInterval: options.collectionInterval ?? 5000,\n uploadThrottle: options.uploadThrottle ?? 2000,\n metricsInterval: options.metricsInterval ?? 30000,\n version: options.version ?? \"\",\n serverName: options.serverName ?? getHostname(),\n sampleRate: options.sampleRate ?? 1,\n errorSampleRate: options.errorSampleRate ?? 1,\n });\n}\n\nexport function captureException(error: Error): void {\n if (!store) return;\n // Auto-detect trace context\n const ctx = getTraceContext();\n captureExceptionWithAttributes(\n error,\n ctx?.attributes,\n ctx?.traceId,\n );\n}\n\nexport function captureExceptionWithAttributes(\n error: Error,\n attributes?: Record<string, string>,\n traceId?: string,\n): void {\n if (!store) return;\n // Auto-detect trace context if not explicitly provided\n const ctx = getTraceContext();\n const resolvedTraceId = traceId ?? ctx?.traceId ?? null;\n const resolvedAttrs = attributes ?? ctx?.attributes;\n const isTask = ctx?.isTask ?? false;\n\n store.addException({\n traceId: resolvedTraceId,\n isTask: isTask || undefined,\n stackTrace: formatErrorStackTrace(error),\n recordedAt: nowISO(),\n attributes: resolvedAttrs,\n isMessage: false,\n });\n}\n\nexport function captureMessage(\n msg: string,\n attributes?: Record<string, string>,\n): void {\n if (!store) return;\n // Auto-detect trace context\n const ctx = getTraceContext();\n const resolvedTraceId = ctx?.traceId ?? null;\n const resolvedAttrs = attributes ?? ctx?.attributes;\n const isTask = ctx?.isTask ?? false;\n\n store.addException({\n traceId: resolvedTraceId,\n isTask: isTask || undefined,\n stackTrace: msg,\n recordedAt: nowISO(),\n attributes: resolvedAttrs,\n isMessage: true,\n });\n}\n\nexport function captureMetric(name: string, value: number): void {\n if (!store) return;\n store.addMetric({\n name,\n value,\n recordedAt: nowISO(),\n });\n}\n\nexport function captureTrace(\n traceId: string,\n endpoint: string,\n durationMs: number,\n startedAt: Date,\n statusCode: number,\n bodySize: number,\n clientIP: string,\n attributes?: Record<string, string>,\n spans?: Span[],\n): void {\n if (!store) return;\n store.addTrace({\n id: traceId,\n endpoint,\n duration: msToNanoseconds(durationMs),\n recordedAt: startedAt.toISOString(),\n statusCode,\n bodySize,\n clientIP,\n attributes,\n spans,\n });\n}\n\nexport function captureTask(\n traceId: string,\n taskName: string,\n durationMs: number,\n startedAt: Date,\n attributes?: Record<string, string>,\n spans?: Span[],\n): void {\n if (!store) return;\n store.addTrace({\n id: traceId,\n endpoint: taskName,\n duration: msToNanoseconds(durationMs),\n recordedAt: startedAt.toISOString(),\n statusCode: 0,\n bodySize: 0,\n clientIP: \"\",\n attributes,\n spans,\n isTask: true,\n });\n}\n\n/** Handle returned by startSpan for tracking span timing */\nexport interface SpanHandle {\n id: string;\n name: string;\n startTime: string;\n startedAt: number;\n}\n\n/**\n * Start a new span. If within a trace context, the span will be\n * automatically added to the trace when ended.\n */\nexport function startSpan(name: string): SpanHandle {\n const now = Date.now();\n return {\n id: generateUUID(),\n name,\n startTime: new Date(now).toISOString(),\n startedAt: now,\n };\n}\n\n/**\n * End a span and get the completed Span object.\n * If within a trace context, automatically adds the span to the trace.\n *\n * @param addToContext - If true (default), adds span to current trace context\n */\nexport function endSpan(span: SpanHandle, addToContext: boolean = true): Span {\n const durationMs = Date.now() - span.startedAt;\n const completedSpan: Span = {\n id: span.id,\n name: span.name,\n startTime: span.startTime,\n duration: msToNanoseconds(durationMs),\n };\n\n // Auto-add to trace context if available\n if (addToContext) {\n addSpanToContext(completedSpan);\n }\n\n return completedSpan;\n}\n\n/**\n * Capture the current trace context as a trace.\n * Call this at the end of a request/task to record the trace.\n *\n * @example\n * ```ts\n * // In Express middleware (after response)\n * withTraceContext({ endpoint: `${req.method} ${req.path}` }, async () => {\n * await handleRequest(req, res);\n * setTraceResponseInfo(res.statusCode, contentLength);\n * captureCurrentTrace(); // Records the trace with all spans\n * });\n * ```\n */\nexport function captureCurrentTrace(): void {\n if (!store) return;\n\n const ctx = getTraceContext();\n if (!ctx) return;\n\n const durationMs = Date.now() - ctx.startedAt.getTime();\n const isError = (ctx.statusCode ?? 0) >= 500;\n\n if (!shouldSample(isError)) return;\n\n if (ctx.isTask) {\n store.addTrace({\n id: ctx.traceId,\n endpoint: ctx.endpoint ?? \"unknown-task\",\n duration: msToNanoseconds(durationMs),\n recordedAt: ctx.startedAt.toISOString(),\n statusCode: 0,\n bodySize: 0,\n clientIP: \"\",\n attributes: Object.keys(ctx.attributes).length > 0 ? ctx.attributes : undefined,\n spans: ctx.spans.length > 0 ? ctx.spans : undefined,\n isTask: true,\n });\n } else {\n store.addTrace({\n id: ctx.traceId,\n endpoint: ctx.endpoint ?? \"unknown\",\n duration: msToNanoseconds(durationMs),\n recordedAt: ctx.startedAt.toISOString(),\n statusCode: ctx.statusCode ?? 0,\n bodySize: ctx.bodySize ?? 0,\n clientIP: ctx.clientIP ?? \"\",\n attributes: Object.keys(ctx.attributes).length > 0 ? ctx.attributes : undefined,\n spans: ctx.spans.length > 0 ? ctx.spans : undefined,\n });\n }\n}\n\nexport function shouldSample(isError: boolean): boolean {\n if (!store) return false;\n const rate = isError ? store.errorSampleRate : store.sampleRate;\n if (rate >= 1) return true;\n if (rate <= 0) return false;\n return Math.random() < rate;\n}\n\nexport function measureTask(\n title: string,\n fn: () => void | Promise<void>,\n): void {\n const traceId = generateUUID();\n const start = Date.now();\n const startDate = new Date(start);\n\n try {\n const result = fn();\n if (result && typeof (result as Promise<void>).then === \"function\") {\n (result as Promise<void>)\n .then(() => {\n const durationMs = Date.now() - start;\n if (shouldSample(false)) {\n captureTask(traceId, title, durationMs, startDate);\n }\n })\n .catch((err: unknown) => {\n const durationMs = Date.now() - start;\n if (shouldSample(true)) {\n captureTask(traceId, title, durationMs, startDate);\n if (err instanceof Error) {\n captureExceptionWithAttributes(err, undefined, traceId);\n }\n }\n throw err;\n });\n } else {\n const durationMs = Date.now() - start;\n if (shouldSample(false)) {\n captureTask(traceId, title, durationMs, startDate);\n }\n }\n } catch (err) {\n const durationMs = Date.now() - start;\n if (shouldSample(true)) {\n captureTask(traceId, title, durationMs, startDate);\n if (err instanceof Error) {\n captureExceptionWithAttributes(err, undefined, traceId);\n }\n }\n throw err;\n }\n}\n\nexport async function shutdown(timeoutMs?: number): Promise<void> {\n if (!store) return;\n const s = store;\n store = null;\n await s.shutdown(timeoutMs);\n}\n","import * as zlib from \"zlib\";\nimport {\n nowISO,\n generateUUID,\n msToNanoseconds,\n} from \"@tracewayapp/core\";\nimport { TypedRing } from \"./typed-ring.js\";\nimport type {\n CollectionFrame,\n ExceptionStackTrace,\n MetricRecord,\n Trace,\n Span,\n ReportRequest,\n} from \"@tracewayapp/core\";\nimport { collectMetrics } from \"./metrics.js\";\n\nexport interface CollectionFrameStoreOptions {\n apiUrl: string;\n token: string;\n debug: boolean;\n maxCollectionFrames: number;\n collectionInterval: number;\n uploadThrottle: number;\n metricsInterval: number;\n version: string;\n serverName: string;\n sampleRate: number;\n errorSampleRate: number;\n}\n\nexport class CollectionFrameStore {\n private current: CollectionFrame | null = null;\n private currentSetAt: number = Date.now();\n private sendQueue: TypedRing<CollectionFrame>;\n private lastUploadStarted: number | null = null;\n\n private collectionTimer: ReturnType<typeof setInterval> | null = null;\n private metricsTimer: ReturnType<typeof setInterval> | null = null;\n\n private readonly apiUrl: string;\n private readonly token: string;\n private readonly debug: boolean;\n private readonly collectionInterval: number;\n private readonly uploadThrottle: number;\n private readonly metricsInterval: number;\n private readonly version: string;\n private readonly serverName: string;\n\n readonly sampleRate: number;\n readonly errorSampleRate: number;\n\n constructor(options: CollectionFrameStoreOptions) {\n this.apiUrl = options.apiUrl;\n this.token = options.token;\n this.debug = options.debug;\n this.collectionInterval = options.collectionInterval;\n this.uploadThrottle = options.uploadThrottle;\n this.metricsInterval = options.metricsInterval;\n this.version = options.version;\n this.serverName = options.serverName;\n this.sampleRate = options.sampleRate;\n this.errorSampleRate = options.errorSampleRate;\n this.sendQueue = new TypedRing<CollectionFrame>(\n options.maxCollectionFrames,\n );\n\n this.collectionTimer = setInterval(() => {\n this.tick();\n }, this.collectionInterval);\n if (this.collectionTimer && typeof this.collectionTimer.unref === \"function\") {\n this.collectionTimer.unref();\n }\n\n this.metricsTimer = setInterval(() => {\n this.collectSystemMetrics();\n }, this.metricsInterval);\n if (this.metricsTimer && typeof this.metricsTimer.unref === \"function\") {\n this.metricsTimer.unref();\n }\n }\n\n private tick(): void {\n if (this.current !== null) {\n if (this.currentSetAt < Date.now() - this.collectionInterval) {\n this.rotateCurrentCollectionFrame();\n this.processSendQueue();\n }\n } else if (this.sendQueue.length > 0) {\n this.processSendQueue();\n }\n }\n\n private ensureCurrent(): CollectionFrame {\n if (this.current === null) {\n this.current = { stackTraces: [], metrics: [], traces: [] };\n this.currentSetAt = Date.now();\n }\n return this.current;\n }\n\n addException(exception: ExceptionStackTrace): void {\n this.ensureCurrent().stackTraces.push(exception);\n }\n\n addMetric(metric: MetricRecord): void {\n this.ensureCurrent().metrics.push(metric);\n }\n\n addTrace(trace: Trace): void {\n this.ensureCurrent().traces.push(trace);\n }\n\n private rotateCurrentCollectionFrame(): void {\n if (this.current !== null) {\n this.sendQueue.push(this.current);\n this.current = null;\n }\n }\n\n private processSendQueue(): void {\n if (\n this.lastUploadStarted === null ||\n this.lastUploadStarted < Date.now() - this.uploadThrottle\n ) {\n this.lastUploadStarted = Date.now();\n const frames = this.sendQueue.readAll();\n if (frames.length > 0) {\n this.triggerUpload(frames);\n }\n }\n }\n\n private triggerUpload(framesToSend: CollectionFrame[]): void {\n const payload: ReportRequest = {\n collectionFrames: framesToSend,\n appVersion: this.version,\n serverName: this.serverName,\n };\n\n let jsonData: string;\n try {\n jsonData = JSON.stringify(payload);\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to serialize frames:\", err);\n }\n return;\n }\n\n let gzipped: Uint8Array;\n try {\n gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to gzip:\", err);\n }\n return;\n }\n\n fetch(this.apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${this.token}`,\n },\n body: gzipped as unknown as BodyInit,\n })\n .then((resp) => {\n if (resp.status === 200) {\n this.sendQueue.remove(framesToSend);\n } else if (this.debug) {\n console.error(`Traceway: upload returned status ${resp.status}`);\n }\n })\n .catch((err) => {\n if (this.debug) {\n console.error(\"Traceway: upload failed:\", err);\n }\n });\n }\n\n private collectSystemMetrics(): void {\n try {\n const metrics = collectMetrics();\n for (const metric of metrics) {\n this.addMetric(metric);\n }\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: failed to collect metrics:\", err);\n }\n }\n }\n\n async shutdown(timeoutMs?: number): Promise<void> {\n if (this.collectionTimer !== null) {\n clearInterval(this.collectionTimer);\n this.collectionTimer = null;\n }\n if (this.metricsTimer !== null) {\n clearInterval(this.metricsTimer);\n this.metricsTimer = null;\n }\n\n this.rotateCurrentCollectionFrame();\n\n const frames = this.sendQueue.readAll();\n if (frames.length === 0) return;\n\n const uploadPromise = (async () => {\n const payload: ReportRequest = {\n collectionFrames: frames,\n appVersion: this.version,\n serverName: this.serverName,\n };\n\n try {\n const jsonData = JSON.stringify(payload);\n const gzipped = new Uint8Array(zlib.gzipSync(Buffer.from(jsonData)));\n\n const resp = await fetch(this.apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${this.token}`,\n },\n body: gzipped as unknown as BodyInit,\n });\n\n if (resp.status === 200) {\n this.sendQueue.remove(frames);\n }\n } catch (err) {\n if (this.debug) {\n console.error(\"Traceway: shutdown upload failed:\", err);\n }\n }\n })();\n\n if (timeoutMs !== undefined) {\n await Promise.race([\n uploadPromise,\n new Promise<void>((resolve) => setTimeout(resolve, timeoutMs)),\n ]);\n } else {\n await uploadPromise;\n }\n }\n}\n","export class TypedRing<T> {\n private arr: (T | null)[];\n private head: number = 0;\n private _capacity: number;\n private _len: number = 0;\n\n constructor(capacity: number) {\n this._capacity = capacity;\n this.arr = new Array<T | null>(capacity).fill(null);\n }\n\n get length(): number {\n return this._len;\n }\n\n get capacity(): number {\n return this._capacity;\n }\n\n push(val: T): void {\n this.arr[this.head] = val;\n this.head = (this.head + 1) % this._capacity;\n if (this._len < this._capacity) {\n this._len += 1;\n }\n }\n\n readAll(): T[] {\n const result: T[] = [];\n for (let i = 0; i < this._len; i++) {\n const idx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n result.push(this.arr[idx] as T);\n }\n return result;\n }\n\n clear(): void {\n for (let i = 0; i < this.arr.length; i++) {\n this.arr[i] = null;\n }\n this.head = 0;\n this._len = 0;\n }\n\n remove(vals: T[]): number {\n if (vals.length === 0) return 0;\n\n const toRemove = new Set<T>(vals);\n let writeIdx = 0;\n let removed = 0;\n\n for (let i = 0; i < this._len; i++) {\n const readIdx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n if (toRemove.has(this.arr[readIdx] as T)) {\n removed++;\n } else {\n if (writeIdx !== i) {\n const destIdx =\n (this.head - this._len + writeIdx + this._capacity) %\n this._capacity;\n this.arr[destIdx] = this.arr[readIdx];\n }\n writeIdx++;\n }\n }\n\n for (let i = writeIdx; i < this._len; i++) {\n const idx =\n (this.head - this._len + i + this._capacity) % this._capacity;\n this.arr[idx] = null;\n }\n\n this._len = writeIdx;\n this.head = (this.head - removed + this._capacity) % this._capacity;\n\n return removed;\n }\n}\n","import * as os from \"os\";\nimport { METRIC_MEM_USED, METRIC_MEM_TOTAL, METRIC_CPU_USED_PCNT } from \"@tracewayapp/core\";\nimport type { MetricRecord } from \"@tracewayapp/core\";\nimport { nowISO } from \"@tracewayapp/core\";\n\nlet prevCpuUsage: NodeJS.CpuUsage | null = null;\nlet prevCpuTime: number | null = null;\n\nexport function collectMetrics(): MetricRecord[] {\n const metrics: MetricRecord[] = [];\n const recordedAt = nowISO();\n\n const mem = process.memoryUsage();\n metrics.push({\n name: METRIC_MEM_USED,\n value: Math.round((mem.rss / 1024 / 1024) * 100) / 100,\n recordedAt,\n });\n\n const totalMem = os.totalmem();\n metrics.push({\n name: METRIC_MEM_TOTAL,\n value: Math.round((totalMem / 1024 / 1024) * 100) / 100,\n recordedAt,\n });\n\n const currentCpuUsage = process.cpuUsage();\n const currentTime = Date.now();\n if (prevCpuUsage !== null && prevCpuTime !== null) {\n const elapsedMs = currentTime - prevCpuTime;\n if (elapsedMs > 0) {\n const userDelta = currentCpuUsage.user - prevCpuUsage.user;\n const systemDelta = currentCpuUsage.system - prevCpuUsage.system;\n const totalDeltaMs = (userDelta + systemDelta) / 1000;\n const cpuCount = os.cpus().length || 1;\n const cpuPercent = (totalDeltaMs / elapsedMs / cpuCount) * 100;\n metrics.push({\n name: METRIC_CPU_USED_PCNT,\n value: Math.round(cpuPercent * 100) / 100,\n recordedAt,\n });\n }\n }\n prevCpuUsage = currentCpuUsage;\n prevCpuTime = currentTime;\n\n return metrics;\n}\n\nexport function resetCpuTracking(): void {\n prevCpuUsage = null;\n prevCpuTime = null;\n}\n","export function formatErrorStackTrace(error: Error): string {\n const lines: string[] = [];\n\n const typeName = error.constructor.name || \"Error\";\n lines.push(`${typeName}: ${error.message}`);\n\n if (error.stack) {\n const stackLines = error.stack.split(\"\\n\");\n for (const line of stackLines) {\n const match = line.match(/^\\s+at\\s+(.+?)\\s+\\((.+):(\\d+):\\d+\\)$/);\n if (match) {\n const funcName = shortenFunctionName(match[1]);\n const file = shortenFilePath(match[2]);\n const lineNum = match[3];\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${lineNum}`);\n continue;\n }\n\n const matchNoParens = line.match(/^\\s+at\\s+(.+):(\\d+):\\d+$/);\n if (matchNoParens) {\n const file = shortenFilePath(matchNoParens[1]);\n const lineNum = matchNoParens[2];\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${lineNum}`);\n continue;\n }\n\n const matchFnOnly = line.match(/^\\s+at\\s+(.+)$/);\n if (matchFnOnly) {\n const funcName = shortenFunctionName(matchFnOnly[1]);\n lines.push(`${funcName}()`);\n lines.push(` <unknown>:0`);\n }\n }\n }\n\n return lines.join(\"\\n\") + \"\\n\";\n}\n\nfunction shortenFunctionName(fn: string): string {\n const slashIdx = fn.lastIndexOf(\"/\");\n if (slashIdx >= 0) {\n fn = fn.slice(slashIdx + 1);\n }\n const dotIdx = fn.indexOf(\".\");\n if (dotIdx >= 0) {\n fn = fn.slice(dotIdx + 1);\n }\n return fn;\n}\n\nfunction shortenFilePath(filePath: string): string {\n const parts = filePath.split(\"/\");\n return parts[parts.length - 1];\n}\n","import { AsyncLocalStorage } from \"async_hooks\";\nimport { generateUUID } from \"@tracewayapp/core\";\nimport type { Span } from \"@tracewayapp/core\";\n\n/**\n * Trace context stored in AsyncLocalStorage.\n * Automatically propagates through async operations.\n */\nexport interface TraceContext {\n /** Unique trace identifier (UUID v4) */\n traceId: string;\n /** Whether this is a background task (vs HTTP request) */\n isTask: boolean;\n /** When the trace started */\n startedAt: Date;\n /** Collected spans within this trace */\n spans: Span[];\n /** Key-value attributes for this trace */\n attributes: Record<string, string>;\n /** For HTTP traces: endpoint like \"GET /api/users\" */\n endpoint?: string;\n /** For HTTP traces: response status code */\n statusCode?: number;\n /** For HTTP traces: response body size */\n bodySize?: number;\n /** For HTTP traces: client IP address */\n clientIP?: string;\n}\n\n/**\n * Options for creating a new trace context.\n */\nexport interface TraceContextOptions {\n /** Custom trace ID (auto-generated if not provided) */\n traceId?: string;\n /** Mark as background task instead of HTTP trace */\n isTask?: boolean;\n /** Initial attributes */\n attributes?: Record<string, string>;\n /** HTTP endpoint (e.g., \"GET /api/users\") */\n endpoint?: string;\n /** Client IP address */\n clientIP?: string;\n}\n\n// The AsyncLocalStorage instance for trace context\nconst asyncLocalStorage = new AsyncLocalStorage<TraceContext>();\n\n/**\n * Get the current trace context, if any.\n * Returns undefined if not within a trace context.\n */\nexport function getTraceContext(): TraceContext | undefined {\n return asyncLocalStorage.getStore();\n}\n\n/**\n * Get the current trace ID, if within a trace context.\n */\nexport function getTraceId(): string | undefined {\n return asyncLocalStorage.getStore()?.traceId;\n}\n\n/**\n * Check if currently within a trace context.\n */\nexport function hasTraceContext(): boolean {\n return asyncLocalStorage.getStore() !== undefined;\n}\n\n/**\n * Run a function within a new trace context.\n * All async operations within will have access to this context.\n *\n * @example\n * ```ts\n * // HTTP request handler\n * withTraceContext({ endpoint: \"GET /api/users\", clientIP: req.ip }, async () => {\n * const users = await db.query(\"SELECT * FROM users\");\n * captureException(new Error(\"oops\")); // Auto-linked to trace\n * return users;\n * });\n *\n * // Background task\n * withTraceContext({ isTask: true, endpoint: \"process-emails\" }, async () => {\n * await processEmails();\n * });\n * ```\n */\nexport function withTraceContext<T>(\n options: TraceContextOptions,\n fn: () => T,\n): T {\n const context: TraceContext = {\n traceId: options.traceId ?? generateUUID(),\n isTask: options.isTask ?? false,\n startedAt: new Date(),\n spans: [],\n attributes: options.attributes ?? {},\n endpoint: options.endpoint,\n clientIP: options.clientIP,\n };\n return asyncLocalStorage.run(context, fn);\n}\n\n/**\n * Run a function within a trace context, automatically capturing the trace on completion.\n * This is a convenience wrapper that handles timing and capture automatically.\n *\n * @example\n * ```ts\n * // In Express middleware\n * app.use((req, res, next) => {\n * runWithTraceContext(\n * {\n * endpoint: `${req.method} ${req.path}`,\n * clientIP: req.ip,\n * attributes: { userId: req.user?.id },\n * },\n * async () => {\n * await next();\n * // Status code and body size set via setTraceResponseInfo()\n * },\n * { captureOnEnd: true }\n * );\n * });\n * ```\n */\nexport function runWithTraceContext<T>(\n options: TraceContextOptions,\n fn: () => T | Promise<T>,\n): T | Promise<T> {\n return withTraceContext(options, fn);\n}\n\n/**\n * Add a completed span to the current trace context.\n * No-op if not within a trace context.\n */\nexport function addSpanToContext(span: Span): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.spans.push(span);\n }\n}\n\n/**\n * Set an attribute on the current trace context.\n * No-op if not within a trace context.\n */\nexport function setTraceAttribute(key: string, value: string): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.attributes[key] = value;\n }\n}\n\n/**\n * Set multiple attributes on the current trace context.\n * No-op if not within a trace context.\n */\nexport function setTraceAttributes(attributes: Record<string, string>): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n Object.assign(ctx.attributes, attributes);\n }\n}\n\n/**\n * Set HTTP response info on the current trace context.\n * Used by framework adapters after the response is sent.\n */\nexport function setTraceResponseInfo(\n statusCode: number,\n bodySize?: number,\n): void {\n const ctx = asyncLocalStorage.getStore();\n if (ctx) {\n ctx.statusCode = statusCode;\n ctx.bodySize = bodySize;\n }\n}\n\n/**\n * Get all spans from the current trace context.\n * Returns empty array if not within a trace context.\n */\nexport function getTraceSpans(): Span[] {\n return asyncLocalStorage.getStore()?.spans ?? [];\n}\n\n/**\n * Get the duration in milliseconds since the trace started.\n * Returns 0 if not within a trace context.\n */\nexport function getTraceDuration(): number {\n const ctx = asyncLocalStorage.getStore();\n if (!ctx) return 0;\n return Date.now() - ctx.startedAt.getTime();\n}\n\n/**\n * Fork the current trace context for a sub-operation.\n * Useful for parallel operations that should have isolated spans.\n * Returns undefined if not within a trace context.\n */\nexport function forkTraceContext<T>(fn: () => T): T | undefined {\n const parentCtx = asyncLocalStorage.getStore();\n if (!parentCtx) return undefined;\n\n const forkedContext: TraceContext = {\n ...parentCtx,\n spans: [], // Forked context gets its own spans\n attributes: { ...parentCtx.attributes },\n };\n\n return asyncLocalStorage.run(forkedContext, () => {\n const result = fn();\n // Merge spans back to parent after completion\n parentCtx.spans.push(...forkedContext.spans);\n return result;\n });\n}\n\n// Export the AsyncLocalStorage instance for advanced use cases\nexport { asyncLocalStorage as traceContextStorage };\n"],"mappings":";AAAA;AAAA,EACE,gBAAAA;AAAA,EACA;AAAA,EACA,UAAAC;AAAA,EACA;AAAA,OACK;;;ACLP,YAAY,UAAU;;;ACAf,IAAM,YAAN,MAAmB;AAAA,EAChB;AAAA,EACA,OAAe;AAAA,EACf;AAAA,EACA,OAAe;AAAA,EAEvB,YAAY,UAAkB;AAC5B,SAAK,YAAY;AACjB,SAAK,MAAM,IAAI,MAAgB,QAAQ,EAAE,KAAK,IAAI;AAAA,EACpD;AAAA,EAEA,IAAI,SAAiB;AACnB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,WAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,KAAK,KAAc;AACjB,SAAK,IAAI,KAAK,IAAI,IAAI;AACtB,SAAK,QAAQ,KAAK,OAAO,KAAK,KAAK;AACnC,QAAI,KAAK,OAAO,KAAK,WAAW;AAC9B,WAAK,QAAQ;AAAA,IACf;AAAA,EACF;AAAA,EAEA,UAAe;AACb,UAAM,SAAc,CAAC;AACrB,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S,SAAS;AAC5B;","names":["generateUUID","nowISO","os","hostname","nowISO","generateUUID"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tracewayapp/backend",
3
- "version": "0.2.0",
3
+ "version": "1.0.0",
4
4
  "description": "Traceway SDK for Node.js backends",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -23,6 +23,6 @@
23
23
  "dev": "tsup --watch"
24
24
  },
25
25
  "dependencies": {
26
- "@tracewayapp/core": "0.2.0"
26
+ "@tracewayapp/core": "1.0.0"
27
27
  }
28
28
  }