Skip to content

Class: SpanContext

Prefactor TypeScript SDK


Prefactor TypeScript SDK / @prefactor/core / 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.

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,
});
// ...
});

new SpanContext(): SpanContext

SpanContext

static getCurrent(): Span | undefined

Defined in: packages/core/src/tracing/context.ts:39

Get the current span from the async context

Span | undefined

The current span, or undefined if no span is active


static getStack(): Span[]

Defined in: packages/core/src/tracing/context.ts:47

Get the full span stack from the async context

Span[]


static enter(span): void

Defined in: packages/core/src/tracing/context.ts:54

Push a span onto the stack for the current async context

Span

void


static exit(): void

Defined in: packages/core/src/tracing/context.ts:62

Pop the current span from the stack for the current async context

void


static run<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

T

Span

The span to set as current

() => T

The function to execute

T

The return value of the function


static runAsync<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

T

Span

The span to set as current

() => Promise<T>

The async function to execute

Promise<T>

A promise resolving to the return value of the function


static clear(): void

Defined in: packages/core/src/tracing/context.ts:107

Clear the current context (primarily for testing)

void