Class: SpanContext
Prefactor TypeScript SDK / @prefactor/core / SpanContext
Class: SpanContext
Section titled “Class: SpanContext”Defined in: packages/core/src/tracing/context.ts:33
SpanContext manages the current span in async execution contexts. This enables automatic parent-child span relationships without manual tracking.
Uses Node.js AsyncLocalStorage which provides async-safe context propagation.
Example
Section titled “Example”const span = tracer.startSpan({ name: 'parent', ... });
await SpanContext.runAsync(span, async () => { // Inside this function, getCurrent() returns the parent span const parent = SpanContext.getCurrent();
const child = tracer.startSpan({ name: 'child', parentSpanId: parent?.spanId, traceId: parent?.traceId, }); // ...});Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new SpanContext():
SpanContext
Returns
Section titled “Returns”SpanContext
Methods
Section titled “Methods”getCurrent()
Section titled “getCurrent()”
staticgetCurrent():Span|undefined
Defined in: packages/core/src/tracing/context.ts:39
Get the current span from the async context
Returns
Section titled “Returns”Span | undefined
The current span, or undefined if no span is active
getStack()
Section titled “getStack()”
staticgetStack():Span[]
Defined in: packages/core/src/tracing/context.ts:47
Get the full span stack from the async context
Returns
Section titled “Returns”Span[]
enter()
Section titled “enter()”
staticenter(span):void
Defined in: packages/core/src/tracing/context.ts:54
Push a span onto the stack for the current async context
Parameters
Section titled “Parameters”Returns
Section titled “Returns”void
exit()
Section titled “exit()”
staticexit():void
Defined in: packages/core/src/tracing/context.ts:62
Pop the current span from the stack for the current async context
Returns
Section titled “Returns”void
staticrun<T>(span,fn):T
Defined in: packages/core/src/tracing/context.ts:75
Run a synchronous function with the given span as the current context
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”The span to set as current
() => T
The function to execute
Returns
Section titled “Returns”T
The return value of the function
runAsync()
Section titled “runAsync()”
staticrunAsync<T>(span,fn):Promise<T>
Defined in: packages/core/src/tracing/context.ts:93
Run an asynchronous function with the given span as the current context
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”The span to set as current
() => Promise<T>
The async function to execute
Returns
Section titled “Returns”Promise<T>
A promise resolving to the return value of the function
clear()
Section titled “clear()”
staticclear():void
Defined in: packages/core/src/tracing/context.ts:107
Clear the current context (primarily for testing)
Returns
Section titled “Returns”void