Skip to content

Conversation

@dot-agi
Copy link
Member

@dot-agi dot-agi commented May 29, 2025

📥 Pull Request

📘 Description

Added native context manager support to TraceContext class, enabling with agentops.start_trace("name") as trace: syntax for automatic trace lifecycle management.

Basic Usage:

import agentops

agentops.init(api_key="your-api-key")

# Automatic trace management
with agentops.start_trace("my_workflow") as trace:
    # Your code here
    pass  # Trace automatically ends with Success/Error status

# With tags
with agentops.start_trace("data_processing", tags=["batch", "production"]):
    process_data()

Changelog:

  • Added __enter__ and __exit__ methods to TraceContext class in agentops/sdk/core.py
  • Automatic trace state management: Success on normal exit, Error on exceptions
  • Support for parallel traces in nested context managers (independent, not parent-child)
  • Thread-safe and asyncio-compatible implementation
  • Added 4 example files: basic usage, parallel traces, error handling, production patterns
  • Created comprehensive documentation at docs/v2/usage/context-managers.mdx
  • Updated SDK reference with context manager usage patterns
  • Maintains full backward compatibility with existing manual trace management

🧪 Testing

  • Context manager protocol tests (__enter__, __exit__ behavior)
  • Exception handling and automatic state management tests
  • Thread safety and asyncio compatibility tests
  • System exception handling tests (KeyboardInterrupt, SystemExit)
  • Generator function integration and early return tests
  • Large data handling and resource cleanup failure tests
  • Backward compatibility and mixed usage pattern tests
  • Performance tests with 100+ sequential traces
  • Edge case tests (None returns, initialization failures)

@codecov
Copy link

codecov bot commented May 29, 2025

Codecov Report

Attention: Patch coverage is 78.46154% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
agentops/sdk/core.py 77.41% 7 Missing ⚠️
agentops/legacy/__init__.py 0.00% 3 Missing ⚠️
agentops/__init__.py 87.50% 2 Missing ⚠️
agentops/enums.py 84.61% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

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 introduces native context manager support for trace management, enabling developers to use Python’s with statement for automatic trace lifecycle handling. Key changes include:

  • Implementation of enter and exit methods on the TraceContext class for automatic state management on trace exit
  • Updates to the AgentOps initialization and client retrieval logic, including the addition of a new tool decorator
  • New example scripts and documentation demonstrating context manager usage for production, parallel, error handling, and basic scenarios

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
examples/context_manager/production_patterns.py New production example showcasing API pattern, batch processing, and monitoring using context managers
examples/context_manager/parallel_traces.py New example illustrating sequential, nested, and concurrent trace management
examples/context_manager/error_handling.py New example covering various exception handling patterns with context managers
examples/context_manager/basic_usage.py New basic example demonstrating native TraceContext usage
examples/context_manager/README.md New README outlining context manager examples and usage instructions
docs/v2/usage/context-managers.mdx Documentation update to cover context manager support and usage patterns
docs/mint.json Updated mint config to include context manager usage documentation
agentops/sdk/core.py Added enter and exit implementations to support context management
agentops/init.py Updated client initialization and decorator imports to support new tool decorator and context manager usage

@dot-agi dot-agi requested a review from Copilot May 29, 2025 20:21

This comment was marked as outdated.

@dot-agi dot-agi requested review from Copilot and tcdent June 6, 2025 19:50
Copy link
Contributor

Copilot AI left a comment

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 introduces native context manager support for automatic trace lifecycle management by adding enter and exit methods to the TraceContext class and updating related references from TracingCore to a global tracer. Key changes include support for parallel traces, updated documentation and examples, and enhanced thread safety via a double-checked locking mechanism during client initialization.

Reviewed Changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 3 comments.

File Description
tests/unit/*.py Updated comments and patch references to reflect the global tracer rename
agentops/sdk/core.py Added context manager methods and improved end_state type handling
agentops/init.py Updated client initialization with thread safety improvements
Other examples and docs (e.g., README.md, .md files) Revised usage examples and documentation to demonstrate new context manager support

@dot-agi dot-agi requested a review from tcdent June 6, 2025 20:56
@dot-agi dot-agi merged commit aa3f549 into main Jun 6, 2025
10 checks passed
@dot-agi dot-agi deleted the feat/trace-context-manager branch June 6, 2025 22:24
@dot-agi dot-agi linked an issue Jun 9, 2025 that may be closed by this pull request
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.

investigate ways to clean sdk

3 participants