Skip to content

Commit

Permalink
fix: telemetry support for sub-seconds intervals (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmehala authored Nov 12, 2024
1 parent d49c147 commit bc70e5a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
26 changes: 15 additions & 11 deletions include/datadog/telemetry/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@
namespace datadog::telemetry {

struct Configuration {
// Turn on or off telemetry module
// Enabled by default.
// Overwritten by `DD_INSTRUMENTATION_TELEMETRY_ENABLED` env var.
// Enable or disable the telemetry module.
// Default: enabled.
// Can be overriden by the `DD_INSTRUMENTATION_TELEMETRY_ENABLED` environment
// variable.
tracing::Optional<bool> enabled;
// Turn on or off metrics reporting
// Overwritten by `DD_TELEMETRY_METRICS_ENABLED` env var.
// Enable or disable telemetry metrics.
// Default: enabled.
// Can be overriden by the `DD_TELEMETRY_METRICS_ENABLED` environment
// variable.
tracing::Optional<bool> report_metrics;
// Interval of metrics payload
// Overwritten by `DD_TELEMETRY_METRICS_INTERVAL_SECONDS` env var.
tracing::Optional<int> metrics_interval_seconds;
// Interval of heartbeat payload
// Overwritten by `DD_TELEMETRY_HEARTBEAT_INTERVAL` env var.
tracing::Optional<int> heartbeat_interval_seconds;
// Interval at which the metrics payload will be sent.
// Can be overriden by `DD_TELEMETRY_METRICS_INTERVAL_SECONDS` environment
// variable.
tracing::Optional<double> metrics_interval_seconds;
// Interval at which the heartbeat payload will be sent.
// Can be overriden by `DD_TELEMETRY_HEARTBEAT_INTERVAL` environment variable.
tracing::Optional<double> heartbeat_interval_seconds;
// `integration_name` is the name of the product integrating this library.
// Example: "nginx", "envoy" or "istio".
tracing::Optional<std::string> integration_name;
Expand Down
30 changes: 18 additions & 12 deletions src/datadog/telemetry/configuration.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <datadog/config.h>
#include <datadog/environment.h>
#include <datadog/telemetry/configuration.h>
#include <datadog/version.h>

#include "parse_util.h"

Expand All @@ -25,7 +26,7 @@ tracing::Expected<Configuration> load_telemetry_env_config() {

if (auto metrics_interval_seconds =
lookup(environment::DD_TELEMETRY_METRICS_INTERVAL_SECONDS)) {
auto maybe_value = parse_uint64(*metrics_interval_seconds, 10);
auto maybe_value = parse_double(*metrics_interval_seconds);
if (auto error = maybe_value.if_error()) {
return *error;
}
Expand All @@ -34,7 +35,7 @@ tracing::Expected<Configuration> load_telemetry_env_config() {

if (auto heartbeat_interval_seconds =
lookup(environment::DD_TELEMETRY_HEARTBEAT_INTERVAL)) {
auto maybe_value = parse_uint64(*heartbeat_interval_seconds, 10);
auto maybe_value = parse_double(*heartbeat_interval_seconds);
if (auto error = maybe_value.if_error()) {
return *error;
}
Expand Down Expand Up @@ -81,30 +82,35 @@ tracing::Expected<FinalizedConfiguration> finalize_config(
// metrics_interval_seconds
auto metrics_interval = pick(env_config->metrics_interval_seconds,
user_config.metrics_interval_seconds, 60);
if (metrics_interval.second <= 0) {
// TBD
return Error{};
if (metrics_interval.second <= 0.) {
return Error{Error::Code::OUT_OF_RANGE_INTEGER,
"Telemetry metrics polling interval must be a positive value"};
}
result.metrics_interval = std::chrono::seconds(metrics_interval.second);
result.metrics_interval =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::duration<double>(metrics_interval.second));

// heartbeat_interval_seconds
auto heartbeat_interval = pick(env_config->heartbeat_interval_seconds,
user_config.heartbeat_interval_seconds, 10);
if (heartbeat_interval.second <= 0) {
// TBD
return Error{};
if (heartbeat_interval.second <= 0.) {
return Error{
Error::Code::OUT_OF_RANGE_INTEGER,
"Telemetry heartbeat polling interval must be a positive value"};
}
result.heartbeat_interval = std::chrono::seconds(heartbeat_interval.second);
result.heartbeat_interval =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::duration<double>(heartbeat_interval.second));

// integration_name
std::tie(origin, result.integration_name) =
pick(env_config->integration_name, user_config.integration_name,
std::string(""));
std::string("datadog"));

// integration_version
std::tie(origin, result.integration_version) =
pick(env_config->integration_version, user_config.integration_version,
std::string(""));
tracing::tracer_version);

return result;
}
Expand Down
9 changes: 5 additions & 4 deletions src/datadog/tracer_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,11 @@ Expected<FinalizedTracerConfig> finalize_config(const TracerConfig &user_config,
to_string(final_config.delegate_trace_sampling), origin);

// Integration name & version
final_config.integration_name =
value_or(env_config->integration_name, user_config.integration_name, "");
final_config.integration_version = value_or(
env_config->integration_version, user_config.integration_version, "");
final_config.integration_name = value_or(
env_config->integration_name, user_config.integration_name, "datadog");
final_config.integration_version =
value_or(env_config->integration_version, user_config.integration_version,
tracer_version);

if (user_config.runtime_id) {
final_config.runtime_id = user_config.runtime_id;
Expand Down
3 changes: 0 additions & 3 deletions src/datadog/tracer_telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ TracerTelemetry::TracerTelemetry(
integration_version_(integration_version),
user_metrics_(user_metrics) {
if (enabled_) {
if (integration_name_.empty()) {
integration_name_ = "datadog";
}
// Register all the metrics that we're tracking by adding them to the
// metrics_snapshots_ container. This allows for simpler iteration logic
// when using the values in `generate-metrics` messages.
Expand Down
4 changes: 2 additions & 2 deletions test/test_tracer_telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TEST_CASE("Tracer telemetry", "[telemetry]") {
auto app_started = nlohmann::json::parse(app_started_message);
REQUIRE(is_valid_telemetry_payload(app_started) == true);
REQUIRE(app_started["request_type"] == "message-batch");
REQUIRE(app_started["payload"].size() == 2);
REQUIRE(app_started["payload"].size() == 1);

auto& app_started_payload = app_started["payload"][0];
CHECK(app_started_payload["request_type"] == "app-started");
Expand Down Expand Up @@ -89,7 +89,7 @@ TEST_CASE("Tracer telemetry", "[telemetry]") {
REQUIRE(is_valid_telemetry_payload(app_started) == true);
REQUIRE(app_started["request_type"] == "message-batch");
REQUIRE(app_started["payload"].is_array());
REQUIRE(app_started["payload"].size() == 2);
REQUIRE(app_started["payload"].size() == 1);

auto& app_started_payload = app_started["payload"][0];
CHECK(app_started_payload["request_type"] == "app-started");
Expand Down

0 comments on commit bc70e5a

Please sign in to comment.