Skip to content

Commit

Permalink
More tracer unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chusitoo committed Jan 3, 2023
1 parent 36cca42 commit 81b8557
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 10 deletions.
17 changes: 11 additions & 6 deletions opentracing-shim/include/shim_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ template<typename T, nostd::enable_if_t<std::is_base_of<opentracing::TextMapWrit
class CarrierWriterShim : public opentelemetry::context::propagation::TextMapCarrier
{
public:

CarrierWriterShim(const T& writer) : writer_(writer) {}

// returns the value associated with the passed key.
Expand All @@ -69,16 +68,13 @@ class CarrierWriterShim : public opentelemetry::context::propagation::TextMapCar
}

private:

const T& writer_;

};

template<typename T, nostd::enable_if_t<std::is_base_of<opentracing::TextMapReader, T>::value, bool> = true>
class CarrierReaderShim : public opentelemetry::context::propagation::TextMapCarrier
{
public:

CarrierReaderShim(const T& reader) : reader_(reader) {}

// returns the value associated with the passed key.
Expand Down Expand Up @@ -112,10 +108,19 @@ class CarrierReaderShim : public opentelemetry::context::propagation::TextMapCar
// Not required for Opentracing reader
}

private:
// list of all the keys in the carrier.
virtual bool Keys(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
reader_.ForeachKey([&callback]
(opentracing::string_view k, opentracing::string_view v) -> opentracing::expected<void> {
callback(k.data());
return opentracing::make_expected();
});
return true;
}

private:
const T& reader_;

};

static inline bool isBaggageEmpty(const BaggagePtr& baggage)
Expand Down
4 changes: 1 addition & 3 deletions opentracing-shim/src/tracer_shim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ using LinksList = std::vector<std::pair<opentelemetry::trace::SpanContext, RefsL
static opentelemetry::trace::StartSpanOptions makeOptionsShim(const opentracing::StartSpanOptions& options) noexcept
{
using opentracing::SpanReferenceType;

opentelemetry::trace::StartSpanOptions options_shim;
// If an explicit start timestamp is specified, a conversion MUST
// be done to match the OpenTracing and OpenTelemetry units.
opentelemetry::trace::StartSpanOptions options_shim;
options_shim.start_system_time = opentelemetry::common::SystemTimestamp{options.start_system_timestamp};
options_shim.start_steady_time = opentelemetry::common::SteadyTimestamp{options.start_steady_timestamp};

const auto& refs = options.references;

// If a list of Span references is specified...
if (!refs.empty())
{
Expand Down
122 changes: 121 additions & 1 deletion opentracing-shim/test/tracer_shim_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,56 @@
// SPDX-License-Identifier: Apache-2.0

#include "tracer_shim.h"
#include "span_context_shim.h"

#include <opentelemetry/baggage/baggage_context.h>
#include <opentracing/noop.h>

#include <gtest/gtest.h>

namespace trace_api = opentelemetry::trace;
namespace nostd = opentelemetry::nostd;
namespace context = opentelemetry::context;
namespace baggage = opentelemetry::baggage;
namespace shim = opentelemetry::opentracingshim;

struct MockPropagator : public context::propagation::TextMapPropagator
{
// Returns the context that is stored in the carrier with the TextMapCarrier as extractor.
context::Context Extract(const context::propagation::TextMapCarrier &carrier,
context::Context &context) noexcept override
{
std::vector<std::pair<std::string, std::string>> kvs;
carrier.Keys([&carrier,&kvs](nostd::string_view k){
kvs.emplace_back(k, carrier.Get(k));
return true;
});
is_extracted = true;
return baggage::SetBaggage(context, nostd::shared_ptr<baggage::Baggage>(new baggage::Baggage(kvs)));
}

// Sets the context for carrier with self defined rules.
void Inject(context::propagation::TextMapCarrier &carrier,
const context::Context &context) noexcept override
{
auto baggage = baggage::GetBaggage(context);
baggage->GetAllEntries([&carrier](nostd::string_view k, nostd::string_view v){
carrier.Set(k, v);
return true;
});
is_injected = true;
}

// Gets the fields set in the carrier by the `inject` method
bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
return true;
}

bool is_extracted = false;
bool is_injected = false;
};

struct TextMapCarrier : opentracing::TextMapReader, opentracing::TextMapWriter {
TextMapCarrier(std::unordered_map<std::string, std::string>& text_map_)
: text_map(text_map_) {}
Expand Down Expand Up @@ -75,11 +115,20 @@ class TracerShimTest : public testing::Test
{
public:
std::shared_ptr<opentracing::Tracer> tracer_shim;
MockPropagator* text_map_format;
MockPropagator* http_headers_format;

protected:
virtual void SetUp()
{
tracer_shim = shim::TracerShim::createTracerShim();
using context::propagation::TextMapPropagator;

text_map_format = new MockPropagator();
http_headers_format = new MockPropagator();

tracer_shim = shim::TracerShim::createTracerShim(trace_api::Provider::GetTracerProvider(),
{ .text_map = nostd::shared_ptr<TextMapPropagator>(text_map_format),
.http_headers = nostd::shared_ptr<TextMapPropagator>(http_headers_format) });
}

virtual void TearDown()
Expand Down Expand Up @@ -119,6 +168,30 @@ TEST_F(TracerShimTest, InjectNullContext)
ASSERT_TRUE(text_map.empty());
}

TEST_F(TracerShimTest, InjectTextMap)
{
ASSERT_FALSE(text_map_format->is_injected);
ASSERT_FALSE(http_headers_format->is_injected);

std::unordered_map<std::string, std::string> text_map;
auto span_shim = tracer_shim->StartSpan("a");
tracer_shim->Inject(span_shim->context(), TextMapCarrier{text_map});
ASSERT_TRUE(text_map_format->is_injected);
ASSERT_FALSE(http_headers_format->is_injected);
}

TEST_F(TracerShimTest, InjectHttpsHeaders)
{
ASSERT_FALSE(text_map_format->is_injected);
ASSERT_FALSE(http_headers_format->is_injected);

std::unordered_map<std::string, std::string> text_map;
auto span_shim = tracer_shim->StartSpan("a");
tracer_shim->Inject(span_shim->context(), HTTPHeadersCarrier{text_map});
ASSERT_FALSE(text_map_format->is_injected);
ASSERT_TRUE(http_headers_format->is_injected);
}

TEST_F(TracerShimTest, ExtractInvalidCarrier)
{
auto result = tracer_shim->Extract(std::cin);
Expand All @@ -131,3 +204,50 @@ TEST_F(TracerShimTest, ExtractNullContext)
auto result = tracer_shim->Extract(TextMapCarrier{text_map});
ASSERT_EQ(result.value(), nullptr);
}

TEST_F(TracerShimTest, ExtractTextMap)
{
ASSERT_FALSE(text_map_format->is_extracted);
ASSERT_FALSE(http_headers_format->is_extracted);

std::unordered_map<std::string, std::string> text_map;
auto result = tracer_shim->Extract(TextMapCarrier{text_map});
ASSERT_EQ(result.value(), nullptr);
ASSERT_TRUE(text_map_format->is_extracted);
ASSERT_FALSE(http_headers_format->is_extracted);
}

TEST_F(TracerShimTest, ExtractHttpsHeaders)
{
ASSERT_FALSE(text_map_format->is_extracted);
ASSERT_FALSE(http_headers_format->is_extracted);

std::unordered_map<std::string, std::string> text_map;
auto result = tracer_shim->Extract(HTTPHeadersCarrier{text_map});
ASSERT_EQ(result.value(), nullptr);
ASSERT_FALSE(text_map_format->is_extracted);
ASSERT_TRUE(http_headers_format->is_extracted);
}

TEST_F(TracerShimTest, ExtractOnlyBaggage)
{
std::unordered_map<std::string, std::string> text_map;
auto span_shim = tracer_shim->StartSpan("a");
span_shim->SetBaggageItem("foo", "bar");

ASSERT_EQ(span_shim->BaggageItem("foo"), "bar");

TextMapCarrier carrier{text_map};
tracer_shim->Inject(span_shim->context(), carrier);

auto span_context = tracer_shim->Extract(carrier);
ASSERT_TRUE(span_context.value() != nullptr);

// auto span_context_shim = dynamic_cast<shim::SpanContextShim*>(span_context.value().get());
// ASSERT_TRUE(span_context_shim != nullptr);
// ASSERT_TRUE(span_context_shim->context().IsValid());

// std::string value;
// ASSERT_TRUE(span_context_shim->BaggageItem("foo", value));
// ASSERT_EQ(value, "bar");
}

0 comments on commit 81b8557

Please sign in to comment.