diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 00000000..c3969d89 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1 @@ +Follow [AGENTS.md](AGENTS.md). diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e468810b..828ab056 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -6,7 +6,7 @@ Before submitting new features or changes to current functionality, please [open issue](https://github.com/DataDog/dd-trace-cpp/issues/new) and discuss your ideas or propose the changes you wish to make. After a resolution is reached, a PR can be submitted for review. -Sefer to the [documentation](docs) to learn about the architecture of the Datadog C++ Tracer Library +Refer to the [documentation](docs) to learn about the architecture of the Datadog C++ Tracer Library and its development processes. In particular, review: - [Conventions](docs/conventions.md); diff --git a/include/datadog/datadog_agent_config.h b/include/datadog/datadog_agent_config.h index e3deb5c4..a6d1483c 100644 --- a/include/datadog/datadog_agent_config.h +++ b/include/datadog/datadog_agent_config.h @@ -11,11 +11,7 @@ // Typical usage of `DatadogAgentConfig` is implicit as part of `TracerConfig`. // See `tracer_config.h`. -#include #include -#include -#include -#include #include "clock.h" #include "config.h" @@ -23,8 +19,7 @@ #include "http_client.h" #include "remote_config/listener.h" -namespace datadog { -namespace tracing { +namespace datadog::tracing { class EventScheduler; class Logger; @@ -100,5 +95,4 @@ Expected finalize_config( const DatadogAgentConfig& config, const std::shared_ptr& logger, const Clock& clock); -} // namespace tracing -} // namespace datadog +} // namespace datadog::tracing diff --git a/src/datadog/datadog_agent_config.cpp b/src/datadog/datadog_agent_config.cpp index 20967db7..52a80eb9 100644 --- a/src/datadog/datadog_agent_config.cpp +++ b/src/datadog/datadog_agent_config.cpp @@ -1,17 +1,42 @@ #include #include -#include -#include -#include - #include "default_http_client.h" #include "parse_util.h" -#include "platform_util.h" #include "threaded_event_scheduler.h" -namespace datadog { -namespace tracing { +namespace datadog::tracing { + +namespace { + +Optional build_agent_url_from_environment_variables() { + if (Optional url_env = lookup(environment::DD_TRACE_AGENT_URL)) { + return std::string{*url_env}; + } + + Optional env_host = lookup(environment::DD_AGENT_HOST); + Optional env_port = lookup(environment::DD_TRACE_AGENT_PORT); + if (env_host || env_port) { + std::string agent_url = "http://"; + const StringView host = env_host.value_or("localhost"); + const bool is_ipv6_without_brackets = + (host.find(':') != StringView::npos) && (host.front() != '['); + if (is_ipv6_without_brackets) { + agent_url += '['; + } + append(agent_url, host); + if (is_ipv6_without_brackets) { + agent_url += ']'; + } + agent_url += ':'; + append(agent_url, env_port.value_or("8126")); + return agent_url; + } + + return nullopt; +} + +} // namespace Expected load_datadog_agent_env_config() { DatadogAgentConfig env_config; @@ -31,18 +56,9 @@ Expected load_datadog_agent_env_config() { env_config.remote_configuration_poll_interval_seconds = *res; } - auto env_host = lookup(environment::DD_AGENT_HOST); - auto env_port = lookup(environment::DD_TRACE_AGENT_PORT); - - if (auto url_env = lookup(environment::DD_TRACE_AGENT_URL)) { - env_config.url = std::string{*url_env}; - } else if (env_host || env_port) { - std::string configured_url = "http://"; - append(configured_url, env_host.value_or("localhost")); - configured_url += ':'; - append(configured_url, env_port.value_or("8126")); - - env_config.url = std::move(configured_url); + if (Optional agent_url = + build_agent_url_from_environment_variables()) { + env_config.url = *std::move(agent_url); } return env_config; @@ -158,5 +174,4 @@ Expected finalize_config( return result; } -} // namespace tracing -} // namespace datadog +} // namespace datadog::tracing diff --git a/test/test_otel_process_ctx.cpp b/test/test_otel_process_ctx.cpp index 65efcc8f..d38aa053 100644 --- a/test/test_otel_process_ctx.cpp +++ b/test/test_otel_process_ctx.cpp @@ -13,6 +13,7 @@ using namespace datadog::tracing; namespace { +#ifdef __linux__ std::map to_map(const char** key_value_array) { std::map out; if (key_value_array == nullptr) return out; @@ -23,7 +24,6 @@ std::map to_map(const char** key_value_array) { return out; } -#ifdef __linux__ std::unique_ptr make_tracer( const RuntimeID& runtime_id, const std::string& service = "otel-ctx-svc", std::shared_ptr logger = std::make_shared()) { diff --git a/test/test_tracer_config.cpp b/test/test_tracer_config.cpp index 737b4040..0bdf5f42 100644 --- a/test/test_tracer_config.cpp +++ b/test/test_tracer_config.cpp @@ -1,11 +1,7 @@ -#include -#include -#include #include #include -#include -#if defined(__linux__) +#ifdef __linux__ // Used to test writing to the `datadog-tracer-info` file; this is a Linux-only // feature. #include @@ -15,19 +11,8 @@ #include #endif -#include -#include -#include -#include -#include #include #include -#include -#include -#include -#include -#include -#include #include "common/environment.h" #include "mocks/collectors.h" @@ -36,7 +21,7 @@ #include "null_logger.h" #include "test.h" -#if defined(__linux__) +#ifdef __linux__ // Used to test writing to the `datadog-tracer-info` file; this is a Linux-only // feature. #include "common/ctx_sharing_helpers.h" @@ -44,15 +29,13 @@ #include "string_util.h" #endif -namespace datadog { -namespace tracing { +namespace datadog::tracing { std::ostream& operator<<(std::ostream& stream, PropagationStyle style) { return stream << to_string_view(style); } -} // namespace tracing -} // namespace datadog +} // namespace datadog::tracing using namespace datadog::test; using namespace datadog::tracing; @@ -480,6 +463,7 @@ TRACER_CONFIG_TEST("TracerConfig::agent") { {"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", "/"}, + {"http://[::1]:8126/", nullopt, "http", "[::1]:8126", "/"}, {"unix:///var/run/datadog/trace-agent.sock", nullopt, "unix", "/var/run/datadog/trace-agent.sock"}, {"unix://var/run/datadog/trace-agent.sock", @@ -526,6 +510,9 @@ TRACER_CONFIG_TEST("TracerConfig::agent") { "dd-agent:8080"}, {"override port with default host", nullopt, "8080", nullopt, "http", "localhost:8080"}, + {"IPv6 host", "::1", nullopt, nullopt, "http", "[::1]:8126"}, + {"IPv6 host with brackets", "[::1]", nullopt, nullopt, "http", + "[::1]:8126"}, // A bogus port number will cause an error in the TCPClient, not // during configuration. For the purposes of configuration, any // value is accepted.