-
Notifications
You must be signed in to change notification settings - Fork 46
/
ot_tracer_test.cc
173 lines (153 loc) · 5.43 KB
/
ot_tracer_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "../src/sampling.h"
#include "../src/utility.h"
#include "in_memory_reporter.h"
#include <algorithm>
#include <stdexcept>
#include <opentracing/noop.h>
#include <zipkin/opentracing.h>
#define CATCH_CONFIG_MAIN
#include <zipkin/catch/catch.hpp>
using namespace zipkin;
namespace ot = opentracing;
static bool hasTag(const Span &span, ot::string_view key, ot::Value value) {
auto tag_annotation = toBinaryAnnotation(key, value);
return std::any_of(
std::begin(span.binaryAnnotations()), std::end(span.binaryAnnotations()),
[&](const BinaryAnnotation &annotation) {
if (tag_annotation.key() != annotation.key() ||
tag_annotation.annotationType() != annotation.annotationType()) {
return false;
}
switch (tag_annotation.annotationType()) {
case BOOL:
return tag_annotation.valueBool() == annotation.valueBool();
case STRING:
return tag_annotation.valueString() == annotation.valueString();
case INT64:
return tag_annotation.valueInt64() == annotation.valueInt64();
case DOUBLE:
return tag_annotation.valueDouble() == annotation.valueDouble();
default:
throw std::runtime_error("Unhandled annotation type");
}
});
}
static bool IsChildOf(const zipkin::Span &a, const zipkin::Span &b) {
return a.isSetParentId() && a.parentId() == b.id() &&
a.traceId() == b.traceId();
}
TEST_CASE("ot_tracer") {
auto reporter = new InMemoryReporter();
ZipkinOtTracerOptions options;
auto tracer =
makeZipkinOtTracer(options, std::unique_ptr<Reporter>(reporter));
SECTION("StartSpan applies the provided tags.") {
{
auto span = tracer->StartSpan(
"a", {ot::SetTag("abc", 123), ot::SetTag("xyz", true)});
CHECK(span);
span->Finish();
}
auto span = reporter->top();
CHECK(span.name() == "a");
CHECK(hasTag(span, "abc", 123));
CHECK(hasTag(span, "xyz", true));
}
SECTION("Uses sampling rate to determine whether to sample a span.") {
auto r = new InMemoryReporter();
ZipkinOtTracerOptions options;
options.sample_rate = 0.0;
auto t = makeZipkinOtTracer(options, std::unique_ptr<Reporter>(r));
auto span_a = t->StartSpan("a");
CHECK(span_a);
span_a->Finish();
CHECK(r->spans().empty());
}
SECTION("Propagates sampling decision to child span") {
ZipkinOtTracerOptions no_sampling;
no_sampling.sample_rate = 0.0;
auto r1 = new InMemoryReporter();
auto no_sampling_tracer =
makeZipkinOtTracer(no_sampling, std::unique_ptr<Reporter>(r1));
ZipkinOtTracerOptions always_sample;
always_sample.sample_rate = 1.0;
auto r2 = new InMemoryReporter();
auto sampling_tracer =
makeZipkinOtTracer(always_sample, std::unique_ptr<Reporter>(r2));
auto span_a = no_sampling_tracer->StartSpan("a");
CHECK(span_a);
span_a->Finish();
auto span_b =
sampling_tracer->StartSpan("b", {ChildOf(&span_a->context())});
CHECK(span_b);
span_b->Finish();
CHECK(r1->spans().empty());
CHECK(r2->spans().empty());
}
SECTION("You can set a single child-of reference when starting a span.") {
auto span_a = tracer->StartSpan("a");
CHECK(span_a);
span_a->Finish();
auto span_b = tracer->StartSpan("b", {ChildOf(&span_a->context())});
CHECK(span_b);
span_b->Finish();
auto spans = reporter->spans();
CHECK(IsChildOf(spans.at(1), spans.at(0)));
}
SECTION("You can set a single follows-from reference when starting a span.") {
auto span_a = tracer->StartSpan("a");
CHECK(span_a);
span_a->Finish();
auto span_b = tracer->StartSpan("b", {FollowsFrom(&span_a->context())});
CHECK(span_b);
span_b->Finish();
auto spans = reporter->spans();
// FollowsFrom is treated the same as ChildOf.
CHECK(IsChildOf(spans.at(1), spans.at(0)));
}
SECTION("Baggage from the span references are copied over to a new span "
"context") {
auto span_a = tracer->StartSpan("a");
CHECK(span_a);
span_a->SetBaggageItem("a", "1");
auto span_b = tracer->StartSpan("b", {ot::ChildOf(&span_a->context())});
CHECK(span_b);
CHECK(span_b->BaggageItem("a") == "1");
}
SECTION("References to non-Zipkin spans and null pointers are ignored.") {
auto noop_tracer = ot::MakeNoopTracer();
auto noop_span = noop_tracer->StartSpan("noop");
CHECK(noop_span);
ot::StartSpanOptions options;
options.references.push_back(std::make_pair(
ot::SpanReferenceType::ChildOfRef, &noop_span->context()));
options.references.push_back(
std::make_pair(ot::SpanReferenceType::ChildOfRef, nullptr));
auto span = tracer->StartSpanWithOptions("a", options);
CHECK(span);
span->Finish();
CHECK(!reporter->top().isSetParentId());
}
SECTION("Calling Finish a second time does nothing.") {
auto span = tracer->StartSpan("a");
CHECK(span);
span->Finish();
CHECK(reporter->size() == 1);
span->Finish();
CHECK(reporter->size() == 1);
}
SECTION("The operation name can be changed after the span is started.") {
auto span = tracer->StartSpan("a");
CHECK(span);
span->SetOperationName("b");
span->Finish();
CHECK(reporter->top().name() == "b");
}
SECTION("Tags can be specified after a span is started.") {
auto span = tracer->StartSpan("a");
CHECK(span);
span->SetTag("abc", 123);
span->Finish();
CHECK(hasTag(reporter->top(), "abc", 123));
}
}