Skip to content

Commit

Permalink
Improve jsonnet parser for rules (#211)
Browse files Browse the repository at this point in the history
* Improve jsonnet parser for rules

- Load all rule groups instead of the first one
- Parse rule groups when they are not wrapped into namespace. This is extremely useful when working with monitoring-mixins.

* Remove extra loop in jsonnet parser

* Update docs/content/hidden-elements.md

Co-authored-by: malcolmholmes <[email protected]>

Co-authored-by: malcolmholmes <[email protected]>
  • Loading branch information
v-zhuravlev and malcolmholmes authored Apr 18, 2022
1 parent 76cf1f3 commit a02770b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
51 changes: 47 additions & 4 deletions docs/content/hidden-elements.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
date: "2021-06-28T00:00:00+00:00"
date: "2022-04-17T00:00:00+00:00"
title: "Hidden Elements"
---

Expand Down Expand Up @@ -82,9 +82,52 @@ Prometheus alerts and recording rules can be defined too, for example:
},
}
```
Here, we first create an alert rule element (`grizzly_alert`), which we then add to `prometheusAlerts`. `PrometheusAlerts` is where Grizzly expects to find alerts for Prometheus configured when using 'hidden
elements.
Here, we first create an alert rule element (`grizzly_alert`), which we then add to namespace `grizzly_rules` inside `prometheusAlerts`. `PrometheusAlerts` is where Grizzly expects to find alerts for Prometheus configured when using 'hidden
elements'.

Likewise, we then create a recording rule element (`grizzly_record`), which we then add to
`prometheusRules`, which is where Grizzly expects to find Prometheus recording rules configured, again
namespace `grizzly_rules` inside `prometheusRules`, which is where Grizzly expects to find Prometheus recording rules configured, again
when using hidden elements.

It also possible to load rules without specifying a namespace at all. In that case `grizzly_rules` namespace would be used. This is useful when loading alerts from monitoring-mixins:

```
{
grizzly_alert:: {
alert: 'PromScrapeFailed',
expr: 'up != 1',
'for': '1m',
labels: {
severity: 'critical',
},
annotations: {
message: 'Prometheus failed to scrape a target {{ $labels.job }} / {{ $labels.instance }}',
},
},
prometheusAlerts+:: {
// namespace removed
groups: [{
name: 'grizzly_alert_rules',
rules: [
$.grizzly_alert,
],
}],
},
grizzly_record:: {
record: 'job:up:sum',
expr: 'sum by(job) (up)',
},
prometheusRules+:: {
// namespace removed
groups: [{
name: 'grizzly_recording_rules',
rules: [
$.grizzly_record,
],
}],
},
}
```
38 changes: 25 additions & 13 deletions pkg/grizzly/grizzly.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,32 @@ local convert(main, apiVersion) = {
},

prometheus:
local groupNamespace(key) = std.objectFields(main[key])[0];
local groupName(key) = main[key][groupNamespace(key)].groups[0].name;
local forceNamespace(contents) =
// if rulesGroup isn't namespaced (monitoring-mixins), then put them into default namespace
if std.objectHas(contents, 'groups') then
// no namespace, wrap into default namespace
{ 'grizzly_rules': contents }
else
// already has namespace
contents
;
local fromMap(key) =
if key in main
then [
makeResource(
'PrometheusRuleGroup',
groupName(key),
spec={
rules: main[key][groupNamespace(key)].groups[0].rules,
},
metadata={ namespace: groupNamespace(key)})
for k in std.objectFields(main[key])
]
if key in main then
local allNamespaced = forceNamespace(main[key]);
[

makeResource(
'PrometheusRuleGroup',
g.name,
spec={
rules: g.rules,
},
metadata={ namespace: ns }
)

for ns in std.objectFields(allNamespaced)
for g in allNamespaced[ns].groups
]
else [];
fromMap('prometheusRules')
+ fromMap('prometheusAlerts'),
Expand Down

0 comments on commit a02770b

Please sign in to comment.