From 4a094f3b40b9fda299366ce159eed00054d97949 Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Fri, 13 Nov 2026 11:14:25 +0100 Subject: [PATCH 1/9] Test Tracer - Telementry destruction order issue --- test/CMakeLists.txt | 9 ++++++++- test/exit_with_live_tracer.cpp | 34 +++++++++++++++++++++------------- test/test_otel_process_ctx.cpp | 14 -------------- test/test_tracer.cpp | 14 ++++++++++++++ 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 86a47e77..cef8f301 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -76,8 +76,15 @@ if(DD_TRACE_TRANSPORT STREQUAL "curl") target_link_libraries(tests PRIVATE CURL::libcurl_static) endif() -# Dedicated executable run by a test in test_otel_process_ctx.cpp +# Dedicated executable run by a test in test_tracer.cpp add_executable(exit_with_live_tracer exit_with_live_tracer.cpp) +target_include_directories(exit_with_live_tracer + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_SOURCE_DIR}/include/datadog +) target_link_libraries(exit_with_live_tracer PRIVATE dd-trace-cpp::static diff --git a/test/exit_with_live_tracer.cpp b/test/exit_with_live_tracer.cpp index 312de979..1cb63f88 100644 --- a/test/exit_with_live_tracer.cpp +++ b/test/exit_with_live_tracer.cpp @@ -1,37 +1,45 @@ // Executable that calls exit() while a Tracer, held by a static wrapper, is // still alive. -#include #include +#include "mocks/event_schedulers.h" +#include "mocks/http_clients.h" + namespace dd = datadog::tracing; namespace { -class NullCollector : public dd::Collector { - public: - dd::Expected send(std::vector>&&, - const std::shared_ptr&) override { - return dd::nullopt; - } - - std::string config() const override { return R"({"type": "NullCollector"})"; } -}; - // Created empty before main(), so before the library statics. std::optional tracer; } // namespace int main() { + const std::shared_ptr http_client = + std::make_shared(); + // Valid agent reply to avoid errors in log + http_client->response_status = 200; + http_client->response_body << "{}"; + + const std::shared_ptr event_scheduler = + std::make_shared(); + dd::TracerConfig config; - config.collector = std::make_shared(); - config.telemetry.enabled = false; + config.agent.http_client = http_client; + config.agent.event_scheduler = event_scheduler; + config.event_scheduler = event_scheduler; const dd::Expected finalized_config = dd::finalize_config(config); if (!finalized_config) { return EXIT_FAILURE; } tracer.emplace(*finalized_config); + + // Because the mock scheduler does nothing, the trace chunk is only flushed + // when the Tracer is destroyed. This trace chunk destruction uses both the + // OTel process context state and the telemetry singleton. + tracer->create_span(); + std::exit(EXIT_SUCCESS); } diff --git a/test/test_otel_process_ctx.cpp b/test/test_otel_process_ctx.cpp index aa6f6404..d38aa053 100644 --- a/test/test_otel_process_ctx.cpp +++ b/test/test_otel_process_ctx.cpp @@ -242,17 +242,3 @@ OTEL_CTX_TEST( } #endif } - -OTEL_CTX_TEST("exit() with a live Tracer in a static wrapper exits cleanly") { -#ifndef __linux__ - SUCCEED("OpenTelemetry process context is Linux-only"); -#else - // Run a dedicated process and check that it exits cleanly. It must be - // independent of other tests, to control the order of statics creation. - const std::string command = - std::string("'") + EXIT_WITH_LIVE_TRACER_EXECUTABLE + "'"; - const int status = std::system(command.c_str()); - REQUIRE(WIFEXITED(status)); - REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS); -#endif -} diff --git a/test/test_tracer.cpp b/test/test_tracer.cpp index 066a9781..286f3887 100644 --- a/test/test_tracer.cpp +++ b/test/test_tracer.cpp @@ -2586,3 +2586,17 @@ TEST_TRACER("_dd.p.ksr is NOT set when overriding the sampling decision") { auto c = collector->first_span(); CHECK(c.tags.count(tags::internal::ksr) == 0); } + +TEST_TRACER("exit() with a live Tracer in a static wrapper exits cleanly") { +#ifndef __linux__ + SUCCEED("Runs on Linux only: it needs a POSIX shell"); +#else + // Run a dedicated process and check that it exits cleanly. It must be + // independent of other tests, to control the order of statics creation. + const std::string command = + std::string("'") + EXIT_WITH_LIVE_TRACER_EXECUTABLE + "'"; + const int status = std::system(command.c_str()); + REQUIRE(WIFEXITED(status)); + REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS); +#endif +} From 47fc70721d9d25a3e187c24c6085ad2a6a7cdd8f Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Fri, 13 Nov 2026 16:15:57 +0100 Subject: [PATCH 2/9] Fix telemetry use after destruction when exiting with a live Tracer --- src/datadog/telemetry/telemetry.cpp | 14 +++++++++++++- src/datadog/telemetry/telemetry_impl.cpp | 17 ++++++++++++++++- src/datadog/telemetry/telemetry_impl.h | 6 ++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/datadog/telemetry/telemetry.cpp b/src/datadog/telemetry/telemetry.cpp index fbdb7099..bd2e070f 100644 --- a/src/datadog/telemetry/telemetry.cpp +++ b/src/datadog/telemetry/telemetry.cpp @@ -42,9 +42,21 @@ TelemetryProxy make_telemetry(const tracing::Optional& init) { init->agent_url, init->clock); } +namespace { + +// Sends the app-closing message when statics are destroyed, as the destructor +// of the telemetry singleton used to do. +struct ShutdownTelemetryAtExit { + ~ShutdownTelemetryAtExit() { shutdown(); } +}; + +} // namespace + TelemetryProxy& instance( const tracing::Optional& init = tracing::nullopt) { - static TelemetryProxy telemetry = make_telemetry(init); + // Never destroyed: a Tracer destroyed during exit() may still use it. + static TelemetryProxy& telemetry = *new TelemetryProxy(make_telemetry(init)); + static const ShutdownTelemetryAtExit shutdown_at_exit; return telemetry; } diff --git a/src/datadog/telemetry/telemetry_impl.cpp b/src/datadog/telemetry/telemetry_impl.cpp index 46583039..5d1117de 100644 --- a/src/datadog/telemetry/telemetry_impl.cpp +++ b/src/datadog/telemetry/telemetry_impl.cpp @@ -364,6 +364,7 @@ void Telemetry::app_started() { } void Telemetry::shutdown() { + is_shut_down_ = true; // cancel_tasks clears tasks_, so ~Telemetry() becomes a no-op after this. cancel_tasks(tasks_); app_closing(); @@ -372,11 +373,20 @@ void Telemetry::shutdown() { } void Telemetry::app_closing() { + std::shared_ptr client; + { + std::lock_guard l{http_client_mutex_}; + client = http_client_; + } + // Already shut down: shutdown() may be called twice, e.g. by the host and at + // exit. + if (!client) return; + // Capture metrics in-between two ticks to be sent with the last payload. capture_metrics(); send_payload("app-closing", app_closing_payload()); - http_client_->drain(clock_().tick + request_timeout); + client->drain(clock_().tick + request_timeout); } void Telemetry::send_payload(StringView request_type, std::string payload) { @@ -869,6 +879,7 @@ void Telemetry::increment_counter(const Counter& id) { void Telemetry::increment_counter(const Counter& id, const std::vector& tags) { + if (is_shut_down_) return; std::lock_guard l{counter_mutex_}; counters_[{id, tags}] += 1; } @@ -879,6 +890,7 @@ void Telemetry::decrement_counter(const Counter& id) { void Telemetry::decrement_counter(const Counter& id, const std::vector& tags) { + if (is_shut_down_) return; std::lock_guard l{counter_mutex_}; auto& v = counters_[{id, tags}]; if (v > 0) v -= 1; @@ -891,6 +903,7 @@ void Telemetry::set_counter(const Counter& id, uint64_t value) { void Telemetry::set_counter(const Counter& id, const std::vector& tags, uint64_t value) { + if (is_shut_down_) return; std::lock_guard l{counter_mutex_}; counters_[{id, tags}] = value; } @@ -901,6 +914,7 @@ void Telemetry::set_rate(const Rate& id, uint64_t value) { void Telemetry::set_rate(const Rate& id, const std::vector& tags, uint64_t value) { + if (is_shut_down_) return; std::lock_guard l{rate_mutex_}; rates_[{id, tags}] = value; } @@ -912,6 +926,7 @@ void Telemetry::add_datapoint(const Distribution& id, uint64_t value) { void Telemetry::add_datapoint(const Distribution& id, const std::vector& tags, uint64_t value) { + if (is_shut_down_) return; std::lock_guard l{distributions_mutex_}; distributions_[{id, tags}].emplace_back(value); } diff --git a/src/datadog/telemetry/telemetry_impl.h b/src/datadog/telemetry/telemetry_impl.h index 2e432a32..3e97e8b6 100644 --- a/src/datadog/telemetry/telemetry_impl.h +++ b/src/datadog/telemetry/telemetry_impl.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -78,6 +79,11 @@ class Telemetry final : public std::enable_shared_from_this { std::mutex http_client_mutex_; + // Set by shutdown(). After it, recording methods return early: their data + // can no longer be sent, and the metric definitions they get may already be + // destroyed by exit(). + std::atomic is_shut_down_{false}; + public: static std::shared_ptr create( FinalizedConfiguration configuration, From 9b7ee7426a49787d864cd61455128482c75ced25 Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Fri, 13 Nov 2026 18:34:26 +0100 Subject: [PATCH 3/9] code cleaning --- src/datadog/telemetry/telemetry.cpp | 23 ++++++++++------------- src/datadog/telemetry/telemetry_impl.cpp | 4 ++-- src/datadog/telemetry/telemetry_impl.h | 5 ++--- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/datadog/telemetry/telemetry.cpp b/src/datadog/telemetry/telemetry.cpp index bd2e070f..364880bc 100644 --- a/src/datadog/telemetry/telemetry.cpp +++ b/src/datadog/telemetry/telemetry.cpp @@ -42,21 +42,18 @@ TelemetryProxy make_telemetry(const tracing::Optional& init) { init->agent_url, init->clock); } -namespace { - -// Sends the app-closing message when statics are destroyed, as the destructor -// of the telemetry singleton used to do. -struct ShutdownTelemetryAtExit { - ~ShutdownTelemetryAtExit() { shutdown(); } -}; - -} // namespace - TelemetryProxy& instance( const tracing::Optional& init = tracing::nullopt) { - // Never destroyed: a Tracer destroyed during exit() may still use it. - static TelemetryProxy& telemetry = *new TelemetryProxy(make_telemetry(init)); - static const ShutdownTelemetryAtExit shutdown_at_exit; + // This singleton is never destroyed on purpose because the Tracer's + // destructor needs it. + static TelemetryProxy& telemetry = *new TelemetryProxy{make_telemetry(init)}; + + // Send the app-closing message when statics are destroyed. + struct ShutdownTelemetryAtExit { + ~ShutdownTelemetryAtExit() { shutdown(); } + }; + static const ShutdownTelemetryAtExit shutdown_telemetry_at_exit; + return telemetry; } diff --git a/src/datadog/telemetry/telemetry_impl.cpp b/src/datadog/telemetry/telemetry_impl.cpp index 5d1117de..d8ac628b 100644 --- a/src/datadog/telemetry/telemetry_impl.cpp +++ b/src/datadog/telemetry/telemetry_impl.cpp @@ -373,13 +373,13 @@ void Telemetry::shutdown() { } void Telemetry::app_closing() { + // Check if the HTTP client is still available, as shutdown() may have already + // been called. std::shared_ptr client; { std::lock_guard l{http_client_mutex_}; client = http_client_; } - // Already shut down: shutdown() may be called twice, e.g. by the host and at - // exit. if (!client) return; // Capture metrics in-between two ticks to be sent with the last payload. diff --git a/src/datadog/telemetry/telemetry_impl.h b/src/datadog/telemetry/telemetry_impl.h index 3e97e8b6..56e8fb46 100644 --- a/src/datadog/telemetry/telemetry_impl.h +++ b/src/datadog/telemetry/telemetry_impl.h @@ -79,9 +79,8 @@ class Telemetry final : public std::enable_shared_from_this { std::mutex http_client_mutex_; - // Set by shutdown(). After it, recording methods return early: their data - // can no longer be sent, and the metric definitions they get may already be - // destroyed by exit(). + // Used to skip recording after shutdown(), because the data can't be sent + // anymore. std::atomic is_shut_down_{false}; public: From 67342200fa12218d59d811ec7cc52d8860906a83 Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Fri, 13 Nov 2026 19:06:02 +0100 Subject: [PATCH 4/9] Simplify Telemetry shutdown() app_closing() and destructor --- src/datadog/telemetry/telemetry_impl.cpp | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/datadog/telemetry/telemetry_impl.cpp b/src/datadog/telemetry/telemetry_impl.cpp index d8ac628b..037c0a5a 100644 --- a/src/datadog/telemetry/telemetry_impl.cpp +++ b/src/datadog/telemetry/telemetry_impl.cpp @@ -288,12 +288,7 @@ void Telemetry::schedule_tasks() { })); } -Telemetry::~Telemetry() { - if (!tasks_.empty()) { - cancel_tasks(tasks_); - app_closing(); - } -} +Telemetry::~Telemetry() { shutdown(); } void Telemetry::log_error(std::string message) { if (!config_.report_logs) return; @@ -364,8 +359,7 @@ void Telemetry::app_started() { } void Telemetry::shutdown() { - is_shut_down_ = true; - // cancel_tasks clears tasks_, so ~Telemetry() becomes a no-op after this. + if (is_shut_down_.exchange(true)) return; cancel_tasks(tasks_); app_closing(); std::lock_guard l{http_client_mutex_}; @@ -373,20 +367,11 @@ void Telemetry::shutdown() { } void Telemetry::app_closing() { - // Check if the HTTP client is still available, as shutdown() may have already - // been called. - std::shared_ptr client; - { - std::lock_guard l{http_client_mutex_}; - client = http_client_; - } - if (!client) return; - // Capture metrics in-between two ticks to be sent with the last payload. capture_metrics(); send_payload("app-closing", app_closing_payload()); - client->drain(clock_().tick + request_timeout); + http_client_->drain(clock_().tick + request_timeout); } void Telemetry::send_payload(StringView request_type, std::string payload) { From a9205b9519d586787ac7b3899774caf39e66a278 Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Fri, 13 Nov 2026 19:08:46 +0100 Subject: [PATCH 5/9] Skip logs after shutdown, for consistency --- src/datadog/telemetry/telemetry_impl.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/datadog/telemetry/telemetry_impl.cpp b/src/datadog/telemetry/telemetry_impl.cpp index 037c0a5a..744aa577 100644 --- a/src/datadog/telemetry/telemetry_impl.cpp +++ b/src/datadog/telemetry/telemetry_impl.cpp @@ -850,6 +850,7 @@ void Telemetry::capture_metrics() { void Telemetry::log(std::string message, telemetry::LogLevel level, Optional stacktrace) { + if (is_shut_down_) return; auto timestamp = std::chrono::duration_cast( clock_().wall.time_since_epoch()) .count(); From fa734c0c67a6028f2cb9937d6ff13217265773f8 Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Fri, 13 Nov 2026 19:27:41 +0100 Subject: [PATCH 6/9] simplify CMakeLists --- test/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cef8f301..f32a697f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -82,8 +82,6 @@ target_include_directories(exit_with_live_tracer PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src - ${CMAKE_SOURCE_DIR}/include - ${CMAKE_SOURCE_DIR}/include/datadog ) target_link_libraries(exit_with_live_tracer PRIVATE From adceb19e542d830881d86ee6c10d6493eecf8f82 Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Fri, 13 Nov 2026 19:37:48 +0100 Subject: [PATCH 7/9] Add a test about double call to telemetry shutdown() --- test/telemetry/test_telemetry.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/telemetry/test_telemetry.cpp b/test/telemetry/test_telemetry.cpp index 9a0cfb41..3f2b9a13 100644 --- a/test/telemetry/test_telemetry.cpp +++ b/test/telemetry/test_telemetry.cpp @@ -376,6 +376,21 @@ TELEMETRY_IMPLEMENTATION_TEST("Tracer telemetry lifecycle") { find_payload(message_batch["payload"], "app-closing"); REQUIRE(app_closing_payload.has_value()); } + + SECTION("shutdown() sends app-closing once, and a second call is safe") { + auto telemetry = Telemetry::create(*finalize_config(), tracer_signature, + logger, client, scheduler, *url); + client->clear(); + + telemetry->shutdown(); + auto message_batch = nlohmann::json::parse(client->request_body); + REQUIRE(is_valid_telemetry_payload(message_batch)); + REQUIRE(find_payload(message_batch["payload"], "app-closing")); + + client->clear(); + telemetry->shutdown(); + CHECK(client->request_body.empty()); + } } TELEMETRY_IMPLEMENTATION_TEST("session ID headers") { From 666b38f98eedfea851310a8e1a37690690d267bd Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Fri, 13 Nov 2026 19:59:19 +0100 Subject: [PATCH 8/9] Avoid calling instance() while its static is being destroyed --- src/datadog/telemetry/telemetry.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/datadog/telemetry/telemetry.cpp b/src/datadog/telemetry/telemetry.cpp index 364880bc..7552be8b 100644 --- a/src/datadog/telemetry/telemetry.cpp +++ b/src/datadog/telemetry/telemetry.cpp @@ -37,9 +37,19 @@ struct Ctor_param final { TelemetryProxy make_telemetry(const tracing::Optional& init) { if (!init || !init->configuration.enabled) return NoopTelemetry{}; - return Telemetry::create(init->configuration, init->tracer_signature, - init->logger, init->client, init->scheduler, - init->agent_url, init->clock); + + std::shared_ptr telemetry = Telemetry::create( + init->configuration, init->tracer_signature, init->logger, init->client, + init->scheduler, init->agent_url, init->clock); + + // Send the app-closing message when statics are destroyed. + struct ShutdownTelemetryAtExit { + std::shared_ptr telemetry_; + ~ShutdownTelemetryAtExit() { telemetry_->shutdown(); } + }; + static const ShutdownTelemetryAtExit shutdown_telemetry_at_exit{telemetry}; + + return telemetry; } TelemetryProxy& instance( @@ -47,13 +57,6 @@ TelemetryProxy& instance( // This singleton is never destroyed on purpose because the Tracer's // destructor needs it. static TelemetryProxy& telemetry = *new TelemetryProxy{make_telemetry(init)}; - - // Send the app-closing message when statics are destroyed. - struct ShutdownTelemetryAtExit { - ~ShutdownTelemetryAtExit() { shutdown(); } - }; - static const ShutdownTelemetryAtExit shutdown_telemetry_at_exit; - return telemetry; } From 56b2b0e3b8cb55c905eb627ea175aee2e8320313 Mon Sep 17 00:00:00 2001 From: Xavier Lamorlette Date: Tue, 17 Nov 2026 13:35:46 +0100 Subject: [PATCH 9/9] Code cleaning next to Test Tracer - Telemetry destruction order fix (#402) --- src/datadog/datadog_agent.cpp | 2 + src/datadog/telemetry/telemetry.cpp | 56 ++++++++++----------- src/datadog/telemetry/telemetry_impl.cpp | 63 ++++++++++++------------ test/test_tracer.cpp | 9 +--- 4 files changed, 63 insertions(+), 67 deletions(-) diff --git a/src/datadog/datadog_agent.cpp b/src/datadog/datadog_agent.cpp index fe1db162..01a5954a 100644 --- a/src/datadog/datadog_agent.cpp +++ b/src/datadog/datadog_agent.cpp @@ -261,6 +261,8 @@ void DatadogAgent::flush() { auto encode_result = msgpack_encode(body, trace_chunks); auto end = std::chrono::steady_clock::now(); + // TODO: wrong unit?!: trace_chunk_serialization_duration is + // "trace_chunk_serialization.ms", but we're sending microseconds! telemetry::distribution::add( metrics::tracer::trace_chunk_serialization_duration, std::chrono::duration_cast(end - beg).count()); diff --git a/src/datadog/telemetry/telemetry.cpp b/src/datadog/telemetry/telemetry.cpp index 7552be8b..cfae2837 100644 --- a/src/datadog/telemetry/telemetry.cpp +++ b/src/datadog/telemetry/telemetry.cpp @@ -4,28 +4,25 @@ #include "telemetry_impl.h" namespace datadog::telemetry { -namespace details { -/// NOTE(@dmehala): Generic overload function (P0051R3) like implementation. +namespace details { +// Generic overload function (P0051R3) like implementation. template struct Overload : Ts... { using Ts::operator()...; }; -/// NOTE(@dmehala): Guide required for C++17. Remove once we switch to C++20. +// Guide required for C++17. Remove once we switch to C++20. template Overload(Ts...) -> Overload; - } // namespace details +// `TelemetryProxy` holds either the real implementation or a no-op +// implementation. using NoopTelemetry = std::monostate; - -/// `TelemetryProxy` holds either the real implementation or a no-op -/// implementation. using TelemetryProxy = std::variant>; -/// NOTE(@dmehala): Here to facilitate Meyer's singleton construction. -struct Ctor_param final { +struct TelemetryParameters final { FinalizedConfiguration configuration; tracing::TracerSignature tracer_signature; std::shared_ptr logger; @@ -35,7 +32,8 @@ struct Ctor_param final { tracing::Clock clock = tracing::default_clock; }; -TelemetryProxy make_telemetry(const tracing::Optional& init) { +TelemetryProxy make_telemetry( + const tracing::Optional& init) { if (!init || !init->configuration.enabled) return NoopTelemetry{}; std::shared_ptr telemetry = Telemetry::create( @@ -53,7 +51,7 @@ TelemetryProxy make_telemetry(const tracing::Optional& init) { } TelemetryProxy& instance( - const tracing::Optional& init = tracing::nullopt) { + const tracing::Optional& init = tracing::nullopt) { // This singleton is never destroyed on purpose because the Tracer's // destructor needs it. static TelemetryProxy& telemetry = *new TelemetryProxy{make_telemetry(init)}; @@ -61,24 +59,24 @@ TelemetryProxy& instance( } void init(FinalizedConfiguration configuration, + tracing::TracerSignature tracer_signature, std::shared_ptr logger, std::shared_ptr client, std::shared_ptr event_scheduler, tracing::HTTPClient::URL agent_url, tracing::Clock clock) { - instance(Ctor_param{configuration, - tracing::TracerSignature(tracing::RuntimeID::generate(), - tracing::get_process_name(), ""), - logger, client, event_scheduler, agent_url, clock}); + instance(TelemetryParameters{configuration, tracer_signature, logger, client, + event_scheduler, agent_url, clock}); } void init(FinalizedConfiguration configuration, - tracing::TracerSignature tracer_signature, std::shared_ptr logger, std::shared_ptr client, std::shared_ptr event_scheduler, tracing::HTTPClient::URL agent_url, tracing::Clock clock) { - instance(Ctor_param{configuration, tracer_signature, logger, client, - event_scheduler, agent_url, clock}); + init(configuration, + tracing::TracerSignature(tracing::RuntimeID::generate(), + tracing::get_process_name(), ""), + logger, client, event_scheduler, agent_url, clock); } void shutdown() { @@ -147,7 +145,7 @@ void error(std::string message, std::string stacktrace) { [&](std::shared_ptr& telemetry) { telemetry->log_error(message, stacktrace); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -159,7 +157,7 @@ void increment(const Counter& counter) { [&](std::shared_ptr& telemetry) { telemetry->increment_counter(counter); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -169,7 +167,7 @@ void increment(const Counter& counter, const std::vector& tags) { [&](std::shared_ptr& telemetry) { telemetry->increment_counter(counter, tags); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -179,7 +177,7 @@ void decrement(const Counter& counter) { [&](std::shared_ptr& telemetry) { telemetry->decrement_counter(counter); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -189,7 +187,7 @@ void decrement(const Counter& counter, const std::vector& tags) { [&](std::shared_ptr& telemetry) { telemetry->decrement_counter(counter, tags); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -199,7 +197,7 @@ void set(const Counter& counter, uint64_t value) { [&](std::shared_ptr& telemetry) { telemetry->set_counter(counter, value); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -210,7 +208,7 @@ void set(const Counter& counter, const std::vector& tags, [&](std::shared_ptr& telemetry) { telemetry->set_counter(counter, tags, value); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -223,7 +221,7 @@ void set(const Rate& rate, uint64_t value) { [&](std::shared_ptr& telemetry) { telemetry->set_rate(rate, value); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -234,7 +232,7 @@ void set(const Rate& rate, const std::vector& tags, [&](std::shared_ptr& telemetry) { telemetry->set_rate(rate, tags, value); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -247,7 +245,7 @@ void add(const Distribution& distribution, uint64_t value) { [&](std::shared_ptr& telemetry) { telemetry->add_datapoint(distribution, value); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } @@ -258,7 +256,7 @@ void add(const Distribution& distribution, const std::vector& tags, [&](std::shared_ptr& telemetry) { telemetry->add_datapoint(distribution, tags, value); }, - [](auto&&) {}, + [](NoopTelemetry) {}, }, instance()); } diff --git a/src/datadog/telemetry/telemetry_impl.cpp b/src/datadog/telemetry/telemetry_impl.cpp index 744aa577..0a0cf3f1 100644 --- a/src/datadog/telemetry/telemetry_impl.cpp +++ b/src/datadog/telemetry/telemetry_impl.cpp @@ -17,35 +17,36 @@ using namespace datadog::tracing; using namespace std::chrono_literals; namespace datadog::telemetry { + namespace internal_metrics { -/// The number of logs created with a given log level. Useful for calculating -/// impact for other features (automatic sending of logs). Levels should be one -/// of `debug`, `info`, `warn`, `error`, `critical`. +// The number of logs created with a given log level. Useful for calculating +// impact for other features (automatic sending of logs). Levels should be one +// of `debug`, `info`, `warn`, `error`, `critical`. const telemetry::Counter logs_created{"logs_created", "general", true}; -/// The number of requests sent to the api endpoint in the agent that errored, -/// tagged by the error type (e.g. `type:timeout`, `type:network`, -/// `type:status_code`) and Endpoint (`endpoint:agent`, `endpoint:agentless`). +// The number of requests sent to the api endpoint in the agent that errored, +// tagged by the error type (e.g. `type:timeout`, `type:network`, +// `type:status_code`) and Endpoint (`endpoint:agent`, `endpoint:agentless`). const telemetry::Counter errors{"telemetry_api.errors", "telemetry", true}; -/// The number of requests sent to a telemetry endpoint, regardless of success, -/// tagged by the endpoint (`endpoint:agent`, `endpoint:agentless`). +// The number of requests sent to a telemetry endpoint, regardless of success, +// tagged by the endpoint (`endpoint:agent`, `endpoint:agentless`). const telemetry::Counter requests{"telemetry_api.requests", "telemetry", true}; -/// The number of responses received from the endpoint, tagged with status code -/// (`status_code:200`, `status_code:404`) and endpoint (`endpoint:agent`, -/// `endpoint:agentless`). +// The number of responses received from the endpoint, tagged with status code +// (`status_code:200`, `status_code:404`) and endpoint (`endpoint:agent`, +// `endpoint:agentless`). const telemetry::Counter responses{"telemetry_api.responses", "telemetry", true}; -/// The size of the payload sent to the stats endpoint in bytes, tagged by the -/// endpoint (`endpoint:agent`, `endpoint:agentless`). +// The size of the payload sent to the stats endpoint in bytes, tagged by the +// endpoint (`endpoint:agent`, `endpoint:agentless`). const telemetry::Distribution bytes_sent{"telemetry_api.bytes", "telemetry", true}; -/// The time it takes to send the payload sent to the endpoint in ms, tagged by -/// the endpoint (`endpoint:agent`, `endpoint:agentless`). +// The time it takes to send the payload sent to the endpoint in ms, tagged by +// the endpoint (`endpoint:agent`, `endpoint:agentless`). [[maybe_unused]] const telemetry::Distribution request_duration{ "telemetry_api.ms", "telemetry", true}; @@ -362,7 +363,7 @@ void Telemetry::shutdown() { if (is_shut_down_.exchange(true)) return; cancel_tasks(tasks_); app_closing(); - std::lock_guard l{http_client_mutex_}; + std::lock_guard lock{http_client_mutex_}; http_client_.reset(); } @@ -377,7 +378,7 @@ void Telemetry::app_closing() { void Telemetry::send_payload(StringView request_type, std::string payload) { std::shared_ptr client; { - std::lock_guard l{http_client_mutex_}; + std::lock_guard lock{http_client_mutex_}; client = http_client_; } if (!client) return; @@ -486,19 +487,19 @@ std::string Telemetry::heartbeat_and_telemetry() { std::unordered_map, std::vector> distributions; { - std::lock_guard l{distributions_mutex_}; + std::lock_guard lock{distributions_mutex_}; std::swap(distributions_, distributions); } std::unordered_map, MetricSnapshot> counters_snapshot; { - std::lock_guard l{counter_mutex_}; + std::lock_guard lock{counter_mutex_}; std::swap(counters_snapshot_, counters_snapshot); } std::unordered_map, MetricSnapshot> rates_snapshot; { - std::lock_guard l{rate_mutex_}; + std::lock_guard lock{rate_mutex_}; std::swap(rates_snapshot_, rates_snapshot); } @@ -531,7 +532,7 @@ std::string Telemetry::heartbeat_and_telemetry() { std::vector old_logs; { - std::lock_guard l{log_mutex_}; + std::lock_guard lock{log_mutex_}; std::swap(old_logs, logs_); } @@ -827,7 +828,7 @@ void Telemetry::capture_metrics() { std::unordered_map, uint64_t> counter_snapshot; { - std::lock_guard l{counter_mutex_}; + std::lock_guard lock{counter_mutex_}; std::swap(counter_snapshot, counters_); } @@ -838,7 +839,7 @@ void Telemetry::capture_metrics() { std::unordered_map, uint64_t> rate_snapshot; { - std::lock_guard l{rate_mutex_}; + std::lock_guard lock{rate_mutex_}; std::swap(rate_snapshot, rates_); } @@ -854,7 +855,7 @@ void Telemetry::log(std::string message, telemetry::LogLevel level, auto timestamp = std::chrono::duration_cast( clock_().wall.time_since_epoch()) .count(); - std::lock_guard l{log_mutex_}; + std::lock_guard lock{log_mutex_}; logs_.emplace_back( telemetry::LogMessage{std::move(message), level, stacktrace, timestamp}); } @@ -866,7 +867,7 @@ void Telemetry::increment_counter(const Counter& id) { void Telemetry::increment_counter(const Counter& id, const std::vector& tags) { if (is_shut_down_) return; - std::lock_guard l{counter_mutex_}; + std::lock_guard lock{counter_mutex_}; counters_[{id, tags}] += 1; } @@ -877,9 +878,9 @@ void Telemetry::decrement_counter(const Counter& id) { void Telemetry::decrement_counter(const Counter& id, const std::vector& tags) { if (is_shut_down_) return; - std::lock_guard l{counter_mutex_}; - auto& v = counters_[{id, tags}]; - if (v > 0) v -= 1; + std::lock_guard lock{counter_mutex_}; + auto& value = counters_[{id, tags}]; + if (value > 0) value -= 1; } void Telemetry::set_counter(const Counter& id, uint64_t value) { @@ -890,7 +891,7 @@ void Telemetry::set_counter(const Counter& id, const std::vector& tags, uint64_t value) { if (is_shut_down_) return; - std::lock_guard l{counter_mutex_}; + std::lock_guard lock{counter_mutex_}; counters_[{id, tags}] = value; } @@ -901,7 +902,7 @@ void Telemetry::set_rate(const Rate& id, uint64_t value) { void Telemetry::set_rate(const Rate& id, const std::vector& tags, uint64_t value) { if (is_shut_down_) return; - std::lock_guard l{rate_mutex_}; + std::lock_guard lock{rate_mutex_}; rates_[{id, tags}] = value; } @@ -913,7 +914,7 @@ void Telemetry::add_datapoint(const Distribution& id, const std::vector& tags, uint64_t value) { if (is_shut_down_) return; - std::lock_guard l{distributions_mutex_}; + std::lock_guard lock{distributions_mutex_}; distributions_[{id, tags}].emplace_back(value); } diff --git a/test/test_tracer.cpp b/test/test_tracer.cpp index 286f3887..e05b019d 100644 --- a/test/test_tracer.cpp +++ b/test/test_tracer.cpp @@ -1,6 +1,3 @@ -// These are tests for `Tracer`. `Tracer` is responsible for creating root -// spans and for extracting spans from propagated trace context. - #include #include #include @@ -39,8 +36,7 @@ #include "null_logger.h" #include "test.h" -namespace datadog { -namespace tracing { +namespace datadog::tracing { std::ostream& operator<<(std::ostream& stream, const Optional& code) { @@ -50,8 +46,7 @@ std::ostream& operator<<(std::ostream& stream, return stream << "null"; } -} // namespace tracing -} // namespace datadog +} // namespace datadog::tracing using namespace datadog::tracing; using namespace std::chrono_literals;