Skip to content

Commit 36cfe59

Browse files
authored
Add support for sampling.priority. (#94)
* Add support for sampling.priority. * Upload test logs. * Fix storing of artifacts. * Redo CI commands. * Update OpenTracing CI version. * Update OT bazel version. * Add missing mutext. * Fix propagation tests.
1 parent 42ed110 commit 36cfe59

10 files changed

+252
-81
lines changed

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ workspace(name = "com_lightstep_tracer_cpp")
33
git_repository(
44
name = "io_opentracing_cpp",
55
remote = "https://github.com/opentracing/opentracing-cpp",
6-
commit = "f3c1f42601d13504c68e2bc81c60604f0de055dd",
6+
commit = "900f9d9297a71ddf4a5dff2051a01493014c07c5",
77
)
88

99
http_archive(

ci/build_plugin.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
[ -z "${OPENTRACING_VERSION}" ] && export OPENTRACING_VERSION="v1.3.0"
5+
[ -z "${OPENTRACING_VERSION}" ] && export OPENTRACING_VERSION="v1.4.0"
66
[ -z "${GRPC_VERSION}" ] && export GRPC_VERSION="v1.10.0"
77

88
# Compile for a portable cpu architecture

ci/install_opentracing.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
[ -z "${OPENTRACING_VERSION}" ] && export OPENTRACING_VERSION="v1.3.0"
5+
[ -z "${OPENTRACING_VERSION}" ] && export OPENTRACING_VERSION="v1.4.0"
66

77
# Build OpenTracing
88
cd /

src/lightstep_span.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "lightstep_span.h"
2+
#include <opentracing/ext/tags.h>
23
#include "utility.h"
34

45
using opentracing::SystemTime;
@@ -7,6 +8,13 @@ using opentracing::SteadyClock;
78
using opentracing::SteadyTime;
89

910
namespace lightstep {
11+
//------------------------------------------------------------------------------
12+
// is_sampled
13+
//------------------------------------------------------------------------------
14+
static bool is_sampled(const opentracing::Value& value) {
15+
return value != opentracing::Value{0} && value != opentracing::Value{0u};
16+
}
17+
1018
//------------------------------------------------------------------------------
1119
// ComputeStartTimestamps
1220
//------------------------------------------------------------------------------
@@ -43,7 +51,7 @@ static bool SetSpanReference(
4351
const std::pair<opentracing::SpanReferenceType,
4452
const opentracing::SpanContext*>& reference,
4553
std::unordered_map<std::string, std::string>& baggage,
46-
collector::Reference& collector_reference) {
54+
collector::Reference& collector_reference, bool& sampled) {
4755
collector_reference.Clear();
4856
switch (reference.first) {
4957
case opentracing::SpanReferenceType::ChildOfRef:
@@ -67,6 +75,7 @@ static bool SetSpanReference(
6775
referenced_context->trace_id());
6876
collector_reference.mutable_span_context()->set_span_id(
6977
referenced_context->span_id());
78+
sampled = sampled || referenced_context->sampled();
7079

7180
referenced_context->ForeachBaggageItem(
7281
[&baggage](const std::string& key, const std::string& value) {
@@ -96,24 +105,40 @@ LightStepSpan::LightStepSpan(
96105
std::unordered_map<std::string, std::string> baggage;
97106
references_.reserve(options.references.size());
98107
collector::Reference collector_reference;
108+
bool sampled = false;
99109
for (auto& reference : options.references) {
100-
if (!SetSpanReference(logger_, reference, baggage, collector_reference)) {
110+
if (!SetSpanReference(logger_, reference, baggage, collector_reference,
111+
sampled)) {
101112
continue;
102113
}
103114
references_.push_back(collector_reference);
104115
}
105116

117+
// If there are any span references, sampled should be true if any of the
118+
// references are sampled; with no refences, we set sampled to true.
119+
if (references_.empty()) {
120+
sampled = true;
121+
}
122+
106123
// Set tags.
107124
for (auto& tag : options.tags) {
108125
tags_[tag.first] = tag.second;
109126
}
110127

128+
// If sampling_priority is set, it overrides whatever sampling decision was
129+
// derived from the referenced spans.
130+
auto sampling_priority_tag = tags_.find(opentracing::ext::sampling_priority);
131+
if (sampling_priority_tag != tags_.end()) {
132+
sampled = is_sampled(sampling_priority_tag->second);
133+
}
134+
111135
// Set opentracing::SpanContext.
112136
auto trace_id = references_.empty()
113137
? GenerateId()
114138
: references_[0].span_context().trace_id();
115139
auto span_id = GenerateId();
116-
span_context_ = LightStepSpanContext{trace_id, span_id, std::move(baggage)};
140+
span_context_ =
141+
LightStepSpanContext{trace_id, span_id, sampled, std::move(baggage)};
117142
}
118143

119144
//------------------------------------------------------------------------------
@@ -135,6 +160,11 @@ void LightStepSpan::FinishWithOptions(
135160
return;
136161
}
137162

163+
// If the span isn't sampled do nothing.
164+
if (!span_context_.sampled()) {
165+
return;
166+
}
167+
138168
auto finish_timestamp = options.finish_steady_timestamp;
139169
if (finish_timestamp == SteadyTime()) {
140170
finish_timestamp = SteadyClock::now();
@@ -230,6 +260,10 @@ void LightStepSpan::SetTag(opentracing::string_view key,
230260
const opentracing::Value& value) noexcept try {
231261
std::lock_guard<std::mutex> lock_guard{mutex_};
232262
tags_[key] = value;
263+
264+
if (key == opentracing::ext::sampling_priority) {
265+
span_context_.set_sampled(is_sampled(value));
266+
}
233267
} catch (const std::exception& e) {
234268
logger_.Error("SetTag failed: ", e.what());
235269
}

src/lightstep_span_context.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ LightStepSpanContext::LightStepSpanContext(
99
std::unordered_map<std::string, std::string>&& baggage) noexcept
1010
: trace_id_{trace_id}, span_id_{span_id}, baggage_{std::move(baggage)} {}
1111

12+
LightStepSpanContext::LightStepSpanContext(
13+
uint64_t trace_id, uint64_t span_id, bool sampled,
14+
std::unordered_map<std::string, std::string>&& baggage) noexcept
15+
: trace_id_{trace_id},
16+
span_id_{span_id},
17+
sampled_{sampled},
18+
baggage_{std::move(baggage)} {}
19+
1220
//------------------------------------------------------------------------------
1321
// operator=
1422
//------------------------------------------------------------------------------
1523
LightStepSpanContext& LightStepSpanContext::operator=(
1624
LightStepSpanContext&& other) noexcept {
1725
trace_id_ = other.trace_id_;
1826
span_id_ = other.span_id_;
27+
sampled_ = other.sampled_;
1928
baggage_ = std::move(other.baggage_);
2029
return *this;
2130
}
@@ -25,7 +34,7 @@ LightStepSpanContext& LightStepSpanContext::operator=(
2534
//------------------------------------------------------------------------------
2635
void LightStepSpanContext::set_baggage_item(
2736
opentracing::string_view key, opentracing::string_view value) noexcept try {
28-
std::lock_guard<std::mutex> lock_guard{baggage_mutex_};
37+
std::lock_guard<std::mutex> lock_guard{mutex_};
2938
baggage_.emplace(key, value);
3039
} catch (const std::exception&) {
3140
// Drop baggage item upon error.
@@ -36,7 +45,7 @@ void LightStepSpanContext::set_baggage_item(
3645
//------------------------------------------------------------------------------
3746
std::string LightStepSpanContext::baggage_item(
3847
opentracing::string_view key) const {
39-
std::lock_guard<std::mutex> lock_guard{baggage_mutex_};
48+
std::lock_guard<std::mutex> lock_guard{mutex_};
4049
auto lookup = baggage_.find(key);
4150
if (lookup != baggage_.end()) {
4251
return lookup->second;
@@ -50,11 +59,27 @@ std::string LightStepSpanContext::baggage_item(
5059
void LightStepSpanContext::ForeachBaggageItem(
5160
std::function<bool(const std::string& key, const std::string& value)> f)
5261
const {
53-
std::lock_guard<std::mutex> lock_guard{baggage_mutex_};
62+
std::lock_guard<std::mutex> lock_guard{mutex_};
5463
for (const auto& baggage_item : baggage_) {
5564
if (!f(baggage_item.first, baggage_item.second)) {
5665
return;
5766
}
5867
}
5968
}
69+
70+
//------------------------------------------------------------------------------
71+
// sampled
72+
//------------------------------------------------------------------------------
73+
bool LightStepSpanContext::sampled() const noexcept {
74+
std::lock_guard<std::mutex> lock_guard{mutex_};
75+
return sampled_;
76+
}
77+
78+
//------------------------------------------------------------------------------
79+
// set_sampled
80+
//------------------------------------------------------------------------------
81+
void LightStepSpanContext::set_sampled(bool sampled) noexcept {
82+
std::lock_guard<std::mutex> lock_guard{mutex_};
83+
sampled_ = sampled;
84+
}
6085
} // namespace lightstep

src/lightstep_span_context.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class LightStepSpanContext : public opentracing::SpanContext {
1616
uint64_t trace_id, uint64_t span_id,
1717
std::unordered_map<std::string, std::string>&& baggage) noexcept;
1818

19+
LightStepSpanContext(
20+
uint64_t trace_id, uint64_t span_id, bool sampled,
21+
std::unordered_map<std::string, std::string>&& baggage) noexcept;
22+
1923
LightStepSpanContext(const LightStepSpanContext&) = delete;
2024
LightStepSpanContext(LightStepSpanContext&&) = delete;
2125

@@ -36,27 +40,31 @@ class LightStepSpanContext : public opentracing::SpanContext {
3640
template <class Carrier>
3741
opentracing::expected<void> Inject(
3842
const PropagationOptions& propagation_options, Carrier& writer) const {
39-
std::lock_guard<std::mutex> lock_guard{baggage_mutex_};
43+
std::lock_guard<std::mutex> lock_guard{mutex_};
4044
return InjectSpanContext(propagation_options, writer, trace_id_, span_id_,
41-
baggage_);
45+
sampled_, baggage_);
4246
}
4347

4448
template <class Carrier>
4549
opentracing::expected<bool> Extract(
4650
const PropagationOptions& propagation_options, Carrier& reader) {
47-
std::lock_guard<std::mutex> lock_guard{baggage_mutex_};
51+
std::lock_guard<std::mutex> lock_guard{mutex_};
4852
return ExtractSpanContext(propagation_options, reader, trace_id_, span_id_,
49-
baggage_);
53+
sampled_, baggage_);
5054
}
5155

5256
uint64_t trace_id() const noexcept { return trace_id_; }
5357
uint64_t span_id() const noexcept { return span_id_; }
5458

59+
bool sampled() const noexcept;
60+
void set_sampled(bool sampled) noexcept;
61+
5562
private:
5663
uint64_t trace_id_ = 0;
5764
uint64_t span_id_ = 0;
5865

59-
mutable std::mutex baggage_mutex_;
66+
mutable std::mutex mutex_;
67+
bool sampled_ = true;
6068
std::unordered_map<std::string, std::string> baggage_;
6169
};
6270
} // namespace lightstep

0 commit comments

Comments
 (0)