Skip to content

Commit f77d126

Browse files
authored
Implemented tracing sampling by db (#2220)
1 parent 44482f1 commit f77d126

File tree

6 files changed

+219
-81
lines changed

6 files changed

+219
-81
lines changed

ydb/core/cms/console/jaeger_tracing_configurator.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class TJaegerTracingConfigurator : public TActorBootstrapped<TJaegerTracingConfi
3232

3333
void ApplyConfigs(const NKikimrConfig::TTracingConfig& cfg);
3434
static TMaybe<ERequestType> GetRequestType(const NKikimrConfig::TTracingConfig::TSelectors& selectors);
35+
static TMaybe<TString> GetDatabase(const NKikimrConfig::TTracingConfig::TSelectors& selectors);
3536
static TSettings<double, TThrottlingSettings> GetSettings(const NKikimrConfig::TTracingConfig& cfg);
3637

3738
TSamplingThrottlingConfigurator TracingConfigurator;
@@ -85,18 +86,28 @@ TMaybe<ERequestType> TJaegerTracingConfigurator::GetRequestType(const NKikimrCon
8586
return {};
8687
}
8788

89+
TMaybe<TString> TJaegerTracingConfigurator::GetDatabase(const NKikimrConfig::TTracingConfig::TSelectors& selectors) {
90+
if (selectors.HasDatabase()) {
91+
return selectors.GetDatabase();
92+
}
93+
return NothingObject;
94+
}
95+
8896
TSettings<double, TThrottlingSettings> TJaegerTracingConfigurator::GetSettings(const NKikimrConfig::TTracingConfig& cfg) {
8997
TSettings<double, TThrottlingSettings> settings;
9098

9199
for (const auto& samplingRule : cfg.GetSampling()) {
100+
const auto& scope = samplingRule.GetScope();
101+
92102
ERequestType requestType;
93-
if (auto parsedRequestType = GetRequestType(samplingRule.GetScope())) {
103+
if (auto parsedRequestType = GetRequestType(scope)) {
94104
requestType = *parsedRequestType;
95105
} else {
96106
ALOG_ERROR(NKikimrServices::CMS_CONFIGS, "failed to parse request type in the rule "
97107
<< samplingRule.ShortDebugString() << ". Skipping the rule");
98108
continue;
99109
}
110+
100111
if (!samplingRule.HasLevel() || !samplingRule.HasFraction() || !samplingRule.HasMaxTracesPerMinute()) {
101112
ALOG_ERROR(NKikimrServices::CMS_CONFIGS, "missing required fields in rule " << samplingRule.ShortDebugString()
102113
<< " (required fields are: level, fraction, max_traces_per_minute). Skipping the rule");
@@ -129,10 +140,19 @@ TSettings<double, TThrottlingSettings> TJaegerTracingConfigurator::GetSettings(c
129140
.MaxTracesBurst = samplingRule.GetMaxTracesBurst(),
130141
},
131142
};
132-
settings.SamplingRules[static_cast<size_t>(requestType)].push_back(rule);
143+
144+
auto& requestTypeRules = settings.SamplingRules[static_cast<size_t>(requestType)];
145+
auto database = GetDatabase(scope);
146+
if (database) {
147+
requestTypeRules.DatabaseRules[*database].push_back(rule);
148+
} else {
149+
requestTypeRules.Global.push_back(rule);
150+
}
133151
}
134152

135153
for (const auto& throttlingRule : cfg.GetExternalThrottling()) {
154+
const auto& scope = throttlingRule.GetScope();
155+
136156
ERequestType requestType;
137157
if (auto parsedRequestType = GetRequestType(throttlingRule.GetScope())) {
138158
requestType = *parsedRequestType;
@@ -161,14 +181,13 @@ TSettings<double, TThrottlingSettings> TJaegerTracingConfigurator::GetSettings(c
161181
.MaxTracesBurst = maxBurst,
162182
},
163183
};
164-
auto& currentRule = settings.ExternalThrottlingRules[static_cast<size_t>(requestType)];
165-
if (currentRule) {
166-
ALOG_WARN(NKikimrServices::CMS_CONFIGS, "duplicate external throttling rule for scope "
167-
<< throttlingRule.GetScope() << ". Adding the limits");
168-
currentRule->Throttler.MaxTracesBurst += rule.Throttler.MaxTracesBurst;
169-
currentRule->Throttler.MaxTracesPerMinute += rule.Throttler.MaxTracesPerMinute;
184+
185+
auto& requestTypeRules = settings.ExternalThrottlingRules[static_cast<size_t>(requestType)];
186+
auto database = GetDatabase(scope);
187+
if (database) {
188+
requestTypeRules.DatabaseRules[*database].push_back(rule);
170189
} else {
171-
currentRule = rule;
190+
requestTypeRules.Global.push_back(rule);
172191
}
173192
}
174193

@@ -181,7 +200,7 @@ TSettings<double, TThrottlingSettings> TJaegerTracingConfigurator::GetSettings(c
181200
},
182201
};
183202

184-
settings.ExternalThrottlingRules[static_cast<size_t>(ERequestType::UNSPECIFIED)] = rule;
203+
settings.ExternalThrottlingRules[static_cast<size_t>(ERequestType::UNSPECIFIED)].Global.push_back(rule);
185204
}
186205

187206
return settings;

ydb/core/jaeger_tracing/sampling_throttling_configurator.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@
99

1010
namespace NKikimr::NJaegerTracing {
1111

12+
namespace {
13+
14+
template<class T>
15+
void PropagateUnspecifiedRequest(TRulesContainer<T>& rules) {
16+
constexpr auto unspecifiedRequestType = static_cast<size_t>(ERequestType::UNSPECIFIED);
17+
const auto& unspecifiedRequestTypeRules = rules[unspecifiedRequestType];
18+
19+
for (size_t requestType = 0; requestType < kRequestTypesCnt; ++requestType) {
20+
if (requestType == unspecifiedRequestType) {
21+
continue;
22+
}
23+
24+
auto& requestTypeDatabaseRules = rules[requestType].DatabaseRules;
25+
auto& requestTypeGlobalRules = rules[requestType].Global;
26+
for (const auto& [database, unspecifiedDatabaseRules] : unspecifiedRequestTypeRules.DatabaseRules) {
27+
auto& databaseRules = requestTypeDatabaseRules[database];
28+
databaseRules.insert(databaseRules.end(), unspecifiedDatabaseRules.begin(),
29+
unspecifiedDatabaseRules.end());
30+
}
31+
requestTypeGlobalRules.insert(requestTypeGlobalRules.end(),
32+
unspecifiedRequestTypeRules.Global.begin(),
33+
unspecifiedRequestTypeRules.Global.end());
34+
}
35+
}
36+
37+
} // namespace anonymous
38+
1239
TSamplingThrottlingConfigurator::TSamplingThrottlingConfigurator(TIntrusivePtr<ITimeProvider> timeProvider,
1340
TIntrusivePtr<IRandomProvider>& randomProvider)
1441
: TimeProvider(std::move(timeProvider))
@@ -23,7 +50,10 @@ TIntrusivePtr<TSamplingThrottlingControl> TSamplingThrottlingConfigurator::GetCo
2350
}
2451

2552
void TSamplingThrottlingConfigurator::UpdateSettings(TSettings<double, TThrottlingSettings> settings) {
26-
CurrentSettings = GenerateThrottlers(std::move(settings));
53+
auto enrichedSettings = GenerateThrottlers(std::move(settings));
54+
PropagateUnspecifiedRequest(enrichedSettings.SamplingRules);
55+
PropagateUnspecifiedRequest(enrichedSettings.ExternalThrottlingRules);
56+
CurrentSettings = std::move(enrichedSettings);
2757

2858
for (auto& control : IssuedControls) {
2959
control->UpdateImpl(GenerateSetup());

ydb/core/jaeger_tracing/sampling_throttling_control_internals.cpp

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,60 @@
33

44
namespace NKikimr::NJaegerTracing {
55

6+
namespace {
7+
8+
template<class T, class TAction>
9+
void ForEachMatchingRule(TRequestTypeRules<T>& rules, const TMaybe<TString>& database, TAction&& action) {
10+
for (auto& rule : rules.Global) {
11+
action(rule);
12+
}
13+
if (database) {
14+
if (auto databaseRules = rules.DatabaseRules.FindPtr(*database)) {
15+
for (auto& rule : *databaseRules) {
16+
action(rule);
17+
}
18+
}
19+
}
20+
}
21+
22+
} // namespace anonymous
23+
624
void TSamplingThrottlingControl::TSamplingThrottlingImpl::HandleTracing(
7-
NWilson::TTraceId& traceId, const TRequestDiscriminator& discriminator) {
8-
auto requestType = discriminator.RequestType;
25+
NWilson::TTraceId& traceId, TRequestDiscriminator discriminator) {
26+
auto requestType = static_cast<size_t>(discriminator.RequestType);
27+
auto database = std::move(discriminator.Database);
928

1029
if (traceId) {
11-
bool throttle = Throttle(requestType);
12-
throttle = Throttle(ERequestType::UNSPECIFIED) && throttle;
30+
bool throttle = true;
31+
32+
ForEachMatchingRule(
33+
Setup.ExternalThrottlingRules[requestType], database,
34+
[&throttle](auto& throttlingRule) {
35+
throttle = throttlingRule.Throttler->Throttle() && throttle;
36+
});
37+
1338
if (throttle) {
1439
traceId = {};
1540
}
1641
}
1742

1843
if (!traceId) {
1944
TMaybe<ui8> level;
20-
if (auto sampled_level = Sample(requestType)) {
21-
level = sampled_level;
22-
}
23-
if (auto sampled_level = Sample(ERequestType::UNSPECIFIED)) {
24-
if (!level || *sampled_level > *level) {
25-
level = sampled_level;
26-
}
27-
}
45+
ForEachMatchingRule(
46+
Setup.SamplingRules[requestType], database,
47+
[&level](auto& samplingRule) {
48+
if (!samplingRule.Sampler.Sample() || samplingRule.Throttler->Throttle()) {
49+
return;
50+
}
51+
if (!level || samplingRule.Level > *level) {
52+
level = samplingRule.Level;
53+
}
54+
});
2855

2956
if (level) {
3057
traceId = NWilson::TTraceId::NewTraceId(*level, Max<ui32>());
3158
}
3259
}
3360
}
3461

35-
bool TSamplingThrottlingControl::TSamplingThrottlingImpl::Throttle(ERequestType requestType) {
36-
auto& throttlingRule = Setup.ExternalThrottlingRules[static_cast<size_t>(requestType)];
37-
if (throttlingRule) {
38-
return throttlingRule->Throttler->Throttle();
39-
} else {
40-
return true;
41-
}
42-
}
43-
44-
TMaybe<ui8> TSamplingThrottlingControl::TSamplingThrottlingImpl::Sample(ERequestType requestType) {
45-
TMaybe<ui8> level;
46-
for (auto& samplingRule : Setup.SamplingRules[static_cast<size_t>(requestType)]) {
47-
if (samplingRule.Sampler.Sample() && !samplingRule.Throttler->Throttle()) {
48-
if (!level || *level < samplingRule.Level) {
49-
level = samplingRule.Level;
50-
}
51-
}
52-
}
53-
return level;
54-
}
55-
5662
} // namespace NKikimr::NJaegerTracing

ydb/core/jaeger_tracing/sampling_throttling_control_internals.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ struct TSamplingThrottlingControl::TSamplingThrottlingImpl {
1919

2020
TSettings<TSampler, TIntrusivePtr<TThrottler>> Setup;
2121

22-
void HandleTracing(NWilson::TTraceId& traceId, const TRequestDiscriminator& discriminator);
23-
24-
private:
25-
bool Throttle(ERequestType requestType);
26-
27-
TMaybe<ui8> Sample(ERequestType requestType);
22+
void HandleTracing(NWilson::TTraceId& traceId, TRequestDiscriminator discriminator);
2823
};
2924

3025
} // namespace NKikimr::NJaegerTracing

0 commit comments

Comments
 (0)