|
15 | 15 | if s > 60 * 60 * 24
|
16 | 16 | then '%.1f days' % (s / 60 / 60 / 24)
|
17 | 17 | else '%.1f hours' % (s / 60 / 60),
|
| 18 | + |
| 19 | + // Handle adding `group left` to join labels into rule by wrapping the rule in () * on(xxx) group_left(xxx) kube_xxx_labels |
| 20 | + // If kind of rule is not defined try to detect rule type by alert name |
| 21 | + wrap_rule_for_labels(rule, config): |
| 22 | + // Detect Kind of rule from name unless hidden `kind field is passed in the rule` |
| 23 | + local kind = |
| 24 | + if 'kind' in rule then rule.kind |
| 25 | + // Handle Alerts |
| 26 | + else if std.objectHas(rule, 'alert') then |
| 27 | + if std.startsWith(rule.alert, 'KubePod') then 'pod' |
| 28 | + else if std.startsWith(rule.alert, 'KubeContainer') then 'pod' |
| 29 | + else if std.startsWith(rule.alert, 'KubeStateful') then 'statefulset' |
| 30 | + else if std.startsWith(rule.alert, 'KubeDeploy') then 'deployment' |
| 31 | + else if std.startsWith(rule.alert, 'KubeDaemon') then 'daemonset' |
| 32 | + else if std.startsWith(rule.alert, 'KubeHpa') then 'horizontalpodautoscaler' |
| 33 | + else if std.startsWith(rule.alert, 'KubeJob') then 'job' |
| 34 | + else 'none' |
| 35 | + else 'none'; |
| 36 | + |
| 37 | + local labels = { |
| 38 | + join_labels: config['%ss_join_labels' % kind], |
| 39 | + // since the label 'job' is reserved, the resource with kind Job uses the label 'job_name' instead |
| 40 | + on_labels: ['%s' % (if kind == 'job' then 'job_name' else kind), '%s' % config.namespaceLabel, '%s' % config.clusterLabel], |
| 41 | + metric: 'kube_%s_labels' % kind, |
| 42 | + }; |
| 43 | + |
| 44 | + // Failed to identify kind - return raw rule |
| 45 | + if kind == 'none' then rule |
| 46 | + // No join labels passed in the config - return raw rule |
| 47 | + else if std.length(labels.join_labels) == 0 then rule |
| 48 | + // Wrap expr with join group left |
| 49 | + else |
| 50 | + rule { |
| 51 | + local expr = super.expr, |
| 52 | + expr: '(%(expr)s) * on (%(on)s) group_left(%(join)s) %(metric)s' % { |
| 53 | + expr: expr, |
| 54 | + on: std.join(',', labels.on_labels), |
| 55 | + join: std.join(',', labels.join_labels), |
| 56 | + metric: labels.metric, |
| 57 | + }, |
| 58 | + }, |
18 | 59 | }
|
0 commit comments