Skip to content

Connectum API Reference / @connectum/otel / shared / wrapAsyncIterable

Function: wrapAsyncIterable() ​

wrapAsyncIterable<T>(iterable, span, direction, recordMessages, endSpanOnComplete?): AsyncGenerator<T>

Defined in: shared.ts:84

Wraps an AsyncIterable to track streaming messages with OTel span events.

Captures the span via closure (not AsyncLocalStorage) to avoid the Node.js ALS context loss in async generators (nodejs/node#42237). Recording events this way does not make the span the active span for the code that produces the messages; use scopeAsyncIterable for that.

When endSpanOnComplete is true, the span lifecycle is managed by the generator itself: the span is ended in the finally block, which runs on normal completion, error, or early break (generator.return()).

Type Parameters ​

T ​

T

Parameters ​

iterable ​

AsyncIterable<T>

The source async iterable (streaming messages)

span ​

Span

The OTel span to record events on

direction ​

"SENT" | "RECEIVED"

'SENT' for outgoing, 'RECEIVED' for incoming messages

recordMessages ​

boolean

Whether to record individual message events

endSpanOnComplete? ​

boolean = false

Whether to end the span when the stream completes

Returns ​

AsyncGenerator<T>

A new AsyncGenerator that yields the same messages with span events