Skip to content

Commit

Permalink
First attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
chusitoo committed Jan 1, 2023
1 parent d30c140 commit 45e0c4b
Show file tree
Hide file tree
Showing 14 changed files with 972 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ branch = main
path = third_party/nlohmann-json
url = https://github.com/nlohmann/json
branch = master

[submodule "third_party/opentracing-cpp"]
path = third_party/opentracing-cpp
url = https://github.com/opentracing/opentracing-cpp.git
branch = master
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ option(BUILD_W3CTRACECONTEXT_TEST "Whether to build w3c trace context" OFF)

option(OTELCPP_MAINTAINER_MODE "Build in maintainer mode (-Wall -Werror)" OFF)

option(WITH_OPENTRACING "Whether to include the Opentracing shim" ON)

set(OTELCPP_PROTO_PATH
""
CACHE PATH "Path to opentelemetry-proto")
Expand Down Expand Up @@ -492,6 +494,10 @@ include_directories(api/include)

add_subdirectory(api)

if(WITH_OPENTRACING)
add_subdirectory(opentracing-shim)
endif()

if(NOT WITH_API_ONLY)
set(BUILD_TESTING ${BUILD_TESTING})
include_directories(sdk/include)
Expand All @@ -501,6 +507,7 @@ if(NOT WITH_API_ONLY)
add_subdirectory(sdk)
add_subdirectory(ext)
add_subdirectory(exporters)

if(BUILD_TESTING)
add_subdirectory(test_common)
endif()
Expand Down
40 changes: 40 additions & 0 deletions opentracing-shim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#set(this_target opentelemetry_opentracing_shim)

#message("CMAKE_CURRENT_LIST_DIR " ${CMAKE_CURRENT_LIST_DIR})
#message("CMAKE_CURRENT_SOURCE_DIR " ${CMAKE_CURRENT_SOURCE_DIR})
#message("CMAKE_SOURCE_DIR " ${CMAKE_SOURCE_DIR})
#message("PROJECT_SOURCE_DIR " ${PROJECT_SOURCE_DIR})

#add_library(${this_target} INTERFACE)
#target_include_directories(
# ${this_target}
# PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
# "$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/third_party/opentracing-cpp/include>"
# "$<INSTALL_INTERFACE:include>")
#
#set_target_properties(${this_target} PROPERTIES EXPORT_NAME "opentracing-shim")
#target_link_libraries(${this_target} INTERFACE ${this_target} opentracing-cpp)
#
#get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
#foreach(dir ${dirs})
# message(STATUS "dir='${dir}'")
#endforeach()
#
#install(
# TARGETS ${this_target}
# EXPORT "${PROJECT_NAME}-target"
# RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
# LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
# ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
#
#install(
# DIRECTORY include/opentelemetry/opentracing-shim
# DESTINATION include/opentelemetry/
# FILES_MATCHING
# PATTERN "*.h")

add_subdirectory(src)

if(BUILD_TESTING)
add_subdirectory(test)
endif() # BUILD_TESTING
134 changes: 134 additions & 0 deletions opentracing-shim/include/shim_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

OPENTELEMETRY_BEGIN_NAMESPACE
namespace opentracingshim::shimutils
{

using opentelemetry::common::AttributeValue;

static inline AttributeValue attributeFromValue(const opentracing::Value& value)
{
static struct
{
AttributeValue operator()(bool v) { return v; }
AttributeValue operator()(double v) { return v; }
AttributeValue operator()(int64_t v) { return v; }
AttributeValue operator()(uint64_t v) { return v; }
AttributeValue operator()(std::string v) { return v.c_str(); }
AttributeValue operator()(opentracing::string_view v) { return nostd::string_view{v.data()}; }
AttributeValue operator()(std::nullptr_t) { return std::string{}; }
AttributeValue operator()(const char* v) { return v; }
AttributeValue operator()(opentracing::util::recursive_wrapper<opentracing::Values>) { return std::string{}; }
AttributeValue operator()(opentracing::util::recursive_wrapper<opentracing::Dictionary>) { return std::string{}; }
} AttributeMapper;

return opentracing::Value::visit(value, AttributeMapper);
}

static inline std::string stringFromValue(const opentracing::Value& value)
{
static struct
{
std::string operator()(bool v) { return v ? "true" : "false"; }
std::string operator()(double v) { return std::to_string(v); }
std::string operator()(int64_t v) { return std::to_string(v); }
std::string operator()(uint64_t v) { return std::to_string(v); }
std::string operator()(std::string v) { return v; }
std::string operator()(opentracing::string_view v) { return std::string{v.data()}; }
std::string operator()(std::nullptr_t) { return std::string{}; }
std::string operator()(const char* v) { return std::string{v}; }
std::string operator()(opentracing::util::recursive_wrapper<opentracing::Values>) { return std::string{}; }
std::string operator()(opentracing::util::recursive_wrapper<opentracing::Dictionary>) { return std::string{}; }
} StringMapper;

return opentracing::Value::visit(value, StringMapper);
}

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

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

// returns the value associated with the passed key.
virtual nostd::string_view Get(nostd::string_view key) const noexcept override
{
return "";
}

// stores the key-value pair.
virtual void Set(nostd::string_view key, nostd::string_view value) noexcept override
{
writer_.Set(key.data(), value.data());
}

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.
virtual nostd::string_view Get(nostd::string_view key) const noexcept override
{
// First try carrier.LookupKey since that can potentially be the fastest approach.
auto result = reader_.LookupKey(key.data());

if (!result.has_value() ||
opentracing::are_errors_equal(result.error(), opentracing::lookup_key_not_supported_error))
{
// Fall back to iterating through all of the keys.
reader_.ForeachKey([key, &result]
(opentracing::string_view k, opentracing::string_view v) -> opentracing::expected<void> {
if (key == k)
{
result = opentracing::make_expected(v);
// Found key, so bail out of the loop with a success error code.
return opentracing::make_unexpected(std::error_code{});
}
return opentracing::make_expected();
});
}

return nostd::string_view{ result.has_value() ? result.value().data() : "" };
}

// stores the key-value pair.
virtual void Set(nostd::string_view key, nostd::string_view value) noexcept override
{
// Not required for Opentracing reader
}

private:

const T& reader_;

};

static inline bool isBaggageEmpty(const BaggagePtr& baggage)
{
if (baggage)
{
return baggage->GetAllEntries([](nostd::string_view, nostd::string_view){
return false;
});
}

return true;
}

} // namespace opentracingshim::shimutils
OPENTELEMETRY_END_NAMESPACE
51 changes: 51 additions & 0 deletions opentracing-shim/include/span_context_shim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "opentelemetry/baggage/baggage.h"
#include "opentelemetry/trace/span_context.h"
#include "opentracing/span.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace opentracingshim
{

using BaggagePtr = nostd::shared_ptr<opentelemetry::baggage::Baggage>;

class SpanContextShim final : public opentracing::SpanContext
{
public:

explicit SpanContextShim(const opentelemetry::trace::SpanContext& context, const BaggagePtr& baggage) :
context_(context), baggage_(baggage) {}

inline SpanContextShim operator=(const SpanContextShim& other)
{
return SpanContextShim(other.context_, other.baggage_);
}

inline const opentelemetry::trace::SpanContext& context() const { return context_; }

inline const BaggagePtr& baggage() const { return baggage_; }

SpanContextShim newWithKeyValue(const nostd::string_view &key, const nostd::string_view &value) const noexcept;

bool BaggageItem(nostd::string_view key, std::string& value) const noexcept;

using VisitBaggageItem = std::function<bool(const std::string& key, const std::string& value)>;
void ForeachBaggageItem(VisitBaggageItem f) const override;

std::unique_ptr<opentracing::SpanContext> Clone() const noexcept override;

private:

const opentelemetry::trace::SpanContext context_;
const BaggagePtr baggage_;

};

} // namespace opentracingshim
OPENTELEMETRY_END_NAMESPACE
56 changes: 56 additions & 0 deletions opentracing-shim/include/span_shim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

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

#include "opentelemetry/baggage/baggage.h"
#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/common/spin_lock_mutex.h"
#include "opentelemetry/trace/span.h"
#include "opentracing/span.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace opentracingshim
{

using SpanPtr = nostd::shared_ptr<opentelemetry::trace::Span>;
using EventEntry = std::pair<opentracing::string_view, opentracing::Value>;

class SpanShim : public opentracing::Span
{
public:

explicit SpanShim(const TracerShim& tracer, const SpanPtr& span, const BaggagePtr& baggage) :
tracer_(tracer), span_(span), context_(span->GetContext(), baggage) {}

void handleError(const opentracing::Value& value) noexcept;

void FinishWithOptions(const opentracing::FinishSpanOptions& finish_span_options) noexcept override;
void SetOperationName(opentracing::string_view name) noexcept override;
void SetTag(opentracing::string_view key, const opentracing::Value& value) noexcept override;
void SetBaggageItem(opentracing::string_view restricted_key, opentracing::string_view value) noexcept override;
std::string BaggageItem(opentracing::string_view restricted_key) const noexcept override;
void Log(std::initializer_list<EventEntry> fields) noexcept override;
void Log(opentracing::SystemTime timestamp, std::initializer_list<EventEntry> fields) noexcept override;
void Log(opentracing::SystemTime timestamp, const std::vector<EventEntry>& fields) noexcept override;
inline const opentracing::SpanContext& context() const noexcept override { return context_; };
inline const opentracing::Tracer& tracer() const noexcept override { return tracer_; };

private:

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

TracerShim tracer_;
SpanPtr span_;
SpanContextShim context_;
mutable opentelemetry::common::SpinLockMutex context_lock_;

};

} // namespace opentracingshim
OPENTELEMETRY_END_NAMESPACE
Loading

0 comments on commit 45e0c4b

Please sign in to comment.