Skip to content

Commit 7ce7d9a

Browse files
committed
Merge remote-tracking branch 'origin/main' into glopes/rem-cfg
2 parents 99d2fe3 + 9647240 commit 7ce7d9a

17 files changed

+453
-200
lines changed

.github/CODEOWNERS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Automatically assign the team as a reviewer.
2+
# https://help.github.com/en/articles/about-code-owners
3+
4+
# Default owners, overridden by file/directory specific owners below
5+
* @DataDog/dd-trace-cpp

.github/pull_request_template.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Description
2+
3+
# Motivation
4+
5+
# Additional Notes
6+
7+
Jira ticket: [PROJ-IDENT]
8+
9+
<!--
10+
# Opening vs Drafting a PR:
11+
When opening a pull request, please open it as a draft to not auto assign reviewers before you feel the pull request is in a reviewable state.
12+
13+
# Linking a JIRA ticket:
14+
Please link your JIRA ticket by adding its identifier between brackets (ex [PROJ-IDENT]) in the PR description, not the title.
15+
This requirement only applies to Datadog employees.
16+
-->

examples/http-server/common/tracingutil.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class HeaderWriter final : public datadog::tracing::DictWriter {
1616
explicit HeaderWriter(httplib::Headers& headers) : headers_(headers) {}
1717

1818
void set(std::string_view key, std::string_view value) override {
19-
headers_.emplace(key, value);
19+
auto found = headers_.find(std::string(key));
20+
if (found == headers_.cend()) {
21+
headers_.emplace(key, value);
22+
} else {
23+
found->second = value;
24+
}
2025
}
2126
};
2227

examples/http-server/proxy/proxy.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,38 @@ int main() {
4949
auto forward_handler = [&tracer, &upstream_client](
5050
const httplib::Request& req,
5151
httplib::Response& res) {
52-
auto span = tracer.create_span();
53-
span.set_name("forward.request");
54-
span.set_resource_name(req.method + " " + req.path);
55-
span.set_tag("network.origin.ip", req.remote_addr);
56-
span.set_tag("network.origin.port", std::to_string(req.remote_port));
57-
span.set_tag("http.url_details.path", req.target);
58-
span.set_tag("http.route", req.path);
59-
span.set_tag("http.method", req.method);
52+
tracingutil::HeaderReader reader(req.headers);
53+
auto span = tracer.extract_or_create_span(reader);
54+
span->set_name("forward.request");
55+
span->set_resource_name(req.method + " " + req.path);
56+
span->set_tag("network.origin.ip", req.remote_addr);
57+
span->set_tag("network.origin.port", std::to_string(req.remote_port));
58+
span->set_tag("http.url_details.path", req.target);
59+
span->set_tag("http.route", req.path);
60+
span->set_tag("http.method", req.method);
6061

6162
httplib::Error er;
6263
httplib::Request forward_request(req);
6364
forward_request.path = req.target;
6465

6566
tracingutil::HeaderWriter writer(forward_request.headers);
66-
span.inject(writer);
67+
span->inject(writer);
6768

6869
upstream_client.send(forward_request, res, er);
6970
if (er != httplib::Error::Success) {
7071
res.status = 500;
71-
span.set_error_message(httplib::to_string(er));
72+
span->set_error_message(httplib::to_string(er));
7273
std::cerr << "Error occurred while proxying request: " << req.target
7374
<< "\n";
7475
} else {
7576
tracingutil::HeaderReader reader(res.headers);
76-
auto status = span.read_sampling_delegation_response(reader);
77+
auto status = span->read_sampling_delegation_response(reader);
7778
if (auto error = status.if_error()) {
7879
std::cerr << error << "\n";
7980
}
8081
}
8182

82-
span.set_tag("http.status_code", std::to_string(res.status));
83+
span->set_tag("http.status_code", std::to_string(res.status));
8384
};
8485

8586
httplib::Server server;

src/datadog/extracted_data.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ struct ExtractedData {
2323
bool delegate_sampling_decision = false;
2424
Optional<int> sampling_priority;
2525
// If this `ExtractedData` was created on account of `PropagationStyle::W3C`,
26+
// then `datadog_w3c_parent_id` contains the parts of the "tracestate"
27+
// refering to the latest datadog parent ID.
28+
Optional<std::string> datadog_w3c_parent_id;
29+
// If this `ExtractedData` was created on account of `PropagationStyle::W3C`,
2630
// then `additional_w3c_tracestate` contains the parts of the "tracestate"
2731
// header that are not the "dd" (Datadog) entry. If there are no other parts,
2832
// then `additional_w3c_tracestate` is null.

src/datadog/extraction_util.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ ExtractedData merge(const std::vector<ExtractedData>& contexts) {
295295
});
296296

297297
if (other != contexts.end()) {
298+
result.datadog_w3c_parent_id = other->datadog_w3c_parent_id;
298299
result.additional_w3c_tracestate = other->additional_w3c_tracestate;
299300
result.additional_datadog_w3c_tracestate =
300301
other->additional_datadog_w3c_tracestate;

src/datadog/glob.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "glob.h"
22

3+
#include <cctype>
34
#include <cstdint>
45

56
namespace datadog {
@@ -18,8 +19,11 @@ bool glob_match(StringView pattern, StringView subject) {
1819
Index next_p = 0; // next [p]attern index
1920
Index next_s = 0; // next [s]ubject index
2021

21-
while (p < pattern.size() || s < subject.size()) {
22-
if (p < pattern.size()) {
22+
const size_t p_size = pattern.size();
23+
const size_t s_size = subject.size();
24+
25+
while (p < p_size || s < s_size) {
26+
if (p < p_size) {
2327
const char pattern_char = pattern[p];
2428
switch (pattern_char) {
2529
case '*':
@@ -30,22 +34,22 @@ bool glob_match(StringView pattern, StringView subject) {
3034
++p;
3135
continue;
3236
case '?':
33-
if (s < subject.size()) {
37+
if (s < s_size) {
3438
++p;
3539
++s;
3640
continue;
3741
}
3842
break;
3943
default:
40-
if (s < subject.size() && subject[s] == pattern_char) {
44+
if (s < s_size && tolower(subject[s]) == tolower(pattern_char)) {
4145
++p;
4246
++s;
4347
continue;
4448
}
4549
}
4650
}
4751
// Mismatch. Maybe restart.
48-
if (0 < next_s && next_s <= subject.size()) {
52+
if (0 < next_s && next_s <= s_size) {
4953
p = next_p;
5054
s = next_s;
5155
continue;

src/datadog/parse_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// This component provides parsing-related miscellanea.
44

5+
#include <cstdint>
56
#include <string>
67
#include <unordered_map>
78
#include <vector>

src/datadog/tags.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "tags.h"
22

3-
#include "string_util.h"
4-
53
namespace datadog {
64
namespace tracing {
75
namespace tags {
@@ -32,11 +30,10 @@ const std::string process_id = "process_id";
3230
const std::string language = "language";
3331
const std::string runtime_id = "runtime-id";
3432
const std::string sampling_decider = "_dd.is_sampling_decider";
33+
const std::string w3c_parent_id = "_dd.parent_id";
3534

3635
} // namespace internal
3736

38-
bool is_internal(StringView tag_name) { return starts_with(tag_name, "_dd."); }
39-
4037
} // namespace tags
4138
} // namespace tracing
4239
} // namespace datadog

src/datadog/tags.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <string>
77

8+
#include "string_util.h"
89
#include "string_view.h"
910

1011
namespace datadog {
@@ -36,11 +37,14 @@ extern const std::string process_id;
3637
extern const std::string language;
3738
extern const std::string runtime_id;
3839
extern const std::string sampling_decider;
40+
extern const std::string w3c_parent_id;
3941
} // namespace internal
4042

4143
// Return whether the specified `tag_name` is reserved for use internal to this
4244
// library.
43-
bool is_internal(StringView tag_name);
45+
inline bool is_internal(StringView tag_name) {
46+
return starts_with(tag_name, "_dd.");
47+
}
4448

4549
} // namespace tags
4650
} // namespace tracing

src/datadog/trace_segment.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,11 @@ bool TraceSegment::inject(DictWriter& writer, const SpanData& span,
418418
writer.set(
419419
"traceparent",
420420
encode_traceparent(span.trace_id, span.span_id, sampling_priority));
421-
writer.set("tracestate",
422-
encode_tracestate(sampling_priority, origin_, trace_tags,
423-
additional_datadog_w3c_tracestate_,
424-
additional_w3c_tracestate_));
421+
writer.set(
422+
"tracestate",
423+
encode_tracestate(span.span_id, sampling_priority, origin_,
424+
trace_tags, additional_datadog_w3c_tracestate_,
425+
additional_w3c_tracestate_));
425426
break;
426427
default:
427428
assert(style == PropagationStyle::NONE);

src/datadog/tracer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
175175
}
176176

177177
auto [trace_id, parent_id, origin, trace_tags, delegate_sampling_decision,
178-
sampling_priority, additional_w3c_tracestate,
178+
sampling_priority, datadog_w3c_parent_id, additional_w3c_tracestate,
179179
additional_datadog_w3c_tracestate, style, headers_examined] =
180180
merge(extracted_contexts);
181181

@@ -277,6 +277,10 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
277277
}
278278
}
279279

280+
if (datadog_w3c_parent_id) {
281+
span_data->tags[tags::internal::w3c_parent_id] = *datadog_w3c_parent_id;
282+
}
283+
280284
Optional<SamplingDecision> sampling_decision;
281285
if (sampling_priority) {
282286
SamplingDecision decision;

src/datadog/w3c_propagation.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ void parse_datadog_tracestate(ExtractedData& result, StringView datadog_value) {
221221
(*result.sampling_priority > 0) == (priority > 0)) {
222222
result.sampling_priority = priority;
223223
}
224+
} else if (key == "p") {
225+
if (value.size() != 16) {
226+
// chaff!
227+
pair_begin = pair_end == end ? end : pair_end + 1;
228+
continue;
229+
}
230+
231+
result.datadog_w3c_parent_id = std::string(value);
224232
} else if (starts_with(key, "t.")) {
225233
// The part of the key that follows "t." is the name of a trace tag,
226234
// except without the "_dd.p." prefix.
@@ -300,6 +308,7 @@ Expected<ExtractedData> extract_w3c(
300308
return result;
301309
}
302310

311+
result.datadog_w3c_parent_id = "0000000000000000";
303312
extract_tracestate(result, headers);
304313

305314
return result;
@@ -326,11 +335,14 @@ std::string encode_traceparent(TraceID trace_id, std::uint64_t span_id,
326335
}
327336

328337
std::string encode_datadog_tracestate(
329-
int sampling_priority, const Optional<std::string>& origin,
338+
uint64_t span_id, int sampling_priority,
339+
const Optional<std::string>& origin,
330340
const std::vector<std::pair<std::string, std::string>>& trace_tags,
331341
const Optional<std::string>& additional_datadog_w3c_tracestate) {
332342
std::string result = "dd=s:";
333343
result += std::to_string(sampling_priority);
344+
result += ";p:";
345+
result += hex_padded(span_id);
334346

335347
if (origin) {
336348
result += ";o:";
@@ -382,12 +394,14 @@ std::string encode_datadog_tracestate(
382394
}
383395

384396
std::string encode_tracestate(
385-
int sampling_priority, const Optional<std::string>& origin,
397+
uint64_t span_id, int sampling_priority,
398+
const Optional<std::string>& origin,
386399
const std::vector<std::pair<std::string, std::string>>& trace_tags,
387400
const Optional<std::string>& additional_datadog_w3c_tracestate,
388401
const Optional<std::string>& additional_w3c_tracestate) {
389-
std::string result = encode_datadog_tracestate(
390-
sampling_priority, origin, trace_tags, additional_datadog_w3c_tracestate);
402+
std::string result =
403+
encode_datadog_tracestate(span_id, sampling_priority, origin, trace_tags,
404+
additional_datadog_w3c_tracestate);
391405

392406
if (additional_w3c_tracestate) {
393407
result += ',';

src/datadog/w3c_propagation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ std::string encode_traceparent(TraceID trace_id, std::uint64_t span_id,
3737

3838
// Return a value for the "tracestate" header containing the specified fields.
3939
std::string encode_tracestate(
40-
int sampling_priority, const Optional<std::string>& origin,
40+
uint64_t span_id, int sampling_priority,
41+
const Optional<std::string>& origin,
4142
const std::vector<std::pair<std::string, std::string>>& trace_tags,
4243
const Optional<std::string>& additional_datadog_w3c_tracestate,
4344
const Optional<std::string>& additional_w3c_tracestate);

test/test_glob.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
using namespace datadog::tracing;
1010

11-
TEST_CASE("glob") {
11+
TEST_CASE("glob", "[glob]") {
1212
struct TestCase {
1313
StringView pattern;
1414
StringView subject;
@@ -42,7 +42,13 @@ TEST_CASE("glob") {
4242
{"", "", true},
4343
{"", "a", false},
4444
{"*", "", true},
45-
{"?", "", false}
45+
{"?", "", false},
46+
47+
// case sensitivity
48+
{"true", "TRUE", true},
49+
{"true", "True", true},
50+
{"true", "tRue", true},
51+
{"false", "FALSE", true}
4652
}));
4753
// clang-format on
4854

0 commit comments

Comments
 (0)