Skip to content

Commit fcc68f1

Browse files
zyfjeffmattklein123
authored andcommitted
tracing: Fix duplicated x-b3-* header (#5019)
Signed-off-by: tianqian.zyf <[email protected]>
1 parent 6b56612 commit fcc68f1

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

source/extensions/tracers/zipkin/zipkin_core_constants.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class ZipkinCoreConstantValues {
3232
const std::string SERVER_ADDR = "sa";
3333

3434
// Zipkin B3 headers
35-
const Http::LowerCaseString X_B3_TRACE_ID{"X-B3-TraceId"};
36-
const Http::LowerCaseString X_B3_SPAN_ID{"X-B3-SpanId"};
37-
const Http::LowerCaseString X_B3_PARENT_SPAN_ID{"X-B3-ParentSpanId"};
38-
const Http::LowerCaseString X_B3_SAMPLED{"X-B3-Sampled"};
39-
const Http::LowerCaseString X_B3_FLAGS{"X-B3-Flags"};
35+
const Http::LowerCaseString X_B3_TRACE_ID{"x-b3-traceid"};
36+
const Http::LowerCaseString X_B3_SPAN_ID{"x-b3-spanid"};
37+
const Http::LowerCaseString X_B3_PARENT_SPAN_ID{"x-b3-parentspanid"};
38+
const Http::LowerCaseString X_B3_SAMPLED{"x-b3-sampled"};
39+
const Http::LowerCaseString X_B3_FLAGS{"x-b3-flags"};
4040

4141
// Zipkin b3 single header
4242
const Http::LowerCaseString B3{"b3"};

source/extensions/tracers/zipkin/zipkin_tracer_impl.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ void ZipkinSpan::setTag(const std::string& name, const std::string& value) {
3030

3131
void ZipkinSpan::injectContext(Http::HeaderMap& request_headers) {
3232
// Set the trace-id and span-id headers properly, based on the newly-created span structure.
33-
request_headers.addReferenceKey(ZipkinCoreConstants::get().X_B3_TRACE_ID,
33+
request_headers.setReferenceKey(ZipkinCoreConstants::get().X_B3_TRACE_ID,
3434
span_.traceIdAsHexString());
35-
request_headers.addReferenceKey(ZipkinCoreConstants::get().X_B3_SPAN_ID, span_.idAsHexString());
35+
request_headers.setReferenceKey(ZipkinCoreConstants::get().X_B3_SPAN_ID, span_.idAsHexString());
3636

3737
// Set the parent-span header properly, based on the newly-created span structure.
3838
if (span_.isSetParentId()) {
39-
request_headers.addReferenceKey(ZipkinCoreConstants::get().X_B3_PARENT_SPAN_ID,
39+
request_headers.setReferenceKey(ZipkinCoreConstants::get().X_B3_PARENT_SPAN_ID,
4040
span_.parentIdAsHexString());
4141
}
4242

4343
// Set the sampled header.
44-
request_headers.addReferenceKey(ZipkinCoreConstants::get().X_B3_SAMPLED,
44+
request_headers.setReferenceKey(ZipkinCoreConstants::get().X_B3_SAMPLED,
4545
span_.sampled() ? ZipkinCoreConstants::get().SAMPLED
4646
: ZipkinCoreConstants::get().NOT_SAMPLED);
4747
}

test/extensions/tracers/zipkin/zipkin_tracer_impl_test.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <chrono>
2+
#include <functional>
23
#include <memory>
34
#include <string>
5+
#include <unordered_map>
46

57
#include "common/http/header_map_impl.h"
68
#include "common/http/headers.h"
@@ -577,6 +579,37 @@ TEST_F(ZipkinDriverTest, ExplicitlySetSampledTrue) {
577579
// Check B3 sampled flag is set to sample
578580
EXPECT_EQ(ZipkinCoreConstants::get().SAMPLED, sampled_entry->value().getStringView());
579581
}
582+
583+
TEST_F(ZipkinDriverTest, DuplicatedHeader) {
584+
setupValidDriver();
585+
request_headers_.addReferenceKey(ZipkinCoreConstants::get().X_B3_TRACE_ID,
586+
Hex::uint64ToHex(generateRandom64()));
587+
request_headers_.addReferenceKey(ZipkinCoreConstants::get().X_B3_SPAN_ID,
588+
Hex::uint64ToHex(generateRandom64()));
589+
request_headers_.addReferenceKey(ZipkinCoreConstants::get().X_B3_PARENT_SPAN_ID,
590+
Hex::uint64ToHex(generateRandom64()));
591+
Tracing::SpanPtr span = driver_->startSpan(config_, request_headers_, operation_name_,
592+
start_time_, {Tracing::Reason::Sampling, false});
593+
594+
typedef std::function<bool(const std::string& key)> DupCallback;
595+
DupCallback dup_callback = [](const std::string& key) -> bool {
596+
static std::unordered_map<std::string, bool> dup;
597+
if (dup.find(key) == dup.end()) {
598+
dup[key] = true;
599+
return false;
600+
}
601+
return true;
602+
};
603+
604+
span->setSampled(true);
605+
span->injectContext(request_headers_);
606+
request_headers_.iterate(
607+
[](const Http::HeaderEntry& header, void* cb) -> Http::HeaderMap::Iterate {
608+
EXPECT_FALSE(static_cast<DupCallback*>(cb)->operator()(header.key().c_str()));
609+
return Http::HeaderMap::Iterate::Continue;
610+
},
611+
&dup_callback);
612+
}
580613
} // namespace Zipkin
581614
} // namespace Tracers
582615
} // namespace Extensions

0 commit comments

Comments
 (0)