forked from grafana/jsonnet-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus.libsonnet
44 lines (39 loc) · 1.87 KB
/
prometheus.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
local util = import 'util.libsonnet';
local kind = 'PrometheusRuleGroup';
local recordingRules = 'prometheusRules';
local alertRules = 'prometheusAlerts';
local resource = import 'resource.libsonnet';
{
getMixinRuleNames(mixins)::
local flatMixins = [mixins[key] for key in std.objectFieldsAll(mixins)];
local mixinRules = std.flattenArrays([mixin.prometheusRules.groups for mixin in flatMixins if std.objectHasAll(mixin, recordingRules)]);
local mixinAlerts = std.flattenArrays([mixin.prometheusAlerts.groups for mixin in flatMixins if std.objectHasAll(mixin, alertRules)]);
[group.name for group in mixinAlerts] + [group.name for group in mixinRules],
fromMaps(rules):: { [k]: util.makeResource(kind, k, { groups: rules }, {}) for k in std.objectFields(rules) },
fromMapsFiltered(rules, excludes):: {
local filterRules(rules, exclude_list) = [rule for rule in rules.groups if !std.member(exclude_list, rule.name)],
[k]: util.makeResource(
kind,
std.strReplace(std.strReplace(std.strReplace(k, '.json', ''), '.yaml', ''), '.yml', ''),
{ groups: filterRules(rules, excludes) },
{}
)
for k in std.objectFields(rules)
},
fromMixins(mixins):: {
[if std.objectHasAll(mixins[key], alertRules) || std.objectHasAll(mixins[key], recordingRules) then key else null]:
util.makeResource(
kind,
std.strReplace(std.strReplace(std.strReplace(key, '.json', ''), '.yaml', ''), '.yml', ''),
(if std.objectHasAll(mixins[key], alertRules) then mixins[key].prometheusAlerts else {})
+ (if std.objectHasAll(mixins[key], recordingRules) then mixins[key].prometheusRules else {})
)
for key in std.objectFields(mixins)
},
rule_group: {
new(namespace, name, group)::
resource.new('PrometheusRuleGroup', name)
+ resource.addMetadata('namespace', namespace)
+ resource.withSpec(group),
},
}