Reference/SDK/TypeScript/Interfaces

Interface: Span

A Span encapsulates logged data and metrics for a unit of work. This interface is shared by all span implementations.

We suggest using one of the various traced methods, instead of creating Spans directly. See Span.traced for full details.

Hierarchy

Implemented by

Methods

close

close(args?): number

Alias for end.

Parameters

NameType
args?EndSpanArgs

Returns

number


end

end(args?): number

Log an end time to the span (defaults to the current time). Returns the logged time.

Will be invoked automatically if the span is constructed with traced.

Parameters

NameType
args?EndSpanArgs

Returns

number

The end time logged to the span metrics.


export

export(): Promise<string>

Serialize the identifiers of this span. The return value can be used to identify this span when starting a subspan elsewhere, such as another process or service, without needing to access this Span object. See the parameters of Span.startSpan for usage details.

Callers should treat the return value as opaque. The serialization format may change from time to time. If parsing is needed, use SpanComponentsV3.fromStr.

Returns

Promise<string>

Serialized representation of this span's identifiers.

Overrides

Exportable.export


flush

flush(): Promise<void>

Flush any pending rows to the server.

Returns

Promise<void>


log

log(event): void

Incrementally update the current span with new data. The event will be batched and uploaded behind the scenes.

Parameters

Returns

void


logFeedback

logFeedback(event): void

Add feedback to the current span. Unlike Experiment.logFeedback and Logger.logFeedback, this method does not accept an id parameter, because it logs feedback to the current span.

Parameters

NameType
eventOmit<LogFeedbackFullArgs, "id">

Returns

void


permalink(): Promise<string>

Format a permalink to the Braintrust application for viewing this span.

Links can be generated at any time, but they will only become viewable after the span and its root have been flushed to the server and ingested.

Returns

Promise<string>

A permalink to the span.


setAttributes

setAttributes(args): void

Set the span's name, type, or other attributes after it's created.

Parameters

NameType
argsOmit<StartSpanArgs, "event">

Returns

void


startSpan

startSpan(args?): Span

Lower-level alternative to traced. This allows you to start a span yourself, and can be useful in situations where you cannot use callbacks. However, spans started with startSpan will not be marked as the "current span", so currentSpan() and traced() will be no-ops. If you want to mark a span as current, use traced instead.

See Span.traced for full details.

Parameters

NameType
args?StartSpanArgs

Returns

Span

The newly-created Span


traced

traced<R>(callback, args?): R

Create a new span and run the provided callback. This is useful if you want to log more detailed trace information beyond the scope of a single log event. Data logged over several calls to Span.log will be merged into one logical row.

Spans created within traced are ended automatically. By default, the span is marked as current, so they can be accessed using braintrust.currentSpan.

Type parameters

Name
R

Parameters

NameTypeDescription
callback(span: Span) => RThe function to be run under the span context.
args?StartSpanArgs & SetCurrentArg-

Returns

R

The result of running callback.

Properties

id

id: string

Row ID of the span.


kind

kind: "span"