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

Separate baggage<->Context api from Baggage Propagator #963

Merged
merged 6 commits into from
Aug 30, 2021
Merged
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
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
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());
}
}