Skip to content

Conversation

@Dwij1704
Copy link
Member

📥 Pull Request

📘 Description
Closes #991

Enhance AgentOps SDK with tracing capabilities

  • Introduced start_trace and end_trace functions for user-managed tracing, allowing concurrent traces.
  • Updated init method to handle auto-starting traces and improved session management.
  • Refactored legacy session handling to integrate with new tracing architecture.
  • Deprecated the use of global active session in favor of trace context management.
  • Improved error handling and logging during SDK initialization and trace lifecycle.
  • Added trace decorator. Updated log_trace_url to include titles for improved logging context.

This is a hybrid approach:

  • Default Behavior (auto_start_session=True, which is the default for init):
    agentops.init() will start a main trace (let's call it the "init trace" or "auto-trace").
    This "init trace" will be automatically managed and ended when the program exits. This means we need to reinstate a form of atexit handling for this specific trace if and only if it was started by init and auto_start_session was true.
    agentops.init() will not return this automatically managed session/trace object to the user. If it did, and the user also tried to end it, it could lead to double-ending.
  • Explicit Trace Management (agentops.start_trace, agentops.end_trace):
    Users can still call agentops.start_trace() to create additional, independent traces.
    agentops.start_trace() will return a Trace (or Session-like) object.
    Users will be responsible for explicitly ending these traces using agentops.end_trace(trace_object).
    These explicit traces will allow for multiple concurrent, user-managed traces, independent of the "init trace."
    auto_start_session=False:
    If agentops.init(auto_start_session=False) is called, no "init trace" is automatically started.
    init() would return None in this case.
    The user is then fully responsible for starting and ending all traces using agentops.start_trace() and agentops.end_trace().

🧪 Testing
View the test code gist

Dwij1704 added 4 commits May 22, 2025 15:12
…ving session handling. Added `trace`, `session`, `agent`, `task`, `workflow`, and `operation` decorators for better instrumentation. Updated `log_trace_url` to include titles for improved logging context. Refactored `Client` initialization trace name and adjusted end trace state handling. Improved error handling during trace logging in `TracingCore` and removed deprecated session decorator usage.
…gs`, `auto_init`, `skip_auto_end_session`, and `fail_safe`. Updated documentation to reflect changes and merged `tags` with `default_tags` for improved session management. Refactored client initialization to accommodate new options.
@bboynton97 bboynton97 self-requested a review May 23, 2025 15:49
@bboynton97
Copy link
Contributor

closes #999

@bboynton97 bboynton97 mentioned this pull request May 23, 2025
Copy link
Contributor

@bboynton97 bboynton97 left a comment

Choose a reason for hiding this comment

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

definitely want tests to pass before merging. we should also add tests for the multi-session functionality as well as a test script / example :)

very nice work Dwij!!

dot-agi and others added 7 commits May 26, 2025 01:07
…ession` in `agentops` module. This change improves code clarity and aligns with the updated session management approach.
…cessor. Added tests for start and end trace URL logging, handling failures gracefully, and verifying root span tracking. Improved test coverage for session decorators and ensured proper handling of unsampled spans. Refactored existing tests for clarity and consistency.
…nd attribute tracking. Updated span creation to use dynamic workflow names and improved error handling. Adjusted span kinds from CLIENT to INTERNAL for better clarity in tracing. Streamlined attribute setting for agents and tasks, ensuring accurate logging of results and metrics.
…r, simplifying the flush process. Updated logging to indicate completion of the flush operation.
…ditions for the API key. Only trigger a warning if a different non-None API key is provided during re-initialization, enhancing the clarity of client behavior.
… legacy session wrapper. Update unit tests to enhance coverage for new session management functionality, including explicit trace handling and decorator behavior. Ensure proper integration between new and legacy APIs for session and trace management.
@codecov
Copy link

codecov bot commented May 27, 2025

Dwij1704 and others added 11 commits May 28, 2025 01:14
…xception handling for token fetching and response processing, ensuring clearer error logging and re-raising of exceptions for better testability. Updated integration tests to reset client state between tests.
…on and error handling. Mock API client and tracing core to avoid real authentication during tests. Simplify concurrency test descriptions and ensure proper cleanup of client state between tests.
…ng all active traces when no context is provided. Refactor end_all_sessions to utilize the new end_trace functionality, ensuring legacy global state is cleared. Introduce thread-safe handling of active traces in TracingCore with locking mechanisms for improved concurrency.
… for invalid trace IDs. This ensures robustness when dealing with mocked spans or non-integer trace IDs, improving overall trace management reliability.
…figuration and initialization. This allows for customizable trace/session naming, improving trace management and clarity in logs. Updated relevant classes and methods to utilize the new parameter.
@dot-agi dot-agi requested a review from Copilot May 27, 2025 22:04
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 improves root span management in the AgentOps SDK by enhancing trace lifecycle handling and updating the tracing API. Key changes include:

  • Introducing explicit start_trace and end_trace functions with improved configuration (including a trace name parameter)
  • Refactoring session management in the client and deprecating legacy session handling
  • Updating decorator behavior and telemetry shutdown procedures

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/integration/test_session_concurrency.py Updated test cases to reflect removal of legacy session decorators and improved error handling
tests/integration/test_auth_flow.py Added client reset fixture for consistent auth flow testing
agentops/semconv/span_attributes.py Added new span attribute for session end state
agentops/sdk/processors.py Removed duplicate log_trace_url calls to streamline span logging
agentops/sdk/decorators/factory.py Introduced type hints and parameter “tags” in decorators, along with enhanced logging messages
agentops/sdk/core.py Added start_trace/end_trace methods with TraceContext management and improved shutdown flushing
agentops/instrumentation/crewai/instrumentation.py Adapted workflow span naming to use configurable trace_name
agentops/helpers/dashboard.py Updated log_trace_url to include an optional title for better logging context
agentops/config.py Extended configuration to support default trace name
agentops/client/client.py Refactored auto-start trace logic and re-initialization handling, deprecating global variables for legacy sessions
agentops/client/api/versions/v3.py Improved error logging in auth token processing
agentops/init.py Added start_trace/end_trace API and integrated new trace_name configuration in SDK init
Comments suppressed due to low confidence (1)

agentops/init.py:193

  • Auto-initializing the SDK within start_trace may lead to side effects; consider requiring explicit initialization to ensure predictable behavior and clearer control flow for SDK consumers.
if not tracing_core.initialized { ... init()  // Attempt to initialize with environment variables / defaults

@Dwij1704 Dwij1704 requested a review from dot-agi May 27, 2025 22:11
@Dwij1704 Dwij1704 dismissed bboynton97’s stale review May 27, 2025 22:12

YADA YADA YADA

Copy link
Member

@dot-agi dot-agi left a comment

Choose a reason for hiding this comment

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

@Dwij1704 Dwij1704 merged commit 5b419d3 into main May 27, 2025
9 of 10 checks passed
@Dwij1704 Dwij1704 deleted the better-root-span-management branch May 27, 2025 22:15
@Dwij1704
Copy link
Member Author

68747470733a2f2f6d65646961312e74656e6f722e636f6d2f6d2f586935516e63756761565541414141432f6477696a706174656c2e676966

For anyone who is wondering, thats me :)

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.

[Feature]: Add back better session control

4 participants