Skip to content

Commit 479a044

Browse files
committed
Refactor pulse_agent2 to s2_agent for improved async context handling and logging
1 parent db76958 commit 479a044

File tree

4 files changed

+10
-23
lines changed

4 files changed

+10
-23
lines changed
-104 Bytes
Binary file not shown.

dist/singlestore_pulse-0.1.tar.gz

-53 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, pulse_agent2
1+
from pulse_otel.main import Pulse, CustomFileSpanExporter, FileLogExporter, pulse_agent, pulse_tool, s2_agent

pulse_otel/main.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
import os
33
from traceloop.sdk import Traceloop
44
from traceloop.sdk.decorators import agent, tool
5-
from opentelemetry import _logs, trace, context
5+
from opentelemetry import _logs, trace
66

77

8-
from opentelemetry.trace import get_current_span, INVALID_SPAN
9-
from opentelemetry.trace.span import Span
10-
from opentelemetry.context import attach, detach, get_current, set_value
8+
from opentelemetry.context import attach, set_value
119
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler, LogData
1210
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor, LogExporter, LogExportResult, SimpleLogRecordProcessor
1311
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
@@ -17,13 +15,11 @@
1715
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
1816
OTLPLogExporter,
1917
)
20-
from fastapi import Request, Response
21-
from fastapi.responses import StreamingResponse
18+
19+
from contextvars import copy_context
2220

2321
from functools import wraps
24-
import uuid
2522
import logging
26-
from typing import Callable
2723
import typing
2824

2925
from pulse_otel.util import (
@@ -35,11 +31,8 @@
3531
from pulse_otel.consts import (
3632
LOCAL_TRACES_FILE,
3733
LOCAL_LOGS_FILE,
38-
SESSION_ID,
39-
HEADER_INCOMING_SESSION_ID,
4034
PROJECT,
4135
LIVE_LOGS_FILE_PATH,
42-
TRACEID_RESPONSE_HEADER,
4336
)
4437
import logging
4538

@@ -280,10 +273,7 @@ async def wrapper(*args, **kwargs):
280273
return wrapper
281274
return decorator
282275

283-
284-
from contextvars import copy_context
285-
286-
def pulse_agent2(name):
276+
def s2_agent(name):
287277
def decorator(func):
288278
@functools.wraps(func)
289279
def wrapper(*args, **kwargs):
@@ -292,26 +282,23 @@ def wrapper(*args, **kwargs):
292282

293283
async def async_wrapper():
294284
add_session_id_to_span_attributes(**kwargs)
295-
logger.info(f"Executing agent: {name} with args: {args}, kwargs: {kwargs}")
296285
tracer = trace.get_tracer(__name__)
297286

287+
# Create new span within the async context
298288
with tracer.start_as_current_span(name) as span:
299289
trace_id_hex = format(span.get_span_context().trace_id, "032x")
300-
logger.info(f"[wrapper] Started span. TraceID: {trace_id_hex}")
290+
logger.debug(f"[s2_agent wrapper] Started span. TraceID: {trace_id_hex}")
301291

292+
# Execute decorated function and yield results
302293
decorated_func = agent(name)(func)
303-
304-
# Run the generator in the captured context
305294
async for item in decorated_func(*args, **kwargs):
306295
yield item
307296

308-
logger.info(f"Agent {name} completed with trace ID: {trace_id_hex}")
309-
297+
# Run the entire async generator in captured context
310298
return ctx.run(async_wrapper)
311299
return wrapper
312300
return decorator
313301

314-
315302
class CustomFileSpanExporter(SpanExporter):
316303
def __init__(self, file_name):
317304
self.file_name = file_name

0 commit comments

Comments
 (0)