Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ Increment the:
max_queue_size, with a warning, matching `BatchSpanProcessor`.
[#4768](https://github.com/open-telemetry/opentelemetry-cpp/pull/4768)

* [API] Correct W3C Baggage encoding and decoding together: `Baggage::ToHeader`
percent-encodes spaces as `%20` instead of `+`, and `Baggage::FromHeader`
preserves literal `+` in keys and values instead of decoding it as a space.
Plus signs are emitted as `%2B`, preserving them when C++ forwards baggage.
Compatibility: spaces encoded as `+` by older C++ senders are now read as
literal plus signs, as they already are by conforming non-C++ receivers.
Older C++ readers already support `%20` and `%2B` from newer senders.
[#4765](https://github.com/open-telemetry/opentelemetry-cpp/pull/4765)

## [1.30.0] 2026-10-22

This release contains:
Expand Down
12 changes: 3 additions & 9 deletions api/include/opentelemetry/baggage/baggage.h

@denizariyan denizariyan Nov 21, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The decode side is intentionally unchanged: FromHeader still decodes + as a space, so headers from older C++ senders keep reading the same way. Whether to stop treating + as a space is left as a maintainer decision; I'm happy to follow up with a separate PR for that if wanted.

I think changing only the encode side makes things worse when C++ sits in the middle of a call chain.

Example: Service A (Go) -> Service B (C++) -> Service C (Go).

  1. A sets baggage expr=1+1. Per the W3C spec, + is a normal character, so A sends baggage: expr=1+1.
  2. B (C++) receives it. FromHeader still decodes + as a space, so B reads the value as 1 1.
  3. B forwards the baggage to C:
    • Before this PR: ToHeader turns the space back into +, so C receives expr=1+1. B misread the value itself, but C still gets the right one.
    • With this PR: ToHeader sends the space as %20, so C receives expr=1%201 and reads it as 1 1. The value is now wrong for every service after B, not just inside B.

The way to avoid this is to also stop decoding + as a space (line 354). The cost is that spaces sent by older C++ versions (as +) would be read as + by newer C++ services. But non-C++ receivers already read them that way today, so this only affects C++ to C++ calls between different versions and fixes the issue at once for newer versions.

IMO the better trade off is to change both sides in a single PR/release.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed: the encode-only change can turn B's existing misinterpretation into a persistent downstream corruption. The proposed update now changes both sides together: spaces are encoded as %20, and a literal + is no longer decoded as a space.

For your exact example, incoming expr=1+1 now remains 1+1 inside C++ B and is forwarded as expr=1%2B1, so a conforming receiver C reads 1+1. Incoming %20 still becomes a space, and %2B/%2b still becomes a plus. The same rules apply to keys and values; metadata remains in wire form.

I changed the old extraction expectation that deliberately treated bar+4 as bar 4 and added public Baggage tests plus carrier/context BaggagePropagator extraction/injection coverage, including the forwarded header for expr=1+1, round-trips, key/value plus signs and spaces, and unchanged metadata. The changelog and public docs now explain the real tradeoff: spaces emitted as + by older C++ senders become literal plus signs at newer C++ receivers. There is no way to distinguish those legacy spaces from intentional plus signs; non-C++ receivers already face that behavior. Older C++ receivers can read the newer sender's %20 and %2B.

This supersedes the earlier encode-only description. The separate inline-API ABI-policy decision in #4765 (comment) remains unresolved; I have not assumed an exception or introduced an arbitrary ABI guard. The paired correction is pushed in 8412e51. The exact public-propagator example fails before and passes after in C++14; 16 baggage + 7 propagator tests pass in each of ABI1/ABI2 configurations, with 22 more propagation tests passing. The changed C++ files pass clang-format 18.1.8. These direct tests do not settle the ABI-policy question or claim full SDK/CMake validation.

Prepared by omp, an AI coding assistant running in the omp harness, on behalf of @hidingmyname.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@denizariyan

Agreed, my approval and the #4745 triage missed the forwarding case.

B decodes the header and calls ToHeader(), built at d9838a56:

A sends            main forwards      this PR forwards   both sides changed
k=1+1              k=1+1              k=1%201            k=1%2B1
k=hello%20world    k=hello+world      k=hello%20world    k=hello%20world

Main and this PR each corrupt one of the two values for every service after B, and only changing both sides forwards both.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The paired correction is already present in the current head, 8412e51e6dc721a9c7a7e20b91f220d07bba9295; the forwarding table above was built from the earlier encode-only d9838a56.

The two requested edits are included:

With that head, expr=1+1 extracts as 1+1 and forwards as expr=1%2B1; hello%20world remains a space value and forwards with %20. The public forwarding regression failed on d9838a56 and passed on 8412e51, as recorded in the earlier reply. All 16 baggage and 7 baggage-propagator tests also passed in the current-head macOS job; that job's failure is in the SDK metrics multi-reader gauge timestamp assertions, not baggage.

The inline-API ABI-policy question for @marcalff / @lalitb remains unresolved. This reply only identifies the implemented semantic correction; it does not assume an ABI exception or mark the review thread resolved.

Posted by the omp AI coding assistant in the omp harness on behalf of @hidingmyname.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks

Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class OPENTELEMETRY_EXPORT Baggage
}

// Returns shared_ptr of baggage after extracting key-value pairs from header
// Percent-decodes keys and values; a literal '+' remains a plus, not a space.
static nostd::shared_ptr<Baggage> FromHeader(nostd::string_view header) noexcept
{
if (header.size() > kMaxSize)
Expand Down Expand Up @@ -165,6 +166,7 @@ class OPENTELEMETRY_EXPORT Baggage
}

// Creates string from baggage object.
// Spaces are encoded as %20 and plus signs as %2B in keys and values.
std::string ToHeader() const noexcept
{
std::string header_s;
Expand Down Expand Up @@ -320,10 +322,6 @@ class OPENTELEMETRY_EXPORT Baggage
{
ret.push_back(c);
}
else if (c == ' ')
{
ret.push_back('+');
}
else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Dropping the space branch changes the observable behavior of an inline function in api/include.

docs/abi-policy.md, "Definitions in API headers", puts such changes in the experimental ABI, with correctness fixes as a possible exception.

#4717 changed API behavior without a guard, so the exception may apply here.

A mixed-version process emits either + or %20, and this reader decodes both.

Is the ungated change acceptable, @marcalff @lalitb?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The decoder is now included in the proposed correction following #4765 (comment): %20 represents a space, while a literal + is preserved rather than decoded as a space. Keeping the old decoder with only the new encoder corrupts expr=1+1 when C++ forwards it: it becomes expr=1%201. With the paired correction it remains 1+1 in C++ and is forwarded as expr=1%2B1.

That means the earlier compatibility argument needs qualification. Older C++ readers already understand %20 and %2B, but newer C++ readers will interpret old C++ senders' form-style + spaces as literal plus signs. This tradeoff is now explicit in the changelog/docs and covered by public Baggage/propagator regression cases. It is not safe to claim unchanged mixed-version behavior in both directions.

The ABI question itself is still unresolved. Both changes alter inline definitions and observable behavior in api/include; docs/abi-policy.md explicitly covers that, including the possibility of a critical correctness exception. Wire-format compatibility does not remove the mixed-header-definition/ODR concern in one process. Commit 8412e51 keeps the current ungated proposal without changing policy or asserting an approved exception. A maintainer decision is still required between accepting the correctness exception and restricting the paired correction to the experimental ABI; this reply does not mark that decision resolved.

Prepared by omp, an AI coding assistant running in the omp harness, on behalf of @hidingmyname.

{
ret.push_back('%');
Expand All @@ -335,7 +333,7 @@ class OPENTELEMETRY_EXPORT Baggage
return ret;
}

// Uri decode key value pairs after extracting from header
// Percent-decode key-value pairs without applying form-style '+' decoding.
static std::string UrlDecode(nostd::string_view str, bool &err)
{
std::string ret;
Expand All @@ -355,10 +353,6 @@ class OPENTELEMETRY_EXPORT Baggage
common::detail::HexDigitValue(str[i + 2])));
i += 2;
}
else if (str[i] == '+')
{
ret.push_back(' ');
}
// See https://www.w3.org/TR/baggage/#definition
else if ((str[i] >= '!') /* 21 */
&& (str[i] <= '~') /* 7E */
Expand Down
75 changes: 72 additions & 3 deletions api/test/baggage/baggage_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ TEST(BaggageTest, ValidateExtractHeader)
{"v1", "v2; metadata"}}, // key and value are trimmed
{"1a-2f%40foo=bar%251,a%2A%2Ffoo-_%2Fbar=bar+4",
{"1a-2f@foo", "a*/foo-_/bar"},
{"bar%1", "bar 4"}}, // decoding is done properly
{"bar%1", "bar+4"}}, // percent escapes decode and a literal plus remains a plus
{"k=hello%20world", {"k"}, {"hello world"}}, // %20 decodes as space
{"field=foo:bar", {"field"}, {"foo:bar"}}, // colon in value
{"mixed=a/b:c?d=e", {"mixed"}, {"a/b:c?d=e"}}, // mixed special characters
{"k1=v1,invalidmember,k2=v2", {"k1", "k2"}, {"v1", "v2"}}, // invalid member is skipped
Expand Down Expand Up @@ -256,10 +257,10 @@ TEST(BaggageTest, ValidateInjectHeader)
{{"k3", "k2", "k1"}, {"", "v2", "v1"}, "k1=v1,k2=v2,k3="}, // empty value
{{"1a-2f@foo", "a*/foo-_/bar"},
{"bar%1", "bar 4"},
"a%2A%2Ffoo-_%2Fbar=bar+4,1a-2f%40foo=bar%251"}, // encoding is done properly
"a%2A%2Ffoo-_%2Fbar=bar%204,1a-2f%40foo=bar%251"}, // encoding is done properly
{{"foo 1"},
{"bar 1; metadata ; flag=encoded%20value"},
"foo+1=bar+1; metadata ; flag=encoded%20value"}};
"foo%201=bar%201; metadata ; flag=encoded%20value"}};

for (auto &testcase : testcases)
{
Expand All @@ -272,6 +273,74 @@ TEST(BaggageTest, ValidateInjectHeader)
}
}

TEST(BaggageTest, SpaceIsPercentEncoded)
{
auto baggage = Baggage().Set("k", "hello world");
auto header = baggage->ToHeader();
EXPECT_EQ(header, "k=hello%20world");

std::string value;
EXPECT_TRUE(Baggage::FromHeader(header)->GetValue("k", value));
EXPECT_EQ(value, "hello world");
}

TEST(BaggageTest, PreserveLiteralPlusOnExtract)
{
struct
{
const char *header;
const char *key;
const char *value;
const char *encoded_header;
} testcases[] = {
{"expr=1+1", "expr", "1+1", "expr=1%2B1"},
{"key+name=hello+world", "key+name", "hello+world", "key%2Bname=hello%2Bworld"},
{"key%2Bname=hello%2bworld", "key+name", "hello+world", "key%2Bname=hello%2Bworld"},
{"key+%20%2B=value+%20%2B;p=+%20%2B", "key+ +", "value+ +;p=+%20%2B",
"key%2B%20%2B=value%2B%20%2B;p=+%20%2B"}};

for (const auto &testcase : testcases)
{
SCOPED_TRACE(testcase.header);
auto baggage = Baggage::FromHeader(testcase.header);
std::string value;
ASSERT_TRUE(baggage->GetValue(testcase.key, value));
EXPECT_EQ(value, testcase.value);
EXPECT_EQ(baggage->ToHeader(), testcase.encoded_header);

auto round_tripped = Baggage::FromHeader(baggage->ToHeader());
ASSERT_TRUE(round_tripped->GetValue(testcase.key, value));
EXPECT_EQ(value, testcase.value);
EXPECT_EQ(round_tripped->ToHeader(), testcase.encoded_header);
}
}

TEST(BaggageTest, SpaceAndPlusRoundTripInKeysAndValues)
{
struct
{
const char *key;
const char *value;
const char *header;
} testcases[] = {{"key name", "hello world", "key%20name=hello%20world"},
{"key+name", "1+1", "key%2Bname=1%2B1"},
{"key + name", "1 + 1", "key%20%2B%20name=1%20%2B%201"},
{"key + name", "1 + 1;p=+%20%2B", "key%20%2B%20name=1%20%2B%201;p=+%20%2B"}};

for (const auto &testcase : testcases)
{
SCOPED_TRACE(testcase.header);
auto baggage = Baggage().Set(testcase.key, testcase.value);
EXPECT_EQ(baggage->ToHeader(), testcase.header);

auto round_tripped = Baggage::FromHeader(baggage->ToHeader());
std::string value;
ASSERT_TRUE(round_tripped->GetValue(testcase.key, value));
EXPECT_EQ(value, testcase.value);
EXPECT_EQ(round_tripped->ToHeader(), testcase.header);
}
}

TEST(BaggageTest, BaggageGet)
{
auto header = header_with_custom_entries(Baggage::kMaxKeyValuePairs);
Expand Down
37 changes: 37 additions & 0 deletions api/test/baggage/propagation/baggage_propagator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,43 @@ TEST(BaggagePropagatorTest, ExtractAndInjectBaggage)
}
}

TEST(BaggagePropagatorTest, PreserveSpaceAndPlusOnExtractAndInject)
{
struct
{
const char *header;
const char *key;
const char *value;
const char *forwarded_header;
} testcases[] = {
{"expr=1+1", "expr", "1+1", "expr=1%2B1"},
{"key+name=hello+world", "key+name", "hello+world", "key%2Bname=hello%2Bworld"},
{"key%2Bname=hello%2bworld", "key+name", "hello+world", "key%2Bname=hello%2Bworld"},
{"key%20name=hello%20world", "key name", "hello world", "key%20name=hello%20world"},
{"key+%20%2B=1+%20%2B1;p=+%20%2B", "key+ +", "1+ +1;p=+%20%2B",
"key%2B%20%2B=1%2B%20%2B1;p=+%20%2B"}};

for (const auto &testcase : testcases)
{
SCOPED_TRACE(testcase.header);
BaggageCarrierTest inbound;
inbound.Set(baggage::kBaggageHeader, testcase.header);
context::Context base;
auto extracted = format.Extract(inbound, base);
std::string value;
ASSERT_TRUE(baggage::GetBaggage(extracted)->GetValue(testcase.key, value));
EXPECT_EQ(value, testcase.value);

BaggageCarrierTest outbound;
format.Inject(outbound, extracted);
EXPECT_EQ(outbound.Get(baggage::kBaggageHeader), testcase.forwarded_header);

auto round_tripped = format.Extract(outbound, base);
ASSERT_TRUE(baggage::GetBaggage(round_tripped)->GetValue(testcase.key, value));
EXPECT_EQ(value, testcase.value);
}
}

TEST(BaggagePropagatorTest, InjectEmptyHeader)
{
// Test Missing baggage from context
Expand Down
30 changes: 30 additions & 0 deletions docs/public/api/GettingStarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,33 @@ Trace Id, and flags. Context Propagation is an important mechanism in distribute
tracing to transfer this Context across service boundary often through HTTP headers.
OpenTelemetry provides a text-based approach to propagate context to remote services
using the W3C Trace Context HTTP headers.

Baggage propagation
~~~~~~~~~~~~~~~~~~~

``opentelemetry::baggage::propagation::BaggagePropagator`` extracts and injects
the W3C ``baggage`` header. ``Baggage::FromHeader`` percent-decodes keys and
values without treating a literal ``+`` as a space. ``Baggage::ToHeader``
encodes spaces as ``%20`` and plus signs as ``%2B``; baggage metadata remains
in its wire form.

For example, an incoming ``baggage: expr=1+1`` has the value ``1+1``, not
``1 1``. Forwarding it produces ``baggage: expr=1%2B1``, which preserves the
value at the next receiver. An incoming ``expr=1%201`` instead has the value
``1 1`` and is forwarded as ``expr=1%201``. These rules apply to keys as well
as values.

Compatibility with older C++ senders
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Older C++ versions encoded spaces as ``+`` and decoded ``+`` as a space.
Consequently, a space sent as ``+`` by an older C++ service is read as a
literal plus sign by a newer C++ service, just as it already is by conforming
non-C++ receivers. A receiver cannot distinguish that legacy space from an
intentional literal plus. Services that need to send spaces to newer C++
receivers should use a sender that emits ``%20``. Older C++ receivers already
decode both ``%20`` and ``%2B``, so spaces and plus signs sent by newer C++
services remain readable by older C++ versions.

Changing encoding and decoding together avoids corrupting literal plus
signs when C++ is an intermediary between other language implementations.
Loading