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
19 changes: 19 additions & 0 deletions include/datadog/telemetry/telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ void init(FinalizedConfiguration configuration,
tracing::HTTPClient::URL agent_url,
tracing::Clock clock = tracing::default_clock);

/// Deterministically shut down the telemetry module.
///
/// Cancels all scheduled tasks, sends the app-closing payload, and waits up
/// to 2 seconds for in-flight requests to complete (HTTPClient::drain()).
/// Then releases the HTTP client reference; if this is the last shared_ptr
/// to the client, the background Curl thread is joined synchronously and any
/// remaining in-flight requests are abandoned without firing their callbacks.
/// If other holders of the client remain, the Curl thread continues running
/// and requests may still complete and fire their callbacks after this call
/// returns (the callbacks guard against this with weak_from_this()). After
/// this call all telemetry send functions become no-ops. Safe to call even if
/// telemetry was never initialized, but must be called at most once after a
/// successful init.
///
/// Call this from the worker process exit path (e.g. before destroying the
/// tracer) so that telemetry's reference to the HTTP client is released
/// promptly, reducing the window before the Curl thread is quiesced.
void shutdown();

/// Sends configuration changes.
///
/// This function is responsible for sending reported configuration changes
Expand Down
114 changes: 67 additions & 47 deletions src/datadog/telemetry/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using NoopTelemetry = std::monostate;

/// `TelemetryProxy` holds either the real implementation or a no-op
/// implementation.
using TelemetryProxy = std::variant<NoopTelemetry, Telemetry>;
using TelemetryProxy = std::variant<NoopTelemetry, std::shared_ptr<Telemetry>>;

/// NOTE(@dmehala): Here to facilitate Meyer's singleton construction.
struct Ctor_param final {
Expand All @@ -37,9 +37,9 @@ struct Ctor_param final {

TelemetryProxy make_telemetry(const tracing::Optional<Ctor_param>& init) {
if (!init || !init->configuration.enabled) return NoopTelemetry{};
return Telemetry{init->configuration, init->tracer_signature, init->logger,
init->client, init->scheduler, init->agent_url,
init->clock};
return Telemetry::create(init->configuration, init->tracer_signature,
init->logger, init->client, init->scheduler,
init->agent_url, init->clock);
}

TelemetryProxy& instance(
Expand Down Expand Up @@ -69,20 +69,30 @@ void init(FinalizedConfiguration configuration,
event_scheduler, agent_url, clock});
}

void send_configuration_change() {
void shutdown() {
std::visit(
details::Overload{
[&](Telemetry& telemetry) { telemetry.send_configuration_change(); },
[](std::shared_ptr<Telemetry>& telemetry) { telemetry->shutdown(); },
[](NoopTelemetry) {},
},
instance());
Comment thread
cataphract marked this conversation as resolved.
}

void send_configuration_change() {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->send_configuration_change();
},
[](NoopTelemetry) {},
},
instance());
}

void capture_configuration_change(
const std::vector<tracing::ConfigMetadata>& new_configuration) {
std::visit(details::Overload{
[&](Telemetry& telemetry) {
telemetry.capture_configuration_change(new_configuration);
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->capture_configuration_change(new_configuration);
},
[](NoopTelemetry) {},
},
Expand All @@ -92,24 +102,28 @@ void capture_configuration_change(
namespace log {
void warning(std::string message) {
std::visit(details::Overload{
[&](Telemetry& telemetry) { telemetry.log_warning(message); },
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->log_warning(message);
},
[](NoopTelemetry) {},
},
instance());
}

void error(std::string message) {
std::visit(details::Overload{
[&](Telemetry& telemetry) { telemetry.log_error(message); },
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->log_error(message);
},
[](NoopTelemetry) {},
},
instance());
}

void error(std::string message, std::string stacktrace) {
std::visit(details::Overload{
[&](Telemetry& telemetry) {
telemetry.log_error(message, stacktrace);
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->log_error(message, stacktrace);
},
[](auto&&) {},
},
Expand All @@ -119,57 +133,60 @@ void error(std::string message, std::string stacktrace) {

namespace counter {
void increment(const Counter& counter) {
std::visit(
details::Overload{
[&](Telemetry& telemetry) { telemetry.increment_counter(counter); },
[](auto&&) {},
},
instance());
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->increment_counter(counter);
},
[](auto&&) {},
},
instance());
}

void increment(const Counter& counter, const std::vector<std::string>& tags) {
std::visit(details::Overload{
[&](Telemetry& telemetry) {
telemetry.increment_counter(counter, tags);
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->increment_counter(counter, tags);
},
[](auto&&) {},
},
instance());
}

void decrement(const Counter& counter) {
std::visit(
details::Overload{
[&](Telemetry& telemetry) { telemetry.decrement_counter(counter); },
[](auto&&) {},
},
instance());
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->decrement_counter(counter);
},
[](auto&&) {},
},
instance());
}

void decrement(const Counter& counter, const std::vector<std::string>& tags) {
std::visit(details::Overload{
[&](Telemetry& telemetry) {
telemetry.decrement_counter(counter, tags);
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->decrement_counter(counter, tags);
},
[](auto&&) {},
},
instance());
}

void set(const Counter& counter, uint64_t value) {
std::visit(
details::Overload{
[&](Telemetry& telemetry) { telemetry.set_counter(counter, value); },
[](auto&&) {},
},
instance());
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_counter(counter, value);
},
[](auto&&) {},
},
instance());
}

void set(const Counter& counter, const std::vector<std::string>& tags,
uint64_t value) {
std::visit(details::Overload{
[&](Telemetry& telemetry) {
telemetry.set_counter(counter, tags, value);
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_counter(counter, tags, value);
},
[](auto&&) {},
},
Expand All @@ -181,29 +198,32 @@ void set(const Counter& counter, const std::vector<std::string>& tags,
namespace rate {
void set(const Rate& rate, uint64_t value) {
std::visit(details::Overload{
[&](Telemetry& telemetry) { telemetry.set_rate(rate, value); },
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_rate(rate, value);
},
[](auto&&) {},
},
instance());
}

void set(const Rate& rate, const std::vector<std::string>& tags,
uint64_t value) {
std::visit(
details::Overload{
[&](Telemetry& telemetry) { telemetry.set_rate(rate, tags, value); },
[](auto&&) {},
},
instance());
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_rate(rate, tags, value);
},
[](auto&&) {},
},
instance());
}
} // namespace rate

namespace distribution {

void add(const Distribution& distribution, uint64_t value) {
std::visit(details::Overload{
[&](Telemetry& telemetry) {
telemetry.add_datapoint(distribution, value);
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->add_datapoint(distribution, value);
},
[](auto&&) {},
},
Expand All @@ -213,8 +233,8 @@ void add(const Distribution& distribution, uint64_t value) {
void add(const Distribution& distribution, const std::vector<std::string>& tags,
uint64_t value) {
std::visit(details::Overload{
[&](Telemetry& telemetry) {
telemetry.add_datapoint(distribution, tags, value);
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->add_datapoint(distribution, tags, value);
},
[](auto&&) {},
},
Expand Down
Loading
Loading