Skip to content

Commit

Permalink
fix baggage propagation for empty/invalid baggage context (#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored May 4, 2022
1 parent 9c734b3 commit 59a48c1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
16 changes: 14 additions & 2 deletions api/include/opentelemetry/baggage/propagation/baggage_propagator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@ 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())
{
carrier.Set(kBaggageHeader, header);
}
}

context::Context Extract(const opentelemetry::context::propagation::TextMapCarrier &carrier,
opentelemetry::context::Context &context) noexcept override
{
nostd::string_view baggage_str = carrier.Get(opentelemetry::baggage::kBaggageHeader);
auto baggage = opentelemetry::baggage::Baggage::FromHeader(baggage_str);
return opentelemetry::baggage::SetBaggage(context, baggage);

if (baggage->ToHeader().size())
{
return opentelemetry::baggage::SetBaggage(context, baggage);
}
else
{
return context;
}
}

bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
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());
}
{
// Invalid baggage in context
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());
}
}

2 comments on commit 59a48c1

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 59a48c1 Previous: 9c734b3 Ratio
BM_BaselineBuffer/2 14634413.719177246 ns/iter 5075492.858886719 ns/iter 2.88

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 59a48c1 Previous: 9c734b3 Ratio
BM_SpinLockThrashing/2/process_time/real_time 1.386978512718564 ms/iter 0.23367776553392827 ms/iter 5.94
BM_ProcYieldSpinLockThrashing/2/process_time/real_time 1.0458922040635261 ms/iter 0.4553000132242839 ms/iter 2.30
BM_NaiveSpinLockThrashing/2/process_time/real_time 0.9974096990694665 ms/iter 0.23426236333073797 ms/iter 4.26
BM_ExtractBaggageWith180Entries 4.102228229804038 ns/iter 1.6379308423341576 ns/iter 2.50

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.