Video in progress

What is OpenTelemetry `baggage`, how does it differ from the trace context, and what are the risks of using it?

Baggage is a set of key-value pairs (the baggage header) that travels with the request across services, for example tenantId or channel. The trace context carries only IDs, while baggage carries business context. But it goes over the network on every call, it can leak to external services, and it does not become a span attribute or a log field on its own.

📖 Answer

traceparent only says "which trace is this, and which span did it come from". Say you want the tenantId that the gateway determined to show up in the log of a service five hops down. Adding it as a parameter to every API is awkward. Baggage solves this:

baggage: tenantId=acme,channel=mobile

In Spring Boot 3 with Micrometer Tracing, baggage fields are enabled through configuration and also end up in MDC:

management:
  tracing:
    baggage:
      remote-fields: tenantId, channel   # propagated via the header
      correlation:
        fields: tenantId                 # also put into MDC

Setting a value in code:

try (BaggageInScope b = tracer.createBaggageInScope("tenantId", tenantId)) {
    orderClient.create(request);   // the header is added automatically
}

Risks:

  • Size: it travels in every HTTP call and Kafka message. Ten fields with long values bloat the header, and some proxies reject a large header.
  • Privacy: baggage can also go out in calls to external APIs. Don't put an email, phone number or token in it, and strip outgoing baggage at the gateway on the edge.
  • Trust: a baggage header that comes from outside is user input. Don't base an authorization decision on the tenantId in it.
  • Not automatic: baggage does not become a span attribute by itself; to search for it in the trace backend you have to write it to the span separately.