Skip to content

Commit e35c68a

Browse files
committed
Address review comments
1 parent c07c8aa commit e35c68a

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

src/datadog/endpoint_inferral.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ enum component_type : std::uint8_t {
3737
is_hex_id = 1 << 3,
3838
is_str = 1 << 4,
3939
};
40+
static constexpr auto all_components =
41+
is_int | is_int_id | is_hex | is_hex_id | is_str;
42+
static_assert(all_components == (is_str << 1) - 1);
4043

4144
StringView to_string(component_type type) {
4245
switch (type) {
@@ -62,7 +65,7 @@ inline uint8_t bool2mask(bool x) { return x ? 0xFF : 0x00; }
6265

6366
component_type component_replacement(StringView path) noexcept {
6467
// viable_components is a bitset of the component types not yet excluded
65-
std::uint8_t viable_components = 0x1F; // (is_str << 1) - 1
68+
std::uint8_t viable_components = all_components;
6669
bool found_special_char = false;
6770
bool found_digit = false;
6871

@@ -114,6 +117,7 @@ component_type component_replacement(StringView path) noexcept {
114117
return component_type::none;
115118
}
116119

120+
// Get least significant set bit to determine component w/ highest precedence
117121
// c++20: use std::countr_zero
118122
std::uint8_t lsb = static_cast<std::uint8_t>(
119123
viable_components &

src/datadog/trace_segment.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ void inject_trace_tags(
8181
}
8282
}
8383

84+
void maybe_calculate_http_endpoint(HttpEndpointCalculationMode renaming_mode,
85+
SpanData& local_root) {
86+
// calculate http.endpoint if:
87+
// a) the feature is not disabled, and
88+
// b) the tag http.endpoint is not already set, and
89+
// c) http.url is set, and
90+
// d) http.route is not set or resource_renaming_mode is ALWAYS_CALCULATE
91+
if (renaming_mode == HttpEndpointCalculationMode::DISABLED ||
92+
local_root.tags.find(tags::http_endpoint) != local_root.tags.end()) {
93+
return;
94+
}
95+
auto http_url_tag = local_root.tags.find(tags::http_url);
96+
const bool should_calculate_endpoint =
97+
http_url_tag != local_root.tags.end() &&
98+
(renaming_mode == HttpEndpointCalculationMode::ALWAYS_CALCULATE ||
99+
local_root.tags.find(tags::http_route) == local_root.tags.end());
100+
101+
if (should_calculate_endpoint) {
102+
Expected<HTTPClient::URL> url_result =
103+
HTTPClient::URL::parse(http_url_tag->second);
104+
if (url_result.has_value()) {
105+
const std::string& path = url_result->path;
106+
local_root.tags[tags::http_endpoint] =
107+
infer_endpoint(path.empty() ? "/" : path);
108+
}
109+
}
110+
}
84111
} // namespace
85112

86113
TraceSegment::TraceSegment(
@@ -250,30 +277,7 @@ void TraceSegment::span_finished() {
250277
span.tags[tags::internal::runtime_id] = runtime_id_.string();
251278
}
252279

253-
// calculate http.endpoint if:
254-
// a) the feature is not disabled, and
255-
// b) the tag http.endpoint is not already set, and
256-
// c) http.url is set, and
257-
// d) http.route is not set or resource_renaming_mode is ALWAYS_CALCULATE
258-
if (resource_renaming_mode_ != HttpEndpointCalculationMode::DISABLED &&
259-
local_root.tags.find(tags::http_endpoint) == local_root.tags.end()) {
260-
auto http_url_tag = local_root.tags.find(tags::http_url);
261-
const bool should_calculate_endpoint =
262-
http_url_tag != local_root.tags.end() &&
263-
(resource_renaming_mode_ ==
264-
HttpEndpointCalculationMode::ALWAYS_CALCULATE ||
265-
local_root.tags.find(tags::http_route) == local_root.tags.end());
266-
267-
if (should_calculate_endpoint) {
268-
Expected<HTTPClient::URL> url_result =
269-
HTTPClient::URL::parse(http_url_tag->second);
270-
if (url_result.has_value()) {
271-
const std::string& path = url_result->path;
272-
local_root.tags[tags::http_endpoint] =
273-
infer_endpoint(path.empty() ? "/" : path);
274-
}
275-
}
276-
}
280+
maybe_calculate_http_endpoint(resource_renaming_mode_, local_root);
277281

278282
if (config_manager_->report_traces()) {
279283
telemetry::distribution::add(metrics::tracer::trace_chunk_size,

test/test_endpoint_inferral.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ TEST_ENDPOINT("hex and hex_id replacement") {
3232
CHECK(infer_endpoint("/x/ab_cd-9") == "/x/{param:hex_id}");
3333
}
3434

35-
TEST_ENDPOINT("str replacement by special or length") {
36-
CHECK(infer_endpoint("/x/a%z") == "/x/{param:str}");
35+
TEST_ENDPOINT("long sequences of more than 20 chars yield str") {
3736
std::string longseg(20, 'a');
3837
std::string path = std::string("/x/") + longseg;
3938
CHECK(infer_endpoint(path) == "/x/{param:str}");
4039
}
4140

4241
TEST_ENDPOINT("other specials yield str") {
43-
const char specials[] = {'&', '\'', '(', ')', '*', '+', ',', ':', '=', '@'};
42+
const char specials[] = {'%', '&', '\'', '(', ')', '*',
43+
'+', ',', ':', '=', '@'};
4444
for (char c : specials) {
4545
std::string s = "/x/a";
4646
s.push_back(c);

0 commit comments

Comments
 (0)