Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix baggage propagation for empty/invalid baggage context #1367

Merged
merged 6 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class BaggagePropagator : public opentelemetry::context::propagation::TextMapPro
const opentelemetry::context::Context &context) noexcept override
{
auto baggage = opentelemetry::baggage::GetBaggage(context);
carrier.Set(kBaggageHeader, baggage->ToHeader());
auto header = baggage->ToHeader();
if (header.size())
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need do something similar in line 36 (new file), and ignore call to SetBaggage if FromHeader() returned an empty or invalid baggage?

Copy link
Member Author

@lalitb lalitb May 3, 2022

Choose a reason for hiding this comment

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

Yes, was thinking so, but it would need more changes in Context API - to add a copy constructor. Just tried to avoid API changes as of now.

Copy link
Member Author

@lalitb lalitb May 3, 2022

Choose a reason for hiding this comment

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

Ok seems the default copy constructor is usable for context::Context, have done the changes as suggested.

{
carrier.Set(kBaggageHeader, header);
}
}

context::Context Extract(const opentelemetry::context::propagation::TextMapCarrier &carrier,
Expand Down
29 changes: 29 additions & 0 deletions api/test/baggage/propagation/baggage_propagator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,32 @@ TEST(BaggagePropagatorTest, ExtractAndInjectBaggage)
EXPECT_EQ(fields[0], baggage::kBaggageHeader.data());
}
}

TEST(BaggagePropagatorTest, InjectEmptyHeader)
{
// Test Missing baggage from context
BaggageCarrierTest carrier;
context::Context ctx = context::Context{};
format.Inject(carrier, ctx);
EXPECT_EQ(carrier.headers_.find(baggage::kBaggageHeader), carrier.headers_.end());

{
// Test empty baggage in context
BaggageCarrierTest carrier1;
carrier1.headers_[baggage::kBaggageHeader.data()] = "";
context::Context ctx1 = context::Context{};
context::Context ctx2 = format.Extract(carrier1, ctx1);
format.Inject(carrier, ctx2);
EXPECT_EQ(carrier.headers_.find(baggage::kBaggageHeader), carrier.headers_.end());
}
{
// Invali baggage in context
lalitb marked this conversation as resolved.
Show resolved Hide resolved
BaggageCarrierTest carrier1;
carrier1.headers_[baggage::kBaggageHeader.data()] = "InvalidBaggageData";
context::Context ctx1 = context::Context{};
context::Context ctx2 = format.Extract(carrier1, ctx1);

format.Inject(carrier, ctx2);
EXPECT_EQ(carrier.headers_.find(baggage::kBaggageHeader), carrier.headers_.end());
}
}