1- import type { IsoChannelHandlers , IsoTracingChannel } from "../../isomorph" ;
2- import { startSpan } from "../../logger" ;
1+ import type {
2+ IsoAsyncLocalStorage ,
3+ IsoChannelHandlers ,
4+ IsoTracingChannel ,
5+ } from "../../isomorph" ;
6+ import {
7+ _internalGetGlobalState ,
8+ BRAINTRUST_CURRENT_SPAN_STORE ,
9+ startSpan ,
10+ } from "../../logger" ;
311import type { Span } from "../../logger" ;
412import { getCurrentUnixTimestamp , isObject } from "../../util" ;
513import type {
@@ -196,6 +204,81 @@ function startSpanForEvent<
196204 return { span, startTime } ;
197205}
198206
207+ function ensureSpanStateForEvent <
208+ TChannel extends AnyAsyncChannel | AnySyncStreamChannel ,
209+ > (
210+ states : WeakMap < object , SpanState > ,
211+ config : ChannelConfig & {
212+ extractInput : (
213+ args : [ ...ArgsOf < TChannel > ] ,
214+ event : StartOf < TChannel > ,
215+ span : Span ,
216+ ) => {
217+ input : unknown ;
218+ metadata : unknown ;
219+ } ;
220+ } ,
221+ event : StartOf < TChannel > ,
222+ channelName : string ,
223+ ) : SpanState {
224+ const key = event as object ;
225+ const existing = states . get ( key ) ;
226+ if ( existing ) {
227+ return existing ;
228+ }
229+
230+ const created = startSpanForEvent < TChannel > ( config , event , channelName ) ;
231+ states . set ( key , created ) ;
232+ return created ;
233+ }
234+
235+ function bindCurrentSpanStoreToStart <
236+ TChannel extends AnyAsyncChannel | AnySyncStreamChannel ,
237+ > (
238+ tracingChannel : IsoTracingChannel < ChannelMessage < TChannel > > ,
239+ states : WeakMap < object , SpanState > ,
240+ config : ChannelConfig & {
241+ extractInput : (
242+ args : [ ...ArgsOf < TChannel > ] ,
243+ event : StartOf < TChannel > ,
244+ span : Span ,
245+ ) => {
246+ input : unknown ;
247+ metadata : unknown ;
248+ } ;
249+ } ,
250+ channelName : string ,
251+ ) : ( ( ) => void ) | undefined {
252+ const state = _internalGetGlobalState ( ) ;
253+ const startChannel = tracingChannel . start ;
254+ const currentSpanStore = state ?. contextManager
255+ ? (
256+ state . contextManager as {
257+ [ BRAINTRUST_CURRENT_SPAN_STORE ] ?: IsoAsyncLocalStorage < Span > ;
258+ }
259+ ) [ BRAINTRUST_CURRENT_SPAN_STORE ]
260+ : undefined ;
261+
262+ if ( ! currentSpanStore || ! startChannel ) {
263+ return undefined ;
264+ }
265+
266+ startChannel . bindStore (
267+ currentSpanStore ,
268+ ( event : ChannelMessage < TChannel > ) =>
269+ ensureSpanStateForEvent < TChannel > (
270+ states ,
271+ config ,
272+ event as StartOf < TChannel > ,
273+ channelName ,
274+ ) . span ,
275+ ) ;
276+
277+ return ( ) => {
278+ startChannel . unbindStore ( currentSpanStore ) ;
279+ } ;
280+ }
281+
199282function logErrorAndEnd <
200283 TChannel extends AnyAsyncChannel | AnySyncStreamChannel ,
201284> ( states : WeakMap < object , SpanState > , event : ErrorOf < TChannel > ) : void {
@@ -220,16 +303,20 @@ export function traceAsyncChannel<TChannel extends AnyAsyncChannel>(
220303 > ;
221304 const states = new WeakMap < object , SpanState > ( ) ;
222305 const channelName = channel . channelName ;
306+ const unbindCurrentSpanStore = bindCurrentSpanStoreToStart (
307+ tracingChannel ,
308+ states ,
309+ config ,
310+ channelName ,
311+ ) ;
223312
224313 const handlers : IsoChannelHandlers < ChannelMessage < TChannel > > = {
225314 start : ( event ) => {
226- states . set (
227- event as object ,
228- startSpanForEvent < TChannel > (
229- config ,
230- event as StartOf < TChannel > ,
231- channelName ,
232- ) ,
315+ ensureSpanStateForEvent < TChannel > (
316+ states ,
317+ config ,
318+ event as StartOf < TChannel > ,
319+ channelName ,
233320 ) ;
234321 } ,
235322 asyncEnd : ( event ) => {
@@ -278,6 +365,7 @@ export function traceAsyncChannel<TChannel extends AnyAsyncChannel>(
278365 tracingChannel . subscribe ( handlers ) ;
279366
280367 return ( ) => {
368+ unbindCurrentSpanStore ?.( ) ;
281369 tracingChannel . unsubscribe ( handlers ) ;
282370 } ;
283371}
@@ -291,16 +379,20 @@ export function traceStreamingChannel<TChannel extends AnyAsyncChannel>(
291379 > ;
292380 const states = new WeakMap < object , SpanState > ( ) ;
293381 const channelName = channel . channelName ;
382+ const unbindCurrentSpanStore = bindCurrentSpanStoreToStart (
383+ tracingChannel ,
384+ states ,
385+ config ,
386+ channelName ,
387+ ) ;
294388
295389 const handlers : IsoChannelHandlers < ChannelMessage < TChannel > > = {
296390 start : ( event ) => {
297- states . set (
298- event as object ,
299- startSpanForEvent < TChannel > (
300- config ,
301- event as StartOf < TChannel > ,
302- channelName ,
303- ) ,
391+ ensureSpanStateForEvent < TChannel > (
392+ states ,
393+ config ,
394+ event as StartOf < TChannel > ,
395+ channelName ,
304396 ) ;
305397 } ,
306398 asyncEnd : ( event ) => {
@@ -438,6 +530,7 @@ export function traceStreamingChannel<TChannel extends AnyAsyncChannel>(
438530 tracingChannel . subscribe ( handlers ) ;
439531
440532 return ( ) => {
533+ unbindCurrentSpanStore ?.( ) ;
441534 tracingChannel . unsubscribe ( handlers ) ;
442535 } ;
443536}
@@ -451,16 +544,20 @@ export function traceSyncStreamChannel<TChannel extends AnySyncStreamChannel>(
451544 > ;
452545 const states = new WeakMap < object , SpanState > ( ) ;
453546 const channelName = channel . channelName ;
547+ const unbindCurrentSpanStore = bindCurrentSpanStoreToStart (
548+ tracingChannel ,
549+ states ,
550+ config ,
551+ channelName ,
552+ ) ;
454553
455554 const handlers : IsoChannelHandlers < ChannelMessage < TChannel > > = {
456555 start : ( event ) => {
457- states . set (
458- event as object ,
459- startSpanForEvent < TChannel > (
460- config ,
461- event as StartOf < TChannel > ,
462- channelName ,
463- ) ,
556+ ensureSpanStateForEvent < TChannel > (
557+ states ,
558+ config ,
559+ event as StartOf < TChannel > ,
560+ channelName ,
464561 ) ;
465562 } ,
466563 end : ( event ) => {
@@ -565,6 +662,7 @@ export function traceSyncStreamChannel<TChannel extends AnySyncStreamChannel>(
565662 tracingChannel . subscribe ( handlers ) ;
566663
567664 return ( ) => {
665+ unbindCurrentSpanStore ?.( ) ;
568666 tracingChannel . unsubscribe ( handlers ) ;
569667 } ;
570668}
0 commit comments