Skip to content

Commit 4092ea8

Browse files
feat(alerts-observ-lib): Add alerts-observ-lib (#1599)
* Add new annotations in commonlib and helper variables func * Use tokens * Add alerts observability library - Add panel with alerts scoped by filteringSelector/groupLabels/instanceLabels - Created annotations for critical, warning, and info alerts. - Add dashboard to server as a reference. But intention to use this library more as dependency on other libraries/mixins. For example, can replace annotations defined directly in node-mixin/snmp-observ-lib. Also other observ libs/mixins in this repo that show alerts as a panel. * fmt * Update default row * Update dashboard out * Apply suggestion from @Dasomeone Co-authored-by: Emily <1282515+Dasomeone@users.noreply.github.com> * Update * Update README --------- Co-authored-by: Emily <1282515+Dasomeone@users.noreply.github.com>
1 parent f53b1c9 commit 4092ea8

File tree

16 files changed

+463
-0
lines changed

16 files changed

+463
-0
lines changed

alerts-observ-lib/.lint

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exclusions:
2+
panel-datasource-rule:
3+
reason: "alertlist panel uses -- Mixed -- datasource by default."

alerts-observ-lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ../Makefile_mixin

alerts-observ-lib/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Alerts observability library
2+
3+
Single `ALERTS` signal with one alerts-overview dashboard (alertlist panel grouped by configurable labels).
4+
Note that this library doesn't provide prometheus alerts themselves, only alerts presenation as annotations and dashboard panel that can be easily imported into other libraries or mixins.
5+
6+
## Example
7+
8+
### Simple init
9+
```jsonnet
10+
local alertslib = import 'alerts-observ-lib/main.libsonnet';
11+
12+
alertslib.new()
13+
+ alertslib.withConfigMixin({
14+
filteringSelector: 'job="integrations/myapp"',
15+
groupLabels: ['cluster', 'container'],
16+
instanceLabels: ['instance'],
17+
uid: 'myapp',
18+
})
19+
| asMonitoringMixin()
20+
```
21+
22+
### Import into another observ-lib
23+
24+
```
25+
# dashboards.libsonnet
26+
local g = import './g.libsonnet';
27+
local commonlib = import 'common-lib/common/main.libsonnet';
28+
local alertsOverviewlib = import 'alerts-observ-lib/main.libsonnet';
29+
30+
31+
{
32+
new(this):
33+
{
34+
local alertsOverview =
35+
alertsOverviewlib.new()
36+
+ alertsOverviewlib.withConfigMixin(this.config),
37+
38+
'some-dashboard-overview.json':
39+
g.dashboard.new(this.config.dashboardNamePrefix + 'overview')
40+
+ g.dashboard.withUid(this.config.uid + '-overview')
41+
+ g.dashboard.withTags(this.config.dashboardTags)
42+
//insert here annotations:
43+
+ g.dashboard.withAnnotations(std.objectValues(alertsOverview.grafana.annotations))
44+
+ g.dashboard.withPanels(
45+
g.util.grid.wrapPanels(
46+
g.util.panel.resolveCollapsedFlagOnRows(
47+
[
48+
g.panel.row.new('Overview')
49+
+ g.panel.row.withCollapsed(false)
50+
+ g.panel.row.withPanels(
51+
[
52+
//insert here resized alerts panel annotations
53+
alertsOverview.grafana.panels.alertsOverview { gridPos: { w: 12, h: 12 } },
54+
// some other panel
55+
panels.serviceMap { gridPos: { w: 12, h: 12 } },
56+
],
57+
),
58+
],
59+
),
60+
), setPanelIDs=false // required to reuse queries
61+
),
62+
},
63+
}
64+
65+
66+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local commonlib = import 'common-lib/common/main.libsonnet';
2+
3+
{
4+
new(this): {
5+
critical:
6+
commonlib.annotations.critical.new(
7+
title='Critical alert',
8+
target=this.signals.alerts.alertsCritical.asTarget(),
9+
)
10+
{
11+
textFormat: '{{alertname}}',
12+
hide: true,
13+
}
14+
+ commonlib.annotations.base.withTagKeys(std.join(',', this.config.groupLabels + this.config.instanceLabels)),
15+
warning:
16+
commonlib.annotations.warning.new(
17+
title='Warning alert',
18+
target=this.signals.alerts.alertsWarning.asTarget(),
19+
)
20+
{
21+
textFormat: '{{alertname}}',
22+
hide: true,
23+
}
24+
+ commonlib.annotations.base.withTagKeys(std.join(',', this.config.groupLabels + this.config.instanceLabels)),
25+
info:
26+
commonlib.annotations.info.new(
27+
title='Info message',
28+
target=this.signals.alerts.alertsInfo.asTarget(),
29+
)
30+
{
31+
textFormat: '{{alertname}}',
32+
hide: true,
33+
}
34+
+ commonlib.annotations.base.withTagKeys(std.join(',', this.config.groupLabels + this.config.instanceLabels)),
35+
},
36+
}

alerts-observ-lib/config.libsonnet

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
local this = self,
3+
filteringSelector: '',
4+
groupLabels: ['job'],
5+
groupMode: 'default', // 'default' or 'custom'
6+
instanceLabels: ['instance'],
7+
uid: 'alerts',
8+
dashboardNamePrefix: '',
9+
dashboardTags: ['alerts'],
10+
dashboardPeriod: 'now-1h',
11+
dashboardTimezone: 'default',
12+
dashboardRefresh: '1m',
13+
metricsSource: ['prometheus'],
14+
signals: {
15+
alerts: (import './signals/alerts.libsonnet')(this),
16+
},
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local g = import './g.libsonnet';
2+
3+
{
4+
local root = self,
5+
new(this)::
6+
local prefix = this.config.dashboardNamePrefix;
7+
local links = this.grafana.links;
8+
local tags = this.config.dashboardTags;
9+
local uid = this.config.uid;
10+
local annotations = this.grafana.annotations;
11+
local refresh = this.config.dashboardRefresh;
12+
local period = this.config.dashboardPeriod;
13+
local timezone = this.config.dashboardTimezone;
14+
{
15+
'alerts-overview.json':
16+
g.dashboard.new(prefix + 'Alerts overview')
17+
+ g.dashboard.withPanels(
18+
g.util.panel.resolveCollapsedFlagOnRows(
19+
g.util.grid.wrapPanels(
20+
[
21+
this.grafana.rows.alertsOverview,
22+
]
23+
)
24+
), setPanelIDs=false
25+
)
26+
+ root.applyCommon(
27+
this.signals.alerts.getVariablesMultiChoice(),
28+
uid + '-alerts-overview',
29+
tags,
30+
links,
31+
annotations,
32+
timezone,
33+
refresh,
34+
period
35+
),
36+
},
37+
38+
applyCommon(vars, uid, tags, links, annotations, timezone, refresh, period)::
39+
g.dashboard.withTags(tags)
40+
+ g.dashboard.withUid(uid)
41+
+ g.dashboard.withLinks(std.objectValues(links))
42+
+ g.dashboard.withTimezone(timezone)
43+
+ g.dashboard.withRefresh(refresh)
44+
+ g.dashboard.time.withFrom(period)
45+
+ g.dashboard.withVariables(vars)
46+
+ g.dashboard.withAnnotations(std.objectValues(annotations)),
47+
}

alerts-observ-lib/dashboards_out/alerts-overview.json

Lines changed: 136 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

alerts-observ-lib/g.libsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'github.com/grafana/grafonnet/gen/grafonnet-v11.4.0/main.libsonnet'

alerts-observ-lib/jsonnetfile.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": 1,
3+
"dependencies": [
4+
{
5+
"source": {
6+
"git": {
7+
"remote": "https://github.com/grafana/jsonnet-libs.git",
8+
"subdir": "common-lib"
9+
}
10+
},
11+
"version": "master"
12+
},
13+
{
14+
"source": {
15+
"git": {
16+
"remote": "https://github.com/grafana/grafonnet.git",
17+
"subdir": "gen/grafonnet-v11.4.0"
18+
}
19+
},
20+
"version": "main"
21+
}
22+
],
23+
"legacyImports": true
24+
}

alerts-observ-lib/main.libsonnet

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
local alerts = import './alerts.libsonnet';
2+
local config = import './config.libsonnet';
3+
local dashboards = import './dashboards.libsonnet';
4+
local g = import './g.libsonnet';
5+
local panels = import './panels.libsonnet';
6+
local rows = import './rows.libsonnet';
7+
local commonlib = import 'common-lib/common/main.libsonnet';
8+
9+
{
10+
withConfigMixin(config): {
11+
config+: config,
12+
},
13+
14+
new(): {
15+
local this = self,
16+
config: config,
17+
signals: {
18+
[sig]: commonlib.signals.unmarshallJsonMulti(
19+
this.config.signals[sig],
20+
type=this.config.metricsSource
21+
)
22+
for sig in std.objectFields(this.config.signals)
23+
},
24+
grafana: {
25+
annotations: (import './annotations.libsonnet').new(this),
26+
links: {},
27+
panels: panels.new(this),
28+
rows: rows.new(this.grafana.panels),
29+
dashboards: dashboards.new(this),
30+
},
31+
32+
prometheus: {
33+
alerts: {},
34+
recordingRules: {},
35+
},
36+
37+
asMonitoringMixin(): {
38+
grafanaDashboards+:: this.grafana.dashboards,
39+
prometheusAlerts+:: this.prometheus.alerts,
40+
prometheusRules+:: this.prometheus.recordingRules,
41+
},
42+
},
43+
}

0 commit comments

Comments
 (0)