Skip to content

Make tracer.trace_context async - #165

Merged
Yuge Zhang (ultmaster) merged 1 commit into
mainfrom
async-trace-context-2
Oct 16, 2025
Merged

Make tracer.trace_context async#165
Yuge Zhang (ultmaster) merged 1 commit into
mainfrom
async-trace-context-2

Conversation

@ultmaster

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI review requested due to automatic review settings October 16, 2025 14:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_context method from @contextmanager to @asynccontextmanager across all tracer implementations
  • Added _trace_context_sync internal method for backward compatibility in synchronous contexts
  • Updated type annotations from Iterator to AsyncGenerator to 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.

Comment thread tests/tracer/test_otel.py
Comment on lines +415 to +417
asyncio.run(_otel_reward_subprocess_async(mode, conn))


Copilot AI Oct 16, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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()

Copilot uses AI. Check for mistakes.
Comment thread tests/tracer/test_otel.py

with tracer.trace_context(name="reward-decorator"):
async with tracer.trace_context(name="reward-decorator"):
returned = compute_reward()

Copilot AI Oct 16, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@ultmaster
Yuge Zhang (ultmaster) merged commit bdf6a8f into main Oct 16, 2025
13 checks passed
@ultmaster
Yuge Zhang (ultmaster) deleted the async-trace-context-2 branch October 28, 2025 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants