envoy icon indicating copy to clipboard operation
envoy copied to clipboard

tracing/zipkin: b3 single header is extracted with top priority but never re-injected, flattening parent-child relationships across hops

Open prembhaskal opened this issue 2 months ago • 2 comments

Title: tracing/zipkin: b3 single header is extracted with top priority but never re-injected, flattening parent-child relationships across hops

Description:

The Zipkin tracer has an extract/inject asymmetry for the B3 single header format:

  1. Extraction prefers b3 single over the multi headers. SpanContextExtractor::extractSpanContext() (and extractSampled()) in source/extensions/tracers/zipkin/span_context_extractor.cc try the b3 single-format header first; when it is present, the x-b3-* multi headers are ignored. (Single-header extraction was added in #4712.)
  2. Injection only writes the multi headers. ZipkinSpan/Span::injectContext() in source/extensions/tracers/zipkin/zipkin_core_types.cc sets x-b3-traceid / x-b3-spanid / x-b3-parentspanid / x-b3-sampled (plus W3C headers under USE_B3_WITH_W3C_PROPAGATION), but never updates or removes the b3 single header. It is forwarded to the upstream unchanged.

Consequence: when a client emits the compact b3: <traceid>-<spanid>-<flags> header (e.g. an OpenTelemetry SDK configured with the b3 single-header inject encoding), the header crosses every Envoy hop byte-identical. Each Envoy in the chain — and any downstream SDK whose B3 extractor also prefers single format, which OTel's does — extracts its parent span id from the same frozen value. Every proxy span and the final destination's server span end up parented directly to the original caller's span: the trace keeps the correct trace id and sampled flag, but the hierarchy silently flattens into siblings instead of nesting hop by hop.

The failure is easy to misattribute because nothing errors and the fresh x-b3-* values Envoy writes are present on the wire — they are simply outranked at extraction by the stale single header. In a service mesh (Istio sidecars) this breaks service dependency graphs built from trace parentage.

Observed on an Envoy v1.29-based build; the relevant code on main is unchanged (extraction priority in span_context_extractor.cc, multi-only injection in zipkin_core_types.cc::injectContext).

Repro steps:

Two Envoys chained (each with the Zipkin tracer configured, 100% sampling), or Istio sidecars on two services:

curl http://envoy-a/ \
  -H 'b3: 0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-1'
  • Headers arriving at envoy-b (log them with a Lua filter before the router): b3 is byte-identical to what the client sent, while x-b3-spanid carries envoy-a's fresh span id — the two formats disagree on the same request.
  • envoy-b's span (and the upstream app's span, if its SDK reads single format) is parented to b7ad6b7169203331 instead of envoy-a's span id → sibling spans in the trace backend.
  • Control: send only multi headers (x-b3-traceid/x-b3-spanid/x-b3-sampled: 1) — each hop rewrites x-b3-spanid and the trace nests correctly.

Config:

Stock Zipkin tracer, no trace_context_option (i.e. default USE_B3); also reproduces with USE_B3_WITH_W3C_PROPAGATION since that option only adds W3C injection, not single-header refresh:

tracing:
  http:
    name: envoy.tracers.zipkin
    typed_config:
      "@type": type.googleapis.com/envoy.config.trace.v3.ZipkinConfig
      collector_cluster: zipkin
      collector_endpoint: /api/v2/spans
      collector_endpoint_version: HTTP_JSON
      trace_id_128bit: true

Suggested fix:

In Span::injectContext(), when the incoming request carried a b3 single header, refresh it with the same traceid-spanid-flags values being written to the multi headers (or, more conservatively, remove it after extraction so receivers fall back to the always-fresh multi headers). Either keeps the two encodings consistent on the wire. Happy to send a PR if maintainers agree on the preferred behavior.

Notes:

  • Related context: #4712 added single-header extraction without injection; #39412 / trace_context_option addressed the analogous B3-vs-W3C dual-format concern but not single-vs-multi B3.
  • openzipkin/b3-propagation's STATUS.md shows single-format support is patchy across the ecosystem generally, which is presumably why injection was never added — but extracting a format with priority while never refreshing it is what makes stale values propagate.

prembhaskal avatar Sep 18 '26 03:09 prembhaskal

Confirmed against current main. SpanContextExtractor::extractSampled() / extractSpanContext() check b3 first, and Span::injectContext() only writes the x-b3-* headers. The only references to the B3 handle in source/ are the three reads in span_context_extractor.cc, so nothing ever rewrites or strips it.

One addition on the impact: "siblings" holds for spans where Envoy creates a new child (CLIENT_SEND: egress/gateway, or spawn_upstream_span). For inbound sidecar spans (SERVER_RECV) with the default shared_span_context: true, Tracer::startSpan reuses the extracted span id instead of creating a child:

https://github.com/envoyproxy/envoy/blob/main/source/extensions/tracers/zipkin/tracer.cc#L134-L141

So in the Istio case the inbound span on the next hop takes the stale id from b3 and merges with the original caller's span rather than becoming a sibling. That's arguably worse for dependency graphs, because it drops the hop instead of just flattening it.

On the fix, +1 to refreshing b3 in injectContext() only when it was present on the incoming request. That keeps both encodings consistent without starting to emit b3 to upstreams that never received it. Removing it after extraction would also work, but it silently changes the headers seen by apps whose SDK only reads single format.

guanchzhou avatar Sep 24 '26 16:09 guanchzhou

Thank you for the superfast fix. 🚀

prembhaskal avatar Sep 29 '26 14:09 prembhaskal