@@ -277,46 +277,32 @@ def wrapper(*args, **kwargs):
277277 return decorator
278278
279279def s2_agent (name ):
280- """
281- s2_agent is a dedicated decorator for for event_generator function of s2ai framework that
282- wraps an asynchronous event_generator function to add tracing
283- capabilities using OpenTelemetry. It captures the current context, creates
284- a new span, and ensures that the decorated function is executed within the
285- captured context.
286-
287- This is being done to make sure parent context is rightfully captured and propagated in a sync function so that
288- the traceID of the current otel span is used is passed on the asynchronous generator function.
289- Args:
290- name (str): The name of the span to be created for tracing.
291- Returns:
292- Callable: A decorator that wraps the target asynchronous generator
293- function, adding tracing and context management.
294- """
295280 def decorator (func ):
296281 @functools .wraps (func )
297282 def wrapper (* args , ** kwargs ):
298- # Capture the current context
299283 ctx = copy_context ()
300-
284+ trace_callback = kwargs .pop ("trace_callback" , None ) # Accept optional callback
285+
301286 async def async_wrapper ():
302287 add_session_id_to_span_attributes (** kwargs )
303288 tracer = trace .get_tracer (__name__ )
304289
305- # Create new span within the async context
306290 with tracer .start_as_current_span (name ) as span :
307291 trace_id_hex = format (span .get_span_context ().trace_id , "032x" )
308292 logger .debug (f"[s2_agent wrapper] Started span. TraceID: { trace_id_hex } " )
309293
310- # Execute decorated function and yield results
294+ if trace_callback :
295+ trace_callback (trace_id_hex ) # Notify the caller
296+
311297 decorated_func = agent (name )(func )
312298 async for item in decorated_func (* args , ** kwargs ):
313299 yield item
314-
315- # Run the entire async generator in captured context
300+
316301 return ctx .run (async_wrapper )
317302 return wrapper
318303 return decorator
319304
305+
320306class CustomFileSpanExporter (SpanExporter ):
321307 def __init__ (self , file_name ):
322308 self .file_name = file_name
0 commit comments