-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathspan_sampler_config.cpp
More file actions
367 lines (325 loc) · 12 KB
/
span_sampler_config.cpp
File metadata and controls
367 lines (325 loc) · 12 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
#include <datadog/environment.h>
#include <datadog/expected.h>
#include <datadog/span_sampler_config.h>
#include <cmath>
#include <fstream>
#include <sstream>
#include <unordered_set>
#include "json.hpp"
#include "json_serializer.h"
#include "stable_config.h"
namespace datadog {
namespace tracing {
namespace {
// Parse a stable config JSON string as an array of sampling rules.
// `customize_rule` is a callable that receives (Rule&, const json_rule&) to set
// rule-specific fields beyond the base matcher and sample_rate.
// Returns nullopt on any parse error (stable config errors are non-fatal).
template <typename Rule, typename Json, typename Customize>
Optional<std::vector<Rule>> parse_stable_config_rules(
const StableConfig &cfg, const std::string &key, Logger &logger,
Customize customize_rule) {
auto val = cfg.lookup(key);
if (!val || val->empty()) return nullopt;
try {
auto json_rules = Json::parse(*val);
if (!json_rules.is_array()) {
logger.log_error([&key](std::ostream &log) {
log << "Unable to parse JSON sampling rules from " << key
<< ": expected a JSON array";
});
return nullopt;
}
std::vector<Rule> rules;
for (const auto &json_rule : json_rules) {
auto matcher = from_json(json_rule);
if (matcher.if_error()) {
logger.log_error([&key](std::ostream &log) {
log << "Unable to parse JSON sampling rules from " << key
<< ": invalid rule matcher";
});
return nullopt;
}
Rule rule{*matcher};
if (auto sr = json_rule.find("sample_rate");
sr != json_rule.end() && sr->is_number()) {
rule.sample_rate = *sr;
}
customize_rule(rule, json_rule);
rules.emplace_back(std::move(rule));
}
return rules;
} catch (...) {
logger.log_error([&key](std::ostream &log) {
log << "Unable to parse JSON sampling rules from " << key;
});
return nullopt;
}
}
std::string to_string(const std::vector<SpanSamplerConfig::Rule> &rules) {
nlohmann::json res;
for (const auto &r : rules) {
nlohmann::json j = r;
j["sample_rate"] = r.sample_rate;
if (r.max_per_second) {
j["max_per_second"] = *r.max_per_second;
}
res.emplace_back(std::move(j));
}
return res.dump();
}
// `env_var` is the name of the environment variable from which `rules_raw` was
// obtained. It's used for error messages.
Expected<std::vector<SpanSamplerConfig::Rule>> parse_rules(StringView rules_raw,
StringView env_var) {
std::vector<SpanSamplerConfig::Rule> rules;
nlohmann::json json_rules;
try {
json_rules = nlohmann::json::parse(rules_raw);
} catch (const nlohmann::json::parse_error &error) {
std::string message;
message += "Unable to parse JSON from ";
append(message, env_var);
message += " value ";
append(message, rules_raw);
message += ": ";
message += error.what();
return Error{Error::SPAN_SAMPLING_RULES_INVALID_JSON, std::move(message)};
}
std::string type = json_rules.type_name();
if (type != "array") {
std::string message;
message += "Trace sampling rules must be an array, but JSON in ";
append(message, env_var);
message += " has type \"";
message += type;
message += "\": ";
append(message, rules_raw);
return Error{Error::SPAN_SAMPLING_RULES_WRONG_TYPE, std::move(message)};
}
const std::unordered_set<std::string> allowed_properties{
"service", "name", "resource", "tags", "sample_rate", "max_per_second"};
for (const auto &json_rule : json_rules) {
auto matcher = from_json(json_rule);
if (auto *error = matcher.if_error()) {
std::string prefix;
prefix += "Unable to create a rule from ";
append(prefix, env_var);
prefix += " JSON ";
append(prefix, rules_raw);
prefix += ": ";
return error->with_prefix(prefix);
}
SpanSamplerConfig::Rule rule{*matcher};
auto sample_rate = json_rule.find("sample_rate");
if (sample_rate != json_rule.end()) {
type = sample_rate->type_name();
if (type != "number") {
std::string message;
message += "Unable to parse a rule from ";
append(message, env_var);
message += " JSON ";
append(message, rules_raw);
message += ". The \"sample_rate\" property of the rule ";
message += json_rule.dump();
message += " is not a number, but instead has type \"";
message += type;
message += "\".";
return Error{Error::SPAN_SAMPLING_RULES_SAMPLE_RATE_WRONG_TYPE,
std::move(message)};
}
rule.sample_rate = *sample_rate;
}
auto max_per_second = json_rule.find("max_per_second");
if (max_per_second != json_rule.end()) {
type = max_per_second->type_name();
if (type != "number") {
std::string message;
message += "Unable to parse a rule from ";
append(message, env_var);
message += " JSON ";
append(message, rules_raw);
message += ". The \"max_per_second\" property of the rule ";
message += json_rule.dump();
message += " is not a number, but instead has type \"";
message += type;
message += "\".";
return Error{Error::SPAN_SAMPLING_RULES_MAX_PER_SECOND_WRONG_TYPE,
std::move(message)};
}
rule.max_per_second = *max_per_second;
}
// Look for unexpected properties.
for (const auto &[key, value] : json_rule.items()) {
if (allowed_properties.count(key)) {
continue;
}
std::string message;
message += "Unexpected property \"";
message += key;
message += "\" having value ";
message += value.dump();
message += " in trace sampling rule ";
message += json_rule.dump();
message += ". Error occurred while parsing from ";
append(message, env_var);
message += ": ";
append(message, rules_raw);
return Error{Error::SPAN_SAMPLING_RULES_UNKNOWN_PROPERTY,
std::move(message)};
}
rules.emplace_back(std::move(rule));
}
return rules;
}
Expected<SpanSamplerConfig> load_span_sampler_env_config(Logger &logger) {
SpanSamplerConfig env_config;
auto rules_env = lookup(environment::DD_SPAN_SAMPLING_RULES);
if (rules_env) {
auto maybe_rules =
parse_rules(*rules_env, name(environment::DD_SPAN_SAMPLING_RULES));
if (auto *error = maybe_rules.if_error()) {
return std::move(*error);
}
env_config.rules = std::move(*maybe_rules);
}
if (auto file_env = lookup(environment::DD_SPAN_SAMPLING_RULES_FILE)) {
if (rules_env) {
const auto rules_file_name =
name(environment::DD_SPAN_SAMPLING_RULES_FILE);
const auto rules_name = name(environment::DD_SPAN_SAMPLING_RULES);
std::string message;
append(message, rules_file_name);
message += " is overridden by ";
append(message, rules_name);
message += ". Since both are set, ";
append(message, rules_name);
message += " takes precedence, and ";
append(message, rules_file_name);
message += " will be ignored.";
logger.log_error(message);
} else {
const auto span_rules_file = std::string(*file_env);
const auto file_error = [&](const char *operation) {
std::string message;
message += "Unable to ";
message += operation;
message += " file \"";
message += span_rules_file;
message += "\" specified as value of environment variable ";
append(message, name(environment::DD_SPAN_SAMPLING_RULES_FILE));
return Error{Error::SPAN_SAMPLING_RULES_FILE_IO, std::move(message)};
};
std::ifstream file(span_rules_file);
if (!file) {
return file_error("open");
}
std::ostringstream rules_stream;
rules_stream << file.rdbuf();
if (!file) {
return file_error("read");
}
auto maybe_rules = parse_rules(
rules_stream.str(), name(environment::DD_SPAN_SAMPLING_RULES_FILE));
if (auto *error = maybe_rules.if_error()) {
std::string prefix;
prefix += "With ";
append(prefix, name(environment::DD_SPAN_SAMPLING_RULES_FILE));
prefix += '=';
append(prefix, *file_env);
prefix += ": ";
return error->with_prefix(prefix);
}
env_config.rules = std::move(*maybe_rules);
}
}
return env_config;
}
} // namespace
SpanSamplerConfig::Rule::Rule(const SpanMatcher &base) : SpanMatcher(base) {}
Expected<FinalizedSpanSamplerConfig> finalize_config(
const SpanSamplerConfig &user_config, Logger &logger,
const StableConfigs *stable_configs) {
Expected<SpanSamplerConfig> env_config = load_span_sampler_env_config(logger);
if (auto error = env_config.if_error()) {
return *error;
}
FinalizedSpanSamplerConfig result;
Optional<std::vector<SpanSamplerConfig::Rule>> env_rules;
Optional<std::vector<SpanSamplerConfig::Rule>> user_rules;
if (!env_config->rules.empty()) {
env_rules = env_config->rules;
}
if (!user_config.rules.empty()) {
user_rules = user_config.rules;
}
Optional<std::vector<SpanSamplerConfig::Rule>> fleet_rules;
Optional<std::vector<SpanSamplerConfig::Rule>> local_rules;
if (stable_configs) {
auto parse_span_rules = [&logger](const StableConfig &cfg,
const std::string &key) {
return parse_stable_config_rules<SpanSamplerConfig::Rule, nlohmann::json>(
cfg, key, logger,
[](SpanSamplerConfig::Rule &rule, const nlohmann::json &json_rule) {
if (auto mps = json_rule.find("max_per_second");
mps != json_rule.end() && mps->is_number()) {
rule.max_per_second = *mps;
}
});
};
fleet_rules =
parse_span_rules(stable_configs->fleet, "DD_SPAN_SAMPLING_RULES");
local_rules =
parse_span_rules(stable_configs->local, "DD_SPAN_SAMPLING_RULES");
}
std::vector<SpanSamplerConfig::Rule> rules = resolve_and_record_config(
fleet_rules, env_rules, user_rules, local_rules, &result.metadata,
ConfigName::SPAN_SAMPLING_RULES, nullptr,
[](const std::vector<SpanSamplerConfig::Rule> &r) {
return to_string(r);
});
for (const auto &rule : rules) {
auto maybe_rate = Rate::from(rule.sample_rate);
if (auto *error = maybe_rate.if_error()) {
std::string prefix;
prefix +=
"Unable to parse sample_rate in span sampling rule with span "
"pattern ";
prefix += nlohmann::json(static_cast<SpanMatcher>(rule)).dump();
prefix += ": ";
return error->with_prefix(prefix);
}
const auto allowed_types = {FP_NORMAL, FP_SUBNORMAL};
if (rule.max_per_second &&
(!(*rule.max_per_second > 0) ||
std::find(std::begin(allowed_types), std::end(allowed_types),
std::fpclassify(*rule.max_per_second)) ==
std::end(allowed_types))) {
std::string message;
message += "Span sampling rule with pattern ";
message += nlohmann::json(static_cast<SpanMatcher>(rule)).dump();
message +=
" should have a max_per_second value greater than zero, but the "
"following value was given: ";
message += std::to_string(*rule.max_per_second);
return Error{Error::MAX_PER_SECOND_OUT_OF_RANGE, std::move(message)};
}
FinalizedSpanSamplerConfig::Rule finalized;
static_cast<SpanMatcher &>(finalized) = rule;
finalized.sample_rate = *maybe_rate;
finalized.max_per_second = rule.max_per_second;
result.rules.push_back(std::move(finalized));
}
return result;
}
std::string to_string(const FinalizedSpanSamplerConfig::Rule &rule) {
// Get the base class's fields, then add our own.
nlohmann::json result = static_cast<const SpanMatcher &>(rule);
result["sample_rate"] = double(rule.sample_rate);
if (rule.max_per_second) {
result["max_per_second"] = *rule.max_per_second;
}
return result.dump();
}
} // namespace tracing
} // namespace datadog