Skip to content

Commit

Permalink
Tracer unit tests wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chusitoo committed Jan 2, 2023
1 parent 45e0c4b commit 321a801
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 118 deletions.
8 changes: 7 additions & 1 deletion opentracing-shim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#set(this_target opentelemetry_opentracing_shim)
set(this_target opentelemetry_opentracing_shim)

#message("CMAKE_CURRENT_LIST_DIR " ${CMAKE_CURRENT_LIST_DIR})
#message("CMAKE_CURRENT_SOURCE_DIR " ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down Expand Up @@ -33,6 +33,12 @@
# FILES_MATCHING
# PATTERN "*.h")

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/third_party/opentracing-cpp/include
${CMAKE_SOURCE_DIR}/third_party/opentracing-cpp/build/include
${CMAKE_SOURCE_DIR}/third_party/opentracing-cpp/3rd_party/include)

add_subdirectory(src)

if(BUILD_TESTING)
Expand Down
2 changes: 1 addition & 1 deletion opentracing-shim/include/span_shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SpanShim : public opentracing::Span

void logImpl(opentracing::SystemTime timestamp, nostd::span<const EventEntry> fields) noexcept;

TracerShim tracer_;
const TracerShim& tracer_;
SpanPtr span_;
SpanContextShim context_;
mutable opentelemetry::common::SpinLockMutex context_lock_;
Expand Down
12 changes: 6 additions & 6 deletions opentracing-shim/include/tracer_shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TracerShim : public opentracing::Tracer
*
* @returns a {@code opentracing::Tracer}.
*/
static inline TracerShim createTracerShim()
static inline std::shared_ptr<opentracing::Tracer> createTracerShim() noexcept
{
return createTracerShim(opentelemetry::trace::Provider::GetTracerProvider());
}
Expand All @@ -48,8 +48,8 @@ class TracerShim : public opentracing::Tracer
* @param propagators the {@code OpenTracingPropagators} instance used to create this shim.
* @returns a {@code opentracing::Tracer}.
*/
static inline TracerShim createTracerShim(const TracerProviderPtr& provider,
const OpenTracingPropagators& propagators = {})
static inline std::shared_ptr<opentracing::Tracer> createTracerShim(const TracerProviderPtr& provider,
const OpenTracingPropagators& propagators = {}) noexcept
{
return createTracerShim(provider->GetTracer("opentracing-shim"), propagators);
}
Expand All @@ -61,10 +61,10 @@ class TracerShim : public opentracing::Tracer
*
* @returns a {@code opentracing::Tracer}.
*/
static inline TracerShim createTracerShim(const TracerPtr& tracer,
const OpenTracingPropagators& propagators = {})
static inline std::shared_ptr<opentracing::Tracer> createTracerShim(const TracerPtr& tracer,
const OpenTracingPropagators& propagators = {}) noexcept
{
return TracerShim(tracer, propagators);
return std::shared_ptr<opentracing::Tracer>(new (std::nothrow) TracerShim(tracer, propagators));
}

std::unique_ptr<opentracing::Span> StartSpanWithOptions(opentracing::string_view operation_name,
Expand Down
20 changes: 7 additions & 13 deletions opentracing-shim/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
set(THIS_TARGET opentelemetry_opentracing_shim)
set(TARGET_NAME opentracing_shim)

include_directories(
${CMAKE_CURRENT_LIST_DIR}/../include
${CMAKE_SOURCE_DIR}/third_party/opentracing-cpp/include
${CMAKE_SOURCE_DIR}/third_party/opentracing-cpp/build/include
${CMAKE_SOURCE_DIR}/third_party/opentracing-cpp/3rd_party/include)
set(this_target opentelemetry_opentracing_shim)
set(target_name opentracing_shim)

file(GLOB_RECURSE SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/*.cc)
file(GLOB_RECURSE HEADER_FILES ${CMAKE_CURRENT_LIST_DIR}/../include/*.h)

add_library(${THIS_TARGET} SHARED ${SOURCE_FILES} ${HEADER_FILES})
add_library(${this_target} SHARED ${SOURCE_FILES} ${HEADER_FILES})

set_target_properties(${THIS_TARGET} PROPERTIES EXPORT_NAME ${TARGET_NAME})
set_target_properties(${this_target} PROPERTIES EXPORT_NAME ${target_name})

target_include_directories(
${THIS_TARGET}
${this_target}
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

target_link_libraries(${THIS_TARGET} PUBLIC opentelemetry_api)
target_link_libraries(${this_target} PUBLIC opentelemetry_api opentracing)

install(
TARGETS ${THIS_TARGET}
TARGETS ${this_target}
EXPORT "${PROJECT_NAME}-target"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down
22 changes: 15 additions & 7 deletions opentracing-shim/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
set(this_target shim_test)

add_executable(${this_target} "${this_target}.cc")
target_link_libraries(${this_target} ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_api)
gtest_add_tests(
TARGET ${this_target}
TEST_PREFIX ${this_target}.
TEST_LIST ${this_target})
file(GLOB source_files ${CMAKE_CURRENT_LIST_DIR}/*.*)

add_executable(${this_target} ${source_files})

target_link_libraries(${this_target}
${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
opentelemetry_api
opentelemetry_opentracing_shim
opentracing)

#gtest_add_tests(
# TARGET ${this_target}
# TEST_PREFIX ${this_target}.
# TEST_LIST ${this_target})
47 changes: 0 additions & 47 deletions opentracing-shim/test/shim_test.cc

This file was deleted.

133 changes: 133 additions & 0 deletions opentracing-shim/test/tracer_shim_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "tracer_shim.h"

#include <opentracing/noop.h>

#include <gtest/gtest.h>

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

struct TextMapCarrier : opentracing::TextMapReader, opentracing::TextMapWriter {
TextMapCarrier(std::unordered_map<std::string, std::string>& text_map_)
: text_map(text_map_) {}

opentracing::expected<void> Set(opentracing::string_view key, opentracing::string_view value) const override {
text_map[key] = value;
return {};
}

opentracing::expected<opentracing::string_view> LookupKey(opentracing::string_view key) const override {
if (!supports_lookup) {
return opentracing::make_unexpected(opentracing::lookup_key_not_supported_error);
}
auto iter = text_map.find(key);
if (iter != text_map.end()) {
return opentracing::string_view{iter->second};
} else {
return opentracing::make_unexpected(opentracing::key_not_found_error);
}
}

opentracing::expected<void> ForeachKey(
std::function<opentracing::expected<void>(opentracing::string_view key, opentracing::string_view value)> f)
const override {
++foreach_key_call_count;
for (const auto& key_value : text_map) {
auto result = f(key_value.first, key_value.second);
if (!result) return result;
}
return {};
}

bool supports_lookup = false;
mutable int foreach_key_call_count = 0;
std::unordered_map<std::string, std::string>& text_map;
};

struct HTTPHeadersCarrier : opentracing::HTTPHeadersReader, opentracing::HTTPHeadersWriter {
HTTPHeadersCarrier(std::unordered_map<std::string, std::string>& text_map_)
: text_map(text_map_) {}

opentracing::expected<void> Set(opentracing::string_view key, opentracing::string_view value) const override {
text_map[key] = value;
return {};
}

opentracing::expected<void> ForeachKey(
std::function<opentracing::expected<void>(opentracing::string_view key, opentracing::string_view value)> f)
const override {
for (const auto& key_value : text_map) {
auto result = f(key_value.first, key_value.second);
if (!result) return result;
}
return {};
}

std::unordered_map<std::string, std::string>& text_map;
};

class TracerShimTest : public testing::Test
{
public:
std::shared_ptr<opentracing::Tracer> tracer_shim;

protected:
virtual void SetUp()
{
tracer_shim = shim::TracerShim::createTracerShim();
}

virtual void TearDown()
{
tracer_shim.reset();
}
};

TEST_F(TracerShimTest, SpanReferenceToCreatingTracer)
{
auto span_shim = tracer_shim->StartSpan("a");
ASSERT_NE(span_shim, nullptr);
ASSERT_EQ(&span_shim->tracer(), tracer_shim.get());
}

TEST_F(TracerShimTest, TracerGloballyRegistered)
{
ASSERT_FALSE(opentracing::Tracer::IsGlobalTracerRegistered());
ASSERT_NE(opentracing::Tracer::InitGlobal(tracer_shim), nullptr);
ASSERT_TRUE(opentracing::Tracer::IsGlobalTracerRegistered());
}

TEST_F(TracerShimTest, InjectInvalidCarrier)
{
auto span_shim = tracer_shim->StartSpan("a");
auto result = tracer_shim->Inject(span_shim->context(), std::cout);
ASSERT_TRUE(opentracing::are_errors_equal(result.error(), opentracing::invalid_carrier_error));
}

TEST_F(TracerShimTest, InjectNullContext)
{
std::unordered_map<std::string, std::string> text_map;
auto noop_tracer = opentracing::MakeNoopTracer();
auto span = noop_tracer->StartSpan("a");
auto result = tracer_shim->Inject(span->context(), TextMapCarrier{text_map});
ASSERT_TRUE(result.has_value());
ASSERT_TRUE(text_map.empty());
}

TEST_F(TracerShimTest, ExtractInvalidCarrier)
{
auto result = tracer_shim->Extract(std::cin);
ASSERT_TRUE(opentracing::are_errors_equal(result.error(), opentracing::invalid_carrier_error));
}

TEST_F(TracerShimTest, ExtractNullContext)
{
std::unordered_map<std::string, std::string> text_map;
auto result = tracer_shim->Extract(TextMapCarrier{text_map});
ASSERT_EQ(result.value(), nullptr);
}
43 changes: 0 additions & 43 deletions opentracing-shim/test/tracer_test.cpp

This file was deleted.

0 comments on commit 321a801

Please sign in to comment.