vedatrace 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -5
- package/dist/index.cjs +282 -81
- package/dist/index.d.cts +116 -52
- package/dist/index.d.mts +116 -52
- package/dist/index.mjs +279 -83
- package/dist/integrations/express.d.cts +1 -1
- package/dist/integrations/express.d.mts +1 -1
- package/dist/integrations/nextjs.d.cts +1 -1
- package/dist/integrations/nextjs.d.mts +1 -1
- package/dist/integrations/react.cjs +1 -2
- package/dist/integrations/react.d.cts +1 -1
- package/dist/integrations/react.d.mts +1 -1
- package/dist/integrations/react.mjs +1 -2
- package/dist/transports/index.cjs +26 -1
- package/dist/transports/index.d.cts +23 -6
- package/dist/transports/index.d.mts +23 -6
- package/dist/transports/index.mjs +26 -2
- package/dist/{types-DUNRMOTv.d.cts → types-BU0UESs9.d.cts} +20 -44
- package/dist/{types-DUNRMOTv.d.mts → types-BU0UESs9.d.mts} +20 -44
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,28 +1,60 @@
|
|
|
1
|
-
import { VedaTraceHttpTransport, VedaTraceConsoleTransport } from './transports/index.mjs';
|
|
1
|
+
import { VedaTraceHttpTransportBrowser, VedaTraceHttpTransport, VedaTraceConsoleTransport } from './transports/index.mjs';
|
|
2
2
|
|
|
3
3
|
class VedaTraceBatcher {
|
|
4
|
-
constructor(transports, config,
|
|
4
|
+
constructor(transports, config, immediateFlush = false) {
|
|
5
5
|
this.transports = transports;
|
|
6
6
|
this.config = config;
|
|
7
|
-
this.onError = onError;
|
|
8
|
-
this.onSuccess = onSuccess;
|
|
9
7
|
this.immediateFlush = immediateFlush;
|
|
10
|
-
|
|
11
|
-
this.startFlushTimer();
|
|
12
|
-
}
|
|
8
|
+
this.context = config.executionContext;
|
|
13
9
|
}
|
|
14
10
|
queue = [];
|
|
15
11
|
flushTimer = null;
|
|
12
|
+
flushDebounceTimer = null;
|
|
16
13
|
isFlushing = false;
|
|
17
14
|
pendingFlush = null;
|
|
18
|
-
|
|
15
|
+
context;
|
|
16
|
+
/** Attach execution context after initialization */
|
|
17
|
+
setContext(ctx) {
|
|
18
|
+
this.context = ctx;
|
|
19
|
+
}
|
|
20
|
+
/** Get current context */
|
|
21
|
+
getContext() {
|
|
22
|
+
return this.context;
|
|
23
|
+
}
|
|
24
|
+
/** Add log to queue with context-aware flush */
|
|
19
25
|
add(log) {
|
|
20
26
|
this.queue.push(log);
|
|
21
|
-
if (this.
|
|
27
|
+
if (!this.flushTimer && !this.immediateFlush) {
|
|
28
|
+
this.startFlushTimer();
|
|
29
|
+
}
|
|
30
|
+
if (this.immediateFlush || this.context) {
|
|
31
|
+
this.debouncedFlush();
|
|
32
|
+
} else if (this.queue.length >= this.config.batchSize) {
|
|
22
33
|
this.flush();
|
|
23
34
|
}
|
|
24
35
|
}
|
|
25
|
-
/**
|
|
36
|
+
/** Debounced flush - prevents rapid-fire flushes */
|
|
37
|
+
debouncedFlush() {
|
|
38
|
+
if (this.flushDebounceTimer) {
|
|
39
|
+
clearTimeout(this.flushDebounceTimer);
|
|
40
|
+
}
|
|
41
|
+
this.flushDebounceTimer = setTimeout(() => {
|
|
42
|
+
this.flushDebounceTimer = null;
|
|
43
|
+
this.flush().catch((error) => {
|
|
44
|
+
if (this.config.onError) {
|
|
45
|
+
this.config.onError(
|
|
46
|
+
error instanceof Error ? error : new Error(String(error))
|
|
47
|
+
);
|
|
48
|
+
} else {
|
|
49
|
+
console.error(
|
|
50
|
+
"[VedaTrace] Debounced flush error:",
|
|
51
|
+
error instanceof Error ? error.message : String(error)
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}, 100);
|
|
56
|
+
}
|
|
57
|
+
/** Flush logs to all transports with waitUntil protection */
|
|
26
58
|
async flush() {
|
|
27
59
|
if (this.isFlushing) {
|
|
28
60
|
return this.pendingFlush ?? Promise.resolve();
|
|
@@ -33,13 +65,16 @@ class VedaTraceBatcher {
|
|
|
33
65
|
this.isFlushing = true;
|
|
34
66
|
const logsToSend = [...this.queue];
|
|
35
67
|
this.queue = [];
|
|
36
|
-
|
|
68
|
+
const flushPromise = this.sendWithRetry(logsToSend).finally(() => {
|
|
37
69
|
this.isFlushing = false;
|
|
38
70
|
this.pendingFlush = null;
|
|
39
71
|
});
|
|
40
|
-
|
|
72
|
+
this.pendingFlush = flushPromise;
|
|
73
|
+
if (this.context) {
|
|
74
|
+
this.context.waitUntil(flushPromise);
|
|
75
|
+
}
|
|
76
|
+
return flushPromise;
|
|
41
77
|
}
|
|
42
|
-
/** Send logs with retry logic */
|
|
43
78
|
async sendWithRetry(logs, attempt = 0) {
|
|
44
79
|
const errors = [];
|
|
45
80
|
for (const transport of this.transports) {
|
|
@@ -57,16 +92,17 @@ class VedaTraceBatcher {
|
|
|
57
92
|
const combinedError = new Error(
|
|
58
93
|
`Failed to send logs after ${this.config.maxRetries} retries: ${errors.map((e) => e.message).join(", ")}`
|
|
59
94
|
);
|
|
60
|
-
if (this.onError) {
|
|
61
|
-
this.onError(combinedError);
|
|
95
|
+
if (this.config.onError) {
|
|
96
|
+
this.config.onError(combinedError);
|
|
62
97
|
} else {
|
|
63
98
|
console.error("[VedaTrace]", combinedError.message);
|
|
64
99
|
}
|
|
65
100
|
return;
|
|
66
101
|
}
|
|
67
|
-
this.onSuccess
|
|
102
|
+
if (this.config.onSuccess) {
|
|
103
|
+
this.config.onSuccess();
|
|
104
|
+
}
|
|
68
105
|
}
|
|
69
|
-
/** Start the flush interval timer */
|
|
70
106
|
startFlushTimer() {
|
|
71
107
|
if (this.flushTimer) {
|
|
72
108
|
clearInterval(this.flushTimer);
|
|
@@ -74,8 +110,8 @@ class VedaTraceBatcher {
|
|
|
74
110
|
this.flushTimer = setInterval(() => {
|
|
75
111
|
if (this.queue.length > 0) {
|
|
76
112
|
this.flush().catch((error) => {
|
|
77
|
-
if (this.onError) {
|
|
78
|
-
this.onError(
|
|
113
|
+
if (this.config.onError) {
|
|
114
|
+
this.config.onError(
|
|
79
115
|
error instanceof Error ? error : new Error(String(error))
|
|
80
116
|
);
|
|
81
117
|
} else {
|
|
@@ -91,63 +127,84 @@ class VedaTraceBatcher {
|
|
|
91
127
|
this.flushTimer.unref();
|
|
92
128
|
}
|
|
93
129
|
}
|
|
94
|
-
/** Stop the flush timer */
|
|
95
130
|
stop() {
|
|
96
131
|
if (this.flushTimer) {
|
|
97
132
|
clearInterval(this.flushTimer);
|
|
98
133
|
this.flushTimer = null;
|
|
99
134
|
}
|
|
135
|
+
if (this.flushDebounceTimer) {
|
|
136
|
+
clearTimeout(this.flushDebounceTimer);
|
|
137
|
+
this.flushDebounceTimer = null;
|
|
138
|
+
}
|
|
100
139
|
}
|
|
101
|
-
/** Start the flush timer (for manual control in edge runtimes) */
|
|
102
140
|
start() {
|
|
103
141
|
if (!this.flushTimer && !this.immediateFlush) {
|
|
104
142
|
this.startFlushTimer();
|
|
105
143
|
}
|
|
106
144
|
}
|
|
107
|
-
/** Delay helper */
|
|
108
145
|
delay(ms) {
|
|
109
146
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
110
147
|
}
|
|
111
|
-
/** Get current queue size */
|
|
112
148
|
getQueueSize() {
|
|
113
149
|
return this.queue.length;
|
|
114
150
|
}
|
|
151
|
+
setExecutionContext(ctx) {
|
|
152
|
+
this.context = ctx;
|
|
153
|
+
}
|
|
115
154
|
}
|
|
116
155
|
|
|
117
156
|
function detectRuntime() {
|
|
118
|
-
if (typeof
|
|
119
|
-
return "node";
|
|
120
|
-
}
|
|
121
|
-
if (typeof globalThis !== "undefined" && "navigator" in globalThis && typeof self === "undefined") {
|
|
122
|
-
return "browser";
|
|
123
|
-
}
|
|
124
|
-
if (typeof caches !== "undefined") {
|
|
157
|
+
if (typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers") {
|
|
125
158
|
return "cloudflare";
|
|
126
159
|
}
|
|
127
|
-
const
|
|
128
|
-
if (
|
|
160
|
+
const g = globalThis;
|
|
161
|
+
if (g?.Deno && g.Deno.version?.deno) {
|
|
129
162
|
return "deno";
|
|
130
163
|
}
|
|
131
|
-
|
|
132
|
-
if (maybeBun?.Bun) {
|
|
164
|
+
if (g?.Bun) {
|
|
133
165
|
return "bun";
|
|
134
166
|
}
|
|
167
|
+
if (typeof g?.WebSocketPair !== "undefined") {
|
|
168
|
+
return "cloudflare";
|
|
169
|
+
}
|
|
170
|
+
if (typeof process !== "undefined" && process.versions?.node) {
|
|
171
|
+
return "node";
|
|
172
|
+
}
|
|
173
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
174
|
+
return "browser";
|
|
175
|
+
}
|
|
176
|
+
if (typeof fetch !== "undefined" && typeof window === "undefined" && typeof process === "undefined") {
|
|
177
|
+
return "edge";
|
|
178
|
+
}
|
|
135
179
|
return "edge";
|
|
136
180
|
}
|
|
137
181
|
function isEdgeRuntime() {
|
|
138
182
|
const runtime = detectRuntime();
|
|
139
183
|
return runtime === "cloudflare" || runtime === "deno" || runtime === "bun" || runtime === "edge";
|
|
140
184
|
}
|
|
185
|
+
function isServerless() {
|
|
186
|
+
const runtime = detectRuntime();
|
|
187
|
+
return runtime === "cloudflare" || runtime === "edge";
|
|
188
|
+
}
|
|
189
|
+
function isLongRunning() {
|
|
190
|
+
const runtime = detectRuntime();
|
|
191
|
+
return runtime === "node" || runtime === "bun" || runtime === "deno";
|
|
192
|
+
}
|
|
193
|
+
function isBrowser() {
|
|
194
|
+
return detectRuntime() === "browser";
|
|
195
|
+
}
|
|
141
196
|
|
|
142
|
-
const SDK_VERSION = "
|
|
197
|
+
const SDK_VERSION = process.env.npm_package_version ?? "0.0.0";
|
|
143
198
|
class VedaTraceLogger {
|
|
144
199
|
batcher = null;
|
|
145
200
|
runtime;
|
|
146
201
|
config;
|
|
147
202
|
childDefaults;
|
|
203
|
+
_context;
|
|
148
204
|
constructor(config = {}, childDefaults = {}) {
|
|
149
205
|
this.runtime = config.runtime ?? detectRuntime();
|
|
150
206
|
this.childDefaults = childDefaults;
|
|
207
|
+
this._context = config.executionContext;
|
|
151
208
|
this.config = {
|
|
152
209
|
service: config.service,
|
|
153
210
|
apiKey: config.apiKey,
|
|
@@ -157,61 +214,68 @@ class VedaTraceLogger {
|
|
|
157
214
|
flushInterval: config.flushInterval ?? 5e3,
|
|
158
215
|
maxRetries: config.maxRetries ?? 3,
|
|
159
216
|
retryDelay: config.retryDelay ?? 1e3,
|
|
160
|
-
onError: config.onError,
|
|
161
|
-
onSuccess: config.onSuccess,
|
|
162
217
|
debug: config.debug ?? false,
|
|
163
218
|
immediateFlush: config.immediateFlush ?? false,
|
|
164
|
-
unrefTimer: config.unrefTimer
|
|
219
|
+
unrefTimer: config.unrefTimer ?? false
|
|
165
220
|
};
|
|
166
221
|
if (!config.disabled) {
|
|
167
222
|
this.initializeBatcher(config);
|
|
168
223
|
}
|
|
169
224
|
}
|
|
170
|
-
/** Initialize the batcher with transports */
|
|
171
225
|
initializeBatcher(config) {
|
|
172
226
|
const transports = config.transports ?? [];
|
|
173
|
-
if (config.apiKey && transports.length === 0) ;
|
|
174
227
|
if (transports.length > 0) {
|
|
228
|
+
const batcherConfig = {
|
|
229
|
+
batchSize: this.config.batchSize,
|
|
230
|
+
flushInterval: this.config.flushInterval,
|
|
231
|
+
maxRetries: this.config.maxRetries,
|
|
232
|
+
retryDelay: this.config.retryDelay,
|
|
233
|
+
unrefTimer: this.config.unrefTimer,
|
|
234
|
+
executionContext: this._context,
|
|
235
|
+
onError: config.onError,
|
|
236
|
+
onSuccess: config.onSuccess
|
|
237
|
+
};
|
|
175
238
|
this.batcher = new VedaTraceBatcher(
|
|
176
239
|
transports,
|
|
177
|
-
|
|
178
|
-
batchSize: this.config.batchSize,
|
|
179
|
-
flushInterval: this.config.flushInterval,
|
|
180
|
-
maxRetries: this.config.maxRetries,
|
|
181
|
-
retryDelay: this.config.retryDelay,
|
|
182
|
-
unrefTimer: this.config.unrefTimer
|
|
183
|
-
},
|
|
184
|
-
this.config.onError,
|
|
185
|
-
this.config.onSuccess,
|
|
240
|
+
batcherConfig,
|
|
186
241
|
this.config.immediateFlush
|
|
187
242
|
);
|
|
188
243
|
}
|
|
189
244
|
}
|
|
190
|
-
/** Set batcher (called from factory function) */
|
|
191
245
|
setBatcher(batcher) {
|
|
192
246
|
this.batcher = batcher;
|
|
193
247
|
}
|
|
194
|
-
/**
|
|
248
|
+
/** Attach execution context for waitUntil support (Cloudflare Workers / Pages) */
|
|
249
|
+
withContext(ctx) {
|
|
250
|
+
this._context = ctx;
|
|
251
|
+
if (this.batcher) {
|
|
252
|
+
this.batcher.setContext(ctx);
|
|
253
|
+
}
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
/** Check if context is attached */
|
|
257
|
+
hasContext() {
|
|
258
|
+
return this._context !== void 0;
|
|
259
|
+
}
|
|
260
|
+
/** Get current execution context */
|
|
261
|
+
getContext() {
|
|
262
|
+
return this._context;
|
|
263
|
+
}
|
|
195
264
|
debug(message, metadata) {
|
|
196
265
|
this.log("debug", message, metadata);
|
|
197
266
|
}
|
|
198
|
-
/** Log at info level */
|
|
199
267
|
info(message, metadata) {
|
|
200
268
|
this.log("info", message, metadata);
|
|
201
269
|
}
|
|
202
|
-
/** Log at warn level */
|
|
203
270
|
warn(message, metadata) {
|
|
204
271
|
this.log("warn", message, metadata);
|
|
205
272
|
}
|
|
206
|
-
/** Log at error level */
|
|
207
273
|
error(message, metadata) {
|
|
208
274
|
this.log("error", message, metadata);
|
|
209
275
|
}
|
|
210
|
-
/** Log at fatal level */
|
|
211
276
|
fatal(message, metadata) {
|
|
212
277
|
this.log("fatal", message, metadata);
|
|
213
278
|
}
|
|
214
|
-
/** Internal log method */
|
|
215
279
|
log(level, message, metadata) {
|
|
216
280
|
if (!this.batcher) {
|
|
217
281
|
return;
|
|
@@ -226,7 +290,7 @@ class VedaTraceLogger {
|
|
|
226
290
|
timestamp: Date.now(),
|
|
227
291
|
metadata: cleanMetadata,
|
|
228
292
|
_sdk: {
|
|
229
|
-
source:
|
|
293
|
+
source: detectRuntime(),
|
|
230
294
|
version: SDK_VERSION
|
|
231
295
|
}
|
|
232
296
|
};
|
|
@@ -239,7 +303,6 @@ class VedaTraceLogger {
|
|
|
239
303
|
}
|
|
240
304
|
this.batcher.add(logEntry);
|
|
241
305
|
}
|
|
242
|
-
/** Create a child logger with default metadata */
|
|
243
306
|
child(defaults) {
|
|
244
307
|
const mergedDefaults = { ...this.childDefaults, ...defaults };
|
|
245
308
|
const childLogger = new VedaTraceLogger(
|
|
@@ -248,7 +311,8 @@ class VedaTraceLogger {
|
|
|
248
311
|
apiKey: this.config.apiKey,
|
|
249
312
|
endpoint: this.config.endpoint,
|
|
250
313
|
environment: this.config.environment,
|
|
251
|
-
disabled: !this.batcher
|
|
314
|
+
disabled: !this.batcher,
|
|
315
|
+
executionContext: this._context
|
|
252
316
|
},
|
|
253
317
|
mergedDefaults
|
|
254
318
|
);
|
|
@@ -257,33 +321,132 @@ class VedaTraceLogger {
|
|
|
257
321
|
}
|
|
258
322
|
return childLogger;
|
|
259
323
|
}
|
|
260
|
-
/** Flush pending logs */
|
|
261
324
|
async flush() {
|
|
262
325
|
if (this.batcher) {
|
|
263
|
-
|
|
326
|
+
const flushPromise = this.batcher.flush();
|
|
327
|
+
if (this._context) {
|
|
328
|
+
this._context.waitUntil(flushPromise);
|
|
329
|
+
}
|
|
330
|
+
return flushPromise;
|
|
264
331
|
}
|
|
265
332
|
}
|
|
266
|
-
/** Stop the batcher and flush timer */
|
|
267
333
|
stop() {
|
|
268
334
|
if (this.batcher) {
|
|
269
335
|
this.batcher.stop();
|
|
270
336
|
}
|
|
271
337
|
}
|
|
272
|
-
/** Start the flush timer (for manual control in edge runtimes) */
|
|
273
338
|
start() {
|
|
274
339
|
if (this.batcher) {
|
|
275
340
|
this.batcher.start();
|
|
276
341
|
}
|
|
277
342
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
class BrowserLifecycle {
|
|
346
|
+
constructor(config) {
|
|
347
|
+
this.config = config;
|
|
348
|
+
this.boundVisibilityHandler = this.handleVisibilityChange.bind(this);
|
|
349
|
+
this.boundPageHideHandler = this.handlePageHide.bind(this);
|
|
350
|
+
this.boundBeforeUnloadHandler = this.handleBeforeUnload.bind(this);
|
|
351
|
+
this.boundUnloadHandler = this.handleUnload.bind(this);
|
|
352
|
+
}
|
|
353
|
+
boundVisibilityHandler;
|
|
354
|
+
boundPageHideHandler;
|
|
355
|
+
boundBeforeUnloadHandler;
|
|
356
|
+
boundUnloadHandler;
|
|
357
|
+
isAttached = false;
|
|
358
|
+
pendingFlush = null;
|
|
359
|
+
/** Start listening for browser lifecycle events */
|
|
360
|
+
attach() {
|
|
361
|
+
if (this.isAttached) return;
|
|
362
|
+
if (typeof document !== "undefined") {
|
|
363
|
+
document.addEventListener("visibilitychange", this.boundVisibilityHandler);
|
|
364
|
+
window.addEventListener("pagehide", this.boundPageHideHandler);
|
|
365
|
+
window.addEventListener("beforeunload", this.boundBeforeUnloadHandler);
|
|
366
|
+
window.addEventListener("unload", this.boundUnloadHandler);
|
|
367
|
+
}
|
|
368
|
+
this.isAttached = true;
|
|
369
|
+
if (this.config.debug) {
|
|
370
|
+
console.log("[VedaTrace] Browser lifecycle handlers attached");
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/** Stop listening for browser lifecycle events */
|
|
374
|
+
detach() {
|
|
375
|
+
if (!this.isAttached) return;
|
|
376
|
+
if (typeof document !== "undefined") {
|
|
377
|
+
document.removeEventListener(
|
|
378
|
+
"visibilitychange",
|
|
379
|
+
this.boundVisibilityHandler
|
|
380
|
+
);
|
|
381
|
+
window.removeEventListener("pagehide", this.boundPageHideHandler);
|
|
382
|
+
window.removeEventListener("beforeunload", this.boundBeforeUnloadHandler);
|
|
383
|
+
window.removeEventListener("unload", this.boundUnloadHandler);
|
|
282
384
|
}
|
|
283
|
-
|
|
284
|
-
|
|
385
|
+
this.isAttached = false;
|
|
386
|
+
if (this.config.debug) {
|
|
387
|
+
console.log("[VedaTrace] Browser lifecycle handlers detached");
|
|
285
388
|
}
|
|
286
|
-
|
|
389
|
+
}
|
|
390
|
+
/** Handle visibility change - flush when page becomes hidden */
|
|
391
|
+
handleVisibilityChange() {
|
|
392
|
+
if (document.visibilityState === "hidden") {
|
|
393
|
+
if (this.config.debug) {
|
|
394
|
+
console.log("[VedaTrace] Page became hidden, flushing logs");
|
|
395
|
+
}
|
|
396
|
+
this.scheduleFlush();
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
/** Handle pagehide event - primary flush handler for Safari */
|
|
400
|
+
handlePageHide(event) {
|
|
401
|
+
if (this.config.debug) {
|
|
402
|
+
console.log(
|
|
403
|
+
"[VedaTrace] Page hide event",
|
|
404
|
+
event.persisted ? "(cached)" : "(navigation)"
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
if (event.persisted) {
|
|
408
|
+
this.scheduleFlush();
|
|
409
|
+
} else {
|
|
410
|
+
this.finalFlush();
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/** Handle beforeunload - backup flush mechanism */
|
|
414
|
+
handleBeforeUnload(event) {
|
|
415
|
+
if (this.config.debug) {
|
|
416
|
+
console.log("[VedaTrace] Before unload event");
|
|
417
|
+
}
|
|
418
|
+
this.finalFlush();
|
|
419
|
+
}
|
|
420
|
+
/** Handle unload - fallback for older browsers */
|
|
421
|
+
handleUnload() {
|
|
422
|
+
if (this.config.debug) {
|
|
423
|
+
console.log("[VedaTrace] Unload event");
|
|
424
|
+
}
|
|
425
|
+
this.finalFlush();
|
|
426
|
+
}
|
|
427
|
+
/** Schedule a debounced flush (for visibility change) */
|
|
428
|
+
scheduleFlush() {
|
|
429
|
+
if (this.pendingFlush) return;
|
|
430
|
+
this.pendingFlush = this.config.flush().finally(() => {
|
|
431
|
+
this.pendingFlush = null;
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Final flush using keepalive fetch
|
|
436
|
+
* For sending logs after the page context is destroyed
|
|
437
|
+
*/
|
|
438
|
+
finalFlush() {
|
|
439
|
+
for (const transport of this.config.transports) {
|
|
440
|
+
if (transport.name === "http" && "flush" in transport) {
|
|
441
|
+
transport.flush?.();
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
this.config.flush().catch(() => {
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
/** Check if handlers are attached */
|
|
448
|
+
isActive() {
|
|
449
|
+
return this.isAttached;
|
|
287
450
|
}
|
|
288
451
|
}
|
|
289
452
|
|
|
@@ -361,31 +524,55 @@ function redactPii(value, mask) {
|
|
|
361
524
|
return result;
|
|
362
525
|
}
|
|
363
526
|
|
|
527
|
+
const RUNTIME_FLUSH_INTERVALS = {
|
|
528
|
+
node: 3e3,
|
|
529
|
+
bun: 3e3,
|
|
530
|
+
deno: 3e3,
|
|
531
|
+
browser: 3e3,
|
|
532
|
+
cloudflare: 1e3,
|
|
533
|
+
edge: 1e3
|
|
534
|
+
};
|
|
364
535
|
function vedatrace(config = {}) {
|
|
536
|
+
const runtime = detectRuntime();
|
|
365
537
|
const logger = new VedaTraceLogger(config);
|
|
366
|
-
const isEdge = isEdgeRuntime();
|
|
367
|
-
const shouldImmediateFlush = config.immediateFlush ?? isEdge;
|
|
368
|
-
const shouldAutoStart = config.autoStart ?? !isEdge;
|
|
369
538
|
if (config.apiKey && (!config.transports || config.transports.length === 0)) {
|
|
370
|
-
const
|
|
539
|
+
const isBrowserEnv = isBrowser();
|
|
540
|
+
const isServerlessEnv = isServerless();
|
|
541
|
+
const isLongRunningEnv = isLongRunning();
|
|
542
|
+
const HttpTransport = isBrowserEnv ? VedaTraceHttpTransportBrowser : VedaTraceHttpTransport;
|
|
543
|
+
const httpConfig = {
|
|
544
|
+
apiKey: config.apiKey,
|
|
545
|
+
keepalive: isBrowserEnv
|
|
546
|
+
};
|
|
371
547
|
if (config.endpoint) httpConfig.endpoint = config.endpoint;
|
|
372
|
-
const httpTransport = new
|
|
548
|
+
const httpTransport = new HttpTransport(httpConfig);
|
|
549
|
+
let immediateFlush = config.immediateFlush ?? false;
|
|
550
|
+
let shouldUnrefTimer = false;
|
|
551
|
+
if (isServerlessEnv) {
|
|
552
|
+
immediateFlush = config.immediateFlush ?? !config.executionContext;
|
|
553
|
+
} else if (isLongRunningEnv) {
|
|
554
|
+
immediateFlush = config.immediateFlush ?? false;
|
|
555
|
+
shouldUnrefTimer = true;
|
|
556
|
+
} else if (isBrowserEnv) {
|
|
557
|
+
immediateFlush = config.immediateFlush ?? false;
|
|
558
|
+
}
|
|
559
|
+
const flushInterval = config.flushInterval ?? RUNTIME_FLUSH_INTERVALS[runtime] ?? 3e3;
|
|
373
560
|
const batcher = new VedaTraceBatcher(
|
|
374
561
|
[httpTransport],
|
|
375
562
|
{
|
|
376
563
|
batchSize: config.batchSize ?? 100,
|
|
377
|
-
flushInterval
|
|
564
|
+
flushInterval,
|
|
378
565
|
maxRetries: config.maxRetries ?? 3,
|
|
379
566
|
retryDelay: config.retryDelay ?? 1e3,
|
|
380
|
-
unrefTimer: config.unrefTimer
|
|
567
|
+
unrefTimer: config.unrefTimer ?? shouldUnrefTimer,
|
|
568
|
+
executionContext: config.executionContext,
|
|
569
|
+
onError: config.onError,
|
|
570
|
+
onSuccess: config.onSuccess
|
|
381
571
|
},
|
|
382
|
-
|
|
383
|
-
config.onSuccess,
|
|
384
|
-
shouldImmediateFlush,
|
|
385
|
-
shouldAutoStart
|
|
572
|
+
immediateFlush
|
|
386
573
|
);
|
|
387
574
|
logger.setBatcher(batcher);
|
|
388
|
-
if (typeof process !== "undefined") {
|
|
575
|
+
if (typeof process !== "undefined" && isLongRunningEnv) {
|
|
389
576
|
const flushLogs = async () => {
|
|
390
577
|
await batcher.flush();
|
|
391
578
|
};
|
|
@@ -393,6 +580,15 @@ function vedatrace(config = {}) {
|
|
|
393
580
|
process.on("SIGTERM", flushLogs);
|
|
394
581
|
process.on("SIGINT", flushLogs);
|
|
395
582
|
}
|
|
583
|
+
if (isBrowserEnv) {
|
|
584
|
+
const lifecycle = new BrowserLifecycle({
|
|
585
|
+
transports: [httpTransport],
|
|
586
|
+
flush: () => batcher.flush(),
|
|
587
|
+
debug: config.debug
|
|
588
|
+
});
|
|
589
|
+
lifecycle.attach();
|
|
590
|
+
logger._lifecycle = lifecycle;
|
|
591
|
+
}
|
|
396
592
|
}
|
|
397
593
|
return logger;
|
|
398
594
|
}
|
|
@@ -409,4 +605,4 @@ function devVedatrace(config = {}) {
|
|
|
409
605
|
});
|
|
410
606
|
}
|
|
411
607
|
|
|
412
|
-
export { VedaTraceBatcher, VedaTraceConsoleTransport, VedaTraceHttpTransport, VedaTraceLogger, vedatrace as default, detectRuntime, devVedatrace, isEdgeRuntime, redact, vedatrace };
|
|
608
|
+
export { BrowserLifecycle, VedaTraceBatcher, VedaTraceConsoleTransport, VedaTraceHttpTransport, VedaTraceHttpTransportBrowser, VedaTraceLogger, vedatrace as default, detectRuntime, devVedatrace, isBrowser, isEdgeRuntime, isLongRunning, isServerless, redact, vedatrace };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
import { V as VedaTraceLoggerInterface, a as VedaTraceConfig, L as LogMetadata } from '../types-
|
|
2
|
+
import { V as VedaTraceLoggerInterface, a as VedaTraceConfig, L as LogMetadata } from '../types-BU0UESs9.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Express.js middleware integration for VedaTrace
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
import { V as VedaTraceLoggerInterface, a as VedaTraceConfig, L as LogMetadata } from '../types-
|
|
2
|
+
import { V as VedaTraceLoggerInterface, a as VedaTraceConfig, L as LogMetadata } from '../types-BU0UESs9.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Express.js middleware integration for VedaTrace
|
|
@@ -39,10 +39,9 @@ function useVedaTrace() {
|
|
|
39
39
|
function useVedaTraceChild(metadata) {
|
|
40
40
|
const parentLogger = useVedaTrace();
|
|
41
41
|
const metadataRef = react.useRef(metadata);
|
|
42
|
-
|
|
42
|
+
return react.useMemo(() => {
|
|
43
43
|
return parentLogger.child(metadataRef.current);
|
|
44
44
|
}, [parentLogger]);
|
|
45
|
-
return childLogger;
|
|
46
45
|
}
|
|
47
46
|
function useVedaTraceLifecycle(componentName, metadata) {
|
|
48
47
|
const logger = useVedaTrace();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ReactNode, ReactElement } from 'react';
|
|
2
|
-
import { a as VedaTraceConfig, V as VedaTraceLoggerInterface, L as LogMetadata } from '../types-
|
|
2
|
+
import { a as VedaTraceConfig, V as VedaTraceLoggerInterface, L as LogMetadata } from '../types-BU0UESs9.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* React integration for VedaTrace
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ReactNode, ReactElement } from 'react';
|
|
2
|
-
import { a as VedaTraceConfig, V as VedaTraceLoggerInterface, L as LogMetadata } from '../types-
|
|
2
|
+
import { a as VedaTraceConfig, V as VedaTraceLoggerInterface, L as LogMetadata } from '../types-BU0UESs9.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* React integration for VedaTrace
|
|
@@ -37,10 +37,9 @@ function useVedaTrace() {
|
|
|
37
37
|
function useVedaTraceChild(metadata) {
|
|
38
38
|
const parentLogger = useVedaTrace();
|
|
39
39
|
const metadataRef = useRef(metadata);
|
|
40
|
-
|
|
40
|
+
return useMemo(() => {
|
|
41
41
|
return parentLogger.child(metadataRef.current);
|
|
42
42
|
}, [parentLogger]);
|
|
43
|
-
return childLogger;
|
|
44
43
|
}
|
|
45
44
|
function useVedaTraceLifecycle(componentName, metadata) {
|
|
46
45
|
const logger = useVedaTrace();
|