Skip to content

feat(telemetry): propagate W3C traceparent from parent process#352

Merged
lizhengfeng101 merged 1 commit into
alibaba:mainfrom
yingjiexu2002:feat/trace-context-inherit
Jul 14, 2026
Merged

feat(telemetry): propagate W3C traceparent from parent process#352
lizhengfeng101 merged 1 commit into
alibaba:mainfrom
yingjiexu2002:feat/trace-context-inherit

Conversation

@yingjiexu2002

Copy link
Copy Markdown
Contributor
  • Add ContextWithTraceParentFromEnv to extract TRACEPARENT env var and inject upstream span context via OTel TextMapPropagator.
  • Register TraceContext+Baggage composite propagator in Init().
  • Wire trace parent propagation into review and scan entry points.
  • Add tests for valid, absent, disabled, and malformed TRACEPARENT.

Description

When OCR is invoked by a parent process (e.g. Claude Code), the parent sets a TRACEPARENT environment variable following the W3C Trace Context specification. Previously, OCR always started a new root trace, making it impossible to correlate OCR spans with the caller's trace.

This PR adds ContextWithTraceParentFromEnv() which reads the TRACEPARENT env var and injects the upstream span context into the Go context via the OTel TextMapPropagator. Both the review and scan commands now use this function so their spans become children of the upstream trace. A TraceContext + Baggage composite propagator is registered during Init() to support extraction.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

  • make test passes locally
  • Manual testing (describe below)

Added TestContextWithTraceParentFromEnv_Extracts — verifies a valid TRACEPARENT is extracted and child spans inherit the upstream trace ID.
Added TestContextWithTraceParentFromEnv_AbsentOrDisabled — verifies no span context is injected when telemetry is disabled or TRACEPARENT is unset.
Added TestContextWithTraceParentFromEnv_Malformed — verifies malformed TRACEPARENT values are gracefully ignored (no panic, no invalid span context).
All tests pass with go test -shuffle=on.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)
  • I have signed the CLA

Related Issues

N/A

- Add ContextWithTraceParentFromEnv to extract TRACEPARENT env var and
  inject upstream span context via OTel TextMapPropagator.
- Register TraceContext+Baggage composite propagator in Init().
- Wire trace parent propagation into review and scan entry points.
- Add tests for valid, absent, disabled, and malformed TRACEPARENT.
@github-actions

Copy link
Copy Markdown
Contributor

OpenCodeReview: No comments generated. Looks good to me.

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@lizhengfeng101 lizhengfeng101 merged commit a32f852 into alibaba:main Jul 14, 2026
4 checks passed
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.

2 participants