Skip to content

Commit

Permalink
Separate baggage<->Context api from Baggage Propagator (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Aug 30, 2021
1 parent 4ae969b commit 7b1842b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
38 changes: 38 additions & 0 deletions api/include/opentelemetry/baggage/baggage_context.h
Original file line number Diff line number Diff line change
@@ -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<opentelemetry::baggage::Baggage> GetBaggage(
const opentelemetry::context::Context &context)
{
context::ContextValue context_value = context.GetValue(kBaggageHeader);
if (nostd::holds_alternative<nostd::shared_ptr<opentelemetry::baggage::Baggage>>(context_value))
{
return nostd::get<nostd::shared_ptr<opentelemetry::baggage::Baggage>>(context_value);
}
static nostd::shared_ptr<opentelemetry::baggage::Baggage> empty_baggage{
new opentelemetry::baggage::Baggage()};
return empty_baggage;
}

inline context::Context SetBaggage(opentelemetry::context::Context &context,
nostd::shared_ptr<opentelemetry::baggage::Baggage> baggage)
{
return context.SetValue(kBaggageHeader, baggage);
}

} // namespace baggage
OPENTELEMETRY_END_NAMESPACE
43 changes: 14 additions & 29 deletions api/include/opentelemetry/baggage/propagation/baggage_propagator.h
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -10,41 +14,22 @@ namespace baggage
namespace propagation
{

static const nostd::string_view kBaggageHeader = "baggage";

inline nostd::shared_ptr<baggage::Baggage> GetBaggage(const context::Context &context)
{
context::ContextValue context_value = context.GetValue(kBaggageHeader);
if (nostd::holds_alternative<nostd::shared_ptr<baggage::Baggage>>(context_value))
{
return nostd::get<nostd::shared_ptr<baggage::Baggage>>(context_value);
}
static nostd::shared_ptr<baggage::Baggage> empty_baggage{new baggage::Baggage()};
return empty_baggage;
}

inline context::Context SetBaggage(context::Context &context,
nostd::shared_ptr<baggage::Baggage> 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<bool(nostd::string_view)> callback) const noexcept override
Expand All @@ -54,4 +39,4 @@ class BaggagePropagator : public context::propagation::TextMapPropagator
};
} // namespace propagation
} // namespace baggage
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
17 changes: 11 additions & 6 deletions api/test/baggage/propagation/baggage_propagator_test.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/baggage/propagation/baggage_propagator.h"
#include <gtest/gtest.h>
#include <map>
#include <string>
#include "opentelemetry/baggage/baggage_context.h"

using namespace opentelemetry;
using namespace opentelemetry::baggage::propagation;
Expand Down Expand Up @@ -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(), "");
}

Expand All @@ -60,20 +65,20 @@ 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<std::string> fields;
format.Fields([&fields](nostd::string_view field) {
fields.push_back(field.data());
return true;
});
EXPECT_EQ(fields.size(), 1);
EXPECT_EQ(fields[0], kBaggageHeader.data());
EXPECT_EQ(fields[0], baggage::kBaggageHeader.data());
}
}

0 comments on commit 7b1842b

Please sign in to comment.