-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.libsonnet
72 lines (66 loc) · 1.89 KB
/
helpers.libsonnet
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
// This file contains helper functions.
// normalize converts raw data into a common format.
local normalize = function(mixin)
local fillNulls = {
rules: {
groups+: [],
},
dashboards: {},
grafanaDashboards: {},
prometheusAlerts: {},
prometheus_alerts: {},
prometheusRules: {},
} + mixin;
{
dashboards: fillNulls.dashboards + fillNulls.grafanaDashboards,
rules: fillNulls.rules + fillNulls.prometheusRules + fillNulls.prometheusAlerts + fillNulls.prometheus_alerts,
};
// merge deeply combines two datasets.
local merge = function(a, b)
local mergeRules = function(rules)
local reducer = function(res, rule)
local collisions = [r for r in res if ("alert" in r && "alert" in rule && r.alert == rule.alert) || ("record" in r && "record" in rule && r.record == rule.record)];
if [] == collisions then
res + [rule]
else
res
;
std.foldl(reducer, rules, [])
;
local mergeGroups = function(groups)
local reducer = function(res, group)
local collisions = [g for g in res if g.name == group.name];
if [] == collisions then
res + [group]
else
local existing = collisions[0];
[g for g in res if g.name != group.name] + [{
name: existing.name,
rules: mergeRules(existing.rules + group.rules),
}]
;
std.foldl(reducer, groups, [])
;
a {
rules: {
groups: mergeGroups(a.rules.groups + b.rules.groups),
},
dashboards: a.dashboards + b.dashboards,
}
;
// apply applies fixups expressed as functions
// See fn.libsonnet
local apply = function(x) function(funcs)
local funcList = if std.isArray(funcs) then funcs else [funcs];
local _apply = function(m, fs)
if std.length(fs) == 0 then
m
else
_apply(fs[0](m), fs[1:]) tailstrict;
_apply(x, funcList)
;
{
normalize: normalize,
merge: merge,
apply: apply,
}