Skip to content

Extract trace context from MCP _meta for distributed tracing#3763

Merged
ChrisJBurns merged 7 commits intomainfrom
otel/meta-trace-context-s1
Feb 11, 2026
Merged

Extract trace context from MCP _meta for distributed tracing#3763
ChrisJBurns merged 7 commits intomainfrom
otel/meta-trace-context-s1

Conversation

@ChrisJBurns
Copy link
Collaborator

@ChrisJBurns ChrisJBurns commented Feb 10, 2026

Summary

  • Wire MetaCarrier into the telemetry middleware to extract traceparent/tracestate from MCP params._meta, enabling distributed tracing per the MCP OTEL spec
  • The MetaCarrier infrastructure (from PR Add W3C Trace Context propagation via MCP _meta #3682) already existed but was not connected to the middleware's trace context extraction
  • After HTTP header extraction, the middleware now also extracts from _meta if present — _meta takes priority as the MCP-specified propagation mechanism
  • This was the highest priority remaining item and was critical for the Align telemetry with OpenTelemetry MCP semantic conventions #3399 success criteria: "Traces produced by vMCP also include spans from the MCPServer's proxyrunner"

Parent issue: #3399

Test plan

  • task test — all telemetry tests pass
  • task lint — 0 issues
  • New TestTelemetryIntegration_MetaTraceContextExtraction verifies:
    • A traceparent in _meta produces a child span with the correct parent trace ID
    • The middleware span's parent span ID matches the span from _meta
    • The span kind is SpanKindServer

🤖 Generated with Claude Code

ChrisJBurns and others added 4 commits February 10, 2026 22:53
- Fix SSE active connections gauge that was incremented but never
  decremented. Move increment into Handler with a defer decrement so
  the gauge correctly tracks SSE connection lifecycle.

- Fix config file `use-legacy-attributes: false` being silently ignored.
  Change UseLegacyAttributes from bool to *bool in app config so nil
  (not set) is distinguishable from explicit false. Add two test cases
  covering both scenarios.

- Log metric creation errors at debug level instead of silently
  discarding them. The OTEL SDK returns no-op instruments on error so
  there is no runtime risk, but logging aids troubleshooting.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ng, and client span kind

- Add mcp.protocol.version span attribute by reading the
  MCP-Protocol-Version header from streamable HTTP requests.

- Add mcp.client.operation.duration histogram metric to vMCP backend
  client using the OTEL MCP semconv bucket boundaries, alongside the
  existing toolhive_vmcp_backend_requests_duration custom metric.

- Include target name in span names per OTEL MCP semconv. Span names
  now follow the "{method} {target}" format, e.g. "tools/call
  github_search" instead of just "tools/call".

- Set SpanKindClient on vMCP backend call spans for proper distributed
  trace linking.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…le-http version

- B3: Remove omitempty from TracingEnabled/MetricsEnabled struct tags
  so YAML `false` values are not silently dropped during serialization
- B6: Move tool call counter from per-request creation to struct
  initialization in NewHTTPMiddleware for consistency with other counters
- B10: Return empty string for streamable-http protocol version in
  mapTransport since the actual backend version is unknown
- T1: Add SSE dual emission test verifying recordSSEConnection emits
  both new and legacy span attributes based on UseLegacyAttributes flag

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Per the MCP OTEL spec, servers should extract traceparent/tracestate
from params._meta and use as parent span context. The MetaCarrier
infrastructure already existed but was not wired into the middleware.

After HTTP header extraction, the middleware now also extracts trace
context from _meta if present. The _meta extraction takes priority
since it is the MCP-specified propagation mechanism.

Includes integration test verifying that a traceparent in _meta
produces a child span with the correct parent trace/span IDs.

Closes S1 from otel-remaining-work.md.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added the size/S Small PR: 100-299 lines changed label Feb 10, 2026
@codecov
Copy link

codecov bot commented Feb 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.50%. Comparing base (0696351) to head (d74883e).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3763      +/-   ##
==========================================
+ Coverage   66.44%   66.50%   +0.06%     
==========================================
  Files         428      428              
  Lines       42087    42090       +3     
==========================================
+ Hits        27965    27993      +28     
+ Misses      11986    11961      -25     
  Partials     2136     2136              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Feb 11, 2026
Base automatically changed from otel/code-quality-b3-b6-b10-t1 to main February 11, 2026 00:57
@github-actions github-actions bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Feb 11, 2026
@ChrisJBurns ChrisJBurns merged commit 60f23f5 into main Feb 11, 2026
36 checks passed
@ChrisJBurns ChrisJBurns deleted the otel/meta-trace-context-s1 branch February 11, 2026 14:08
Copy link
Contributor

@jerm-dro jerm-dro left a comment

Choose a reason for hiding this comment

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

Nice test!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants