Make tracer.trace_context async - #165
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR changes the tracer.trace_context method from a synchronous context manager to an asynchronous one to better support async tracing workflows.
Key changes:
- Converted
trace_contextmethod from@contextmanagerto@asynccontextmanageracross all tracer implementations - Added
_trace_context_syncinternal method for backward compatibility in synchronous contexts - Updated type annotations from
IteratortoAsyncGeneratorto match the async pattern
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| agentlightning/tracer/base.py | Updated base class to make trace_context async and added _trace_context_sync for compatibility |
| agentlightning/tracer/otel.py | Converted trace_context to async context manager |
| agentlightning/tracer/http.py | Added async trace_context wrapper around existing sync implementation |
| agentlightning/tracer/agentops.py | Converted trace_context to async and added sync wrapper |
| agentlightning/runner/legacy.py | Updated calls to use appropriate sync/async context manager |
| agentlightning/runner/agent.py | Updated to use async trace_context |
| tests/tracer/test_otel.py | Added async wrapper function and updated test calls to use async context |
| tests/tracer/test_integration.py | Updated to use sync context manager and changed port configuration |
| tests/runner/test_runner_context.py | Updated mock tracer to use async context manager |
| tests/runner/test_agent_runner.py | Updated mock tracer to use async context manager |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| asyncio.run(_otel_reward_subprocess_async(mode, conn)) | ||
|
|
||
|
|
There was a problem hiding this comment.
The asyncio.run() call creates a new event loop for each subprocess call. Consider reusing an existing event loop or using asyncio.new_event_loop() with proper cleanup to avoid potential conflicts with existing event loops.
| asyncio.run(_otel_reward_subprocess_async(mode, conn)) | |
| loop = asyncio.new_event_loop() | |
| try: | |
| asyncio.set_event_loop(loop) | |
| loop.run_until_complete(_otel_reward_subprocess_async(mode, conn)) | |
| finally: | |
| loop.close() |
|
|
||
| with tracer.trace_context(name="reward-decorator"): | ||
| async with tracer.trace_context(name="reward-decorator"): | ||
| returned = compute_reward() |
There was a problem hiding this comment.
The synchronous function compute_reward() is being called within an async context manager. Consider making this function async or using await if it performs any I/O operations to maintain consistency with the async pattern.
No description provided.