Skip to content

Commit

Permalink
fix: trim inspected headers (#137)
Browse files Browse the repository at this point in the history
Fixed a regression introduced in #42 preventing HTTP headers with
leading spaces to be parsed.
  • Loading branch information
dmehala authored Jun 28, 2024
1 parent 8112767 commit 73ba630
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/datadog/extraction_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Expected<Optional<std::uint64_t>> extract_id_header(const DictReader& headers,
if (!found) {
return result;
}
auto parsed_id = parse_uint64(*found, base);
auto parsed_id = parse_uint64(trim(*found), base);
if (auto* error = parsed_id.if_error()) {
std::string prefix;
prefix += "Could not extract ";
Expand Down Expand Up @@ -127,7 +127,7 @@ Expected<ExtractedData> extract_datadog(
result.parent_id = *parent_id;

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

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

const StringView sampling_priority_header = "x-b3-sampled";
if (auto found = headers.lookup(sampling_priority_header)) {
auto sampling_priority = parse_int(*found, 10);
auto sampling_priority = parse_int(trim(*found), 10);
if (auto* error = sampling_priority.if_error()) {
std::string prefix;
prefix += "Could not extract B3-style sampling priority from ";
Expand Down
25 changes: 25 additions & 0 deletions test/test_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,15 @@ TEST_CASE("span extraction") {
TraceID(123),
456,
2},
{__LINE__,
"datadog style with leading and trailing spaces",
{PropagationStyle::DATADOG},
{{"x-datadog-trace-id", " 123 "},
{"x-datadog-parent-id", " 456 "},
{"x-datadog-sampling-priority", " 2 "}},
TraceID(123),
456,
2},
{__LINE__,
"datadog style without sampling priority",
{PropagationStyle::DATADOG},
Expand All @@ -468,6 +477,15 @@ TEST_CASE("span extraction") {
TraceID(0xabc),
0xdef,
0},
{__LINE__,
"B3 style with leading and trailing spaces",
{PropagationStyle::B3},
{{"x-b3-traceid", " abc "},
{"x-b3-spanid", " def "},
{"x-b3-sampled", " 0 "}},
TraceID(0xabc),
0xdef,
0},
{__LINE__,
"B3 style without sampling priority",
{PropagationStyle::B3},
Expand Down Expand Up @@ -598,6 +616,13 @@ TEST_CASE("span extraction") {
67667974448284343ULL, // expected_parent_id
1}, // expected_sampling_priority

{__LINE__, "valid: w3.org example 1 with leading and trailing spaces",
" 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01 ", // traceparent
nullopt,
*TraceID::parse_hex("4bf92f3577b34da6a3ce929d0e0e4736"), // expected_trace_id
67667974448284343ULL, // expected_parent_id
1}, // expected_sampling_priority

{__LINE__, "valid: w3.org example 2",
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00", // traceparent
nullopt,
Expand Down

0 comments on commit 73ba630

Please sign in to comment.