Skip to content
Open
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
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rules_foreign_cc_dependencies([
git_repository(
name = "io_opentracing_cpp",
remote = "https://github.com/opentracing/opentracing-cpp",
commit = "ac50154a7713877f877981c33c3375003b6ebfe1",
commit = "4bb431f7728eaf383a07e86f9754a5b67575dab0",
)

git_repository(
Expand Down
2 changes: 1 addition & 1 deletion ci/install_opentracing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

[ -z "${OPENTRACING_VERSION}" ] && export OPENTRACING_VERSION="v1.5.0"
[ -z "${OPENTRACING_VERSION}" ] && export OPENTRACING_VERSION="v1.6.0"

# Build OpenTracing
cd /
Expand Down
2 changes: 2 additions & 0 deletions src/common/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ struct JsonValueVisitor {

void operator()(const char* s) { WriteEscapedString(writer, s); }

void operator()(opentracing::string_view s) { WriteEscapedString(writer, s); }

void operator()(const opentracing::Values& values) {
writer << '[';
size_t i = 0;
Expand Down
1 change: 1 addition & 0 deletions src/tracer/legacy/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lightstep_cc_library(
"//src/tracer:lightstep_span_context_interface",
"//src/tracer:utility_lib",
"//src/tracer:tag_lib",
":legacy_immutable_span_context_lib",
],
)

Expand Down
13 changes: 13 additions & 0 deletions src/tracer/legacy/legacy_immutable_span_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ void LegacyImmutableSpanContext::ForeachBaggageItem(
}
}
}

//--------------------------------------------------------------------------------------------------
// Clone
//--------------------------------------------------------------------------------------------------
std::unique_ptr<opentracing::SpanContext> LegacyImmutableSpanContext::Clone()
const noexcept try {
std::unique_ptr<opentracing::SpanContext> result{
new LegacyImmutableSpanContext{trace_id_, span_id_, sampled_,
BaggageProtobufMap{baggage_}}};
return result;
} catch (const std::exception& /*e*/) {
return nullptr;
}
} // namespace lightstep
3 changes: 3 additions & 0 deletions src/tracer/legacy/legacy_immutable_span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class LegacyImmutableSpanContext final : public LightStepSpanContext {

bool sampled() const noexcept override { return sampled_; }

// opentracing::SpanContext
void ForeachBaggageItem(
std::function<bool(const std::string& key, const std::string& value)> f)
const override;
Expand All @@ -40,6 +41,8 @@ class LegacyImmutableSpanContext final : public LightStepSpanContext {
return this->InjectImpl(propagation_options, writer);
}

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

private:
uint64_t trace_id_;
uint64_t span_id_;
Expand Down
35 changes: 34 additions & 1 deletion src/tracer/legacy/legacy_span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "common/random.h"
#include "common/utility.h"
#include "tracer/legacy/legacy_immutable_span_context.h"
#include "tracer/tag.h"
#include "tracer/utility.h"

Expand Down Expand Up @@ -227,8 +228,25 @@ std::string LegacySpan::BaggageItem(
//------------------------------------------------------------------------------
void LegacySpan::Log(std::initializer_list<
std::pair<opentracing::string_view, opentracing::Value>>
fields) noexcept {
this->Log(SystemClock::now(), fields);
}

void LegacySpan::Log(opentracing::SystemTime timestamp,
std::initializer_list<std::pair<opentracing::string_view,
opentracing::Value>>
fields) noexcept try {
auto timestamp = SystemClock::now();
std::lock_guard<std::mutex> lock_guard{mutex_};
*span_.mutable_logs()->Add() =
ToLog(timestamp, std::begin(fields), std::end(fields));
} catch (const std::exception& e) {
logger_.Error("Log failed: ", e.what());
}

void LegacySpan::Log(
opentracing::SystemTime timestamp,
const std::vector<std::pair<opentracing::string_view, opentracing::Value>>&
fields) noexcept try {
std::lock_guard<std::mutex> lock_guard{mutex_};
*span_.mutable_logs()->Add() =
ToLog(timestamp, std::begin(fields), std::end(fields));
Expand All @@ -250,6 +268,21 @@ void LegacySpan::ForeachBaggageItem(
}
}

//--------------------------------------------------------------------------------------------------
// Clone
//--------------------------------------------------------------------------------------------------
std::unique_ptr<opentracing::SpanContext> LegacySpan::Clone() const
noexcept try {
std::lock_guard<std::mutex> lock_guard{mutex_};
std::unique_ptr<opentracing::SpanContext> result{
new LegacyImmutableSpanContext{
this->trace_id(), this->span_id(), sampled_,
BaggageProtobufMap{span_.span_context().baggage()}}};
return result;
} catch (const std::exception& /*e*/) {
return nullptr;
}

//------------------------------------------------------------------------------
// sampled
//------------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions src/tracer/legacy/legacy_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class LegacySpan final : public opentracing::Span, public LightStepSpanContext {
std::pair<opentracing::string_view, opentracing::Value>>
fields) noexcept override;

void Log(opentracing::SystemTime timestamp,
std::initializer_list<
std::pair<opentracing::string_view, opentracing::Value>>
fields) noexcept override;

void Log(opentracing::SystemTime timestamp,
const std::vector<
std::pair<opentracing::string_view, opentracing::Value>>&
fields) noexcept override;

const opentracing::SpanContext& context() const noexcept override {
return *this;
}
Expand All @@ -55,6 +65,8 @@ class LegacySpan final : public opentracing::Span, public LightStepSpanContext {
std::function<bool(const std::string& key, const std::string& value)> f)
const override;

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

bool sampled() const noexcept override;

uint64_t trace_id() const noexcept override {
Expand Down
28 changes: 28 additions & 0 deletions src/tracer/lightstep_span_context.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
#include "tracer/lightstep_span_context.h"

#include "common/utility.h"

namespace lightstep {
//--------------------------------------------------------------------------------------------------
// ToHexString
//--------------------------------------------------------------------------------------------------
static std::string ToHexString(uint64_t x) {
std::array<char, Num64BitHexDigits> buffer;
return Uint64ToHex(x, buffer.data());
}

//--------------------------------------------------------------------------------------------------
// ToTraceID
//--------------------------------------------------------------------------------------------------
std::string LightStepSpanContext::ToTraceID() const noexcept try {
return ToHexString(this->trace_id());
} catch (const std::exception& /*e*/) {
return {};
}

//--------------------------------------------------------------------------------------------------
// ToSpanID
//--------------------------------------------------------------------------------------------------
std::string LightStepSpanContext::ToSpanID() const noexcept try {
return ToHexString(this->span_id());
} catch (const std::exception& /*e*/) {
return {};
}

//------------------------------------------------------------------------------
// operator==
//------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/tracer/lightstep_span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class LightStepSpanContext : public opentracing::SpanContext {
virtual opentracing::expected<void> Inject(
const PropagationOptions& propagation_options,
const opentracing::HTTPHeadersWriter& writer) const = 0;

std::string ToTraceID() const noexcept final;

std::string ToSpanID() const noexcept final;
};

bool operator==(const LightStepSpanContext& lhs,
Expand Down
38 changes: 37 additions & 1 deletion src/tracer/span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "common/random.h"
#include "common/utility.h"
#include "tracer/legacy/legacy_immutable_span_context.h"
#include "tracer/serialization.h"
#include "tracer/tag.h"
#include "tracer/utility.h"
Expand Down Expand Up @@ -157,7 +158,15 @@ std::string Span::BaggageItem(opentracing::string_view restricted_key) const
void Span::Log(std::initializer_list<
std::pair<opentracing::string_view, opentracing::Value>>
fields) noexcept try {
auto timestamp = SystemClock::now();
this->Log(SystemClock::now(), fields);
} catch (const std::exception& e) {
tracer_->logger().Error("Log failed: ", e.what());
}

void Span::Log(opentracing::SystemTime timestamp,
std::initializer_list<
std::pair<opentracing::string_view, opentracing::Value>>
fields) noexcept try {
SpinLockGuard lock_guard{mutex_};
if (is_finished_) {
return;
Expand All @@ -167,6 +176,19 @@ void Span::Log(std::initializer_list<
tracer_->logger().Error("Log failed: ", e.what());
}

void Span::Log(
opentracing::SystemTime timestamp,
const std::vector<std::pair<opentracing::string_view, opentracing::Value>>&
fields) noexcept try {
SpinLockGuard lock_guard{mutex_};
if (is_finished_) {
return;
}
WriteLog(stream_, timestamp, fields.data(), fields.data() + fields.size());
} catch (const std::exception& e) {
tracer_->logger().Error("Log failed: ", e.what());
}

//------------------------------------------------------------------------------
// ForeachBaggageItem
//------------------------------------------------------------------------------
Expand All @@ -181,6 +203,20 @@ void Span::ForeachBaggageItem(
}
}

//--------------------------------------------------------------------------------------------------
// Clone
//--------------------------------------------------------------------------------------------------
std::unique_ptr<opentracing::SpanContext> Span::Clone() const noexcept try {
SpinLockGuard lock_guard{mutex_};
BaggageProtobufMap baggage_copy{baggage_.begin(), baggage_.end()};
std::unique_ptr<opentracing::SpanContext> result{
new LegacyImmutableSpanContext{trace_id_, span_id_, sampled_,
std::move(baggage_copy)}};
return result;
} catch (const std::exception& /*e*/) {
return nullptr;
}

//------------------------------------------------------------------------------
// sampled
//------------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions src/tracer/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ class Span final : public opentracing::Span, public LightStepSpanContext {
std::pair<opentracing::string_view, opentracing::Value>>
fields) noexcept override;

void Log(opentracing::SystemTime timestamp,
std::initializer_list<
std::pair<opentracing::string_view, opentracing::Value>>
fields) noexcept override;

void Log(opentracing::SystemTime timestamp,
const std::vector<
std::pair<opentracing::string_view, opentracing::Value>>&
fields) noexcept override;

const opentracing::SpanContext& context() const noexcept override {
return *this;
}
Expand Down Expand Up @@ -76,6 +86,8 @@ class Span final : public opentracing::Span, public LightStepSpanContext {
return this->InjectImpl(propagation_options, writer);
}

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

// LightStepSpanContext
bool sampled() const noexcept override;

Expand Down
29 changes: 28 additions & 1 deletion test/tracer/tracer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "test/recorder/in_memory_recorder.h"
#include "test/utility.h"
#include "tracer/legacy/legacy_tracer_impl.h"
#include "tracer/lightstep_span_context.h"
#include "tracer/tag.h"
#include "tracer/tracer_impl.h"

Expand Down Expand Up @@ -197,8 +198,12 @@ TEST_CASE("tracer") {
auto span = tracer->StartSpan("a");
REQUIRE(span);
span->Log({{"abc", 123}});
span->Log(
std::chrono::system_clock::now(),
std::vector<std::pair<opentracing::string_view, opentracing::Value>>{
{"abc", 123}});
span->Finish();
REQUIRE(recorder->top().logs().size() == 1);
REQUIRE(recorder->top().logs().size() == 2);
}

SECTION(tracer_type + ": Logs can be added with FinishSpanOptions.") {
Expand All @@ -219,6 +224,28 @@ TEST_CASE("tracer") {
span->SetTag("abc", 123);
span->Log({{"abc", 123}});
}

SECTION(tracer_type + ": span contexts can be cloned") {
auto span = tracer->StartSpan("a");
REQUIRE(span);
span->SetBaggageItem("abc", "123");
auto span_context1 = span->context().Clone();
REQUIRE(span_context1 != nullptr);
auto span_context2 = span_context1->Clone();
REQUIRE(span_context2 != nullptr);
REQUIRE(dynamic_cast<const LightStepSpanContext&>(span->context()) ==
dynamic_cast<LightStepSpanContext&>(*span_context1));
}

SECTION(tracer_type + ": supports accessing context IDs as strings") {
auto span = tracer->StartSpan("a");
REQUIRE(span);
auto trace_id = span->context().ToTraceID();
REQUIRE(!trace_id.empty());
auto span_id = span->context().ToSpanID();
REQUIRE(!span_id.empty());
REQUIRE(trace_id != span_id);
}
}
}

Expand Down