taist 0.1.14 → 0.1.15
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/lib/trace-context.js +19 -6
- package/package.json +1 -1
package/lib/trace-context.js
CHANGED
|
@@ -5,22 +5,35 @@
|
|
|
5
5
|
* across function calls without explicit parameter passing.
|
|
6
6
|
*
|
|
7
7
|
* This is the same approach used by OpenTelemetry, Datadog, and other APM tools.
|
|
8
|
+
*
|
|
9
|
+
* IMPORTANT: Uses globalThis to share context across bundled modules.
|
|
10
|
+
* Without this, each bundle would have its own AsyncLocalStorage instance,
|
|
11
|
+
* breaking context propagation across bundle boundaries.
|
|
8
12
|
*/
|
|
9
13
|
|
|
10
14
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
11
15
|
|
|
12
|
-
//
|
|
13
|
-
|
|
16
|
+
// Use globalThis to share context across all bundles/modules
|
|
17
|
+
// This ensures trace context propagates even when code is bundled separately
|
|
18
|
+
const TAIST_CONTEXT_KEY = '__taist_trace_context__';
|
|
19
|
+
const TAIST_COUNTER_KEY = '__taist_id_counter__';
|
|
20
|
+
|
|
21
|
+
if (!globalThis[TAIST_CONTEXT_KEY]) {
|
|
22
|
+
globalThis[TAIST_CONTEXT_KEY] = new AsyncLocalStorage();
|
|
23
|
+
}
|
|
24
|
+
if (globalThis[TAIST_COUNTER_KEY] === undefined) {
|
|
25
|
+
globalThis[TAIST_COUNTER_KEY] = 0;
|
|
26
|
+
}
|
|
14
27
|
|
|
15
|
-
//
|
|
16
|
-
|
|
28
|
+
// Global storage for trace context (shared via globalThis)
|
|
29
|
+
export const traceContext = globalThis[TAIST_CONTEXT_KEY];
|
|
17
30
|
|
|
18
31
|
/**
|
|
19
32
|
* Generate a unique trace/span ID
|
|
20
33
|
* @returns {string} Unique identifier
|
|
21
34
|
*/
|
|
22
35
|
export function generateId() {
|
|
23
|
-
return `__${++
|
|
36
|
+
return `__${++globalThis[TAIST_COUNTER_KEY]}_${Date.now()}`;
|
|
24
37
|
}
|
|
25
38
|
|
|
26
39
|
/**
|
|
@@ -76,5 +89,5 @@ export function startTrace(fn) {
|
|
|
76
89
|
* Reset the ID counter (for testing)
|
|
77
90
|
*/
|
|
78
91
|
export function resetIdCounter() {
|
|
79
|
-
|
|
92
|
+
globalThis[TAIST_COUNTER_KEY] = 0;
|
|
80
93
|
}
|