Skip to content

Conversation

@sipercai
Copy link
Collaborator

@sipercai sipercai commented Jan 9, 2026

Description

This PR adds OpenTelemetry instrumentation support for Claude Agent SDK, enabling automatic tracing and metrics collection for Claude agent conversations, LLM calls, and tool executions.

Fixes #103

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test

Lint check

tox -c tox-loongsuite.ini -e lint-loongsuite-instrumentation-claude-agent-sdk

Unit tests (Python 3.12 + latest)

tox -c tox-loongsuite.ini -e py312-test-loongsuite-instrumentation-claude-agent-sdk-latest

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

- Initial implementation of Claude Agent SDK instrumentation
- Support for agent query sessions via Hooks mechanism
- Support for tool execution tracing (PreToolUse/PostToolUse hooks)
- Integration with opentelemetry-util-genai ExtendedTelemetryHandler
- Span attributes following OpenTelemetry GenAI Semantic Conventions
- Support for Alibaba Cloud DashScope Anthropic-compatible API

Change-Id: aebd1fd0-3afc-4a37-af74-3800c117aaf0
Change-Id: I187974bfe8b44b9f592ffcb368e11d98c41f1a30
Co-developed-by: Cursor <[email protected]>
Change-Id: Ie6eab7ffae40e000b3b2c55a0abe50848490c1e7
Co-developed-by: Cursor <[email protected]>
Change-Id: I2656979e57ed2e9b3110867f9e5f6321d45cb3e2
Co-developed-by: Cursor <[email protected]>
Change-Id: Ibf6f934583ed0e76a4f79016cbfbade94a05acec
Co-developed-by: Cursor <[email protected]>
Change-Id: I1420318408a53e563499c43a3cc2ae86ed0aa929
Co-developed-by: Cursor <[email protected]>
Change-Id: Iab09e483df20ef8ad44545e36d0d83f5ae0cae1d
Co-developed-by: Cursor <[email protected]>
@sipercai sipercai force-pushed the liuyu/claude-clean branch from c3e3afb to 462b1a0 Compare January 9, 2026 03:39
sipercai and others added 6 commits January 9, 2026 14:06
Change-Id: I55134f7e3ef30b7192deab801b12b132b250a31c
Co-developed-by: Cursor <[email protected]>
Change-Id: I44bf3572fa7d8bf7d47b665d3463433a93076f9c
Co-developed-by: Cursor <[email protected]>
Change-Id: I4308f8d8ecc7048c1bc9d1d9ec824addad1c5912
Co-developed-by: Cursor <[email protected]>
Change-Id: Ice677164663bc066e830b3cd24c09697d2dd0fb1
Co-developed-by: Cursor <[email protected]>
Change-Id: Ieadc2e7ea2193e301c171c62eee9da3179c95199
Co-developed-by: Cursor <[email protected]>
Copy link

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 adds OpenTelemetry instrumentation for the Claude Agent SDK, enabling automatic tracing and metrics collection for Claude agent conversations, LLM calls, and tool executions. The implementation introduces a new instrumentation package that follows OpenTelemetry GenAI semantic conventions.

Changes:

  • Adds comprehensive instrumentation for Claude Agent SDK including agent sessions, tool executions, and token tracking
  • Implements test suites covering unit tests, integration tests, edge cases, and attribute validation
  • Configures CI/CD workflows for automated testing and linting across multiple Python versions

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tox-loongsuite.ini Adds tox environment configuration for claude-agent-sdk instrumentation testing
.github/workflows/loongsuite_test_0.yml Adds CI test jobs for Python 3.10-3.13 across oldest/latest dependency versions
.github/workflows/loongsuite_lint_0.yml Adds linting job for claude-agent-sdk instrumentation
util/opentelemetry-util-genai/.../pre_uploader.py Adds pyright ignore comment for numpy import
instrumentation-loongsuite/loongsuite-instrumentation-claude-agent-sdk/* New instrumentation package with source code, tests, and documentation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +342 to +347
# Override span start time
if llm_invocation.span and start_time:
start_time_ns = int(start_time * 1_000_000_000)
try:
if hasattr(llm_invocation.span, "_start_time"):
llm_invocation.span._start_time = start_time_ns # type: ignore
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

Accessing private attribute _start_time directly is fragile and violates encapsulation. This implementation relies on internal implementation details of the span object that may change. Consider using a public API if available, or document this as a known technical debt with a TODO comment explaining why this is necessary and what the proper solution would be.

Suggested change
# Override span start time
if llm_invocation.span and start_time:
start_time_ns = int(start_time * 1_000_000_000)
try:
if hasattr(llm_invocation.span, "_start_time"):
llm_invocation.span._start_time = start_time_ns # type: ignore
# Override span start time.
# TODO(telemetry): Avoid relying on the private `_start_time` attribute.
# The long-term fix is to plumb a public `start_time` parameter through
# ExtendedTelemetryHandler.start_llm and the underlying span creation,
# so the desired start time can be set via a supported API instead of
# mutating internal span state here. Until that is available, we perform
# a best-effort adjustment guarded by hasattr and try/except so that
# failures do not break tracing.
if llm_invocation.span and start_time:
start_time_ns = int(start_time * 1_000_000_000)
try:
if hasattr(llm_invocation.span, "_start_time"):
setattr( # type: ignore[attr-defined]
llm_invocation.span, "_start_time", start_time_ns
)

Copilot uses AI. Check for mistakes.
type=RuntimeError,
),
)
except Exception:
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Copilot uses AI. Check for mistakes.
type=RuntimeError,
),
)
except Exception:
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Copilot uses AI. Check for mistakes.
count += 1
if count > 5: # Prevent infinite loop
break
except Exception:
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except Exception:
except Exception:
# Ignore exceptions here; this test only verifies that instrumentation
# can handle an empty prompt without crashing the test suite.

Copilot uses AI. Check for mistakes.
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.

feat: Add instrumentation for claude agent sdk

4 participants