-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtrace_sampler_config.cpp
More file actions
267 lines (230 loc) · 9.03 KB
/
Copy pathtrace_sampler_config.cpp
File metadata and controls
267 lines (230 loc) · 9.03 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
#include <datadog/environment.h>
#include <datadog/trace_sampler_config.h>
#include <datadog/trace_source.h>
#include <cmath>
#include <unordered_set>
#include "json.hpp"
#include "json_serializer.h"
#include "parse_util.h"
#include "string_util.h"
#include "tags.h"
namespace datadog {
namespace tracing {
namespace {
Expected<TraceSamplerConfig> load_trace_sampler_env_config() {
TraceSamplerConfig env_config;
if (auto rules_env = lookup(environment::DD_TRACE_SAMPLING_RULES)) {
nlohmann::json json_rules;
try {
json_rules = nlohmann::json::parse(*rules_env);
} catch (const nlohmann::json::parse_error &error) {
std::string message;
message += "Unable to parse JSON from ";
append(message, name(environment::DD_TRACE_SAMPLING_RULES));
message += " value ";
append(message, *rules_env);
message += ": ";
message += error.what();
return Error{Error::TRACE_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 ";
append(message, name(environment::DD_TRACE_SAMPLING_RULES));
message += " has JSON type \"";
message += type;
message += "\": ";
append(message, *rules_env);
return Error{Error::TRACE_SAMPLING_RULES_WRONG_TYPE, std::move(message)};
}
const std::unordered_set<std::string> allowed_properties{
"service", "name", "resource", "tags", "sample_rate"};
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, name(environment::DD_TRACE_SAMPLING_RULES));
prefix += " value ";
append(prefix, *rules_env);
prefix += ": ";
return error->with_prefix(prefix);
}
TraceSamplerConfig::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, name(environment::DD_TRACE_SAMPLING_RULES));
message += " value ";
append(message, *rules_env);
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::TRACE_SAMPLING_RULES_SAMPLE_RATE_WRONG_TYPE,
std::move(message)};
}
rule.sample_rate = *sample_rate;
}
// 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 ";
append(message, name(environment::DD_TRACE_SAMPLING_RULES));
message += ": ";
append(message, *rules_env);
return Error{Error::TRACE_SAMPLING_RULES_UNKNOWN_PROPERTY,
std::move(message)};
}
env_config.rules.emplace_back(std::move(rule));
}
}
if (auto sample_rate_env = lookup(environment::DD_TRACE_SAMPLE_RATE)) {
auto maybe_sample_rate = parse_double(*sample_rate_env);
if (auto *error = maybe_sample_rate.if_error()) {
std::string prefix;
prefix += "While parsing ";
append(prefix, name(environment::DD_TRACE_SAMPLE_RATE));
prefix += ": ";
return error->with_prefix(prefix);
}
env_config.sample_rate = *maybe_sample_rate;
}
if (auto limit_env = lookup(environment::DD_TRACE_RATE_LIMIT)) {
auto maybe_max_per_second = parse_double(*limit_env);
if (auto *error = maybe_max_per_second.if_error()) {
std::string prefix;
prefix += "While parsing ";
append(prefix, name(environment::DD_TRACE_RATE_LIMIT));
prefix += ": ";
return error->with_prefix(prefix);
}
env_config.max_per_second = *maybe_max_per_second;
}
return env_config;
}
std::string to_string(const std::vector<TraceSamplerConfig::Rule> &rules) {
nlohmann::json res;
for (const auto &r : rules) {
auto j = nlohmann::json(static_cast<SpanMatcher>(r));
j["sample_rate"] = r.sample_rate;
res.emplace_back(std::move(j));
}
return res.dump();
}
} // namespace
TraceSamplerConfig::Rule::Rule(const SpanMatcher &base) : SpanMatcher(base) {}
Expected<FinalizedTraceSamplerConfig> finalize_config(
const TraceSamplerConfig &config) {
Expected<TraceSamplerConfig> env_config = load_trace_sampler_env_config();
if (auto error = env_config.if_error()) {
return *error;
}
FinalizedTraceSamplerConfig result;
std::vector<TraceSamplerConfig::Rule> rules;
if (!env_config->rules.empty()) {
rules = std::move(env_config->rules);
result.metadata[ConfigName::TRACE_SAMPLING_RULES] = {
ConfigMetadata(ConfigName::TRACE_SAMPLING_RULES, to_string(rules),
ConfigMetadata::Origin::ENVIRONMENT_VARIABLE)};
} else if (!config.rules.empty()) {
rules = std::move(config.rules);
result.metadata[ConfigName::TRACE_SAMPLING_RULES] = {
ConfigMetadata(ConfigName::TRACE_SAMPLING_RULES, to_string(rules),
ConfigMetadata::Origin::CODE)};
}
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 trace sampling rule with root span "
"pattern ";
prefix += nlohmann::json(rule).dump();
prefix += ": ";
return error->with_prefix(prefix);
}
TraceSamplerRule finalized_rule;
finalized_rule.matcher = rule;
finalized_rule.rate = *maybe_rate;
finalized_rule.mechanism = SamplingMechanism::RULE;
result.rules.emplace_back(std::move(finalized_rule));
}
Optional<double> sample_rate = resolve_and_record_config(
env_config->sample_rate, config.sample_rate, &result.metadata,
ConfigName::TRACE_SAMPLING_RATE, 1.0,
[](const double &d) { return to_string(d, 1); });
bool is_sample_rate_provided = env_config->sample_rate || config.sample_rate;
// If `sample_rate` was specified, then it translates to a "catch-all" rule
// appended to the end of `rules`. First, though, we have to make sure the
// sample rate is valid.
if (sample_rate && is_sample_rate_provided) {
auto maybe_rate = Rate::from(*sample_rate);
if (auto *error = maybe_rate.if_error()) {
return error->with_prefix(
"Unable to parse overall sample_rate for trace sampling: ");
}
TraceSamplerRule finalized_rule;
finalized_rule.rate = *maybe_rate;
finalized_rule.matcher = catch_all;
finalized_rule.mechanism = SamplingMechanism::RULE;
result.rules.emplace_back(std::move(finalized_rule));
}
double max_per_second = resolve_and_record_config(
env_config->max_per_second, config.max_per_second, &result.metadata,
ConfigName::TRACE_SAMPLING_LIMIT, 100.0,
[](const double &d) { return std::to_string(d); });
const auto allowed_types = {FP_NORMAL, FP_SUBNORMAL};
if (!(max_per_second > 0) ||
std::find(std::begin(allowed_types), std::end(allowed_types),
std::fpclassify(max_per_second)) == std::end(allowed_types)) {
std::string message;
message +=
"Trace sampling max_per_second must be greater than zero, but the "
"following value was given: ";
message += std::to_string(max_per_second);
return Error{Error::MAX_PER_SECOND_OUT_OF_RANGE, std::move(message)};
}
result.max_per_second = max_per_second;
return result;
}
FinalizedTraceSamplerConfig
FinalizedTraceSamplerConfig::apm_tracing_disabled_config() {
FinalizedTraceSamplerConfig config;
// Set a rule to always keep spans with an appsec trace source.
TraceSamplerRule appsec_rule;
appsec_rule.rate = Rate::one();
appsec_rule.matcher.tags.emplace(tags::internal::trace_source,
to_tag(Source::appsec));
appsec_rule.mechanism = SamplingMechanism::APP_SEC;
// The rule is not subject to the global limiter.
appsec_rule.bypass_limiter = true;
config.rules.emplace_back(std::move(appsec_rule));
// Set default sampling rate to 1.0. This is balanced by the limiter define
// below.
TraceSamplerRule all;
all.rate = Rate::one();
all.mechanism = SamplingMechanism::DEFAULT;
config.rules.emplace_back(std::move(all));
// For service liveness (services showed in the service catalog for
// example), allow 1 trace/min.
config.max_per_second = 1. / 60;
return config;
}
} // namespace tracing
} // namespace datadog