diff --git a/WORKSPACE b/WORKSPACE index 6fe24089..76b434ca 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -4,10 +4,11 @@ # Abseil's absl::string_view and absl::optional instead. # # In the context of an Envoy build, the Abseil libraries point to whatever -# versions Envoy uses. +# versions Envoy uses, including some source patches. # # To test this library's Bazel build independent of Envoy, we need to specify -# versions of the Abseil libraries. That is what this file is for. +# versions of the Abseil libraries, including Envoy's patches. That is what this +# file is for. # These rules are based on , # accessed December 6, 2022. @@ -18,6 +19,8 @@ http_archive( urls = ["https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip"], sha256 = "aabf6c57e3834f8dc3873a927f37eaf69975d4b28117fc7427dfb1c661542a87", strip_prefix = "abseil-cpp-98eb410c93ad059f9bba1bf43f5bb916fc92a5ea", + patches = ["//:abseil.patch"], + patch_args = ["-p1"], ) http_archive( diff --git a/abseil.patch b/abseil.patch new file mode 100644 index 00000000..27deda37 --- /dev/null +++ b/abseil.patch @@ -0,0 +1,31 @@ +diff --git a/absl/base/options.h b/absl/base/options.h +index 6868c77b..e544fe6c 100644 +--- a/absl/base/options.h ++++ b/absl/base/options.h +@@ -135,7 +135,7 @@ + // absl::optional is a typedef of std::optional, use the feature macro + // ABSL_USES_STD_OPTIONAL. + +-#define ABSL_OPTION_USE_STD_OPTIONAL 2 ++#define ABSL_OPTION_USE_STD_OPTIONAL 0 + + + // ABSL_OPTION_USE_STD_STRING_VIEW +@@ -162,7 +162,7 @@ + // absl::string_view is a typedef of std::string_view, use the feature macro + // ABSL_USES_STD_STRING_VIEW. + +-#define ABSL_OPTION_USE_STD_STRING_VIEW 2 ++#define ABSL_OPTION_USE_STD_STRING_VIEW 0 + + // ABSL_OPTION_USE_STD_VARIANT + // +@@ -188,7 +188,7 @@ + // absl::variant is a typedef of std::variant, use the feature macro + // ABSL_USES_STD_VARIANT. + +-#define ABSL_OPTION_USE_STD_VARIANT 2 ++#define ABSL_OPTION_USE_STD_VARIANT 0 + + + // ABSL_OPTION_USE_INLINE_NAMESPACE diff --git a/src/datadog/collector_response.cpp b/src/datadog/collector_response.cpp index 0b61a249..2edb7b16 100644 --- a/src/datadog/collector_response.cpp +++ b/src/datadog/collector_response.cpp @@ -6,9 +6,9 @@ namespace tracing { std::string CollectorResponse::key(StringView service, StringView environment) { std::string result; result += "service:"; - result += service; + append(result, service); result += ",env:"; - result += environment; + append(result, environment); return result; } diff --git a/src/datadog/curl.cpp b/src/datadog/curl.cpp index c825490f..ce84a1e3 100644 --- a/src/datadog/curl.cpp +++ b/src/datadog/curl.cpp @@ -247,7 +247,7 @@ Expected CurlImpl::post(const HTTPClient::URL &url, } log_on_error(curl_multi_wakeup(multi_handle_)); - return std::nullopt; + return nullopt; } catch (CURLcode error) { return Error{Error::CURL_REQUEST_SETUP_FAILED, curl_easy_strerror(error)}; } @@ -445,7 +445,7 @@ Optional CurlImpl::HeaderReader::lookup(StringView key) const { const auto found = response_headers_lower_->find(buffer_); if (found == response_headers_lower_->end()) { - return std::nullopt; + return nullopt; } return found->second; } diff --git a/src/datadog/datadog_agent.cpp b/src/datadog/datadog_agent.cpp index 59e45186..540d23a2 100644 --- a/src/datadog/datadog_agent.cpp +++ b/src/datadog/datadog_agent.cpp @@ -25,7 +25,7 @@ const StringView traces_api_path = "/v0.4/traces"; HTTPClient::URL traces_endpoint(const HTTPClient::URL& agent_url) { auto traces_url = agent_url; - traces_url.path += traces_api_path; + append(traces_url.path, traces_api_path); return traces_url; } @@ -59,10 +59,10 @@ std::variant parse_agent_traces_response( "Parsing the Datadog Agent's response to traces we sent it failed. " "The response is expected to be a JSON object, but instead it's a JSON " "value with type \""; - message += type; + append(message, type); message += '\"'; message += "\nError occurred for response body (begins on next line):\n"; - message += body; + append(message, body); return message; } @@ -78,14 +78,14 @@ std::variant parse_agent_traces_response( message += "Parsing the Datadog Agent's response to traces we sent it failed. " "The \""; - message += sample_rates_property; + append(message, sample_rates_property); message += "\" property of the response is expected to be a JSON object, but " "instead it's a JSON value with type \""; - message += type; + append(message, type); message += '\"'; message += "\nError occurred for response body (begins on next line):\n"; - message += body; + append(message, body); return message; } @@ -99,10 +99,10 @@ std::variant parse_agent_traces_response( "for the key \""; message += key; message += "\". Rate should be a number, but it's a \""; - message += type; + append(message, type); message += "\" instead."; message += "\nError occurred for response body (begins on next line):\n"; - message += body; + append(message, body); return message; } auto maybe_rate = Rate::from(value); @@ -115,7 +115,7 @@ std::variant parse_agent_traces_response( message += "\": "; message += error->message; message += "\nError occurred for response body (begins on next line):\n"; - message += body; + append(message, body); return message; } sample_rates.emplace(key, *maybe_rate); @@ -128,7 +128,7 @@ std::variant parse_agent_traces_response( "JSON error: "; message += error.what(); message += "\nError occurred for response body (begins on next line):\n"; - message += body; + append(message, body); return message; } @@ -161,7 +161,7 @@ Expected DatadogAgent::send( std::lock_guard lock(mutex_); incoming_trace_chunks_.push_back( TraceChunk{std::move(spans), response_handler}); - return std::nullopt; + return nullopt; } nlohmann::json DatadogAgent::config_json() const { diff --git a/src/datadog/datadog_agent_config.cpp b/src/datadog/datadog_agent_config.cpp index 822031c0..616be3c3 100644 --- a/src/datadog/datadog_agent_config.cpp +++ b/src/datadog/datadog_agent_config.cpp @@ -18,7 +18,7 @@ Expected DatadogAgentConfig::parse(StringView input) { if (after_scheme == input.end()) { std::string message; message += "Datadog Agent URL is missing the \"://\" separator: \""; - message += input; + append(message, input); message += '\"'; return Error{Error::URL_MISSING_SEPARATOR, std::move(message)}; } @@ -31,13 +31,13 @@ Expected DatadogAgentConfig::parse(StringView input) { if (found == std::end(supported)) { std::string message; message += "Unsupported URI scheme \""; - message += scheme; + append(message, scheme); message += "\" in Datadog Agent URL \""; - message += input; + append(message, input); message += "\". The following are supported:"; for (const auto& supported_scheme : supported) { message += ' '; - message += supported_scheme; + append(message, supported_scheme); } return Error{Error::URL_UNSUPPORTED_SCHEME, std::move(message)}; } @@ -59,9 +59,9 @@ Expected DatadogAgentConfig::parse(StringView input) { "Unix domain socket paths for Datadog Agent must be absolute, i.e. " "must begin with a " "\"/\". The path \""; - message += authority_and_path; + append(message, authority_and_path); message += "\" is not absolute. Error occurred for URL: \""; - message += input; + append(message, input); message += '\"'; return Error{Error::URL_UNIX_DOMAIN_SOCKET_PATH_NOT_ABSOLUTE, std::move(message)}; @@ -119,13 +119,13 @@ Expected finalize_config( std::string configured_url = config.url; if (auto url_env = lookup(environment::DD_TRACE_AGENT_URL)) { - configured_url = *url_env; + assign(configured_url, *url_env); } else if (env_host || env_port) { configured_url.clear(); configured_url += "http://"; - configured_url += env_host.value_or("localhost"); + append(configured_url, env_host.value_or("localhost")); configured_url += ':'; - configured_url += env_port.value_or("8126"); + append(configured_url, env_port.value_or("8126")); } auto url = config.parse(configured_url); diff --git a/src/datadog/dict_reader.h b/src/datadog/dict_reader.h index 33452158..c6ee310f 100644 --- a/src/datadog/dict_reader.h +++ b/src/datadog/dict_reader.h @@ -16,7 +16,7 @@ class DictReader { public: virtual ~DictReader() {} - // Return the value at the specified `key`, or return `std::nullopt` if there + // Return the value at the specified `key`, or return `nullopt` if there // is no value at `key`. virtual Optional lookup(StringView key) const = 0; diff --git a/src/datadog/environment.cpp b/src/datadog/environment.cpp index 571fbc69..780bb42b 100644 --- a/src/datadog/environment.cpp +++ b/src/datadog/environment.cpp @@ -14,7 +14,7 @@ Optional lookup(Variable variable) { const char *name = variable_names[variable]; const char *value = std::getenv(name); if (!value) { - return std::nullopt; + return nullopt; } return StringView{value}; } diff --git a/src/datadog/expected.h b/src/datadog/expected.h index 4597cc2a..f442fc55 100644 --- a/src/datadog/expected.h +++ b/src/datadog/expected.h @@ -2,7 +2,7 @@ // This component provides a class template, `Expected`, that is either an // instance of `T` or an instance of `Error`. `Expected` is either -// `std::nullopt` or an instance of `Error`. +// `nullopt` or an instance of `Error`. // // `Expected` is inspired by, but incompatible with, C++23's `std::expected`. // diff --git a/src/datadog/msgpack.cpp b/src/datadog/msgpack.cpp index 05bef574..7f58b5ae 100644 --- a/src/datadog/msgpack.cpp +++ b/src/datadog/msgpack.cpp @@ -24,7 +24,7 @@ std::string make_overflow_message(StringView type, std::size_t actual, std::size_t max) { std::string message; message += "Cannot msgpack encode "; - message += type; + append(message, type); message += " of size "; message += std::to_string(actual); message += ", which exceeds the protocol maximum of "; diff --git a/src/datadog/net_util.cpp b/src/datadog/net_util.cpp index 9c6e6e58..b1ca0087 100644 --- a/src/datadog/net_util.cpp +++ b/src/datadog/net_util.cpp @@ -12,7 +12,7 @@ namespace tracing { Optional get_hostname() { char buffer[256]; if (::gethostname(buffer, sizeof buffer)) { - return std::nullopt; + return nullopt; } return buffer; } diff --git a/src/datadog/optional.h b/src/datadog/optional.h index d1fc76f9..8ce7be2b 100644 --- a/src/datadog/optional.h +++ b/src/datadog/optional.h @@ -39,9 +39,11 @@ namespace tracing { #ifdef DD_USE_ABSEIL_FOR_ENVOY template using Optional = absl::optional; +inline constexpr auto nullopt = absl::nullopt; #else template using Optional = std::optional; +inline constexpr auto nullopt = std::nullopt; #endif // defined DD_USE_ABSEIL_FOR_ENVOY } // namespace tracing diff --git a/src/datadog/parse_util.cpp b/src/datadog/parse_util.cpp index 23731963..59bfe14f 100644 --- a/src/datadog/parse_util.cpp +++ b/src/datadog/parse_util.cpp @@ -22,21 +22,21 @@ Expected parse_integer(StringView input, int base, StringView kind) { if (status.ec == std::errc::invalid_argument) { std::string message; message += "Is not a valid integer: \""; - message += input; + append(message, input); message += '\"'; return Error{Error::INVALID_INTEGER, std::move(message)}; } else if (status.ptr != input.end()) { std::string message; message += "Integer has trailing characters in: \""; - message += input; + append(message, input); message += '\"'; return Error{Error::INVALID_INTEGER, std::move(message)}; } else if (status.ec == std::errc::result_out_of_range) { std::string message; message += "Integer is not within the range of "; - message += kind; + append(message, kind); message += ": "; - message += input; + append(message, input); return Error{Error::OUT_OF_RANGE_INTEGER, std::move(message)}; } return value; @@ -85,13 +85,13 @@ Expected parse_double(StringView input) { message += "Is not a valid number, or is out of the range of double precision " "floating point: \""; - message += input; + append(message, input); message += '\"'; return Error{Error::INVALID_DOUBLE, std::move(message)}; } else if (!stream.eof()) { std::string message; message += "Number has trailing characters in: \""; - message += input; + append(message, input); message += '\"'; return Error{Error::INVALID_DOUBLE, std::move(message)}; } diff --git a/src/datadog/span.cpp b/src/datadog/span.cpp index ce3f4984..0156b86c 100644 --- a/src/datadog/span.cpp +++ b/src/datadog/span.cpp @@ -66,7 +66,7 @@ std::uint64_t Span::trace_id() const { return data_->trace_id; } Optional Span::parent_id() const { if (data_->parent_id == 0) { - return std::nullopt; + return nullopt; } return data_->parent_id; } @@ -77,12 +77,12 @@ bool Span::error() const { return data_->error; } Optional Span::lookup_tag(StringView name) const { if (tags::is_internal(name)) { - return std::nullopt; + return nullopt; } const auto found = data_->tags.find(std::string(name)); if (found == data_->tags.end()) { - return std::nullopt; + return nullopt; } return found->second; } @@ -99,12 +99,16 @@ void Span::remove_tag(StringView name) { } } -void Span::set_service_name(StringView service) { data_->service = service; } +void Span::set_service_name(StringView service) { + assign(data_->service, service); +} -void Span::set_service_type(StringView type) { data_->service_type = type; } +void Span::set_service_type(StringView type) { + assign(data_->service_type, type); +} void Span::set_resource_name(StringView resource) { - data_->resource = resource; + assign(data_->resource, resource); } void Span::set_error(bool is_error) { @@ -130,7 +134,7 @@ void Span::set_error_stack(StringView type) { data_->tags.insert_or_assign("error.stack", std::string(type)); } -void Span::set_name(StringView value) { data_->name = value; } +void Span::set_name(StringView value) { assign(data_->name, value); } void Span::set_end_time(std::chrono::steady_clock::time_point end_time) { end_time_ = end_time; diff --git a/src/datadog/span_data.cpp b/src/datadog/span_data.cpp index 538886af..52456cfa 100644 --- a/src/datadog/span_data.cpp +++ b/src/datadog/span_data.cpp @@ -20,7 +20,7 @@ Optional lookup( if (found != map.end()) { return found->second; } - return std::nullopt; + return nullopt; } } // namespace @@ -122,7 +122,7 @@ Expected msgpack_encode(std::string& destination, const SpanData& span) { }); // clang-format on - return std::nullopt; + return nullopt; } } // namespace tracing diff --git a/src/datadog/span_matcher.cpp b/src/datadog/span_matcher.cpp index 7778ab5c..e9020361 100644 --- a/src/datadog/span_matcher.cpp +++ b/src/datadog/span_matcher.cpp @@ -56,14 +56,14 @@ Expected SpanMatcher::from_json(const nlohmann::json& json) { StringView expected_type) -> Optional { type = value.type_name(); if (type == expected_type) { - return std::nullopt; + return nullopt; } std::string message; message += "Rule property \""; - message += property; + append(message, property); message += "\" should have type \""; - message += expected_type; + append(message, expected_type); message += "\", but has type \""; message += type; message += "\": "; diff --git a/src/datadog/span_sampler_config.cpp b/src/datadog/span_sampler_config.cpp index e6da8200..2c5e573e 100644 --- a/src/datadog/span_sampler_config.cpp +++ b/src/datadog/span_sampler_config.cpp @@ -26,9 +26,9 @@ Expected> parse_rules(StringView rules_raw, } catch (const nlohmann::json::parse_error &error) { std::string message; message += "Unable to parse JSON from "; - message += env_var; + append(message, env_var); message += " value "; - message += rules_raw; + append(message, rules_raw); message += ": "; message += error.what(); return Error{Error::SPAN_SAMPLING_RULES_INVALID_JSON, std::move(message)}; @@ -38,15 +38,15 @@ Expected> parse_rules(StringView rules_raw, if (type != "array") { std::string message; message += "Trace sampling rules must be an array, but JSON in "; - message += env_var; + append(message, env_var); message += " has type \""; message += type; message += "\": "; - message += rules_raw; + append(message, rules_raw); return Error{Error::SPAN_SAMPLING_RULES_WRONG_TYPE, std::move(message)}; } - const std::unordered_set allowed_properties{ + const std::unordered_set allowed_properties{ "service", "name", "resource", "tags", "sample_rate", "max_per_second"}; for (const auto &json_rule : json_rules) { @@ -54,9 +54,9 @@ Expected> parse_rules(StringView rules_raw, if (auto *error = matcher.if_error()) { std::string prefix; prefix += "Unable to create a rule from "; - prefix += env_var; + append(prefix, env_var); prefix += " JSON "; - prefix += rules_raw; + append(prefix, rules_raw); prefix += ": "; return error->with_prefix(prefix); } @@ -69,9 +69,9 @@ Expected> parse_rules(StringView rules_raw, if (type != "number") { std::string message; message += "Unable to parse a rule from "; - message += env_var; + append(message, env_var); message += " JSON "; - message += rules_raw; + append(message, rules_raw); message += ". The \"sample_rate\" property of the rule "; message += json_rule.dump(); message += " is not a number, but instead has type \""; @@ -89,9 +89,9 @@ Expected> parse_rules(StringView rules_raw, if (type != "number") { std::string message; message += "Unable to parse a rule from "; - message += env_var; + append(message, env_var); message += " JSON "; - message += rules_raw; + append(message, rules_raw); message += ". The \"max_per_second\" property of the rule "; message += json_rule.dump(); message += " is not a number, but instead has type \""; @@ -116,9 +116,9 @@ Expected> parse_rules(StringView rules_raw, message += " in trace sampling rule "; message += json_rule.dump(); message += ". Error occurred while parsing from "; - message += env_var; + append(message, env_var); message += ": "; - message += rules_raw; + append(message, rules_raw); return Error{Error::SPAN_SAMPLING_RULES_UNKNOWN_PROPERTY, std::move(message)}; } @@ -155,13 +155,13 @@ Expected finalize_config( name(environment::DD_SPAN_SAMPLING_RULES_FILE); const auto rules_name = name(environment::DD_SPAN_SAMPLING_RULES); std::string message; - message += rules_file_name; + append(message, rules_file_name); message += " is overridden by "; - message += rules_name; + append(message, rules_name); message += ". Since both are set, "; - message += rules_name; + append(message, rules_name); message += " takes precedence, and "; - message += rules_file_name; + append(message, rules_file_name); message += " will be ignored."; logger.log_error(message); } else { @@ -174,7 +174,7 @@ Expected finalize_config( message += " file \""; message += span_rules_file; message += "\" specified as value of environment variable "; - message += name(environment::DD_SPAN_SAMPLING_RULES_FILE); + append(message, name(environment::DD_SPAN_SAMPLING_RULES_FILE)); return Error{Error::SPAN_SAMPLING_RULES_FILE_IO, std::move(message)}; }; @@ -195,9 +195,9 @@ Expected finalize_config( if (auto *error = maybe_rules.if_error()) { std::string prefix; prefix += "With "; - prefix += name(environment::DD_SPAN_SAMPLING_RULES_FILE); + append(prefix, name(environment::DD_SPAN_SAMPLING_RULES_FILE)); prefix += '='; - prefix += *file_env; + append(prefix, *file_env); prefix += ": "; return error->with_prefix(prefix); } diff --git a/src/datadog/string_view.h b/src/datadog/string_view.h index 306fd233..cf459a0e 100644 --- a/src/datadog/string_view.h +++ b/src/datadog/string_view.h @@ -25,6 +25,8 @@ // This file defines `datadog::tracing::StringView`, a type that is an alias // for either `std::string_view` or `absl::string_view`. +#include + #ifdef DD_USE_ABSEIL_FOR_ENVOY // Abseil examples, including usage in Envoy, include Abseil headers in quoted // style instead of angle bracket style, per Bazel's default build behavior. @@ -42,5 +44,19 @@ using StringView = absl::string_view; using StringView = std::string_view; #endif // defined DD_USE_ABSEIL_FOR_ENVOY +// When `StringView` is not the same as `std::string_view`, +// `operator+=(string&, StringView)` isn't defined. To work around this, use +// `append` everywhere. +inline void append(std::string& destination, StringView text) { + destination.append(text.data(), text.size()); +} + +// When `StringView` is not the same as `std::string_view`, +// `operator=(string&, StringView)` isn't defined. To work around this, use +// `assign` everywhere. +inline void assign(std::string& destination, StringView text) { + destination.assign(text.data(), text.size()); +} + } // namespace tracing } // namespace datadog diff --git a/src/datadog/tag_propagation.cpp b/src/datadog/tag_propagation.cpp index 03dbb70b..6c17be39 100644 --- a/src/datadog/tag_propagation.cpp +++ b/src/datadog/tag_propagation.cpp @@ -38,7 +38,7 @@ Expected decode_tag( if (separator == entry.end()) { std::string message; message += "invalid key=value pair for encoded tag: missing \"=\" in: "; - message += entry; + append(message, entry); return Error{Error::MALFORMED_TRACE_TAGS, std::move(message)}; } @@ -47,7 +47,7 @@ Expected decode_tag( // Among duplicate keys, most recent value wins. destination.insert_or_assign(std::string(key), std::string(value)); - return std::nullopt; + return nullopt; } void append_tag(std::string& serialized_tags, StringView tag_key, @@ -77,7 +77,7 @@ Expected> decode_tags( if (auto* error = result.if_error()) { std::string prefix; prefix += "Error decoding trace tags \""; - prefix += header_value; + append(prefix, header_value); prefix += "\": "; return error->with_prefix(prefix); } diff --git a/src/datadog/trace_sampler_config.cpp b/src/datadog/trace_sampler_config.cpp index d0676603..6cea55ec 100644 --- a/src/datadog/trace_sampler_config.cpp +++ b/src/datadog/trace_sampler_config.cpp @@ -26,9 +26,9 @@ Expected finalize_config( } catch (const nlohmann::json::parse_error &error) { std::string message; message += "Unable to parse JSON from "; - message += name(environment::DD_TRACE_SAMPLING_RULES); + append(message, name(environment::DD_TRACE_SAMPLING_RULES)); message += " value "; - message += *rules_env; + append(message, *rules_env); message += ": "; message += error.what(); return Error{Error::TRACE_SAMPLING_RULES_INVALID_JSON, @@ -39,15 +39,15 @@ Expected finalize_config( if (type != "array") { std::string message; message += "Trace sampling rules must be an array, but "; - message += name(environment::DD_TRACE_SAMPLING_RULES); + append(message, name(environment::DD_TRACE_SAMPLING_RULES)); message += " has JSON type \""; message += type; message += "\": "; - message += *rules_env; + append(message, *rules_env); return Error{Error::TRACE_SAMPLING_RULES_WRONG_TYPE, std::move(message)}; } - const std::unordered_set allowed_properties{ + const std::unordered_set allowed_properties{ "service", "name", "resource", "tags", "sample_rate"}; for (const auto &json_rule : json_rules) { @@ -55,9 +55,9 @@ Expected finalize_config( if (auto *error = matcher.if_error()) { std::string prefix; prefix += "Unable to create a rule from "; - prefix += name(environment::DD_TRACE_SAMPLING_RULES); + append(prefix, name(environment::DD_TRACE_SAMPLING_RULES)); prefix += " value "; - prefix += *rules_env; + append(prefix, *rules_env); prefix += ": "; return error->with_prefix(prefix); } @@ -70,9 +70,9 @@ Expected finalize_config( if (type != "number") { std::string message; message += "Unable to parse a rule from "; - message += name(environment::DD_TRACE_SAMPLING_RULES); + append(message, name(environment::DD_TRACE_SAMPLING_RULES)); message += " value "; - message += *rules_env; + append(message, *rules_env); message += ". The \"sample_rate\" property of the rule "; message += json_rule.dump(); message += " is not a number, but instead has type \""; @@ -97,9 +97,9 @@ Expected finalize_config( message += " in trace sampling rule "; message += json_rule.dump(); message += ". Error occurred while parsing "; - message += name(environment::DD_TRACE_SAMPLING_RULES); + append(message, name(environment::DD_TRACE_SAMPLING_RULES)); message += ": "; - message += *rules_env; + append(message, *rules_env); return Error{Error::TRACE_SAMPLING_RULES_UNKNOWN_PROPERTY, std::move(message)}; } @@ -132,7 +132,7 @@ Expected finalize_config( if (auto *error = maybe_sample_rate.if_error()) { std::string prefix; prefix += "While parsing "; - prefix += name(environment::DD_TRACE_SAMPLE_RATE); + append(prefix, name(environment::DD_TRACE_SAMPLE_RATE)); prefix += ": "; return error->with_prefix(prefix); } @@ -160,7 +160,7 @@ Expected finalize_config( if (auto *error = maybe_max_per_second.if_error()) { std::string prefix; prefix += "While parsing "; - prefix += name(environment::DD_TRACE_RATE_LIMIT); + append(prefix, name(environment::DD_TRACE_RATE_LIMIT)); prefix += ": "; return error->with_prefix(prefix); } diff --git a/src/datadog/tracer.cpp b/src/datadog/tracer.cpp index b5434e0e..79f43672 100644 --- a/src/datadog/tracer.cpp +++ b/src/datadog/tracer.cpp @@ -41,17 +41,17 @@ class DatadogExtractionPolicy : public ExtractionPolicy { StringView header, StringView kind) { auto found = headers.lookup(header); if (!found) { - return std::nullopt; + return nullopt; } auto result = parse_uint64(*found, 10); if (auto* error = result.if_error()) { std::string prefix; prefix += "Could not extract Datadog-style "; - prefix += kind; + append(prefix, kind); prefix += "ID from "; - prefix += header; + append(prefix, header); prefix += ": "; - prefix += *found; + append(prefix, *found); prefix += ' '; return error->with_prefix(prefix); } @@ -74,15 +74,15 @@ class DatadogExtractionPolicy : public ExtractionPolicy { const StringView header = "x-datadog-sampling-priority"; auto found = headers.lookup(header); if (!found) { - return std::nullopt; + return nullopt; } auto result = parse_int(*found, 10); if (auto* error = result.if_error()) { std::string prefix; prefix += "Could not extract Datadog-style sampling priority from "; - prefix += header; + append(prefix, header); prefix += ": "; - prefix += *found; + append(prefix, *found); prefix += ' '; return error->with_prefix(prefix); } @@ -94,7 +94,7 @@ class DatadogExtractionPolicy : public ExtractionPolicy { if (found) { return std::string(*found); } - return std::nullopt; + return nullopt; } Optional trace_tags(const DictReader& headers) override { @@ -102,7 +102,7 @@ class DatadogExtractionPolicy : public ExtractionPolicy { if (found) { return std::string(*found); } - return std::nullopt; + return nullopt; } }; @@ -111,17 +111,17 @@ class B3ExtractionPolicy : public DatadogExtractionPolicy { StringView header, StringView kind) { auto found = headers.lookup(header); if (!found) { - return std::nullopt; + return nullopt; } auto result = parse_uint64(*found, 16); if (auto* error = result.if_error()) { std::string prefix; prefix += "Could not extract B3-style "; - prefix += kind; + append(prefix, kind); prefix += "ID from "; - prefix += header; + append(prefix, header); prefix += ": "; - prefix += *found; + append(prefix, *found); prefix += ' '; return error->with_prefix(prefix); } @@ -144,15 +144,15 @@ class B3ExtractionPolicy : public DatadogExtractionPolicy { const StringView header = "x-b3-sampled"; auto found = headers.lookup(header); if (!found) { - return std::nullopt; + return nullopt; } auto result = parse_int(*found, 10); if (auto* error = result.if_error()) { std::string prefix; prefix += "Could not extract B3-style sampling priority from "; - prefix += header; + append(prefix, header); prefix += ": "; - prefix += *found; + append(prefix, *found); prefix += ' '; return error->with_prefix(prefix); } @@ -255,7 +255,7 @@ Tracer::Tracer(const FinalizedTracerConfig& config, defaults_(std::make_shared(config.defaults)), injection_styles_(config.injection_styles), extraction_styles_(config.extraction_styles), - hostname_(config.report_hostname ? get_hostname() : std::nullopt), + hostname_(config.report_hostname ? get_hostname() : nullopt), tags_header_max_size_(config.tags_header_size) { if (auto* collector = std::get_if>(&config.collector)) { @@ -287,10 +287,9 @@ Span Tracer::create_span(const SpanConfig& config) { const auto span_data_ptr = span_data.get(); const auto segment = std::make_shared( logger_, collector_, trace_sampler_, span_sampler_, defaults_, - injection_styles_, hostname_, std::nullopt /* origin */, - tags_header_max_size_, + injection_styles_, hostname_, nullopt /* origin */, tags_header_max_size_, std::unordered_map{} /* trace_tags */, - std::nullopt /* sampling_decision */, std::move(span_data)); + nullopt /* sampling_decision */, std::move(span_data)); Span span{span_data_ptr, segment, generator_, clock_}; return span; } diff --git a/src/datadog/tracer_config.cpp b/src/datadog/tracer_config.cpp index d3441572..32656166 100644 --- a/src/datadog/tracer_config.cpp +++ b/src/datadog/tracer_config.cpp @@ -83,7 +83,7 @@ Expected parse_propagation_styles(StringView input) { message += "Unsupported propagation style \""; message += token; message += "\" in list \""; - message += input; + append(message, input); message += "\". The following styles are supported: Datadog, B3."; return Error{Error::UNKNOWN_PROPAGATION_STYLE, std::move(message)}; } @@ -102,11 +102,11 @@ Expected> parse_tags( if (separator == token.end()) { std::string message; message += "Unable to parse a key/value from the tag text \""; - message += token; + append(message, token); message += "\" because it does not contain the separator character \":\". " "Error occurred in list of tags \""; - message += input; + append(message, input); message += "\"."; return Error{Error::TAG_MISSING_SEPARATOR, std::move(message)}; } @@ -127,17 +127,17 @@ Expected finalize_config(const TracerConfig &config) { result.defaults = config.defaults; if (auto service_env = lookup(environment::DD_SERVICE)) { - result.defaults.service = *service_env; + assign(result.defaults.service, *service_env); } if (result.defaults.service.empty()) { return Error{Error::SERVICE_NAME_REQUIRED, "Service name is required."}; } if (auto environment_env = lookup(environment::DD_ENV)) { - result.defaults.environment = *environment_env; + assign(result.defaults.environment, *environment_env); } if (auto version_env = lookup(environment::DD_VERSION)) { - result.defaults.version = *version_env; + assign(result.defaults.version, *version_env); } if (auto tags_env = lookup(environment::DD_TAGS)) { @@ -145,7 +145,7 @@ Expected finalize_config(const TracerConfig &config) { if (auto *error = tags.if_error()) { std::string prefix; prefix += "Unable to parse "; - prefix += name(environment::DD_TAGS); + append(prefix, name(environment::DD_TAGS)); prefix += " environment variable: "; return error->with_prefix(prefix); } @@ -199,7 +199,7 @@ Expected finalize_config(const TracerConfig &config) { if (auto *error = styles.if_error()) { std::string prefix; prefix += "Unable to parse "; - prefix += name(environment::DD_PROPAGATION_STYLE_EXTRACT); + append(prefix, name(environment::DD_PROPAGATION_STYLE_EXTRACT)); prefix += " environment variable: "; return error->with_prefix(prefix); } @@ -212,7 +212,7 @@ Expected finalize_config(const TracerConfig &config) { if (auto *error = styles.if_error()) { std::string prefix; prefix += "Unable to parse "; - prefix += name(environment::DD_PROPAGATION_STYLE_INJECT); + append(prefix, name(environment::DD_PROPAGATION_STYLE_INJECT)); prefix += " environment variable: "; return error->with_prefix(prefix); } diff --git a/test/mocks/dict_readers.h b/test/mocks/dict_readers.h index 707e9ed7..fb0147f3 100644 --- a/test/mocks/dict_readers.h +++ b/test/mocks/dict_readers.h @@ -18,7 +18,7 @@ class MockDictReader : public DictReader { Optional lookup(StringView key) const override { auto found = map_->find(std::string(key)); if (found == map_->end()) { - return std::nullopt; + return nullopt; } return found->second; } diff --git a/test/span.cpp b/test/span.cpp index c35cc155..df491a52 100644 --- a/test/span.cpp +++ b/test/span.cpp @@ -234,19 +234,18 @@ TEST_CASE(".error() and .set_error*()") { }; auto test_case = GENERATE(values( - {{"No error → no error.", [](Span&) {}, false, std::nullopt, std::nullopt, - std::nullopt}, + {{"No error → no error.", [](Span&) {}, false, nullopt, nullopt, nullopt}, {"set_error(true) → error", [](Span& span) { span.set_error(true); }, - true, std::nullopt, std::nullopt, std::nullopt}, + true, nullopt, nullopt, nullopt}, {"set_error_message → error and error message", [](Span& span) { span.set_error_message("oops!"); }, true, "oops!", - std::nullopt, std::nullopt}, + nullopt, nullopt}, {"set_error_type → error and error type", - [](Span& span) { span.set_error_type("errno"); }, true, std::nullopt, - "errno", std::nullopt}, + [](Span& span) { span.set_error_type("errno"); }, true, nullopt, + "errno", nullopt}, {"set_error_stack → error and error stack", [](Span& span) { span.set_error_stack("this is C++, fool"); }, true, - std::nullopt, std::nullopt, "this is C++, fool"}, + nullopt, nullopt, "this is C++, fool"}, {"set all of them → error, error message, error type, and error stack", [](Span& span) { span.set_error_message("oops!"); @@ -261,7 +260,7 @@ TEST_CASE(".error() and .set_error*()") { span.set_error_stack("this too"); span.set_error(false); }, - false, std::nullopt, std::nullopt, std::nullopt}})); + false, nullopt, nullopt, nullopt}})); TracerConfig config; config.defaults.service = "testsvc"; diff --git a/test/span_sampler.cpp b/test/span_sampler.cpp index 8f60fd02..1395ab16 100644 --- a/test/span_sampler.cpp +++ b/test/span_sampler.cpp @@ -103,7 +103,7 @@ SpanSamplerConfig::Rule by_name_and_tags( return rule; } -const auto x = std::nullopt; +const auto x = nullopt; } // namespace diff --git a/test/tracer.cpp b/test/tracer.cpp index 71226c35..97e37bc5 100644 --- a/test/tracer.cpp +++ b/test/tracer.cpp @@ -282,7 +282,7 @@ TEST_CASE("span extraction") { true, false, {{"x-datadog-trace-id", "123"}, {"x-datadog-origin", "anything"}}, - std::nullopt}, + nullopt}, {"datadog and B3 agree", true, true, @@ -290,7 +290,7 @@ TEST_CASE("span extraction") { {"x-b3-traceid", "f"}, {"x-datadog-parent-id", "14"}, {"x-b3-spanid", "e"}}, - std::nullopt}, + nullopt}, {"datadog and B3 disagree on trace ID", true, true, @@ -473,14 +473,14 @@ TEST_CASE("span extraction") { {{"x-datadog-trace-id", "123"}, {"x-datadog-parent-id", "456"}}, 123, 456, - std::nullopt}, + nullopt}, {"datadog style without sampling priority and without parent ID", true, false, {{"x-datadog-trace-id", "123"}, {"x-datadog-origin", "whatever"}}, 123, - std::nullopt, - std::nullopt}, + nullopt, + nullopt}, {"B3 style", false, true, @@ -496,7 +496,7 @@ TEST_CASE("span extraction") { {{"x-b3-traceid", "abc"}, {"x-b3-spanid", "def"}}, 0xabc, 0xdef, - std::nullopt}, + nullopt}, {"Datadog and B3 style together", true, true, @@ -518,7 +518,7 @@ TEST_CASE("span extraction") { {"x-b3-spanid", "e"}}, 255, 14, - std::nullopt}, + nullopt}, })); CAPTURE(test_case.name); diff --git a/test/tracer_config.cpp b/test/tracer_config.cpp index 37c56c84..bbf9a44f 100644 --- a/test/tracer_config.cpp +++ b/test/tracer_config.cpp @@ -94,7 +94,7 @@ class EnvGuard { // For brevity when we're tabulating a lot of test cases with parse // `Optional<...>` data members. -const auto x = std::nullopt; +const auto x = nullopt; // Here's an attempt at a portable secure temporary file. // There's no standard solution, and it's generally hard on Windows. @@ -198,25 +198,22 @@ TEST_CASE("TracerConfig::defaults") { }; auto test_case = GENERATE(values({ - {"empty", "", {}, std::nullopt}, + {"empty", "", {}, nullopt}, {"missing colon", "foo", {}, Error::TAG_MISSING_SEPARATOR}, {"trailing comma", "foo:bar, baz:123,", {}, Error::TAG_MISSING_SEPARATOR}, - {"overwrite value", "foo:baz", {{"foo", "baz"}}, std::nullopt}, + {"overwrite value", "foo:baz", {{"foo", "baz"}}, nullopt}, {"additional values", "baz:123, bam:three", {{"baz", "123"}, {"bam", "three"}}, - std::nullopt}, + nullopt}, {"commas optional", "baz:123 bam:three", {{"baz", "123"}, {"bam", "three"}}, - std::nullopt}, - {"last one wins", - "baz:123 baz:three", - {{"baz", "three"}}, - std::nullopt}, + nullopt}, + {"last one wins", "baz:123 baz:three", {{"baz", "three"}}, nullopt}, })); // This will be overriden by the DD_TAGS environment variable. @@ -423,18 +420,17 @@ TEST_CASE("TracerConfig::agent") { }; auto test_case = GENERATE(values({ - {"http://dd-agent:8126", std::nullopt, "http", "dd-agent:8126", ""}, - {"http://dd-agent:8126/", std::nullopt, "http", "dd-agent:8126", "/"}, - {"https://dd-agent:8126/", std::nullopt, "https", "dd-agent:8126", - "/"}, - {"unix:///var/run/datadog/trace-agent.sock", std::nullopt, "unix", + {"http://dd-agent:8126", nullopt, "http", "dd-agent:8126", ""}, + {"http://dd-agent:8126/", nullopt, "http", "dd-agent:8126", "/"}, + {"https://dd-agent:8126/", nullopt, "https", "dd-agent:8126", "/"}, + {"unix:///var/run/datadog/trace-agent.sock", nullopt, "unix", "/var/run/datadog/trace-agent.sock"}, {"unix://var/run/datadog/trace-agent.sock", Error::URL_UNIX_DOMAIN_SOCKET_PATH_NOT_ABSOLUTE}, - {"http+unix:///run/datadog/trace-agent.sock", std::nullopt, - "http+unix", "/run/datadog/trace-agent.sock"}, - {"https+unix:///run/datadog/trace-agent.sock", std::nullopt, - "https+unix", "/run/datadog/trace-agent.sock"}, + {"http+unix:///run/datadog/trace-agent.sock", nullopt, "http+unix", + "/run/datadog/trace-agent.sock"}, + {"https+unix:///run/datadog/trace-agent.sock", nullopt, "https+unix", + "/run/datadog/trace-agent.sock"}, {"tcp://localhost:8126", Error::URL_UNSUPPORTED_SCHEME}, {"/var/run/datadog/trace-agent.sock", Error::URL_MISSING_SEPARATOR}, }));