Skip to content

Commit 052c2e7

Browse files
committed
Opentracing 1.6 API support
Implemented Clone(), ToTraceID(), ToSpanID(), extra variants of Log() methods for SpanContext. Test cases for Clone(), ToTraceID(), and ToSpanID().
1 parent b781c2c commit 052c2e7

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

ci/build_plugin.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ apt-get install --no-install-recommends --no-install-suggests -y \
99
git \
1010
ca-certificates
1111

12-
export OPENTRACING_VERSION=1.5.0
12+
export OPENTRACING_VERSION=1.6.0
1313

1414
# Compile for a portable cpu architecture
1515
export CFLAGS="-march=x86-64"

ci/install_dependencies.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ apt-get install --no-install-recommends --no-install-suggests -y \
1111

1212
# Build OpenTracing
1313
cd /
14-
export OPENTRACING_VERSION=1.5.0
14+
export OPENTRACING_VERSION=1.6.0
1515
git clone -b v$OPENTRACING_VERSION https://github.com/opentracing/opentracing-cpp.git
1616
cd opentracing-cpp
1717
mkdir .build && cd .build

zipkin/include/zipkin/span_context.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace zipkin {
2424
*/
2525
struct AnnotationSet {
2626
AnnotationSet() : cs_(false), cr_(false), ss_(false), sr_(false) {}
27+
AnnotationSet(const AnnotationSet &set) : cs_{set.cs_}, cr_{set.cr_}, ss_{set.ss_}, sr_{set.sr_} {}
2728
bool cs_ : 1;
2829
bool cr_ : 1;
2930
bool ss_ : 1;
@@ -58,6 +59,15 @@ class SpanContext {
5859
: trace_id_{trace_id}, id_{id}, parent_id_{parent_id}, flags_{flags},
5960
is_initialized_{true} {}
6061

62+
/**
63+
* Copy constructor that creates a context object from the given SpanContext
64+
* instance.
65+
*/
66+
SpanContext(const SpanContext &span)
67+
: trace_id_{span.trace_id_}, id_{span.id_}, parent_id_{span.parent_id_},
68+
annotation_values_{span.annotation_values_}, flags_{span.flags_},
69+
is_initialized_{span.is_initialized_} {}
70+
6171
bool isSampled() const { return flags_ & zipkin::sampled_flag; }
6272

6373
/**

zipkin_opentracing/src/opentracing.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class OtSpanContext : public ot::SpanContext {
5050
public:
5151
OtSpanContext() = default;
5252

53+
OtSpanContext(const OtSpanContext &span)
54+
: span_context_{span.span_context_}, baggage_{span.baggage_} {}
55+
5356
explicit OtSpanContext(zipkin::SpanContext &&span_context)
5457
: span_context_{std::move(span_context)} {}
5558

@@ -69,6 +72,20 @@ class OtSpanContext : public ot::SpanContext {
6972
return *this;
7073
}
7174

75+
std::unique_ptr<SpanContext> Clone() const noexcept override try {
76+
return std::unique_ptr<SpanContext> (new OtSpanContext(*this));
77+
} catch (...) {
78+
return nullptr;
79+
}
80+
81+
std::string ToTraceID() const noexcept override {
82+
return span_context_.traceIdAsHexString();
83+
}
84+
85+
std::string ToSpanID() const noexcept override {
86+
return span_context_.idAsHexString();
87+
}
88+
7289
void ForeachBaggageItem(
7390
std::function<bool(const std::string &, const std::string &)> f)
7491
const override {
@@ -258,6 +275,14 @@ class OtSpan : public ot::Span {
258275
void Log(std::initializer_list<std::pair<string_view, Value>>
259276
fields) noexcept override {}
260277

278+
void Log(
279+
SystemTime timestamp,
280+
std::initializer_list<std::pair<string_view, Value>> fields) noexcept override {}
281+
282+
void Log(
283+
SystemTime timestamp,
284+
const std::vector<std::pair<string_view, Value>>& fields) noexcept override {}
285+
261286
const ot::SpanContext &context() const noexcept override {
262287
return span_context_;
263288
}

zipkin_opentracing/test/ot_tracer_test.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ static bool IsChildOf(const zipkin::Span &a, const zipkin::Span &b) {
3737
a.traceId() == b.traceId();
3838
}
3939

40+
class TextMapCarrier : public ot::TextMapWriter {
41+
public:
42+
TextMapCarrier(std::unordered_map<std::string, std::string> &text_map)
43+
: text_map_(text_map) {}
44+
45+
ot::expected<void> Set(ot::string_view key, ot::string_view value) const override {
46+
text_map_[key] = value;
47+
return {};
48+
}
49+
50+
private:
51+
std::unordered_map<std::string, std::string> &text_map_;
52+
};
53+
4054
TEST_CASE("ot_tracer") {
4155
auto reporter = new InMemoryReporter();
4256
ZipkinOtTracerOptions options;
@@ -167,4 +181,58 @@ TEST_CASE("ot_tracer") {
167181
span->Finish();
168182
CHECK(hasTag(reporter->top(), "abc", 123));
169183
}
184+
185+
SECTION("Get SpanContext and check that it is valid after Finish") {
186+
auto span = tracer->StartSpan("a");
187+
CHECK(span);
188+
span->SetTag("abc", 123);
189+
auto &ctx = span->context();
190+
span->Finish();
191+
auto trace_id = ctx.ToTraceID();
192+
CHECK(trace_id != "");
193+
auto span_id = ctx.ToSpanID();
194+
CHECK(span_id != "");
195+
}
196+
197+
SECTION("Check SpanContext.Clone() preserves attributes") {
198+
auto span = tracer->StartSpan("a");
199+
CHECK(span);
200+
span->SetTag("abc", 123);
201+
span->SetBaggageItem("a", "1");
202+
auto &ctx = span->context();
203+
span->Finish();
204+
auto ctx2 = ctx.Clone();
205+
206+
CHECK(ctx.ToTraceID() == ctx2->ToTraceID());
207+
CHECK(ctx.ToSpanID() == ctx2->ToSpanID());
208+
209+
std::unordered_map<std::string, std::string> items;
210+
ctx.ForeachBaggageItem(
211+
[&items] (const std::string& key, const std::string& value) {
212+
items[key] = value;
213+
return true;
214+
}
215+
);
216+
217+
std::unordered_map<std::string, std::string> items2;
218+
ctx2->ForeachBaggageItem(
219+
[&items2] (const std::string& key, const std::string& value) {
220+
items2[key] = value;
221+
return true;
222+
}
223+
);
224+
CHECK(items == items2);
225+
226+
// Cross-check: serialize span context to a carrier
227+
// and compare low-level representation
228+
items.clear();
229+
items2.clear();
230+
231+
TextMapCarrier carrier{items};
232+
tracer->Inject(ctx, carrier);
233+
TextMapCarrier carrier2{items2};
234+
tracer->Inject(*ctx2, carrier2);
235+
236+
CHECK(items == items2);
237+
}
170238
}

0 commit comments

Comments
 (0)