Skip to content

Commit d57bcfe

Browse files
authored
feat: introduce the option to disable Remote Configuration (#106)
Disable Remote Configuration programmatically or using `DD_REMOTE_CONFIGURATION_ENABLED=0`. - Add support for `DD_REMOTE_CONFIGURATION_ENABLED` environment variable - Cease periodic request for Remote Configuration when the feature is disabled. - Store and cancel scheduled tasks all at once. - chore: Relocate string utility functions from `parse_util` to `string_util`.
1 parent 2ea1f8f commit d57bcfe

18 files changed

+85
-78
lines changed

src/datadog/curl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "http_client.h"
2020
#include "json.hpp"
2121
#include "logger.h"
22-
#include "parse_util.h"
22+
#include "string_util.h"
2323
#include "string_view.h"
2424

2525
namespace datadog {
@@ -438,8 +438,8 @@ std::size_t CurlImpl::on_read_header(char *data, std::size_t,
438438
return length;
439439
}
440440

441-
const auto key = strip(range(begin, colon));
442-
const auto value = strip(range(colon + 1, end));
441+
const auto key = trim(range(begin, colon));
442+
const auto value = trim(range(colon + 1, end));
443443

444444
std::string key_lower;
445445
key_lower.reserve(key.size());

src/datadog/datadog_agent.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,16 @@ DatadogAgent::DatadogAgent(
160160
remote_configuration_endpoint_(remote_configuration_endpoint(config.url)),
161161
http_client_(config.http_client),
162162
event_scheduler_(config.event_scheduler),
163-
cancel_scheduled_flush_(event_scheduler_->schedule_recurring_event(
164-
config.flush_interval, [this]() { flush(); })),
165163
flush_interval_(config.flush_interval),
166164
request_timeout_(config.request_timeout),
167165
shutdown_timeout_(config.shutdown_timeout),
168166
remote_config_(tracer_signature, config_manager) {
169167
assert(logger_);
170168
assert(tracer_telemetry_);
169+
170+
tasks_.emplace_back(event_scheduler_->schedule_recurring_event(
171+
config.flush_interval, [this]() { flush(); }));
172+
171173
if (tracer_telemetry_->enabled()) {
172174
// Callback for successful telemetry HTTP requests, to examine HTTP
173175
// status.
@@ -194,35 +196,39 @@ DatadogAgent::DatadogAgent(
194196
// Every 10 seconds, have the tracer telemetry capture the metrics
195197
// values. Every 60 seconds, also report those values to the datadog
196198
// agent.
197-
cancel_telemetry_timer_ = event_scheduler_->schedule_recurring_event(
199+
tasks_.emplace_back(event_scheduler_->schedule_recurring_event(
198200
std::chrono::seconds(10), [this, n = 0]() mutable {
199201
n++;
200202
tracer_telemetry_->capture_metrics();
201203
if (n % 6 == 0) {
202204
send_heartbeat_and_telemetry();
203205
}
204-
});
206+
}));
205207
}
206208

207-
cancel_remote_configuration_task_ =
208-
event_scheduler_->schedule_recurring_event(
209-
config.remote_configuration_poll_interval,
210-
[this] { get_and_apply_remote_configuration_updates(); });
209+
if (config.remote_configuration_enabled) {
210+
tasks_.emplace_back(event_scheduler_->schedule_recurring_event(
211+
config.remote_configuration_poll_interval,
212+
[this] { get_and_apply_remote_configuration_updates(); }));
213+
}
211214
}
212215

213216
DatadogAgent::~DatadogAgent() {
214217
const auto deadline = clock_().tick + shutdown_timeout_;
215-
cancel_scheduled_flush_();
218+
219+
for (auto&& cancel_task : tasks_) {
220+
cancel_task();
221+
}
222+
216223
flush();
217-
cancel_remote_configuration_task_();
224+
218225
if (tracer_telemetry_->enabled()) {
219-
// This action only needs to occur if tracer telemetry is enabled.
220-
cancel_telemetry_timer_();
221226
tracer_telemetry_->capture_metrics();
222227
// The app-closing message is bundled with a message containing the
223228
// final metric values.
224229
send_app_closing();
225230
}
231+
226232
http_client_->drain(deadline);
227233
}
228234

src/datadog/datadog_agent.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ class DatadogAgent : public Collector {
4646
HTTPClient::URL remote_configuration_endpoint_;
4747
std::shared_ptr<HTTPClient> http_client_;
4848
std::shared_ptr<EventScheduler> event_scheduler_;
49-
EventScheduler::Cancel cancel_scheduled_flush_;
50-
EventScheduler::Cancel cancel_telemetry_timer_;
51-
EventScheduler::Cancel cancel_remote_configuration_task_;
49+
std::vector<EventScheduler::Cancel> tasks_;
5250
std::chrono::steady_clock::duration flush_interval_;
5351
// Callbacks for submitting telemetry data
5452
HTTPClient::ResponseHandler telemetry_on_response_;

src/datadog/datadog_agent_config.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace tracing {
1515
Expected<DatadogAgentConfig> load_datadog_agent_env_config() {
1616
DatadogAgentConfig env_config;
1717

18+
if (auto rc_enabled = lookup(environment::DD_REMOTE_CONFIGURATION_ENABLED)) {
19+
env_config.remote_configuration_enabled = !falsy(*rc_enabled);
20+
}
21+
1822
if (auto raw_rc_poll_interval_value =
1923
lookup(environment::DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS)) {
2024
auto res = parse_int(*raw_rc_poll_interval_value, 10);
@@ -122,6 +126,10 @@ Expected<FinalizedDatadogAgentConfig> finalize_config(
122126
"positive number of seconds."};
123127
}
124128

129+
result.remote_configuration_enabled =
130+
value_or(env_config->remote_configuration_enabled,
131+
user_config.remote_configuration_enabled, true);
132+
125133
const auto [origin, url] =
126134
pick(env_config->url, user_config.url, "http://localhost:8126");
127135
auto parsed_url = HTTPClient::URL::parse(url);

src/datadog/datadog_agent_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ struct DatadogAgentConfig {
5656
Optional<int> request_timeout_milliseconds;
5757
// Maximum amount of time the process is allowed to wait before shutting down.
5858
Optional<int> shutdown_timeout_milliseconds;
59+
// Enable the capability that allows to remotely configure and change the
60+
// behavior of the tracer.
61+
Optional<bool> remote_configuration_enabled;
5962
// How often, in seconds, to query the Datadog Agent for remote configuration
6063
// updates.
6164
Optional<int> remote_configuration_poll_interval_seconds;
@@ -71,6 +74,7 @@ class FinalizedDatadogAgentConfig {
7174

7275
public:
7376
Clock clock;
77+
bool remote_configuration_enabled;
7478
std::shared_ptr<HTTPClient> http_client;
7579
std::shared_ptr<EventScheduler> event_scheduler;
7680
HTTPClient::URL url;

src/datadog/environment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace environment {
3030
MACRO(DD_INSTRUMENTATION_TELEMETRY_ENABLED) \
3131
MACRO(DD_PROPAGATION_STYLE_EXTRACT) \
3232
MACRO(DD_PROPAGATION_STYLE_INJECT) \
33+
MACRO(DD_REMOTE_CONFIGURATION_ENABLED) \
3334
MACRO(DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS) \
3435
MACRO(DD_SERVICE) \
3536
MACRO(DD_SPAN_SAMPLING_RULES) \

src/datadog/extraction_util.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "json.hpp"
1111
#include "logger.h"
1212
#include "parse_util.h"
13+
#include "string_util.h"
1314
#include "tag_propagation.h"
1415
#include "tags.h"
1516

src/datadog/http_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "http_client.h"
22

3-
#include "parse_util.h"
3+
#include "string_util.h"
44

55
namespace datadog {
66
namespace tracing {

src/datadog/parse_util.cpp

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "parse_util.h"
22

33
#include <algorithm>
4-
#include <cassert>
54
#include <cctype>
65
#include <charconv>
7-
#include <iterator>
6+
#include <cstddef>
7+
#include <cstdint>
88
#include <sstream>
99
#include <string>
1010

@@ -44,20 +44,10 @@ Expected<Integer> parse_integer(StringView input, int base, StringView kind) {
4444

4545
} // namespace
4646

47-
StringView strip(StringView input) {
48-
const auto not_whitespace = [](unsigned char ch) {
49-
return !std::isspace(ch);
50-
};
51-
const char *const begin =
52-
std::find_if(input.begin(), input.end(), not_whitespace);
53-
const char *const end =
54-
std::find_if(input.rbegin(), std::make_reverse_iterator(begin),
55-
not_whitespace)
56-
.base();
57-
58-
assert(begin <= end);
59-
60-
return StringView{begin, std::size_t(end - begin)};
47+
bool falsy(StringView input) {
48+
auto lower = std::string{input};
49+
to_lower(lower);
50+
return lower == "0" || lower == "false" || lower == "no";
6151
}
6252

6353
Expected<std::uint64_t> parse_uint64(StringView input, int base) {
@@ -99,27 +89,13 @@ Expected<double> parse_double(StringView input) {
9989
return value;
10090
}
10191

102-
bool starts_with(StringView subject, StringView prefix) {
103-
if (prefix.size() > subject.size()) {
104-
return false;
105-
}
106-
107-
return std::mismatch(subject.begin(), subject.end(), prefix.begin()).second ==
108-
prefix.end();
109-
}
110-
111-
void to_lower(std::string &text) {
112-
std::transform(text.begin(), text.end(), text.begin(),
113-
[](unsigned char ch) { return std::tolower(ch); });
114-
}
115-
11692
// List items are separated by an optional comma (",") and any amount of
11793
// whitespace.
11894
// Leading and trailing whitespace is ignored.
11995
std::vector<StringView> parse_list(StringView input) {
12096
using uchar = unsigned char;
12197

122-
input = strip(input);
98+
input = trim(input);
12399
std::vector<StringView> items;
124100
if (input.empty()) {
125101
return items;

src/datadog/parse_util.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
// This component provides parsing-related miscellanea.
44

5-
#include <cstddef>
6-
#include <cstdint>
5+
#include <string>
76
#include <unordered_map>
87
#include <vector>
98

@@ -13,20 +12,16 @@
1312
namespace datadog {
1413
namespace tracing {
1514

16-
// Return a `string_view` over the specified range of characters `[begin, end)`.
17-
inline StringView range(const char* begin, const char* end) {
18-
return StringView{begin, std::size_t(end - begin)};
19-
}
20-
21-
// Remove leading and trailing whitespace (as determined by `std::isspace`) from
22-
// the specified `input`.
23-
StringView strip(StringView input);
15+
bool falsy(StringView input);
2416

2517
// Return a non-negative integer parsed from the specified `input` with respect
2618
// to the specified `base`, or return an `Error` if no such integer can be
2719
// parsed. It is an error unless all of `input` is consumed by the parse.
2820
// Leading and trailing whitespace are not ignored.
2921
Expected<std::uint64_t> parse_uint64(StringView input, int base);
22+
23+
// Return an integer parsed from the specified `input` with respect to the
24+
// specified `base`, or return an `Error` if no such integer can be parsed.
3025
Expected<int> parse_int(StringView input, int base);
3126

3227
// Return a floating point number parsed from the specified `input`, or return
@@ -35,12 +30,6 @@ Expected<int> parse_int(StringView input, int base);
3530
// ignored.
3631
Expected<double> parse_double(StringView input);
3732

38-
// Return whether the specified `prefix` is a prefix of the specified `subject`.
39-
bool starts_with(StringView subject, StringView prefix);
40-
41-
// Convert the specified `text` to lower case in-place.
42-
void to_lower(std::string& text);
43-
4433
// List items are separated by an optional comma (",") and any amount of
4534
// whitespace.
4635
// Leading and trailing whitespace are ignored.

0 commit comments

Comments
 (0)