diff --git a/api/include/opentelemetry/baggage/baggage_context.h b/api/include/opentelemetry/baggage/baggage_context.h new file mode 100644 index 0000000000..b2b61703f8 --- /dev/null +++ b/api/include/opentelemetry/baggage/baggage_context.h @@ -0,0 +1,38 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/context/context.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE + +namespace baggage +{ + +static const std::string kBaggageHeader = "baggage"; + +inline nostd::shared_ptr GetBaggage( + const opentelemetry::context::Context &context) +{ + context::ContextValue context_value = context.GetValue(kBaggageHeader); + if (nostd::holds_alternative>(context_value)) + { + return nostd::get>(context_value); + } + static nostd::shared_ptr empty_baggage{ + new opentelemetry::baggage::Baggage()}; + return empty_baggage; +} + +inline context::Context SetBaggage(opentelemetry::context::Context &context, + nostd::shared_ptr baggage) +{ + return context.SetValue(kBaggageHeader, baggage); +} + +} // namespace baggage +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/baggage/propagation/baggage_propagator.h b/api/include/opentelemetry/baggage/propagation/baggage_propagator.h index aa669dd1d9..11abd3107e 100644 --- a/api/include/opentelemetry/baggage/propagation/baggage_propagator.h +++ b/api/include/opentelemetry/baggage/propagation/baggage_propagator.h @@ -1,6 +1,10 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + #pragma once #include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/baggage/baggage_context.h" #include "opentelemetry/context/propagation/text_map_propagator.h" #include "opentelemetry/version.h" @@ -10,41 +14,22 @@ namespace baggage namespace propagation { -static const nostd::string_view kBaggageHeader = "baggage"; - -inline nostd::shared_ptr GetBaggage(const context::Context &context) -{ - context::ContextValue context_value = context.GetValue(kBaggageHeader); - if (nostd::holds_alternative>(context_value)) - { - return nostd::get>(context_value); - } - static nostd::shared_ptr empty_baggage{new baggage::Baggage()}; - return empty_baggage; -} - -inline context::Context SetBaggage(context::Context &context, - nostd::shared_ptr baggage) -{ - return context.SetValue(kBaggageHeader, baggage); -} - -class BaggagePropagator : public context::propagation::TextMapPropagator +class BaggagePropagator : public opentelemetry::context::propagation::TextMapPropagator { public: - void Inject(context::propagation::TextMapCarrier &carrier, - const context::Context &context) noexcept override + void Inject(opentelemetry::context::propagation::TextMapCarrier &carrier, + const opentelemetry::context::Context &context) noexcept override { - auto baggage = GetBaggage(context); + auto baggage = opentelemetry::baggage::GetBaggage(context); carrier.Set(kBaggageHeader, baggage->ToHeader()); } - context::Context Extract(const context::propagation::TextMapCarrier &carrier, - context::Context &context) noexcept override + context::Context Extract(const opentelemetry::context::propagation::TextMapCarrier &carrier, + opentelemetry::context::Context &context) noexcept override { - nostd::string_view baggage_str = carrier.Get(kBaggageHeader); - auto baggage = Baggage::FromHeader(baggage_str); - return SetBaggage(context, baggage); + nostd::string_view baggage_str = carrier.Get(opentelemetry::baggage::kBaggageHeader); + auto baggage = opentelemetry::baggage::Baggage::FromHeader(baggage_str); + return opentelemetry::baggage::SetBaggage(context, baggage); } bool Fields(nostd::function_ref callback) const noexcept override @@ -54,4 +39,4 @@ class BaggagePropagator : public context::propagation::TextMapPropagator }; } // namespace propagation } // namespace baggage -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/baggage/propagation/baggage_propagator_test.cc b/api/test/baggage/propagation/baggage_propagator_test.cc index ad54c841ae..0f1f8f338c 100644 --- a/api/test/baggage/propagation/baggage_propagator_test.cc +++ b/api/test/baggage/propagation/baggage_propagator_test.cc @@ -1,7 +1,12 @@ + +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + #include "opentelemetry/baggage/propagation/baggage_propagator.h" #include #include #include +#include "opentelemetry/baggage/baggage_context.h" using namespace opentelemetry; using namespace opentelemetry::baggage::propagation; @@ -35,7 +40,7 @@ TEST(BaggagePropagatorTest, ExtractNoBaggageHeader) carrier.headers_ = {}; context::Context ctx1 = context::Context{}; context::Context ctx2 = format.Extract(carrier, ctx1); - auto ctx2_baggage = baggage::propagation::GetBaggage(ctx2); + auto ctx2_baggage = baggage::GetBaggage(ctx2); EXPECT_EQ(ctx2_baggage->ToHeader(), ""); } @@ -60,13 +65,13 @@ TEST(BaggagePropagatorTest, ExtractAndInjectBaggage) for (auto baggage : baggages) { BaggageCarrierTest carrier1; - carrier1.headers_[kBaggageHeader.data()] = baggage.first; - context::Context ctx1 = context::Context{}; - context::Context ctx2 = format.Extract(carrier1, ctx1); + carrier1.headers_[baggage::kBaggageHeader.data()] = baggage.first; + context::Context ctx1 = context::Context{}; + context::Context ctx2 = format.Extract(carrier1, ctx1); BaggageCarrierTest carrier2; format.Inject(carrier2, ctx2); - EXPECT_EQ(carrier2.headers_[kBaggageHeader.data()], baggage.second); + EXPECT_EQ(carrier2.headers_[baggage::kBaggageHeader.data()], baggage.second); std::vector fields; format.Fields([&fields](nostd::string_view field) { @@ -74,6 +79,6 @@ TEST(BaggagePropagatorTest, ExtractAndInjectBaggage) return true; }); EXPECT_EQ(fields.size(), 1); - EXPECT_EQ(fields[0], kBaggageHeader.data()); + EXPECT_EQ(fields[0], baggage::kBaggageHeader.data()); } }