Support distributed tracing #6
Open
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.
Distributed Tracing for Claude Code
Problem Statement
The current
trace-claude-codeskill creates standalone traces for each Claude Code session. However, in production environments, Claude Code often runs as part of a larger system:In these scenarios, operators need end-to-end visibility across the entire request lifecycle, not just isolated Claude Code traces. The Claude Code spans should nest under their parent orchestrator's trace.
Requirements
Proposed Solution: Session Context File
Overview
Introduce a session context file that orchestrators write before starting Claude Code. The hooks read this file to determine parent span context.
Why a File (Not Environment Variables)?
Session Context Schema
{ "parent_span": "<braintrust_exported_span_string>", "project": "optional-project-override" }parent_spanspan.export())projectBRAINTRUST_CC_PROJECTor default)File Location
Default:
/tmp/braintrust_session.jsonConfigurable via:
BRAINTRUST_SESSION_CONTEXT_FILEenvironment variableImplementation Plan
Phase 1: Hook Changes
1.1 Update
common.shAdd function to read and parse session context:
1.2 Update
session_start.shModify to nest under parent span when context is available:
1.3 Update Other Hooks
Ensure
user_prompt_submit.sh,post_tool_use.sh, andstop_hook.shuse the correctroot_span_idfrom session state (which now may be an external trace root).Phase 2: Documentation
2.1 Update SKILL.md
Add section on distributed tracing:
2. Start Claude Code
Claude Code hooks automatically detect the session context and nest traces accordingly.
3. View nested traces
In Braintrust, the Claude Code session appears as a child of your orchestrator span.
Byte 0: Version (3)
Byte 1: Object type (2 = PROJECT_LOGS)
Byte 2: Number of UUID fields
Bytes 3+: For each UUID: 1-byte field_id + 16-byte UUID
Remaining: JSON metadata
Field IDs:
1 = OBJECT_ID
2 = ROW_ID
3 = SPAN_ID ← Use for span_parents
4 = ROOT_SPAN_ID ← Use for root_span_id