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;