Skip to content

Commit fcb2850

Browse files
committed
grafana dashboards
1 parent 6649238 commit fcb2850

File tree

28 files changed

+3908
-0
lines changed

28 files changed

+3908
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
function createGrafanaPanelObject(
2+
metric,
3+
datasource,
4+
graphType
5+
) {
6+
// Create a panel object to be used within dashboards.
7+
const panel = {
8+
"datasource": datasource,
9+
"fieldConfig": {
10+
"defaults": {
11+
"color": {
12+
"mode": "palette-classic"
13+
},
14+
"custom": {
15+
"axisCenteredZero": false,
16+
"axisColorMode": "text",
17+
"axisLabel": "",
18+
"axisPlacement": "auto",
19+
"barAlignment": 0,
20+
"drawStyle": "line",
21+
"fillOpacity": 40,
22+
"gradientMode": "opacity",
23+
"hideFrom": {
24+
"legend": false,
25+
"tooltip": false,
26+
"viz": false
27+
},
28+
"lineInterpolation": "smooth",
29+
"lineWidth": 2,
30+
"pointSize": 5,
31+
"scaleDistribution": {
32+
"type": "linear"
33+
},
34+
"showPoints": "auto",
35+
"spanNulls": false,
36+
"stacking": {
37+
"group": "A",
38+
"mode": "none"
39+
},
40+
"thresholdsStyle": {
41+
"mode": "off"
42+
}
43+
},
44+
"mappings": [],
45+
"thresholds": {
46+
"mode": "absolute",
47+
"steps": [
48+
{
49+
"color": "green",
50+
"value": null
51+
},
52+
{
53+
"color": "red",
54+
"value": 80
55+
}
56+
]
57+
},
58+
"min": 0
59+
},
60+
"overrides": []
61+
},
62+
"gridPos": {
63+
"h": 8,
64+
"w": 12,
65+
"x": 0,
66+
"y": 0
67+
},
68+
"options": {
69+
"legend": {
70+
"calcs": [],
71+
"displayMode": "list",
72+
"placement": "bottom",
73+
"showLegend": true
74+
},
75+
"tooltip": {
76+
"mode": "single",
77+
"sort": "none"
78+
},
79+
// "displayLabels": [
80+
// "percent"
81+
// ]
82+
83+
},
84+
"id": 1,
85+
"targets": [{
86+
"datasource": datasource,
87+
"editorMode": "builder",
88+
"expr": metric.metric.replace(/.*\/.*\//g, ''),
89+
"instant": false,
90+
"range": true,
91+
"refId": "A"
92+
}],
93+
"title": metric.metric.replace(/.*\/.*\//g, ''),
94+
"type": graphType,
95+
"interval": "2s"
96+
}
97+
return panel;
98+
}
99+
100+
function updateGrafanaPanelObject(
101+
metric,
102+
datasource,
103+
graphType
104+
) {
105+
// Create a panel object to be used within dashboards.
106+
const panel = {
107+
"datasource": datasource,
108+
"fieldConfig": {
109+
"defaults": {
110+
"color": {
111+
"mode": "palette-classic"
112+
},
113+
"custom": {
114+
"axisCenteredZero": false,
115+
"axisColorMode": "text",
116+
"axisLabel": "",
117+
"axisPlacement": "auto",
118+
"barAlignment": 0,
119+
"drawStyle": "line",
120+
"fillOpacity": 40,
121+
"gradientMode": "opacity",
122+
"hideFrom": {
123+
"legend": false,
124+
"tooltip": false,
125+
"viz": false
126+
},
127+
"lineInterpolation": "smooth",
128+
"lineWidth": 2,
129+
"pointSize": 5,
130+
"scaleDistribution": {
131+
"type": "linear"
132+
},
133+
"showPoints": "auto",
134+
"spanNulls": false,
135+
"stacking": {
136+
"group": "A",
137+
"mode": "none"
138+
},
139+
"thresholdsStyle": {
140+
"mode": "off"
141+
}
142+
},
143+
"mappings": [],
144+
"thresholds": {
145+
"mode": "absolute",
146+
"steps": [
147+
{
148+
"color": "green",
149+
"value": null
150+
},
151+
{
152+
"color": "red",
153+
"value": 80
154+
}
155+
]
156+
},
157+
"min": 0
158+
},
159+
"overrides": []
160+
},
161+
"gridPos": {
162+
"h": 8,
163+
"w": 12,
164+
"x": 0,
165+
"y": 0
166+
},
167+
"options": {
168+
"legend": {
169+
"calcs": [],
170+
"displayMode": "list",
171+
"placement": "bottom",
172+
"showLegend": true
173+
},
174+
"tooltip": {
175+
"mode": "single",
176+
"sort": "none"
177+
},
178+
"displayLabels": [
179+
"percent"
180+
]
181+
},
182+
"id": 1,
183+
"targets": [{
184+
"datasource": datasource,
185+
"editorMode": "builder",
186+
"expr": metric.replace(/.*\/.*\//g, ''),
187+
"instant": false,
188+
"range": true,
189+
"refId": "A"
190+
}],
191+
"title": metric.replace(/.*\/.*\//g, ''),
192+
"type": graphType,
193+
"interval": "2s"
194+
}
195+
return panel;
196+
}
197+
198+
module.exports = { createGrafanaPanelObject, updateGrafanaPanelObject };
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const axios = require('axios');
2+
const createGrafanaPanelObject = require('./createGrafanaPanelObject,js');
3+
4+
async function createGrafanaDashboard(
5+
metrix,
6+
datasource,
7+
) {
8+
// create dashboard object boilerplate
9+
const dashboard = {
10+
"dashboard": {
11+
"id": null,
12+
"uid": metric.meric.replace(/.*\/.*\//g, ''),
13+
"title": metric.meric.replace(/.*\/.*\//g, ''),
14+
"tags": ["templated"],
15+
"timezone": "browser",
16+
"schemaVersion": 16,
17+
"version": 0,
18+
"refresh": "10s",
19+
panels: [],
20+
},
21+
folderId: 0,
22+
overwrite: true,
23+
};
24+
25+
26+
// push panel into dashboard object with a line for each metric in promQLQueries object
27+
dashboard.dashboard.panels.push(createGrafanaPanelObject(metrix, datasource));
28+
29+
try {
30+
// POST request to Grafana Dashboard API to create a dashboard
31+
const dashboardResponse = await axios.post(
32+
'http://localhost:32000/api/dashboards/db',
33+
JSON.stringify(dashboard),
34+
{
35+
headers: {
36+
'Content-Type': 'application/json',
37+
'Authorization': 'Bearer glsa_pITqM0BIfNHNKL4PsXJqmTYQl0D9QGxF_486f63e1'
38+
},
39+
}
40+
);
41+
42+
// Descriptive error log for developers
43+
if (dashboardResponse.status >= 400) {
44+
console.log(
45+
'Error with POST request to Grafana Dashboards API. In createGrafanaDashboardObject.'
46+
);
47+
} else {
48+
// A simple console log to show when graphs are done being posted to Grafana.
49+
console.log(`📊 Grafana graphs 📊 for the ${containerName} container are ready!!`);
50+
}
51+
} catch (err) {
52+
console.log(err);
53+
}
54+
}
55+
56+
module.exports = createGrafanaDashboard;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const axios = require('axios');
2+
3+
async function getGrafanaDatasource() {
4+
// Fetch datasource information from grafana API.
5+
// This datasource is PRECONFIGURED on launch using grafana config.
6+
const datasourceResponse = await axios.get('http://localhost:32000/api/datasources');
7+
8+
// Create a datasource object to be used within panels.
9+
const datasource = {
10+
type: datasourceResponse[0].type,
11+
uid: datasourceResponse[0].uid,
12+
};
13+
14+
return datasource;
15+
}
16+
17+
module.exports = getGrafanaDatasource;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const mongoose = require('mongoose');
2+
3+
const { Schema } = mongoose;
4+
5+
const GrafanaAPIKeySchema = new Schema({
6+
token: {
7+
type: String,
8+
unique: true,
9+
}
10+
});
11+
12+
module.exports = mongoose.model('grafanaAPI', GrafanaAPIKeySchema);

0 commit comments

Comments
 (0)