Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/datadog/datadog_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::microseconds>(end - beg).count());
Expand Down
56 changes: 27 additions & 29 deletions src/datadog/telemetry/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename... Ts>
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 <class... Ts>
Overload(Ts...) -> Overload<Ts...>;

} // 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<NoopTelemetry, std::shared_ptr<Telemetry>>;

/// 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<tracing::Logger> logger;
Expand All @@ -35,7 +32,8 @@ struct Ctor_param final {
tracing::Clock clock = tracing::default_clock;
};

TelemetryProxy make_telemetry(const tracing::Optional<Ctor_param>& init) {
TelemetryProxy make_telemetry(
const tracing::Optional<TelemetryParameters>& init) {
if (!init || !init->configuration.enabled) return NoopTelemetry{};

std::shared_ptr<Telemetry> telemetry = Telemetry::create(
Expand All @@ -53,32 +51,32 @@ TelemetryProxy make_telemetry(const tracing::Optional<Ctor_param>& init) {
}

TelemetryProxy& instance(
const tracing::Optional<Ctor_param>& init = tracing::nullopt) {
const tracing::Optional<TelemetryParameters>& 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)};
return telemetry;
}

void init(FinalizedConfiguration configuration,
tracing::TracerSignature tracer_signature,
std::shared_ptr<tracing::Logger> logger,
std::shared_ptr<tracing::HTTPClient> client,
std::shared_ptr<tracing::EventScheduler> 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<tracing::Logger> logger,
std::shared_ptr<tracing::HTTPClient> client,
std::shared_ptr<tracing::EventScheduler> 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() {
Expand Down Expand Up @@ -147,7 +145,7 @@ void error(std::string message, std::string stacktrace) {
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->log_error(message, stacktrace);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -159,7 +157,7 @@ void increment(const Counter& counter) {
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->increment_counter(counter);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -169,7 +167,7 @@ void increment(const Counter& counter, const std::vector<std::string>& tags) {
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->increment_counter(counter, tags);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -179,7 +177,7 @@ void decrement(const Counter& counter) {
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->decrement_counter(counter);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -189,7 +187,7 @@ void decrement(const Counter& counter, const std::vector<std::string>& tags) {
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->decrement_counter(counter, tags);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -199,7 +197,7 @@ void set(const Counter& counter, uint64_t value) {
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_counter(counter, value);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -210,7 +208,7 @@ void set(const Counter& counter, const std::vector<std::string>& tags,
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_counter(counter, tags, value);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -223,7 +221,7 @@ void set(const Rate& rate, uint64_t value) {
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_rate(rate, value);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -234,7 +232,7 @@ void set(const Rate& rate, const std::vector<std::string>& tags,
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_rate(rate, tags, value);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -247,7 +245,7 @@ void add(const Distribution& distribution, uint64_t value) {
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->add_datapoint(distribution, value);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand All @@ -258,7 +256,7 @@ void add(const Distribution& distribution, const std::vector<std::string>& tags,
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->add_datapoint(distribution, tags, value);
},
[](auto&&) {},
[](NoopTelemetry) {},
},
instance());
}
Expand Down
63 changes: 32 additions & 31 deletions src/datadog/telemetry/telemetry_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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();
}

Expand All @@ -377,7 +378,7 @@ void Telemetry::app_closing() {
void Telemetry::send_payload(StringView request_type, std::string payload) {
std::shared_ptr<tracing::HTTPClient> client;
{
std::lock_guard l{http_client_mutex_};
std::lock_guard lock{http_client_mutex_};
client = http_client_;
}
if (!client) return;
Expand Down Expand Up @@ -486,19 +487,19 @@ std::string Telemetry::heartbeat_and_telemetry() {
std::unordered_map<MetricContext<Distribution>, std::vector<uint64_t>>
distributions;
{
std::lock_guard l{distributions_mutex_};
std::lock_guard lock{distributions_mutex_};
std::swap(distributions_, distributions);
}

std::unordered_map<MetricContext<Counter>, MetricSnapshot> counters_snapshot;
{
std::lock_guard l{counter_mutex_};
std::lock_guard lock{counter_mutex_};
std::swap(counters_snapshot_, counters_snapshot);
}

std::unordered_map<MetricContext<Rate>, MetricSnapshot> rates_snapshot;
{
std::lock_guard l{rate_mutex_};
std::lock_guard lock{rate_mutex_};
std::swap(rates_snapshot_, rates_snapshot);
}

Expand Down Expand Up @@ -531,7 +532,7 @@ std::string Telemetry::heartbeat_and_telemetry() {

std::vector<telemetry::LogMessage> old_logs;
{
std::lock_guard l{log_mutex_};
std::lock_guard lock{log_mutex_};
std::swap(old_logs, logs_);
}

Expand Down Expand Up @@ -827,7 +828,7 @@ void Telemetry::capture_metrics() {

std::unordered_map<MetricContext<Counter>, uint64_t> counter_snapshot;
{
std::lock_guard l{counter_mutex_};
std::lock_guard lock{counter_mutex_};
std::swap(counter_snapshot, counters_);
}

Expand All @@ -838,7 +839,7 @@ void Telemetry::capture_metrics() {

std::unordered_map<MetricContext<Rate>, uint64_t> rate_snapshot;
{
std::lock_guard l{rate_mutex_};
std::lock_guard lock{rate_mutex_};
std::swap(rate_snapshot, rates_);
}

Expand All @@ -854,7 +855,7 @@ void Telemetry::log(std::string message, telemetry::LogLevel level,
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(
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});
}
Expand All @@ -866,7 +867,7 @@ void Telemetry::increment_counter(const Counter& id) {
void Telemetry::increment_counter(const Counter& id,
const std::vector<std::string>& tags) {
if (is_shut_down_) return;
std::lock_guard l{counter_mutex_};
std::lock_guard lock{counter_mutex_};
counters_[{id, tags}] += 1;
}

Expand All @@ -877,9 +878,9 @@ void Telemetry::decrement_counter(const Counter& id) {
void Telemetry::decrement_counter(const Counter& id,
const std::vector<std::string>& 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) {
Expand All @@ -890,7 +891,7 @@ void Telemetry::set_counter(const Counter& id,
const std::vector<std::string>& 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;
}

Expand All @@ -901,7 +902,7 @@ void Telemetry::set_rate(const Rate& id, uint64_t value) {
void Telemetry::set_rate(const Rate& id, const std::vector<std::string>& 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;
}

Expand All @@ -913,7 +914,7 @@ void Telemetry::add_datapoint(const Distribution& id,
const std::vector<std::string>& 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);
}

Expand Down
9 changes: 2 additions & 7 deletions test/test_tracer.cpp
Original file line number Diff line number Diff line change
@@ -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 <datadog/error.h>
#include <datadog/hex.h>
#include <datadog/null_collector.h>
Expand Down Expand Up @@ -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<Error::Code>& code) {
Expand All @@ -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;
Expand Down
Loading