Skip to content

Commit db76958

Browse files
committed
Add pulse_agent2 decorator for enhanced async context management and logging
1 parent 283fe61 commit db76958

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed
162 Bytes
Binary file not shown.

dist/singlestore_pulse-0.1.tar.gz

183 Bytes
Binary file not shown.

pulse_otel/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from pulse_otel.main import Pulse, CustomFileSpanExporter, FileLogExporter, pulse_agent, pulse_tool
1+
from pulse_otel.main import Pulse, CustomFileSpanExporter, FileLogExporter, pulse_agent, pulse_tool, pulse_agent2

pulse_otel/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,37 @@ async def wrapper(*args, **kwargs):
281281
return decorator
282282

283283

284+
from contextvars import copy_context
285+
286+
def pulse_agent2(name):
287+
def decorator(func):
288+
@functools.wraps(func)
289+
def wrapper(*args, **kwargs):
290+
# Capture the current context
291+
ctx = copy_context()
292+
293+
async def async_wrapper():
294+
add_session_id_to_span_attributes(**kwargs)
295+
logger.info(f"Executing agent: {name} with args: {args}, kwargs: {kwargs}")
296+
tracer = trace.get_tracer(__name__)
297+
298+
with tracer.start_as_current_span(name) as span:
299+
trace_id_hex = format(span.get_span_context().trace_id, "032x")
300+
logger.info(f"[wrapper] Started span. TraceID: {trace_id_hex}")
301+
302+
decorated_func = agent(name)(func)
303+
304+
# Run the generator in the captured context
305+
async for item in decorated_func(*args, **kwargs):
306+
yield item
307+
308+
logger.info(f"Agent {name} completed with trace ID: {trace_id_hex}")
309+
310+
return ctx.run(async_wrapper)
311+
return wrapper
312+
return decorator
313+
314+
284315
class CustomFileSpanExporter(SpanExporter):
285316
def __init__(self, file_name):
286317
self.file_name = file_name

0 commit comments

Comments
 (0)