Skip to content

Commit 6591741

Browse files
authored
Update hello-world (#1108)
* Update hello-world Add more examples Move config into separate file * Lint fmt * Update README * Add sreens
1 parent 52d1107 commit 6591741

File tree

5 files changed

+139
-98
lines changed

5 files changed

+139
-98
lines changed

helloworld-observ-lib/README.md

Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,16 @@ jb install https://github.com/grafana/jsonnet-libs/helloworld-observ-lib
7676

7777
## Pros of using modular observabilty format
7878

79-
- Uses (jsonnet)[https://jsonnet.org/learning/tutorial.html], jsonnet-bundler, and grafonnet[https://github.com/grafana/grafonnet]
79+
- Uses [jsonnet](https://jsonnet.org/learning/tutorial.html), jsonnet-bundler, and [grafonnet](https://github.com/grafana/grafonnet)
8080
- Highly customizable and flexible:
8181

82-
Any object like `panel`, `target` (query) can be easily referenced by key and then overriden before output of the lib is provided by using jsonnet (patching)[https://tanka.dev/tutorial/environments#patching] technique:
82+
Any object like `panel`, `target` (query) can be easily referenced by key and then overriden before output of the lib is provided by using jsonnet [patching](https://tanka.dev/tutorial/environments#patching) technique:
8383

8484
```jsonnet
8585
local helloworldlib = import './main.libsonnet';
8686
8787
local helloworld =
88-
helloworldlib.new(
89-
filteringSelector='job="integrations/helloworld"',
90-
uid='myhelloworld',
91-
groupLabels=['environment', 'cluster'],
92-
instanceLabels=['host'],
93-
)
94-
+
88+
helloworldlib.new() +
9589
{
9690
grafana+: {
9791
panels+: {
@@ -117,11 +111,42 @@ You can use lib to fill in [monitoring-mixin](https://monitoring.mixins.dev/) st
117111
local helloworldlib = import 'helloworld-observ-lib/main.libsonnet';
118112
119113
local helloworld =
120-
helloworldlib.new(
121-
filteringSelector='job="integrations/helloworld"',
122-
uid='myhelloworld',
123-
groupLabels=['environment', 'cluster'],
124-
instanceLabels=['host'],
114+
helloworldlib.new();
115+
116+
// populate monitoring-mixin:
117+
{
118+
grafanaDashboards+:: helloworld.grafana.dashboards,
119+
prometheusAlerts+:: helloworld.prometheus.alerts,
120+
prometheusRules+:: helloworld.prometheus.recordingRules,
121+
}
122+
```
123+
124+
### Example 2: Monitoring-mixin example with custom config
125+
126+
127+
Any modular library should include as mandator configuration paramaters:
128+
- `filteringSelector` - Static selector to apply to ALL dashboard variables of type query, panel queries, alerts and recording rules.
129+
- `groupLabels` - one or more labels that can be used to identify 'group' of instances. In simple cases, can be 'job' or 'cluster'.
130+
- `instanceLabels` - one or more labels that can be used to identify single entity of instances. In simple cases, can be 'instance' or 'pod'.
131+
- `uid` - UID to prefix all dashboards original uids
132+
- `dashboardNamePrefix` - Use as prefix for all Dashboards and (optional) rule groups
133+
134+
By changing those you can install same mixin two or more times into same Grafana/Prometheus, or import them into other mixins, without any potential problem of conflicting dashboard ids or intersecting PromQL queries:
135+
136+
First:
137+
138+
```
139+
local helloworldlib = import './main.libsonnet';
140+
141+
local helloworld =
142+
helloworldlib.new()
143+
+ helloworldlib.withConfigMixin(
144+
{
145+
filteringSelector: 'job=~"integrations/first"',
146+
uid: 'firsthelloworld',
147+
groupLabels: ['environment', 'cluster', 'job'],
148+
instanceLabels: ['instance'],
149+
}
125150
);
126151
127152
// populate monitoring-mixin:
@@ -132,23 +157,43 @@ local helloworld =
132157
}
133158
```
134159

135-
### Example 2: Changing specific panel before rendering dashboards
160+
Second:
136161

137-
We can point to any object (i.e grafana.panels.panel1) and modify it by using (jsonnnet mixins)[https://jsonnet.org/learning/tutorial.html].
162+
```
163+
local helloworldlib = import './main.libsonnet';
138164
139-
For example, let's modify panel's default draw style to bars by mutating it with (grafonnet)[https://grafana.github.io/grafonnet/API/panel/timeSeries/index.html#fn-fieldconfigdefaultscustomwithdrawstyle]
165+
local helloworld =
166+
helloworldlib.new()
167+
+ helloworldlib.withConfigMixin(
168+
{
169+
filteringSelector: 'job=~"integrations/second"',
170+
uid: 'secondhelloworld',
171+
groupLabels: ['environment', 'cluster', 'job'],
172+
instanceLabels: ['instance'],
173+
}
174+
);
175+
176+
// populate monitoring-mixin:
177+
{
178+
grafanaDashboards+:: helloworld.grafana.dashboards,
179+
prometheusAlerts+:: helloworld.prometheus.alerts,
180+
prometheusRules+:: helloworld.prometheus.recordingRules,
181+
}
182+
```
183+
184+
185+
### Example 3: Changing specific panel before rendering dashboards
186+
187+
We can point to any object (i.e grafana.panels.panel1) and modify it by using [jsonnnet mixins](https://jsonnet.org/learning/tutorial.html).
188+
189+
For example, let's modify panel's default draw style to bars by mutating it with [grafonnet](https://grafana.github.io/grafonnet/API/panel/timeSeries/index.html#fn-fieldconfigdefaultscustomwithdrawstyle):
140190

141191
```
142192
local g = import './g.libsonnet';
143193
local helloworldlib = import 'helloworld-observ-lib/main.libsonnet';
144194
145195
local helloworld =
146-
helloworldlib.new(
147-
filteringSelector='job="integrations/helloworld"',
148-
uid='myhelloworld',
149-
groupLabels=['environment', 'cluster'],
150-
instanceLabels=['host'],
151-
)
196+
helloworldlib.new()
152197
+ {
153198
grafana+: {
154199
panels+: {
@@ -167,7 +212,7 @@ local helloworld =
167212
168213
```
169214

170-
### Example 3: Optional logs collection
215+
### Example 4: Optional logs collection
171216

172217
Grafana Loki datasource is used to populate logs dashboard and also for quering annotations.
173218

@@ -177,12 +222,7 @@ To opt-out, you can set `enableLokiLogs: false` in config:
177222
local helloworldlib = import 'helloworld-observ-lib/main.libsonnet';
178223
179224
local helloworld =
180-
helloworldlib.new(
181-
filteringSelector='job="integrations/helloworld"',
182-
uid='myhelloworld',
183-
groupLabels=['environment', 'cluster'],
184-
instanceLabels=['host'],
185-
)
225+
helloworldlib.new()
186226
+ helloworldlib.withConfigMixin(
187227
{
188228
// disable loki logs
@@ -202,5 +242,14 @@ local helloworld =
202242

203243
To speed up developing observability libs as-code, we recommend to work in the following dev enviroment:
204244

205-
- Setup Vscode with [Jsonnet Language Server][https://marketplace.visualstudio.com/items?itemName=Grafana.vscode-jsonnet]
245+
- Setup Vscode with [Jsonnet Language Server](https://marketplace.visualstudio.com/items?itemName=Grafana.vscode-jsonnet)
206246
- Setup format on save in vscode to lint jsonnet automatically.
247+
- use [grizzly](https://github.com/grafana/grizzly):
248+
- `export GRAFANA_URL=http://localhost:3000`
249+
- `grr apply -t "Dashboard/*" mixin.libsonnet` or `grr watch -t "Dashboard/*" . mixin.libsonnet`
250+
251+
## What is generated from this example
252+
253+
![Dashboard 1](https://github.com/grafana/jsonnet-libs/assets/14870891/440f761b-355d-4cea-8659-c37b30b733a9 "Dashboard 1")
254+
![Dashboard 2](https://github.com/grafana/jsonnet-libs/assets/14870891/440f761b-355d-4cea-8659-c37b30b733a9 "Dashboard 2")
255+
![Dashboard 3](https://github.com/grafana/jsonnet-libs/assets/14870891/440f761b-355d-4cea-8659-c37b30b733a9 "Dashboard 3")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// any modular library should include as inputs:
3+
// 'dashboardNamePrefix' - Use as prefix for all Dashboards and (optional) rule groups
4+
// 'filteringSelector' - Static selector to apply to ALL dashboard variables of type query, panel queries, alerts and recording rules.
5+
// 'groupLabels' - one or more labels that can be used to identify 'group' of instances. In simple cases, can be 'job' or 'cluster'.
6+
// 'instanceLabels' - one or more labels that can be used to identify single entity of instances. In simple cases, can be 'instance' or 'pod'.
7+
// 'uid' - UID to prefix all dashboards original uids
8+
filteringSelector: 'job="integrations/helloworld"',
9+
groupLabels: ['job'],
10+
instanceLabels: ['instance'],
11+
dashboardTags: [self.uid],
12+
uid: 'helloworld',
13+
dashboardNamePrefix: '',
14+
15+
// additional params can be added if needed
16+
criticalEvents: '90',
17+
alertsThreshold1: '90',
18+
alertsThreshold2: '75',
19+
dashboardPeriod: 'now-1h',
20+
dashboardTimezone: 'default',
21+
dashboardRefresh: '1m',
22+
23+
// logs lib related
24+
enableLokiLogs: true,
25+
logsExtraFilters: '',
26+
extraLogLabels: ['level'],
27+
logsVolumeGroupBy: 'level',
28+
showLogsVolume: true,
29+
}

helloworld-observ-lib/links.libsonnet

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local g = import './g.libsonnet';
2+
{
3+
new(this):
4+
{
5+
local link = g.dashboard.link,
6+
backToFleet:
7+
link.link.new('Back to Helloworld fleet', '/d/' + this.grafana.dashboards.fleet.uid)
8+
+ link.link.options.withKeepTime(true),
9+
backToOverview:
10+
link.link.new('Back to Helloworld overview', '/d/' + this.grafana.dashboards.overview.uid)
11+
+ link.link.options.withKeepTime(true),
12+
otherDashboards:
13+
link.dashboards.new('All Helloworld dashboards', this.config.dashboardTags)
14+
+ link.dashboards.options.withIncludeVars(true)
15+
+ link.dashboards.options.withKeepTime(true)
16+
+ link.dashboards.options.withAsDropdown(true),
17+
},
18+
}

helloworld-observ-lib/main.libsonnet

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,32 @@
11
local alerts = import './alerts.libsonnet';
22
local annotations = import './annotations.libsonnet';
3+
local config = import './config.libsonnet';
34
local dashboards = import './dashboards.libsonnet';
45
local datasources = import './datasources.libsonnet';
56
local g = import './g.libsonnet';
7+
local links = import './links.libsonnet';
68
local panels = import './panels.libsonnet';
79
local targets = import './targets.libsonnet';
810
local variables = import './variables.libsonnet';
911

1012
{
11-
// any lib should have have configurable params:
12-
// 'filteringSelector' - Static selector to apply to ALL dashboard variables of type query, panel queries, alerts and recording rules.
13-
// 'uid' - UID to prefix all dashboards original uids
14-
// 'dashboardNamePrefix' - Use as prefix for all Dashboards and (optional) rule groups
15-
// 'groupLabels' - one or more labels that can be used to identify 'group' of instances. In simple cases, can be 'job' or 'cluster'.
16-
// 'instanceLabels' - one or more labels that can be used to identify single entity of instances. In simple cases, can be 'instance' or 'pod'.
1713

1814
withConfigMixin(config): {
1915
config+: config,
2016
},
21-
new(
22-
filteringSelector,
23-
groupLabels=['job'],
24-
instanceLabels=['instance'],
25-
dashboardNamePrefix='',
26-
dashboardTags=[uid],
27-
uid,
28-
): {
2917

30-
local this = self,
31-
config: {
32-
// any modular library should inlcude as inputs:
33-
// 'dashboardNamePrefix' - Use as prefix for all Dashboards and (optional) rule groups
34-
// 'filteringSelector' - Static selector to apply to ALL dashboard variables of type query, panel queries, alerts and recording rules.
35-
// 'groupLabels' - one or more labels that can be used to identify 'group' of instances. In simple cases, can be 'job' or 'cluster'.
36-
// 'instanceLabels' - one or more labels that can be used to identify single entity of instances. In simple cases, can be 'instance' or 'pod'.
37-
// 'uid' - UID to prefix all dashboards original uids
38-
groupLabels: groupLabels,
39-
instanceLabels: instanceLabels,
40-
filteringSelector: filteringSelector,
41-
dashboardTags: dashboardTags,
42-
uid: uid,
43-
dashboardNamePrefix: dashboardNamePrefix,
44-
45-
// additional params can be added if needed
46-
criticalEvents: '90',
47-
alertsThreshold1: '90',
48-
alertsThreshold2: '75',
49-
dashboardPeriod: 'now-1h',
50-
dashboardTimezone: 'default',
51-
dashboardRefresh: '1m',
18+
new(): {
5219

53-
// logs lib related
54-
enableLokiLogs: true,
55-
logsExtraFilters: '',
56-
extraLogLabels: ['level'],
57-
logsVolumeGroupBy: 'level',
58-
showLogsVolume: true,
59-
},
20+
local this = self,
21+
config: config,
6022

6123
grafana: {
6224
variables: variables.new(this, varMetric='up{%(filteringSelector)s}' % this.config),
6325
targets: targets.new(this),
6426
annotations: annotations.new(this),
65-
// common dashboards links here
66-
links: {
67-
local link = g.dashboard.link,
68-
backToFleet:
69-
link.link.new('Back to Helloworld fleet', '/d/' + this.grafana.dashboards.fleet.uid)
70-
+ link.link.options.withKeepTime(true),
71-
backToOverview:
72-
link.link.new('Back to Helloworld overview', '/d/' + this.grafana.dashboards.overview.uid)
73-
+ link.link.options.withKeepTime(true),
74-
otherDashboards:
75-
link.dashboards.new('All Helloworld dashboards', this.config.dashboardTags)
76-
+ link.dashboards.options.withIncludeVars(true)
77-
+ link.dashboards.options.withKeepTime(true)
78-
+ link.dashboards.options.withAsDropdown(true),
79-
},
80-
27+
links: links.new(this),
8128
panels: panels.new(this),
8229
dashboards: dashboards.new(this),
83-
8430
},
8531

8632
prometheus: {

helloworld-observ-lib/mixin.libsonnet

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
local helloworldlib = import './main.libsonnet';
22

33
local helloworld =
4-
helloworldlib.new(
5-
filteringSelector='job=~"integrations/helloworld"',
6-
uid='myhelloworld',
7-
groupLabels=['environment', 'cluster', 'job'],
8-
instanceLabels=['instance'],
9-
)
4+
helloworldlib.new()
105
+ helloworldlib.withConfigMixin(
116
{
7+
filteringSelector: 'job=~"integrations/helloworld"',
8+
uid: 'myhelloworld',
9+
groupLabels: ['environment', 'cluster', 'job'],
10+
instanceLabels: ['instance'],
1211
// disable loki logs
13-
enableLokiLogs: true,
12+
enableLokiLogs: false,
1413
}
1514
);
1615

0 commit comments

Comments
 (0)