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/ diff --git a/include/opentracing/span.h b/include/opentracing/span.h index 86ffbaf..7e076a4 100644 --- a/include/opentracing/span.h +++ b/include/opentracing/span.h @@ -26,6 +26,11 @@ class SpanContext { virtual void ForeachBaggageItem( std::function f) const = 0; + + // Clone creates a copy of SpanContext. + // + // Returns nullptr on failure. + virtual std::unique_ptr Clone() const noexcept = 0; }; struct LogRecord { diff --git a/mocktracer/src/mock_span.cpp b/mocktracer/src/mock_span.cpp index 0e97ff4..5459ff8 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 08bf7a2..ac6eb2b 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::CopyData(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{}}; + CopyData(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 e8b76de..8a1f38a 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 CopyData(SpanContextData& data) const; template expected Inject(const PropagationOptions& propagation_options, @@ -49,6 +49,8 @@ class MockSpanContext : public SpanContext { return ExtractSpanContext(propagation_options, 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 {