Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
Support copying span contexts. (#56)
Browse files Browse the repository at this point in the history
* Support copying span contexts.

* Fill in missing APIs.

* s/SetData/CopyData/

* Update ABI.
  • Loading branch information
rnburn authored Aug 27, 2018
1 parent e383361 commit 301ac82
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
5 changes: 5 additions & 0 deletions include/opentracing/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class SpanContext {
virtual void ForeachBaggageItem(
std::function<bool(const std::string& key, const std::string& value)> f)
const = 0;

// Clone creates a copy of SpanContext.
//
// Returns nullptr on failure.
virtual std::unique_ptr<SpanContext> Clone() const noexcept = 0;
};

struct LogRecord {
Expand Down
2 changes: 1 addition & 1 deletion mocktracer/src/mock_span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_));
Expand Down
10 changes: 9 additions & 1 deletion mocktracer/src/mock_span_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock_guard{baggage_mutex_};
data.baggage = data_.baggage;
}

std::unique_ptr<SpanContext> MockSpanContext::Clone() const noexcept try {
auto result = std::unique_ptr<MockSpanContext>{new MockSpanContext{}};
CopyData(result->data_);
return std::unique_ptr<SpanContext>{result.release()};
} catch (const std::exception& /*e*/) {
return nullptr;
}
} // namespace mocktracer
END_OPENTRACING_ABI_NAMESPACE
} // namespace opentracing
4 changes: 3 additions & 1 deletion mocktracer/src/mock_span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class Carrier>
expected<void> Inject(const PropagationOptions& propagation_options,
Expand All @@ -49,6 +49,8 @@ class MockSpanContext : public SpanContext {
return ExtractSpanContext(propagation_options, reader, data_);
}

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

private:
friend MockSpan;

Expand Down
4 changes: 4 additions & 0 deletions src/noop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class NoopSpanContext : public SpanContext {
void ForeachBaggageItem(
std::function<bool(const std::string& key,
const std::string& value)> /*f*/) const override {}

std::unique_ptr<SpanContext> Clone() const noexcept override {
return std::unique_ptr<SpanContext>{new (std::nothrow) NoopSpanContext{}};
}
};

class NoopSpan : public Span {
Expand Down

0 comments on commit 301ac82

Please sign in to comment.