Skip to content

Commit 073c94c

Browse files
authored
feat: Assert for bad max_query_lookback config (#16362)
**What this PR does / why we need it**: Add a new assert to our jsonnet manifest that checks that there's no tenant with a retention period that can't be queried (due to low max_query_lookback)
1 parent 6abb12d commit 073c94c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

‎production/ksonnet/loki/common.libsonnet

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ local k = import 'ksonnet-util/kausal.libsonnet';
4444
container.mixin.readinessProbe.httpGet.withPort($._config.http_listen_port) +
4545
container.mixin.readinessProbe.withInitialDelaySeconds(15) +
4646
container.mixin.readinessProbe.withTimeoutSeconds(1),
47+
48+
parseDuration(duration)::
49+
if std.endsWith(duration, 's') then
50+
std.parseInt(std.substr(duration, 0, std.length(duration) - 1))
51+
else if std.endsWith(duration, 'm') then
52+
std.parseInt(std.substr(duration, 0, std.length(duration) - 1)) * 60
53+
else if std.endsWith(duration, 'h') then
54+
std.parseInt(std.substr(duration, 0, std.length(duration) - 1)) * 3600
55+
else if std.endsWith(duration, 'd') then
56+
std.parseInt(std.substr(duration, 0, std.length(duration) - 1)) * 86400
57+
else
58+
error 'unable to parse duration %s' % duration,
4759
},
4860

4961
// functions for k8s objects

‎production/ksonnet/loki/overrides.libsonnet

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
local common = import 'common.libsonnet';
12
local k = import 'ksonnet-util/kausal.libsonnet';
2-
33
{
44
_config+: {
55
overrides: {
@@ -39,4 +39,47 @@ local k = import 'ksonnet-util/kausal.libsonnet';
3939
+ (if std.length($._config.multi_kv_config) > 0 then { multi_kv_config: $._config.multi_kv_config } else {}),
4040
),
4141
}),
42+
43+
local checkRetentionStreams(retentionStreams, maxQueryLookback) =
44+
std.foldl(
45+
function(acc, retentionStream) acc && common.parseDuration(retentionStream.period) <= common.parseDuration(maxQueryLookback),
46+
retentionStreams,
47+
true
48+
),
49+
50+
isLookbackLongerThanRetention(tenantCfg)::
51+
local retentionPeriod = tenantCfg.retention_period;
52+
local lookback = tenantCfg.max_query_lookback;
53+
if std.objectHas(tenantCfg, 'max_query_lookback') &&
54+
std.objectHas(tenantCfg, 'retention_period') then
55+
common.parseDuration(lookback) >= common.parseDuration(retentionPeriod)
56+
else
57+
true,
58+
59+
isLookbackLongerThanStreamRetention(tenantCfg)::
60+
local retentionStream = tenantCfg.retention_stream;
61+
local lookback = tenantCfg.max_query_lookback;
62+
if std.objectHas(tenantCfg, 'max_query_lookback') &&
63+
std.objectHas(tenantCfg, 'retention_stream') then
64+
checkRetentionStreams(retentionStream, lookback)
65+
else
66+
true,
67+
68+
checkTenantRetention(tenant)::
69+
local tenantCfg = $._config.overrides[tenant];
70+
if $.isLookbackLongerThanRetention(tenantCfg) &&
71+
$.isLookbackLongerThanStreamRetention(tenantCfg) then
72+
true
73+
else
74+
false,
75+
76+
local tenants = std.objectFields($._config.overrides),
77+
78+
local validRetentionsCheck = std.foldl(
79+
function(acc, tenant) if !$.checkTenantRetention(tenant) then { valid: false, failedTenant: [tenant] + acc.failedTenant } else acc,
80+
tenants,
81+
{ valid: true, failedTenant: [] }
82+
),
83+
84+
assert validRetentionsCheck.valid : 'retention period longer than max_query_lookback for tenants %s' % std.join(', ', validRetentionsCheck.failedTenant),
4285
}

0 commit comments

Comments
 (0)