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

Support copying span contexts. #56

Merged
merged 6 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
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