-
Notifications
You must be signed in to change notification settings - Fork 16
feat(telemetry): implement app-extended-heartbeat event #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
445f045
33bede8
d29b065
f42f6d8
c0cab71
6766649
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,6 +227,11 @@ void Telemetry::schedule_tasks() { | |
| tasks_.emplace_back(scheduler_->schedule_recurring_event( | ||
| config_.metrics_interval, [this]() mutable { capture_metrics(); })); | ||
| } | ||
|
|
||
| tasks_.emplace_back(scheduler_->schedule_recurring_event( | ||
| config_.extended_heartbeat_interval, [this]() { | ||
| send_payload("app-extended-heartbeat", extended_heartbeat_payload()); | ||
| })); | ||
| } | ||
|
|
||
| Telemetry::~Telemetry() { | ||
|
|
@@ -678,6 +683,28 @@ std::string Telemetry::app_started_payload() { | |
| return batch.dump(); | ||
| } | ||
|
|
||
| std::string Telemetry::extended_heartbeat_payload() { | ||
| auto configuration_json = nlohmann::json::array(); | ||
|
|
||
| for (const auto& product : config_.products) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| for (const auto& [_, config_metadatas] : product.configurations) { | ||
| for (const auto& config_metadata : config_metadatas) { | ||
| configuration_json.emplace_back( | ||
| generate_configuration_field(config_metadata)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure you want to call this function here, as it will increment the seq-id as if a new configuration had been added, maybe you want to split the function in 2, encode the field, and increment for new configs, like so https://github.com/DataDog/dd-trace-cpp/pull/289/changes#diff-8e4b8c344253799b7a41954c017a79f7b026dca44849dc2dec9460120dc57a53R800 |
||
| } | ||
| } | ||
| } | ||
|
|
||
| auto extended_hb_msg = nlohmann::json{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should integrations too be sent? eg https://github.com/DataDog/dd-trace-cpp/pull/289/changes#diff-8e4b8c344253799b7a41954c017a79f7b026dca44849dc2dec9460120dc57a53R745 |
||
| {"request_type", "app-extended-heartbeat"}, | ||
| {"payload", nlohmann::json{{"configuration", configuration_json}}}, | ||
| }; | ||
|
|
||
| auto batch = generate_telemetry_body("message-batch"); | ||
| batch["payload"] = nlohmann::json::array({std::move(extended_hb_msg)}); | ||
| return batch.dump(); | ||
| } | ||
|
|
||
| nlohmann::json Telemetry::generate_telemetry_body(std::string request_type) { | ||
| std::time_t tracer_time = std::chrono::duration_cast<std::chrono::seconds>( | ||
| clock_().wall.time_since_epoch()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,19 +46,27 @@ struct FakeEventScheduler : public EventScheduler { | |
| size_t count_tasks = 0; | ||
| std::function<void()> heartbeat_callback = nullptr; | ||
| std::function<void()> metrics_callback = nullptr; | ||
| std::function<void()> extended_heartbeat_callback = nullptr; | ||
| Optional<std::chrono::steady_clock::duration> heartbeat_interval; | ||
| Optional<std::chrono::steady_clock::duration> metrics_interval; | ||
| Optional<std::chrono::steady_clock::duration> extended_heartbeat_interval; | ||
| bool cancelled = false; | ||
|
|
||
| // NOTE: White box testing. This is a limitation of the event scheduler API. | ||
| // Tasks are registered in order: heartbeat (0), metrics (1, if enabled), | ||
| // extended heartbeat (last). | ||
| Cancel schedule_recurring_event(std::chrono::steady_clock::duration interval, | ||
| std::function<void()> callback) override { | ||
| if (count_tasks == 0) { | ||
| heartbeat_callback = callback; | ||
| heartbeat_interval = interval; | ||
| } else if (count_tasks == 1) { | ||
| } else if (interval <= std::chrono::minutes(1)) { | ||
| // Metrics interval is <= 60s; extended heartbeat is much larger. | ||
| metrics_callback = callback; | ||
| metrics_interval = interval; | ||
| } else { | ||
| extended_heartbeat_callback = callback; | ||
| extended_heartbeat_interval = interval; | ||
| } | ||
| count_tasks++; | ||
| return [this]() { cancelled = true; }; | ||
|
|
@@ -74,6 +82,11 @@ struct FakeEventScheduler : public EventScheduler { | |
| metrics_callback(); | ||
| } | ||
|
|
||
| void trigger_extended_heartbeat() { | ||
| assert(extended_heartbeat_callback != nullptr); | ||
| extended_heartbeat_callback(); | ||
| } | ||
|
|
||
| std::string config() const override { | ||
| return nlohmann::json::object({{"type", "FakeEventScheduler"}}).dump(); | ||
| } | ||
|
|
@@ -391,6 +404,49 @@ TELEMETRY_IMPLEMENTATION_TEST("Tracer telemetry API") { | |
| REQUIRE(find_payload(message_batch["payload"], "app-heartbeat")); | ||
| } | ||
|
|
||
| SECTION("generates an extended heartbeat with configuration") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you change the above to also capture configuration changes, maybe you want to test it later with remote config changes like so https://github.com/DataDog/dd-trace-cpp/pull/289/changes#diff-6a69962f102d55319c4c00418c82707a9c13a11fe0f75195e6b60fd50da7627aR914 |
||
| client->clear(); | ||
|
|
||
| Product product; | ||
| product.name = Product::Name::tracing; | ||
| product.enabled = true; | ||
| product.version = tracer_version; | ||
| product.configurations = | ||
| std::unordered_map<ConfigName, std::vector<ConfigMetadata>>{ | ||
| {ConfigName::SERVICE_NAME, | ||
| {ConfigMetadata(ConfigName::SERVICE_NAME, "my-service", | ||
| ConfigMetadata::Origin::CODE)}}, | ||
| }; | ||
|
|
||
| Configuration cfg; | ||
| cfg.products.emplace_back(std::move(product)); | ||
|
|
||
| auto scheduler2 = std::make_shared<FakeEventScheduler>(); | ||
| Telemetry telemetry2{*finalize_config(cfg), | ||
| tracer_signature, | ||
| logger, | ||
| client, | ||
| scheduler2, | ||
| *url}; | ||
|
|
||
| client->clear(); | ||
| scheduler2->trigger_extended_heartbeat(); | ||
|
|
||
| auto message_batch = nlohmann::json::parse(client->request_body); | ||
| REQUIRE(is_valid_telemetry_payload(message_batch)); | ||
|
|
||
| auto ext_hb = | ||
| find_payload(message_batch["payload"], "app-extended-heartbeat"); | ||
| REQUIRE(ext_hb.has_value()); | ||
|
|
||
| auto& config_payload = (*ext_hb)["payload"]["configuration"]; | ||
| REQUIRE(config_payload.is_array()); | ||
| REQUIRE(config_payload.size() == 1); | ||
| CHECK(config_payload[0]["name"] == "service"); | ||
| CHECK(config_payload[0]["value"] == "my-service"); | ||
| CHECK(config_payload[0]["origin"] == "code"); | ||
| } | ||
|
|
||
| SECTION("metrics reporting") { | ||
| SECTION("counters are correctly serialized in generate-metrics payload") { | ||
| client->clear(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this could be a decimal, so that later on you don't have to static_cast(*maybe_value), but can just deal with a double