Skip to content

Commit 73ba630

Browse files
authored
fix: trim inspected headers (#137)
Fixed a regression introduced in #42 preventing HTTP headers with leading spaces to be parsed.
1 parent 8112767 commit 73ba630

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/datadog/extraction_util.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Expected<Optional<std::uint64_t>> extract_id_header(const DictReader& headers,
7171
if (!found) {
7272
return result;
7373
}
74-
auto parsed_id = parse_uint64(*found, base);
74+
auto parsed_id = parse_uint64(trim(*found), base);
7575
if (auto* error = parsed_id.if_error()) {
7676
std::string prefix;
7777
prefix += "Could not extract ";
@@ -127,7 +127,7 @@ Expected<ExtractedData> extract_datadog(
127127
result.parent_id = *parent_id;
128128

129129
if (auto found = headers.lookup("x-datadog-sampling-priority")) {
130-
auto sampling_priority = parse_int(*found, 10);
130+
auto sampling_priority = parse_int(trim(*found), 10);
131131
if (auto* error = sampling_priority.if_error()) {
132132
std::string prefix;
133133
prefix +=
@@ -166,7 +166,7 @@ Expected<ExtractedData> extract_b3(
166166
result.style = PropagationStyle::B3;
167167

168168
if (auto found = headers.lookup("x-b3-traceid")) {
169-
auto parsed = TraceID::parse_hex(*found);
169+
auto parsed = TraceID::parse_hex(trim(*found));
170170
if (auto* error = parsed.if_error()) {
171171
std::string prefix = "Could not extract B3-style trace ID from \"";
172172
append(prefix, *found);
@@ -185,7 +185,7 @@ Expected<ExtractedData> extract_b3(
185185

186186
const StringView sampling_priority_header = "x-b3-sampled";
187187
if (auto found = headers.lookup(sampling_priority_header)) {
188-
auto sampling_priority = parse_int(*found, 10);
188+
auto sampling_priority = parse_int(trim(*found), 10);
189189
if (auto* error = sampling_priority.if_error()) {
190190
std::string prefix;
191191
prefix += "Could not extract B3-style sampling priority from ";

test/test_tracer.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,15 @@ TEST_CASE("span extraction") {
445445
TraceID(123),
446446
456,
447447
2},
448+
{__LINE__,
449+
"datadog style with leading and trailing spaces",
450+
{PropagationStyle::DATADOG},
451+
{{"x-datadog-trace-id", " 123 "},
452+
{"x-datadog-parent-id", " 456 "},
453+
{"x-datadog-sampling-priority", " 2 "}},
454+
TraceID(123),
455+
456,
456+
2},
448457
{__LINE__,
449458
"datadog style without sampling priority",
450459
{PropagationStyle::DATADOG},
@@ -468,6 +477,15 @@ TEST_CASE("span extraction") {
468477
TraceID(0xabc),
469478
0xdef,
470479
0},
480+
{__LINE__,
481+
"B3 style with leading and trailing spaces",
482+
{PropagationStyle::B3},
483+
{{"x-b3-traceid", " abc "},
484+
{"x-b3-spanid", " def "},
485+
{"x-b3-sampled", " 0 "}},
486+
TraceID(0xabc),
487+
0xdef,
488+
0},
471489
{__LINE__,
472490
"B3 style without sampling priority",
473491
{PropagationStyle::B3},
@@ -598,6 +616,13 @@ TEST_CASE("span extraction") {
598616
67667974448284343ULL, // expected_parent_id
599617
1}, // expected_sampling_priority
600618

619+
{__LINE__, "valid: w3.org example 1 with leading and trailing spaces",
620+
" 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01 ", // traceparent
621+
nullopt,
622+
*TraceID::parse_hex("4bf92f3577b34da6a3ce929d0e0e4736"), // expected_trace_id
623+
67667974448284343ULL, // expected_parent_id
624+
1}, // expected_sampling_priority
625+
601626
{__LINE__, "valid: w3.org example 2",
602627
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00", // traceparent
603628
nullopt,

0 commit comments

Comments
 (0)