Skip to content

Commit b83764f

Browse files
committed
feat: Add cpu&mem dashboards in grafana plugin
1 parent 60d0c6e commit b83764f

File tree

6 files changed

+979
-1
lines changed

6 files changed

+979
-1
lines changed

docs/book/src/plugins/grafana-v1-alpha.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,20 @@ See an example of how to use the plugin in your project:
7777
- Description:
7878
- Per-second rate of total reconciliation as measured over the last 5 minutes
7979
- Per-second rate of reconciliation errors as measured over the last 5 minutes
80+
- Sample: <img width="1430" src="https://user-images.githubusercontent.com/18136486/176122555-f3493658-6c99-4ad6-a9b7-63d85620d370.png">
8081

81-
<img width="1430" alt="Screen Shot 2022-06-28 at 3 43 10 AM" src="https://user-images.githubusercontent.com/18136486/176122555-f3493658-6c99-4ad6-a9b7-63d85620d370.png">
82+
#### CPU & Memory Usage
83+
84+
- Metrics:
85+
- process_cpu_seconds_total
86+
- process_resident_memory_bytes
87+
- Query:
88+
- rate(process_cpu_seconds_total{job="$job", namespace="$namespace", pod="$pod"}[5m]) \* 100
89+
- process_resident_memory_bytes{job="$job", namespace="$namespace", pod="$pod"}
90+
- Description:
91+
- Per-second rate of CPU usage as measured over the last 5 minutes
92+
- Allocated Memory for the running controller
93+
- Sample: <img width="1381" src="https://user-images.githubusercontent.com/18136486/177239808-7d94b17d-692c-4166-8875-6d9332e05bcb.png">
8294

8395
## Subcommands
8496

pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ func (s *editScaffolder) Scaffold() error {
5050

5151
return scaffold.Execute(
5252
&templates.RuntimeManifest{},
53+
&templates.ResourcesManifest{},
5354
)
5455
}

pkg/plugins/optional/grafana/v1alpha/scaffolds/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ func (s *initScaffolder) Scaffold() error {
5050

5151
return scaffold.Execute(
5252
&templates.RuntimeManifest{},
53+
&templates.ResourcesManifest{},
5354
)
5455
}
Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package templates
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
23+
)
24+
25+
var _ machinery.Template = &ResourcesManifest{}
26+
27+
// Kustomization scaffolds a file that defines the kustomization scheme for the prometheus folder
28+
type ResourcesManifest struct {
29+
machinery.TemplateMixin
30+
}
31+
32+
// SetTemplateDefaults implements file.Template
33+
func (f *ResourcesManifest) SetTemplateDefaults() error {
34+
if f.Path == "" {
35+
f.Path = filepath.Join("grafana", "controller-resources-metrics.json")
36+
}
37+
38+
f.TemplateBody = controllerResourcesTemplate
39+
40+
f.IfExistsAction = machinery.OverwriteFile
41+
42+
return nil
43+
}
44+
45+
// nolint: lll
46+
const controllerResourcesTemplate = `{
47+
"__inputs": [
48+
{
49+
"name": "DS_PROMETHEUS",
50+
"label": "Prometheus",
51+
"description": "",
52+
"type": "datasource",
53+
"pluginId": "prometheus",
54+
"pluginName": "Prometheus"
55+
}
56+
],
57+
"__requires": [
58+
{
59+
"type": "datasource",
60+
"id": "prometheus",
61+
"name": "Prometheus",
62+
"version": "1.0.0"
63+
}
64+
],
65+
"annotations": {
66+
"list": [
67+
{
68+
"builtIn": 1,
69+
"datasource": "-- Grafana --",
70+
"enable": true,
71+
"hide": true,
72+
"iconColor": "rgba(0, 211, 255, 1)",
73+
"name": "Annotations & Alerts",
74+
"target": {
75+
"limit": 100,
76+
"matchAny": false,
77+
"tags": [],
78+
"type": "dashboard"
79+
},
80+
"type": "dashboard"
81+
}
82+
]
83+
},
84+
"editable": true,
85+
"fiscalYearStartMonth": 0,
86+
"graphTooltip": 0,
87+
"links": [],
88+
"liveNow": false,
89+
"panels": [
90+
{
91+
"datasource": "${DS_PROMETHEUS}",
92+
"fieldConfig": {
93+
"defaults": {
94+
"color": {
95+
"mode": "continuous-GrYlRd"
96+
},
97+
"custom": {
98+
"axisLabel": "",
99+
"axisPlacement": "auto",
100+
"barAlignment": 0,
101+
"drawStyle": "line",
102+
"fillOpacity": 20,
103+
"gradientMode": "scheme",
104+
"hideFrom": {
105+
"legend": false,
106+
"tooltip": false,
107+
"viz": false
108+
},
109+
"lineInterpolation": "smooth",
110+
"lineWidth": 3,
111+
"pointSize": 5,
112+
"scaleDistribution": {
113+
"type": "linear"
114+
},
115+
"showPoints": "auto",
116+
"spanNulls": false,
117+
"stacking": {
118+
"group": "A",
119+
"mode": "none"
120+
},
121+
"thresholdsStyle": {
122+
"mode": "off"
123+
}
124+
},
125+
"mappings": [],
126+
"thresholds": {
127+
"mode": "absolute",
128+
"steps": [
129+
{
130+
"color": "green",
131+
"value": null
132+
},
133+
{
134+
"color": "red",
135+
"value": 80
136+
}
137+
]
138+
},
139+
"unit": "percent"
140+
},
141+
"overrides": []
142+
},
143+
"gridPos": {
144+
"h": 8,
145+
"w": 12,
146+
"x": 0,
147+
"y": 0
148+
},
149+
"id": 2,
150+
"interval": "1m",
151+
"links": [],
152+
"options": {
153+
"legend": {
154+
"calcs": [],
155+
"displayMode": "list",
156+
"placement": "bottom"
157+
},
158+
"tooltip": {
159+
"mode": "single",
160+
"sort": "none"
161+
}
162+
},
163+
"pluginVersion": "8.4.3",
164+
"targets": [
165+
{
166+
"datasource": "${DS_PROMETHEUS}",
167+
"exemplar": true,
168+
"expr": "rate(process_cpu_seconds_total{job=\"$job\", namespace=\"$namespace\", pod=\"$pod\"}[5m]) * 100",
169+
"format": "time_series",
170+
"interval": "",
171+
"intervalFactor": 2,
172+
"legendFormat": "Pod: {{"{{pod}}"}} | Container: {{"{{container}}"}}",
173+
"refId": "A",
174+
"step": 10
175+
}
176+
],
177+
"title": "Controller CPU Usage",
178+
"type": "timeseries"
179+
},
180+
{
181+
"datasource": "${DS_PROMETHEUS}",
182+
"fieldConfig": {
183+
"defaults": {
184+
"color": {
185+
"mode": "continuous-GrYlRd"
186+
},
187+
"custom": {
188+
"axisLabel": "",
189+
"axisPlacement": "auto",
190+
"barAlignment": 0,
191+
"drawStyle": "line",
192+
"fillOpacity": 20,
193+
"gradientMode": "scheme",
194+
"hideFrom": {
195+
"legend": false,
196+
"tooltip": false,
197+
"viz": false
198+
},
199+
"lineInterpolation": "smooth",
200+
"lineWidth": 3,
201+
"pointSize": 5,
202+
"scaleDistribution": {
203+
"type": "linear"
204+
},
205+
"showPoints": "auto",
206+
"spanNulls": false,
207+
"stacking": {
208+
"group": "A",
209+
"mode": "none"
210+
},
211+
"thresholdsStyle": {
212+
"mode": "off"
213+
}
214+
},
215+
"mappings": [],
216+
"thresholds": {
217+
"mode": "absolute",
218+
"steps": [
219+
{
220+
"color": "green",
221+
"value": null
222+
},
223+
{
224+
"color": "red",
225+
"value": 80
226+
}
227+
]
228+
},
229+
"unit": "bytes"
230+
},
231+
"overrides": []
232+
},
233+
"gridPos": {
234+
"h": 8,
235+
"w": 12,
236+
"x": 12,
237+
"y": 0
238+
},
239+
"id": 4,
240+
"interval": "1m",
241+
"links": [],
242+
"options": {
243+
"legend": {
244+
"calcs": [],
245+
"displayMode": "list",
246+
"placement": "bottom"
247+
},
248+
"tooltip": {
249+
"mode": "single",
250+
"sort": "none"
251+
}
252+
},
253+
"pluginVersion": "8.4.3",
254+
"targets": [
255+
{
256+
"datasource": "${DS_PROMETHEUS}",
257+
"exemplar": true,
258+
"expr": "process_resident_memory_bytes{job=\"$job\", namespace=\"$namespace\", pod=\"$pod\"}",
259+
"format": "time_series",
260+
"interval": "",
261+
"intervalFactor": 2,
262+
"legendFormat": "Pod: {{"{{pod}}"}} | Container: {{"{{container}}"}}",
263+
"refId": "A",
264+
"step": 10
265+
}
266+
],
267+
"title": "Controller Memory Usage",
268+
"type": "timeseries"
269+
}
270+
],
271+
"refresh": "",
272+
"style": "dark",
273+
"tags": [],
274+
"templating": {
275+
"list": [
276+
{
277+
"datasource": "${DS_PROMETHEUS}",
278+
"definition": "label_values(controller_runtime_reconcile_total{namespace=~\"$namespace\"}, job)",
279+
"hide": 0,
280+
"includeAll": false,
281+
"multi": false,
282+
"name": "job",
283+
"options": [],
284+
"query": {
285+
"query": "label_values(controller_runtime_reconcile_total{namespace=~\"$namespace\"}, job)",
286+
"refId": "StandardVariableQuery"
287+
},
288+
"refresh": 2,
289+
"regex": "",
290+
"skipUrlSync": false,
291+
"sort": 0,
292+
"type": "query"
293+
},
294+
{
295+
"current": {
296+
"selected": false,
297+
"text": "observability",
298+
"value": "observability"
299+
},
300+
"datasource": "${DS_PROMETHEUS}",
301+
"definition": "label_values(controller_runtime_reconcile_total, namespace)",
302+
"hide": 0,
303+
"includeAll": false,
304+
"multi": false,
305+
"name": "namespace",
306+
"options": [],
307+
"query": {
308+
"query": "label_values(controller_runtime_reconcile_total, namespace)",
309+
"refId": "StandardVariableQuery"
310+
},
311+
"refresh": 1,
312+
"regex": "",
313+
"skipUrlSync": false,
314+
"sort": 0,
315+
"type": "query"
316+
},
317+
{
318+
"current": {
319+
"selected": false,
320+
"text": "All",
321+
"value": "$__all"
322+
},
323+
"datasource": "${DS_PROMETHEUS}",
324+
"definition": "label_values(controller_runtime_reconcile_total{namespace=~\"$namespace\", job=~\"$job\"}, pod)",
325+
"hide": 2,
326+
"includeAll": true,
327+
"label": "pod",
328+
"multi": true,
329+
"name": "pod",
330+
"options": [],
331+
"query": {
332+
"query": "label_values(controller_runtime_reconcile_total{namespace=~\"$namespace\", job=~\"$job\"}, pod)",
333+
"refId": "StandardVariableQuery"
334+
},
335+
"refresh": 2,
336+
"regex": "",
337+
"skipUrlSync": false,
338+
"sort": 0,
339+
"type": "query"
340+
}
341+
]
342+
},
343+
"time": {
344+
"from": "now-15m",
345+
"to": "now"
346+
},
347+
"timepicker": {},
348+
"timezone": "",
349+
"title": "Controller-Resources-Metrics",
350+
"weekStart": ""
351+
}
352+
`

0 commit comments

Comments
 (0)