@xylabs/telemetry 4.9.13 → 4.9.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/dist/neutral/index.mjs +21 -4
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/types/span.d.ts +2 -1
- package/dist/types/span.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/span.ts +30 -8
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
// src/span.ts
|
|
2
2
|
import {
|
|
3
3
|
context,
|
|
4
|
+
propagation,
|
|
5
|
+
ROOT_CONTEXT,
|
|
4
6
|
trace
|
|
5
7
|
} from "@opentelemetry/api";
|
|
8
|
+
function cloneContextWithoutSpan(activeCtx, configKeys = []) {
|
|
9
|
+
let newCtx = ROOT_CONTEXT;
|
|
10
|
+
const baggage = propagation.getBaggage(activeCtx);
|
|
11
|
+
if (baggage) {
|
|
12
|
+
newCtx = propagation.setBaggage(newCtx, baggage);
|
|
13
|
+
}
|
|
14
|
+
for (const key of configKeys) {
|
|
15
|
+
const value = activeCtx.getValue(key);
|
|
16
|
+
if (value !== void 0) {
|
|
17
|
+
newCtx = newCtx.setValue(key, value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return newCtx;
|
|
21
|
+
}
|
|
6
22
|
function span(name, fn, tracer) {
|
|
7
23
|
if (tracer) {
|
|
8
24
|
const span2 = tracer.startSpan(name);
|
|
@@ -20,9 +36,9 @@ function span(name, fn, tracer) {
|
|
|
20
36
|
function spanRoot(name, fn, tracer) {
|
|
21
37
|
if (tracer) {
|
|
22
38
|
const activeContext = context.active();
|
|
23
|
-
const noSpanContext =
|
|
39
|
+
const noSpanContext = cloneContextWithoutSpan(activeContext);
|
|
24
40
|
const span2 = tracer.startSpan(name, {}, noSpanContext);
|
|
25
|
-
return context.with(trace.setSpan(
|
|
41
|
+
return context.with(trace.setSpan(noSpanContext, span2), () => {
|
|
26
42
|
try {
|
|
27
43
|
return fn();
|
|
28
44
|
} finally {
|
|
@@ -50,9 +66,9 @@ async function spanAsync(name, fn, tracer) {
|
|
|
50
66
|
async function spanRootAsync(name, fn, tracer) {
|
|
51
67
|
if (tracer) {
|
|
52
68
|
const activeContext = context.active();
|
|
53
|
-
const noSpanContext =
|
|
69
|
+
const noSpanContext = cloneContextWithoutSpan(activeContext);
|
|
54
70
|
const span2 = tracer.startSpan(name, {}, noSpanContext);
|
|
55
|
-
return await context.with(trace.setSpan(
|
|
71
|
+
return await context.with(trace.setSpan(noSpanContext, span2), async () => {
|
|
56
72
|
try {
|
|
57
73
|
return await fn();
|
|
58
74
|
} finally {
|
|
@@ -64,6 +80,7 @@ async function spanRootAsync(name, fn, tracer) {
|
|
|
64
80
|
}
|
|
65
81
|
}
|
|
66
82
|
export {
|
|
83
|
+
cloneContextWithoutSpan,
|
|
67
84
|
span,
|
|
68
85
|
spanAsync,
|
|
69
86
|
spanRoot,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/span.ts"],"sourcesContent":["import {\n
|
|
1
|
+
{"version":3,"sources":["../../src/span.ts"],"sourcesContent":["import type {\n Context,\n Tracer,\n} from '@opentelemetry/api'\nimport {\n context, propagation, ROOT_CONTEXT, trace,\n} from '@opentelemetry/api'\n\nexport function cloneContextWithoutSpan(activeCtx: Context, configKeys: symbol[] = []): Context {\n // Start from root to ensure no span is propagated\n let newCtx = ROOT_CONTEXT\n\n // Copy baggage\n const baggage = propagation.getBaggage(activeCtx)\n if (baggage) {\n newCtx = propagation.setBaggage(newCtx, baggage)\n }\n\n // Copy custom config keys\n for (const key of configKeys) {\n const value = activeCtx.getValue(key)\n if (value !== undefined) {\n newCtx = newCtx.setValue(key, value)\n }\n }\n\n return newCtx\n}\n\nexport function span<T>(name: string, fn: () => T, tracer?: Tracer): T {\n if (tracer) {\n const span = tracer.startSpan(name)\n return context.with(trace.setSpan(context.active(), span), () => {\n try {\n return fn()\n } finally {\n span.end()\n }\n })\n } else {\n return fn()\n }\n}\n\nexport function spanRoot<T>(name: string, fn: () => T, tracer?: Tracer): T {\n if (tracer) {\n // Get current active context for configuration\n const activeContext = context.active()\n\n // Create a new context with no active span\n const noSpanContext = cloneContextWithoutSpan(activeContext)\n\n // Create a new span in the context without an active span\n const span = tracer.startSpan(name, {}, noSpanContext)\n\n // Use the active context but replace its span with our new root span\n return context.with(trace.setSpan(noSpanContext, span), () => {\n try {\n return fn()\n } finally {\n span.end()\n }\n })\n } else {\n return fn()\n }\n}\n\nexport async function spanAsync<T>(\n name: string,\n fn: () => Promise<T>,\n tracer?: Tracer,\n): Promise<T> {\n if (tracer) {\n const span = tracer.startSpan(name)\n return await context.with(trace.setSpan(context.active(), span), async () => {\n try {\n return await fn()\n } finally {\n span.end()\n }\n })\n } else {\n return await fn()\n }\n}\n\nexport async function spanRootAsync<T>(\n name: string,\n fn: () => Promise<T>,\n tracer?: Tracer,\n): Promise<T> {\n if (tracer) {\n const activeContext = context.active()\n\n const noSpanContext = cloneContextWithoutSpan(activeContext)\n\n // Create a new span in the context without an active span\n const span = tracer.startSpan(name, {}, noSpanContext)\n\n // Use the active context but replace its span with our new root span\n return await context.with(trace.setSpan(noSpanContext, span), async () => {\n try {\n return await fn()\n } finally {\n span.end()\n }\n })\n } else {\n return await fn()\n }\n}\n"],"mappings":";AAIA;AAAA,EACE;AAAA,EAAS;AAAA,EAAa;AAAA,EAAc;AAAA,OAC/B;AAEA,SAAS,wBAAwB,WAAoB,aAAuB,CAAC,GAAY;AAE9F,MAAI,SAAS;AAGb,QAAM,UAAU,YAAY,WAAW,SAAS;AAChD,MAAI,SAAS;AACX,aAAS,YAAY,WAAW,QAAQ,OAAO;AAAA,EACjD;AAGA,aAAW,OAAO,YAAY;AAC5B,UAAM,QAAQ,UAAU,SAAS,GAAG;AACpC,QAAI,UAAU,QAAW;AACvB,eAAS,OAAO,SAAS,KAAK,KAAK;AAAA,IACrC;AAAA,EACF;AAEA,SAAO;AACT;AAEO,SA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AO,MAAM,QAAQ,KAAK,MAAM,QAAQ,eAAeA,KAAI,GAAG,YAAY;AACxE,UAAI;AACF,eAAO,MAAM,GAAG;AAAA,MAClB,UAAE;AACA,QAAAA,MAAK,IAAI;AAAA,MACX;AAAA,IACF,CAAC;AAAA,EACH,OAAO;AACL,WAAO,MAAM,GAAG;AAAA,EAClB;AACF;","names":["span"]}
|
package/dist/types/span.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Context, Tracer } from '@opentelemetry/api';
|
|
2
|
+
export declare function cloneContextWithoutSpan(activeCtx: Context, configKeys?: symbol[]): Context;
|
|
2
3
|
export declare function span<T>(name: string, fn: () => T, tracer?: Tracer): T;
|
|
3
4
|
export declare function spanRoot<T>(name: string, fn: () => T, tracer?: Tracer): T;
|
|
4
5
|
export declare function spanAsync<T>(name: string, fn: () => Promise<T>, tracer?: Tracer): Promise<T>;
|
package/dist/types/span.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"span.d.ts","sourceRoot":"","sources":["../../src/span.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"span.d.ts","sourceRoot":"","sources":["../../src/span.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,MAAM,EACP,MAAM,oBAAoB,CAAA;AAK3B,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,OAAO,EAAE,UAAU,GAAE,MAAM,EAAO,GAAG,OAAO,CAmB9F;AAED,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAarE;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAsBzE;AAED,wBAAsB,SAAS,CAAC,CAAC,EAC/B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,CAAC,CAaZ;AAED,wBAAsB,aAAa,CAAC,CAAC,EACnC,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,CAAC,CAoBZ"}
|
package/package.json
CHANGED
package/src/span.ts
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Context,
|
|
3
|
+
Tracer,
|
|
4
|
+
} from '@opentelemetry/api'
|
|
1
5
|
import {
|
|
2
|
-
context,
|
|
3
|
-
trace, type Tracer,
|
|
6
|
+
context, propagation, ROOT_CONTEXT, trace,
|
|
4
7
|
} from '@opentelemetry/api'
|
|
5
8
|
|
|
9
|
+
export function cloneContextWithoutSpan(activeCtx: Context, configKeys: symbol[] = []): Context {
|
|
10
|
+
// Start from root to ensure no span is propagated
|
|
11
|
+
let newCtx = ROOT_CONTEXT
|
|
12
|
+
|
|
13
|
+
// Copy baggage
|
|
14
|
+
const baggage = propagation.getBaggage(activeCtx)
|
|
15
|
+
if (baggage) {
|
|
16
|
+
newCtx = propagation.setBaggage(newCtx, baggage)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Copy custom config keys
|
|
20
|
+
for (const key of configKeys) {
|
|
21
|
+
const value = activeCtx.getValue(key)
|
|
22
|
+
if (value !== undefined) {
|
|
23
|
+
newCtx = newCtx.setValue(key, value)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return newCtx
|
|
28
|
+
}
|
|
29
|
+
|
|
6
30
|
export function span<T>(name: string, fn: () => T, tracer?: Tracer): T {
|
|
7
31
|
if (tracer) {
|
|
8
32
|
const span = tracer.startSpan(name)
|
|
@@ -24,13 +48,13 @@ export function spanRoot<T>(name: string, fn: () => T, tracer?: Tracer): T {
|
|
|
24
48
|
const activeContext = context.active()
|
|
25
49
|
|
|
26
50
|
// Create a new context with no active span
|
|
27
|
-
const noSpanContext =
|
|
51
|
+
const noSpanContext = cloneContextWithoutSpan(activeContext)
|
|
28
52
|
|
|
29
53
|
// Create a new span in the context without an active span
|
|
30
54
|
const span = tracer.startSpan(name, {}, noSpanContext)
|
|
31
55
|
|
|
32
56
|
// Use the active context but replace its span with our new root span
|
|
33
|
-
return context.with(trace.setSpan(
|
|
57
|
+
return context.with(trace.setSpan(noSpanContext, span), () => {
|
|
34
58
|
try {
|
|
35
59
|
return fn()
|
|
36
60
|
} finally {
|
|
@@ -67,17 +91,15 @@ export async function spanRootAsync<T>(
|
|
|
67
91
|
tracer?: Tracer,
|
|
68
92
|
): Promise<T> {
|
|
69
93
|
if (tracer) {
|
|
70
|
-
// Get current active context for configuration
|
|
71
94
|
const activeContext = context.active()
|
|
72
95
|
|
|
73
|
-
|
|
74
|
-
const noSpanContext = trace.deleteSpan(activeContext)
|
|
96
|
+
const noSpanContext = cloneContextWithoutSpan(activeContext)
|
|
75
97
|
|
|
76
98
|
// Create a new span in the context without an active span
|
|
77
99
|
const span = tracer.startSpan(name, {}, noSpanContext)
|
|
78
100
|
|
|
79
101
|
// Use the active context but replace its span with our new root span
|
|
80
|
-
return await context.with(trace.setSpan(
|
|
102
|
+
return await context.with(trace.setSpan(noSpanContext, span), async () => {
|
|
81
103
|
try {
|
|
82
104
|
return await fn()
|
|
83
105
|
} finally {
|