diff --git a/src/datadog/w3c_propagation.cpp b/src/datadog/w3c_propagation.cpp index cd504184..ae8290be 100644 --- a/src/datadog/w3c_propagation.cpp +++ b/src/datadog/w3c_propagation.cpp @@ -255,6 +255,10 @@ void parse_datadog_trace_state(ExtractedData& result, } const auto key = pair.substr(0, kv_separator); + const bool has_padding = trim(key).size() != key.size(); + if (has_padding) { + continue; + } const auto value = pair.substr(kv_separator + 1); if (key == "o") { result.origin = std::string{value}; diff --git a/test/test_span.cpp b/test/test_span.cpp index 6b6f0d40..b9e6db1a 100644 --- a/test/test_span.cpp +++ b/test/test_span.cpp @@ -815,6 +815,23 @@ TEST_SPAN("injecting W3C tracestate header") { // The "s:0" comes from the sampling decision in `traceparent_drop`. "dd=s:0;p:$parent_id;foo:bar;boing:boing"}, + {__LINE__, + "drop whitespace-padded fields", + { + {"traceparent", traceparent_drop}, + {"tracestate", "dd=foo:bar; boing:boing; t.x:y"}, + }, + // The "s:0" comes from the sampling decision in `traceparent_drop`. + "dd=s:0;p:$parent_id;foo:bar"}, + + {__LINE__, + "keep spaces at the end of values", + { + {"traceparent", traceparent_drop}, + {"tracestate", "dd=o:France ;t.foo:x ;foo:bar ;boing:boing"}, + }, + "dd=s:0;p:$parent_id;o:France ;t.foo:x ;foo:bar ;boing:boing"}, + {__LINE__, "all of the above", { diff --git a/test/test_tracer.cpp b/test/test_tracer.cpp index 4ab69071..066a9781 100644 --- a/test/test_tracer.cpp +++ b/test/test_tracer.cpp @@ -997,6 +997,32 @@ TEST_TRACER("span extraction") { "0000000000000000", // expected_datadog_w3c_parent_id, }, + { + __LINE__, + "dd entry with whitespace-padded keys", + traceparent_drop, // traceparent + "dd=foo:bar; t.x:y; ;baz :bam;o:France", // tracestate + 0, // expected_sampling_priority + "France", // expected_origin + {}, // expected_trace_tags + nullopt, // expected_additional_w3c_tracestate + "foo:bar", // expected_additional_datadog_w3c_tracestate + "0000000000000000", // expected_datadog_w3c_parent_id, + }, + + { + __LINE__, + "dd entry with spaces at the end of values", + traceparent_drop, // traceparent + "dd=t.foo:bar ;o:France ;qux:quux", // tracestate + 0, // expected_sampling_priority + "France ", // expected_origin + {{"_dd.p.foo", "bar "}}, // expected_trace_tags + nullopt, // expected_additional_w3c_tracestate + "qux:quux", // expected_additional_datadog_w3c_tracestate + "0000000000000000", // expected_datadog_w3c_parent_id, + }, + { __LINE__, "origin, trace tags, parent, and extra fields",