From 78c03030a46b1a6576439af23d5268ee932f4d24 Mon Sep 17 00:00:00 2001 From: Ryan Burn Date: Tue, 20 Feb 2018 17:00:10 -0500 Subject: [PATCH 1/4] Support copying span contexts. --- include/opentracing/span.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/opentracing/span.h b/include/opentracing/span.h index 86ffbaf..f885234 100644 --- a/include/opentracing/span.h +++ b/include/opentracing/span.h @@ -26,6 +26,9 @@ class SpanContext { virtual void ForeachBaggageItem( std::function f) const = 0; + + // Clone creates a copy of SpanContext. + virtual std::unique_ptr Clone() const = 0; }; struct LogRecord { From f1d2543bf4adc2a5d1bbf665c032768c94f6bfff Mon Sep 17 00:00:00 2001 From: Ryan Burn Date: Tue, 20 Feb 2018 18:53:55 -0500 Subject: [PATCH 2/4] Fill in missing APIs. --- include/opentracing/span.h | 4 +++- mocktracer/src/mock_span_context.cpp | 10 +++++++++- mocktracer/src/mock_span_context.h | 4 +++- src/noop.cpp | 4 ++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/opentracing/span.h b/include/opentracing/span.h index f885234..7e076a4 100644 --- a/include/opentracing/span.h +++ b/include/opentracing/span.h @@ -28,7 +28,9 @@ class SpanContext { const = 0; // Clone creates a copy of SpanContext. - virtual std::unique_ptr Clone() const = 0; + // + // Returns nullptr on failure. + virtual std::unique_ptr Clone() const noexcept = 0; }; struct LogRecord { diff --git a/mocktracer/src/mock_span_context.cpp b/mocktracer/src/mock_span_context.cpp index 08bf7a2..b65d9cf 100644 --- a/mocktracer/src/mock_span_context.cpp +++ b/mocktracer/src/mock_span_context.cpp @@ -21,12 +21,20 @@ void MockSpanContext::ForeachBaggageItem( } } -void MockSpanContext::SetData(SpanContextData& data) { +void MockSpanContext::SetData(SpanContextData& data) const { data.trace_id = data_.trace_id; data.span_id = data_.span_id; std::lock_guard lock_guard{baggage_mutex_}; data.baggage = data_.baggage; } + +std::unique_ptr MockSpanContext::Clone() const noexcept try { + auto result = std::unique_ptr{new MockSpanContext{}}; + SetData(result->data_); + return std::unique_ptr{result.release()}; +} catch (const std::exception& /*e*/) { + return nullptr; +} } // namespace mocktracer END_OPENTRACING_ABI_NAMESPACE } // namespace opentracing diff --git a/mocktracer/src/mock_span_context.h b/mocktracer/src/mock_span_context.h index febd8a5..0179b41 100644 --- a/mocktracer/src/mock_span_context.h +++ b/mocktracer/src/mock_span_context.h @@ -33,7 +33,7 @@ class MockSpanContext : public SpanContext { uint64_t span_id() const noexcept { return data_.span_id; } - void SetData(SpanContextData& data); + void SetData(SpanContextData& data) const; template expected Inject(Carrier& writer) const { @@ -47,6 +47,8 @@ class MockSpanContext : public SpanContext { return ExtractSpanContext(reader, data_); } + std::unique_ptr Clone() const noexcept override; + private: friend MockSpan; diff --git a/src/noop.cpp b/src/noop.cpp index 1d1dbe4..e0e9daf 100644 --- a/src/noop.cpp +++ b/src/noop.cpp @@ -8,6 +8,10 @@ class NoopSpanContext : public SpanContext { void ForeachBaggageItem( std::function /*f*/) const override {} + + std::unique_ptr Clone() const noexcept override { + return std::unique_ptr{new (std::nothrow) NoopSpanContext{}}; + } }; class NoopSpan : public Span { From 77673ee479e7548196e6ca7d49362688afdc8ce2 Mon Sep 17 00:00:00 2001 From: Ryan Burn Date: Wed, 2 May 2018 17:11:48 -0400 Subject: [PATCH 3/4] s/SetData/CopyData/ --- mocktracer/src/mock_span.cpp | 2 +- mocktracer/src/mock_span_context.cpp | 4 ++-- mocktracer/src/mock_span_context.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mocktracer/src/mock_span.cpp b/mocktracer/src/mock_span.cpp index 1caaca3..fc52347 100644 --- a/mocktracer/src/mock_span.cpp +++ b/mocktracer/src/mock_span.cpp @@ -117,7 +117,7 @@ void MockSpan::FinishWithOptions(const FinishSpanOptions& options) noexcept { data_.duration = finish_timestamp - start_steady_; - span_context_.SetData(data_.span_context); + span_context_.CopyData(data_.span_context); if (recorder_ != nullptr) { recorder_->RecordSpan(std::move(data_)); diff --git a/mocktracer/src/mock_span_context.cpp b/mocktracer/src/mock_span_context.cpp index b65d9cf..ac6eb2b 100644 --- a/mocktracer/src/mock_span_context.cpp +++ b/mocktracer/src/mock_span_context.cpp @@ -21,7 +21,7 @@ void MockSpanContext::ForeachBaggageItem( } } -void MockSpanContext::SetData(SpanContextData& data) const { +void MockSpanContext::CopyData(SpanContextData& data) const { data.trace_id = data_.trace_id; data.span_id = data_.span_id; std::lock_guard lock_guard{baggage_mutex_}; @@ -30,7 +30,7 @@ void MockSpanContext::SetData(SpanContextData& data) const { std::unique_ptr MockSpanContext::Clone() const noexcept try { auto result = std::unique_ptr{new MockSpanContext{}}; - SetData(result->data_); + CopyData(result->data_); return std::unique_ptr{result.release()}; } catch (const std::exception& /*e*/) { return nullptr; diff --git a/mocktracer/src/mock_span_context.h b/mocktracer/src/mock_span_context.h index 0179b41..c62c9a3 100644 --- a/mocktracer/src/mock_span_context.h +++ b/mocktracer/src/mock_span_context.h @@ -33,7 +33,7 @@ class MockSpanContext : public SpanContext { uint64_t span_id() const noexcept { return data_.span_id; } - void SetData(SpanContextData& data) const; + void CopyData(SpanContextData& data) const; template expected Inject(Carrier& writer) const { From 2ad67eaa18fe8ae2942ac9fc8b7a09049392180d Mon Sep 17 00:00:00 2001 From: Ryan Burn Date: Mon, 27 Aug 2018 14:27:34 -0700 Subject: [PATCH 4/4] Update ABI. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 45710f1..93f828f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ project(opentracing-cpp) # # Also, whenever the ABI is between versions and in development # suffix the ABI version number with "_unstable". -set(OPENTRACING_ABI_VERSION "2") +set(OPENTRACING_ABI_VERSION "3_unstable") # Version number follows semver # See https://semver.org/