-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathconfig_manager.cpp
More file actions
466 lines (388 loc) · 14.9 KB
/
Copy pathconfig_manager.cpp
File metadata and controls
466 lines (388 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include "config_manager.h"
#include <datadog/telemetry/telemetry.h>
#include "parse_util.h"
#include "string_util.h"
#include "trace_sampler.h"
namespace datadog {
namespace tracing {
namespace {
nlohmann::json to_json(const SpanDefaults& defaults) {
auto result = nlohmann::json::object({});
#define TO_JSON(FIELD) \
if (!defaults.FIELD.empty()) result[#FIELD] = defaults.FIELD
TO_JSON(service);
TO_JSON(service_type);
TO_JSON(environment);
TO_JSON(version);
TO_JSON(name);
TO_JSON(tags);
#undef TO_JSON
return result;
}
nlohmann::json to_json(const std::vector<TraceSamplerRule>& rules) {
std::vector<nlohmann::json> j;
for (const auto& rule : rules) {
j.push_back(to_json(rule));
}
return j;
}
using Rules = std::vector<TraceSamplerRule>;
using Tags = std::unordered_map<std::string, std::string>;
Expected<Tags> parse_tags_from_sampling_rules(const nlohmann::json& json_tags) {
assert(json_tags.is_array());
Tags tags;
for (const auto& json_tag_entry : json_tags) {
auto key = json_tag_entry.find("key");
if (key == json_tag_entry.cend() || key->is_string() == false) {
std::string err_msg =
"failed to parse tags from sampling rule: the required \"key\" field "
"is either missing "
"or incorrectly formatted. (input: ";
err_msg += json_tags.dump();
err_msg += ")";
return Error{Error::TRACE_SAMPLING_RULES_INVALID_JSON,
std::move(err_msg)};
}
auto value = json_tag_entry.find("value_glob");
if (value == json_tag_entry.cend() || value->is_string() == false) {
std::string err_msg =
"failed to parse tags from sampling rule: the required "
"\"value_glob\" field is either "
"missing or incorrectly formatted. (input: ";
err_msg += json_tags.dump();
err_msg += ")";
return Error{Error::TRACE_SAMPLING_RULES_INVALID_JSON,
std::move(err_msg)};
}
tags.emplace(*key, *value);
}
return tags;
}
Expected<TraceSamplerRule> parse_rule(const nlohmann::json& json_rule) {
if (!json_rule.is_object()) {
std::string err_msg =
"failed to parse sampling rule: expected a JSON object but got a ";
err_msg += json_rule.type_name();
err_msg += " instead. (input: ";
err_msg += json_rule.dump();
err_msg += ")";
return Error{Error::TRACE_SAMPLING_RULES_INVALID_JSON, std::move(err_msg)};
}
auto make_error = [&json_rule](StringView field_name) {
std::string err_msg = "failed to parse sampling rule: the required \"";
append(err_msg, field_name);
err_msg += "\" field is missing. (input: ";
err_msg += json_rule.dump();
err_msg += ")";
return Error{Error::TRACE_SAMPLING_RULES_INVALID_JSON, std::move(err_msg)};
};
const auto make_property_error = [&json_rule](StringView property,
const nlohmann::json& value,
StringView expected_type) {
std::string message;
message += "rule property \"";
append(message, property);
message += "\" should have type \"";
append(message, expected_type);
message += "\", but has type \"";
message += value.type_name();
message += "\": ";
message += value.dump();
message += " in rule ";
message += json_rule.dump();
return Error{Error::RULE_PROPERTY_WRONG_TYPE, std::move(message)};
};
TraceSamplerRule rule;
// Required: service, resource, sample_rate, provenance.
if (auto service = json_rule.find("service"); service != json_rule.cend()) {
if (service->is_string() == false) {
return make_property_error("service", *service, "string");
}
rule.matcher.service = *service;
} else {
return make_error("service");
}
if (auto resource = json_rule.find("resource");
resource != json_rule.cend()) {
if (resource->is_string() == false) {
return make_property_error("resource", *resource, "string");
}
rule.matcher.resource = *resource;
} else {
return make_error("resource");
}
if (auto sample_rate = json_rule.find("sample_rate");
sample_rate != json_rule.end()) {
if (sample_rate->is_number() == false) {
return make_property_error("sample_rate", *sample_rate, "number");
}
auto maybe_rate = Rate::from(*sample_rate);
if (auto error = maybe_rate.if_error()) {
return *error;
}
rule.rate = *maybe_rate;
} else {
return make_error("sample_rate");
}
if (auto provenance_it = json_rule.find("provenance");
provenance_it != json_rule.cend()) {
if (!provenance_it->is_string()) {
return make_property_error("provenance", *provenance_it, "string");
}
auto provenance = to_lower(provenance_it->get<StringView>());
if (provenance == "customer") {
rule.mechanism = SamplingMechanism::REMOTE_RULE;
} else if (provenance == "dynamic") {
rule.mechanism = SamplingMechanism::REMOTE_ADAPTIVE_RULE;
} else {
std::string err_msg = "failed to parse sampling rule: unknown \"";
err_msg += provenance;
err_msg += "\" value. Expected either \"customer\" or \"dynamic\"";
return Error{Error::TRACE_SAMPLING_RULES_UNKNOWN_PROPERTY,
std::move(err_msg)};
}
} else {
return make_error("provenance");
}
// Parse optional fields: name, tags
if (auto name = json_rule.find("name"); name != json_rule.cend()) {
if (name->is_string() == false) {
return make_property_error("name", *name, "string");
}
rule.matcher.name = *name;
}
if (auto tags = json_rule.find("tags"); tags != json_rule.cend()) {
if (tags->is_array() == false) {
return make_property_error("tags", *tags, "array");
}
auto maybe_tags = parse_tags_from_sampling_rules(*tags);
if (auto* error = maybe_tags.if_error()) {
return *error;
}
rule.matcher.tags = std::move(*maybe_tags);
}
return rule;
}
Expected<Rules> parse_trace_sampling_rules(const nlohmann::json& json_rules) {
Rules parsed_rules;
for (const auto& json_rule : json_rules) {
auto maybe_rule = parse_rule(json_rule);
if (auto error = maybe_rule.if_error()) {
return *error;
}
parsed_rules.emplace_back(std::move(*maybe_rule));
}
return parsed_rules;
}
Expected<ConfigManager::Update> parse_dynamic_config(const nlohmann::json& j) {
auto make_err_property_msg = [](StringView field_name,
StringView unexpected_type) {
std::string err_msg{"field \""};
append(err_msg, field_name);
err_msg += "\" expected a float number value but got ";
append(err_msg, unexpected_type);
err_msg += " instead.";
return Error{Error::Code::REMOTE_CONFIGURATION_INVALID_JSON,
std::move(err_msg)};
};
ConfigManager::Update config_update;
if (auto sampling_rate_it = j.find("tracing_sampling_rate");
sampling_rate_it != j.cend() && !sampling_rate_it->is_null()) {
if (!sampling_rate_it->is_number_float()) {
return make_err_property_msg("tracing_sampling_rate",
sampling_rate_it->type_name());
}
auto maybe_rate = Rate::from(sampling_rate_it->get<double>());
if (auto* error = maybe_rate.if_error()) {
return error->with_prefix(
"\"tracing_sampling_rate\" field got an invalid input. ");
}
config_update.trace_sampling_rate = *maybe_rate;
}
if (auto tags_it = j.find("tracing_tags");
tags_it != j.cend() && !tags_it->is_null()) {
if (!tags_it->is_array()) {
return make_err_property_msg("tracing_tags", tags_it->type_name());
}
auto maybe_tags = parse_tags(tags_it->get<std::vector<StringView>>());
if (auto error = maybe_tags.if_error()) {
return *error;
}
config_update.tags = std::move(*maybe_tags);
}
if (auto tracing_enabled_it = j.find("tracing_enabled");
tracing_enabled_it != j.cend() && !tracing_enabled_it->is_null()) {
if (!tracing_enabled_it->is_boolean()) {
return make_err_property_msg("tracing_enabled",
tracing_enabled_it->type_name());
}
config_update.report_traces = tracing_enabled_it->get<bool>();
}
if (auto tracing_sampling_rules_it = j.find("tracing_sampling_rules");
tracing_sampling_rules_it != j.cend() &&
!tracing_sampling_rules_it->is_null()) {
if (!tracing_sampling_rules_it->is_array()) {
return make_err_property_msg("tracing_sampling_rules",
tracing_sampling_rules_it->type_name());
}
auto maybe_sampling_rules =
parse_trace_sampling_rules(*tracing_sampling_rules_it);
if (auto error = maybe_sampling_rules.if_error()) {
return *error;
}
config_update.trace_sampling_rules = std::move(*maybe_sampling_rules);
}
return config_update;
}
} // namespace
namespace rc = datadog::remote_config;
ConfigManager::ConfigManager(const FinalizedTracerConfig& config)
: clock_(config.clock),
trace_sampler_(
std::make_shared<TraceSampler>(config.trace_sampler, clock_)),
rules_(config.trace_sampler.rules),
span_defaults_(std::make_shared<SpanDefaults>(config.defaults)),
report_traces_(config.report_traces) {
// Extract winning value (last entry) from each config's metadata history
for (const auto& [name, metadata_vec] : config.metadata) {
if (!metadata_vec.empty()) {
default_metadata_[name] = metadata_vec.back();
}
}
}
rc::Products ConfigManager::get_products() { return rc::product::APM_TRACING; }
rc::Capabilities ConfigManager::get_capabilities() {
using namespace rc::capability;
return APM_TRACING_SAMPLE_RATE | APM_TRACING_TAGS | APM_TRACING_ENABLED |
APM_TRACING_SAMPLE_RULES;
}
Optional<std::string> ConfigManager::on_update(const Configuration& config) {
if (config.product != rc::product::Flag::APM_TRACING) {
return nullopt;
}
const auto config_json = nlohmann::json::parse(config.content);
auto maybe_config_update = parse_dynamic_config(config_json.at("lib_config"));
if (auto err = maybe_config_update.if_error()) {
return err
->with_prefix("Failed to parse APM remote configuration payload: ")
.message;
}
apply_update(*maybe_config_update);
return nullopt;
}
void ConfigManager::on_revert(const Configuration&) { apply_update({}); }
std::shared_ptr<TraceSampler> ConfigManager::trace_sampler() {
std::lock_guard<std::mutex> lock(mutex_);
return trace_sampler_;
}
std::shared_ptr<const SpanDefaults> ConfigManager::span_defaults() {
std::lock_guard<std::mutex> lock(mutex_);
return span_defaults_.value();
}
bool ConfigManager::report_traces() {
std::lock_guard<std::mutex> lock(mutex_);
return report_traces_.value();
}
void ConfigManager::apply_update(const ConfigManager::Update& conf) {
std::vector<ConfigMetadata> metadata;
// TODO: We're doing too much while holding the lock. Refactor to reduce lock
// contention.
{
std::lock_guard<std::mutex> lock(mutex_);
// NOTE(@dmehala): Sampling rules are generally not well specified.
//
// Rules are evaluated in the order they are inserted, which means the most
// specific matching rule might not be evaluated, even though it should be.
// For now, we must follow this legacy behavior.
//
// Additionally, I exploit this behavior to avoid a merge operation.
// The resulting array can contain duplicate `SpanMatcher`, but only the
// first encountered one will be evaluated, acting as an override.
//
// Remote Configuration rules will/should always be placed at the begining
// of the array, ensuring they are evaluated first.
auto rules = rules_;
if (!conf.trace_sampling_rate) {
auto found = default_metadata_.find(ConfigName::TRACE_SAMPLING_RATE);
if (found != default_metadata_.cend()) {
metadata.push_back(found->second);
}
} else {
ConfigMetadata trace_sampling_metadata(
ConfigName::TRACE_SAMPLING_RATE,
to_string(*conf.trace_sampling_rate, 1),
ConfigMetadata::Origin::REMOTE_CONFIG);
TraceSamplerRule rule;
rule.rate = *conf.trace_sampling_rate;
rule.matcher = catch_all;
rule.mechanism = SamplingMechanism::RULE;
// Convention: Catch-all rules should ALWAYS be the last in the list.
// If a catch-all rule already exists, replace it.
// If NOT, add the new one at the end of the rules list.
if (rules.empty()) {
rules.emplace_back(std::move(rule));
} else {
if (auto& last_rule = rules.back(); last_rule.matcher == catch_all) {
last_rule = rule;
} else {
rules.emplace_back(std::move(rule));
}
}
metadata.emplace_back(std::move(trace_sampling_metadata));
}
if (!conf.trace_sampling_rules) {
auto found = default_metadata_.find(ConfigName::TRACE_SAMPLING_RULES);
if (found != default_metadata_.cend()) {
metadata.emplace_back(found->second);
}
} else {
rules.insert(rules.cbegin(), conf.trace_sampling_rules->begin(),
conf.trace_sampling_rules->end());
ConfigMetadata trace_sampling_rules_metadata(
ConfigName::TRACE_SAMPLING_RULES,
to_json(*conf.trace_sampling_rules).dump(),
ConfigMetadata::Origin::REMOTE_CONFIG);
metadata.emplace_back(std::move(trace_sampling_rules_metadata));
}
trace_sampler_->set_rules(std::move(rules));
if (!conf.tags) {
reset_config(ConfigName::TAGS, span_defaults_, metadata);
} else {
ConfigMetadata tags_metadata(ConfigName::TAGS, join_tags(*conf.tags),
ConfigMetadata::Origin::REMOTE_CONFIG);
if (*conf.tags != span_defaults_.value()->tags) {
auto new_span_defaults =
std::make_shared<SpanDefaults>(*span_defaults_.value());
new_span_defaults->tags = std::move(*conf.tags);
span_defaults_ = new_span_defaults;
metadata.emplace_back(std::move(tags_metadata));
}
}
if (!conf.report_traces) {
reset_config(ConfigName::REPORT_TRACES, report_traces_, metadata);
} else {
if (conf.report_traces != report_traces_.value()) {
report_traces_ = *conf.report_traces;
metadata.emplace_back(ConfigName::REPORT_TRACES,
to_string(*conf.report_traces),
ConfigMetadata::Origin::REMOTE_CONFIG);
}
}
}
telemetry::capture_configuration_change(metadata);
}
template <typename T>
void ConfigManager::reset_config(ConfigName name, T& conf,
std::vector<ConfigMetadata>& metadata) {
if (conf.is_original_value()) return;
conf.reset();
metadata.emplace_back(default_metadata_[name]);
}
nlohmann::json ConfigManager::config_json() const {
std::lock_guard<std::mutex> lock(mutex_);
return nlohmann::json{{"defaults", to_json(*span_defaults_.value())},
{"trace_sampler", trace_sampler_->config_json()},
{"report_traces", report_traces_.value()}};
}
} // namespace tracing
} // namespace datadog