feat: add OpenTelemetry tracing support#530
Draft
wingding12 wants to merge 1 commit intoanthropics:mainfrom
Draft
feat: add OpenTelemetry tracing support#530wingding12 wants to merge 1 commit intoanthropics:mainfrom
wingding12 wants to merge 1 commit intoanthropics:mainfrom
Conversation
Add opt-in OpenTelemetry tracing via the `tracer` parameter in
ClaudeAgentOptions. When a tracer is provided, spans are created for
key operations:
- claude_agent_sdk.query - for query() function calls
- claude_agent_sdk.session - for ClaudeSDKClient sessions
- claude_agent_sdk.control - for control protocol requests
- claude_agent_sdk.permission - for tool permission checks
- claude_agent_sdk.hook - for hook callbacks
- claude_agent_sdk.mcp - for MCP server requests
Usage:
```python
from opentelemetry import trace
tracer = trace.get_tracer("my-app")
options = ClaudeAgentOptions(tracer=tracer)
```
Install with: pip install claude-agent-sdk[telemetry]
Closes anthropics#452
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add opt-in OpenTelemetry tracing support via the
tracerparameter inClaudeAgentOptions. This enables observability for applications using the Claude SDK.Changes
tracerparameter toClaudeAgentOptionsTracingContextclass and span utilitiesClaudeSDKClienttelemetryoptional dependency (opentelemetry-api>=1.20.0)Span Names
When tracing is enabled, the following spans are created:
claude_agent_sdk.queryclaude_agent_sdk.sessionclaude_agent_sdk.controlclaude_agent_sdk.permissionclaude_agent_sdk.hookclaude_agent_sdk.mcpUsage
Install with:
pip install claude-agent-sdk[telemetry]Test Plan
Closes #452