effect icon indicating copy to clipboard operation
effect copied to clipboard

HttpTraceContext: B3 debug (`d`) and legacy `X-B3-Sampled: true` are parsed as unsampled, so the whole server trace is dropped

Open aakarshitv opened this issue 2 months ago • 0 comments

What version of Effect is running?

effect 4.0.0 (same code on main at 480bba2)

What steps can reproduce the bug?

// repro.mjs — npm i [email protected] && node repro.mjs
import * as HttpTraceContext from "effect/http/HttpTraceContext"
import * as Headers from "effect/http/Headers"

const T = "80f198ee56343ba864fe8b2a57d3eff7"
const S = "e457b5a2e4d86bd1"

const show = (label, h) => {
  const r = HttpTraceContext.fromHeaders(Headers.fromInput(h))
  console.log(label.padEnd(36), r._tag === "Some" ? `sampled=${r.value.sampled}` : "None")
}

show("b3: {trace}-{span}-d", { b3: `${T}-${S}-d` })
show("x-b3-sampled: true", { "x-b3-traceid": T, "x-b3-spanid": S, "x-b3-sampled": "true" })
show("x-b3-sampled: 0, x-b3-flags: 1", { "x-b3-traceid": T, "x-b3-spanid": S, "x-b3-sampled": "0", "x-b3-flags": "1" })
show("traceparent, all-zero trace-id", { traceparent: `00-${"0".repeat(32)}-${S}-01` })
show("traceparent, all-zero parent-id", { traceparent: `00-${T}-${"0".repeat(16)}-01` })

Output:

b3: {trace}-{span}-d                 sampled=false
x-b3-sampled: true                   sampled=false
x-b3-sampled: 0, x-b3-flags: 1       sampled=false
traceparent, all-zero trace-id       sampled=true
traceparent, all-zero parent-id      sampled=true

What is the expected behavior?

From the B3 propagation spec:

  • In the single b3 header, the sampling state is 1, 0 or d. d means debug, and debug implies an accept decision, so b3: {trace}-{span}-d should give sampled: true.
  • For X-B3-Sampled, the spec asks implementations to tolerate the legacy true / false values. true should give sampled: true.
  • X-B3-Flags: 1 is debug and also implies accept.

From W3C Trace Context §3.2.2.3–3.2.2.4, an all-zero trace-id or parent-id is invalid. The traceparent header should then be ignored. Today it is accepted, and fromHeaders goes on to use the all-zero ID.

What do you see instead?

b3: …-d and X-B3-Sampled: true both give sampled: false. The OpenTelemetry B3 propagator and Zipkin read both as sampled.

This loses data. HttpMiddleware.tracer uses the result as the parent of the server span (packages/effect/src/http/HttpMiddleware.ts:209). When the parent has sampled === false, every child span is unsampled too (packages/effect/src/internal/effect.ts:6017-6020). So a request that an upstream service marked for debug tracing, or that came from a tracer still sending true, has its whole server-side trace dropped. Debug is meant to have the opposite effect.

Additional information

The cause is in packages/effect/src/http/HttpTraceContext.ts:

  • b3: sampled: parts[2] ? parts[2] === "1" : true. This does not handle "d".
  • xb3: sampled: headers["x-b3-sampled"] ? headers["x-b3-sampled"] === "1" : true. This does not handle "true" or x-b3-flags.
  • w3c: /^[0-9a-f]{32}$/i and /^[0-9a-f]{16}$/i both match the all-zero value.

There are no tests for HttpTraceContext yet.

A possible fix:

// b3
sampled: parts[2] ? parts[2] === "1" || parts[2] === "d" : true

// xb3
sampled: headers["x-b3-flags"] === "1" ||
  (headers["x-b3-sampled"] ? headers["x-b3-sampled"] === "1" || headers["x-b3-sampled"] === "true" : true)

// w3c: reject all-zero ids
const allZero = /^0+$/
if (!w3cTraceId.test(traceId) || !w3cSpanId.test(spanId) || allZero.test(traceId) || allZero.test(spanId)) {
  return Option.none()
}

I'm happy to open a PR with the fix and tests.

aakarshitv avatar Oct 03 '26 07:10 aakarshitv