Skip to content

Support multikey-single key context interoperability. #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions src/propagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,18 @@ static opentracing::expected<bool> ExtractSpanContext(
uint64_t& span_id, std::unordered_map<std::string, std::string>& baggage,
KeyCompare key_compare) {
if (propagation_options.use_single_key) {
return ExtractSpanContextSingleKey(carrier, trace_id, span_id, baggage,
key_compare);
} else {
return ExtractSpanContextMultiKey(carrier, trace_id, span_id, baggage,
key_compare);
auto span_context_maybe = ExtractSpanContextSingleKey(
carrier, trace_id, span_id, baggage, key_compare);

// If no span context was found, fall back to multikey extraction so as to
// support interoperability with other tracers.
if (!span_context_maybe || *span_context_maybe) {
return span_context_maybe;
}
}

return ExtractSpanContextMultiKey(carrier, trace_id, span_id, baggage,
key_compare);
}

opentracing::expected<bool> ExtractSpanContext(
Expand Down
21 changes: 19 additions & 2 deletions test/propagation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,15 @@ TEST_CASE("propagation") {
}

TEST_CASE("propagation - single key") {
auto recorder = new InMemoryRecorder();
auto recorder = new InMemoryRecorder{};
PropagationOptions propagation_options;
propagation_options.use_single_key = true;
auto tracer = std::shared_ptr<opentracing::Tracer>{new LightStepTracerImpl{
propagation_options, std::unique_ptr<Recorder>{recorder}}};
auto multikey_tracer =
std::shared_ptr<opentracing::Tracer>{new LightStepTracerImpl{
PropagationOptions{},
std::unique_ptr<Recorder>{new InMemoryRecorder{}}}};
std::unordered_map<std::string, std::string> text_map;
TextMapCarrier text_map_carrier(text_map);
auto span = tracer->StartSpan("a");
Expand All @@ -259,14 +263,27 @@ TEST_CASE("propagation - single key") {
CHECK(text_map_carrier.foreach_key_call_count == 0);
}

SECTION(
"Extract falls back to the multikey format if it doesn't find a "
"single-key header") {
text_map.clear();
CHECK(multikey_tracer->Inject(span->context(), text_map_carrier));
CHECK(text_map.size() > 1);
auto span_context_maybe = tracer->Extract(text_map_carrier);
CHECK((span_context_maybe && span_context_maybe->get()));
}

SECTION(
"When LookupKey is used, a nullptr is returned if there is no "
"span_context") {
text_map.clear();
text_map_carrier.supports_lookup = true;
auto span_context_maybe = tracer->Extract(text_map_carrier);
CHECK((span_context_maybe && span_context_maybe->get() == nullptr));
CHECK(text_map_carrier.foreach_key_call_count == 0);

// We have to iterate through all of the keys once when extraction falls
// back to the multikey format.
CHECK(text_map_carrier.foreach_key_call_count == 1);
}

SECTION("Verify only valid base64 characters are used.") {
Expand Down