This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dashboard.go
467 lines (441 loc) · 15.5 KB
/
dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package dashboard
import (
"fmt"
"net/http"
"os"
"context"
"github.com/K-Phoen/grabana"
"github.com/K-Phoen/grabana/alert"
"github.com/K-Phoen/grabana/dashboard"
"github.com/K-Phoen/grabana/logs"
"github.com/K-Phoen/grabana/row"
"github.com/K-Phoen/grabana/stat"
"github.com/K-Phoen/grabana/target/prometheus"
"github.com/K-Phoen/grabana/timeseries"
"github.com/K-Phoen/grabana/timeseries/axis"
"github.com/K-Phoen/grabana/variable/query"
)
const (
DefaultStatTextSize = 12
DefaultStatValueSize = 20
DefaultAlertEvaluateEvery = "10s"
DefaultAlertFor = "10s"
DefaultDashboardUUID = "Wasp"
DefaultRequirementLabelKey = "requirement_name"
)
const (
AlertTypeQuantile99 = "quantile_99"
AlertTypeErrors = "errors"
AlertTypeTimeouts = "timeouts"
)
type WaspAlert struct {
Name string
AlertType string
TestName string
GenName string
RequirementGroupName string
AlertIf alert.ConditionEvaluator
CustomAlert timeseries.Option
}
// Dashboard is a Wasp dashboard
type Dashboard struct {
Name string
DataSourceName string
Folder string
GrafanaURL string
GrafanaToken string
extendedOpts []dashboard.Option
builder dashboard.Builder
}
// NewDashboard creates new dashboard
func NewDashboard(reqs []WaspAlert, opts []dashboard.Option) (*Dashboard, error) {
name := os.Getenv("DASHBOARD_NAME")
if name == "" {
return nil, fmt.Errorf("DASHBOARD_NAME must be provided")
}
dsn := os.Getenv("DATA_SOURCE_NAME")
if dsn == "" {
return nil, fmt.Errorf("DATA_SOURCE_NAME must be provided")
}
dbf := os.Getenv("DASHBOARD_FOLDER")
if dbf == "" {
return nil, fmt.Errorf("DASHBOARD_FOLDER must be provided")
}
grafanaURL := os.Getenv("GRAFANA_URL")
if grafanaURL == "" {
return nil, fmt.Errorf("GRAFANA_URL must be provided")
}
grafanaToken := os.Getenv("GRAFANA_TOKEN")
if grafanaToken == "" {
return nil, fmt.Errorf("GRAFANA_TOKEN must be provided")
}
dash := &Dashboard{
Name: name,
DataSourceName: dsn,
Folder: dbf,
GrafanaURL: grafanaURL,
GrafanaToken: grafanaToken,
extendedOpts: opts,
}
err := dash.Build(name, dsn, reqs)
if err != nil {
return nil, fmt.Errorf("failed to build dashboard: %s", err)
}
return dash, nil
}
// Deploy deploys this dashboard to some Grafana folder
func (m *Dashboard) Deploy() (*grabana.Dashboard, error) {
ctx := context.Background()
client := grabana.NewClient(&http.Client{}, m.GrafanaURL, grabana.WithAPIToken(m.GrafanaToken))
fo, err := client.FindOrCreateFolder(ctx, m.Folder)
if err != nil {
fmt.Printf("Could not find or create folder: %s\n", err)
os.Exit(1)
}
return client.UpsertDashboard(ctx, fo, m.builder)
}
// defaultStatWidget creates default Stat widget
func defaultStatWidget(name, datasourceName, target, legend string) row.Option {
return row.WithStat(
name,
stat.Transparent(),
stat.DataSource(datasourceName),
stat.Text(stat.TextValueAndName),
stat.Orientation(stat.OrientationHorizontal),
stat.TitleFontSize(DefaultStatTextSize),
stat.ValueFontSize(DefaultStatValueSize),
stat.Span(2),
stat.WithPrometheusTarget(target, prometheus.Legend(legend)),
)
}
// defaultLastValueAlertWidget creates default last value alert
func defaultLastValueAlertWidget(a WaspAlert) timeseries.Option {
if a.CustomAlert != nil {
return a.CustomAlert
}
return timeseries.Alert(
a.Name,
alert.For(DefaultAlertFor),
alert.OnExecutionError(alert.ErrorKO),
alert.Description(a.Name),
alert.Tags(map[string]string{
"service": "wasp",
DefaultRequirementLabelKey: a.RequirementGroupName,
}),
alert.WithLokiQuery(
a.Name,
InlineLokiAlertParams(a.AlertType, a.TestName, a.GenName),
),
alert.If(alert.Last, a.Name, a.AlertIf),
alert.EvaluateEvery(DefaultAlertEvaluateEvery),
)
}
// defaultLabelValuesVar creates a dashboard variable with All/Multiple options
func defaultLabelValuesVar(name, datasourceName string) dashboard.Option {
return dashboard.VariableAsQuery(
name,
query.DataSource(datasourceName),
query.Multiple(),
query.IncludeAll(),
query.Request(fmt.Sprintf("label_values(%s)", name)),
query.Sort(query.NumericalAsc),
)
}
// timeSeriesWithAlerts creates timeseries graphs per alert + definition of alert
func timeSeriesWithAlerts(datasourceName string, alertDefs []WaspAlert) []dashboard.Option {
dashboardOpts := make([]dashboard.Option, 0)
for _, a := range alertDefs {
// for wasp metrics we also create additional row per alert
tsOpts := []timeseries.Option{
timeseries.Transparent(),
timeseries.Span(12),
timeseries.Height("200px"),
timeseries.DataSource(datasourceName),
timeseries.Legend(timeseries.Bottom),
}
tsOpts = append(tsOpts, defaultLastValueAlertWidget(a))
var rowTitle string
// for wasp metrics we also create additional row per alert
if a.CustomAlert == nil {
rowTitle = fmt.Sprintf("Alert: %s, Requirement: %s", a.Name, a.RequirementGroupName)
tsOpts = append(tsOpts, timeseries.WithPrometheusTarget(InlineLokiAlertParams(a.AlertType, a.TestName, a.GenName)))
} else {
rowTitle = fmt.Sprintf("External alert: %s, Requirement: %s", a.Name, a.RequirementGroupName)
}
// all the other custom alerts may burden the dashboard,
dashboardOpts = append(dashboardOpts,
dashboard.Row(
rowTitle,
row.Collapse(),
row.HideTitle(),
row.WithTimeSeries(a.Name, tsOpts...),
))
}
return dashboardOpts
}
func AddVariables(datasourceName string) []dashboard.Option {
opts := []dashboard.Option{
defaultLabelValuesVar("go_test_name", datasourceName),
defaultLabelValuesVar("gen_name", datasourceName),
defaultLabelValuesVar("branch", datasourceName),
defaultLabelValuesVar("commit", datasourceName),
defaultLabelValuesVar("call_group", datasourceName),
}
return opts
}
// dashboard is internal appendable representation of all Dashboard widgets
func (m *Dashboard) dashboard(datasourceName string, requirements []WaspAlert) []dashboard.Option {
panelQuery := map[string]string{
"branch": `=~"${branch:pipe}"`,
"commit": `=~"${commit:pipe}"`,
}
defaultOpts := []dashboard.Option{
dashboard.UID(m.Name),
dashboard.AutoRefresh("5"),
dashboard.Time("now-30m", "now"),
dashboard.Tags([]string{"generated", "load-test"}),
}
defaultOpts = append(defaultOpts, AddVariables(datasourceName)...)
defaultOpts = append(defaultOpts, WASPLoadStatsRow(datasourceName, panelQuery))
defaultOpts = append(defaultOpts, WASPDebugDataRow(datasourceName, panelQuery, false))
defaultOpts = append(defaultOpts, timeSeriesWithAlerts(datasourceName, requirements)...)
defaultOpts = append(defaultOpts, m.extendedOpts...)
return defaultOpts
}
// Build creates dashboard instance
func (m *Dashboard) Build(dashboardName, datasourceName string, requirements []WaspAlert) error {
b, err := dashboard.New(
dashboardName,
m.dashboard(datasourceName, requirements)...,
)
if err != nil {
return fmt.Errorf("failed to create a dashboard builder: %s", err)
}
m.builder = b
return nil
}
// JSON render dashboard as JSON
func (m *Dashboard) JSON() ([]byte, error) {
return m.builder.MarshalIndentJSON()
}
// InlineLokiAlertParams is specific params for predefined alerts for wasp dashboard
func InlineLokiAlertParams(queryType, testName, genName string) string {
switch queryType {
case AlertTypeQuantile99:
return fmt.Sprintf(`
avg(quantile_over_time(0.99, {go_test_name="%s", test_data_type=~"responses", gen_name="%s"}
| json
| unwrap duration [10s]) / 1e6)`, testName, genName)
case AlertTypeErrors:
return fmt.Sprintf(`
max_over_time({go_test_name="%s", test_data_type=~"stats", gen_name="%s"}
| json
| unwrap failed [10s]) by (go_test_name, gen_name)`, testName, genName)
case AlertTypeTimeouts:
return fmt.Sprintf(`
max_over_time({go_test_name="%s", test_data_type=~"stats", gen_name="%s"}
| json
| unwrap callTimeout [10s]) by (go_test_name, gen_name)`, testName, genName)
default:
return ""
}
}
func WASPLoadStatsRow(dataSource string, query map[string]string) dashboard.Option {
queryString := ""
for key, value := range query {
queryString += key + value + ", "
}
return dashboard.Row(
"WASP Load Stats",
defaultStatWidget(
"RPS (Now)",
dataSource,
`sum(last_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"stats", gen_name=~"${gen_name:pipe}"}
| json
| unwrap current_rps [1s]) by (node_id, go_test_name, gen_name)) by (__stream_shard__)`,
`{{go_test_name}} {{gen_name}} RPS`,
),
defaultStatWidget(
"VUs (Now)",
dataSource,
`sum(max_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"stats", gen_name=~"${gen_name:pipe}"}
| json
| unwrap current_instances [$__range]) by (node_id, go_test_name, gen_name)) by (__stream_shard__)`,
`{{go_test_name}} {{gen_name}} VUs`,
),
defaultStatWidget(
"Responses/sec (Now)",
dataSource,
`sum(count_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"responses", gen_name=~"${gen_name:pipe}"} [1s])) by (node_id, go_test_name, gen_name)`,
`{{go_test_name}} {{gen_name}} Responses/sec`,
),
defaultStatWidget(
"Successful requests (Total)",
dataSource,
`
sum(max_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"stats", gen_name=~"${gen_name:pipe}"}
| json
| unwrap success [$__range]) by (node_id, go_test_name, gen_name)) by (__stream_shard__)
`,
`{{go_test_name}} {{gen_name}} Successful requests`,
),
defaultStatWidget(
"Errored requests (Total)",
dataSource,
`
sum(max_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"stats", gen_name=~"${gen_name:pipe}"}
| json
| unwrap failed [$__range]) by (node_id, go_test_name, gen_name)) by (__stream_shard__)
`,
`{{go_test_name}} {{gen_name}} Errored requests`,
),
defaultStatWidget(
"Timed out requests (Total)",
dataSource,
`
sum(max_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"stats", gen_name=~"${gen_name:pipe}"}
| json
| unwrap callTimeout [$__range]) by (node_id, go_test_name, gen_name)) by (__stream_shard__)
`,
`{{go_test_name}} {{gen_name}} Timed out requests`,
),
RPSVUPerScheduleSegmentsPanel(dataSource, query),
RPSPanel(dataSource, query),
row.WithTimeSeries(
"Latency quantiles over groups (99, 95, 50)",
timeseries.Legend(timeseries.Hide),
timeseries.Transparent(),
timeseries.Span(6),
timeseries.Height("300px"),
timeseries.DataSource(dataSource),
timeseries.Legend(timeseries.Bottom),
timeseries.Axis(
axis.Unit("ms"),
axis.Label("ms"),
),
timeseries.WithPrometheusTarget(`
quantile_over_time(0.99, {`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"responses", gen_name=~"${gen_name:pipe}"}
| json
| unwrap duration [$__interval]) by (go_test_name, gen_name) / 1e6`,
prometheus.Legend("{{go_test_name}} {{gen_name}} Q 99 - {{error}}"),
),
timeseries.WithPrometheusTarget(`
quantile_over_time(0.95, {`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"responses", gen_name=~"${gen_name:pipe}"}
| json
| unwrap duration [$__interval]) by (go_test_name, gen_name) / 1e6`,
prometheus.Legend("{{go_test_name}} {{gen_name}} Q 95 - {{error}}"),
),
timeseries.WithPrometheusTarget(
`
quantile_over_time(0.50, {`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"responses", gen_name=~"${gen_name:pipe}"}
| json
| unwrap duration [$__interval]) by (go_test_name, gen_name) / 1e6`,
prometheus.Legend("{{go_test_name}} {{gen_name}} Q 50 - {{error}}"),
),
),
row.WithTimeSeries(
"Responses latencies by types over time (Generator, CallGroup)",
timeseries.Legend(timeseries.Hide),
timeseries.Transparent(),
timeseries.Span(6),
timeseries.Height("300px"),
timeseries.DataSource(dataSource),
timeseries.Axis(
axis.Unit("ms"),
axis.Label("ms"),
),
timeseries.Legend(timeseries.Bottom),
timeseries.WithPrometheusTarget(`
last_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"responses", gen_name=~"${gen_name:pipe}", call_group=~"${call_group}"}
| json
| unwrap duration [$__interval]) / 1e6`,
prometheus.Legend("{{go_test_name}} {{gen_name}} {{call_group}} T: {{timeout}} E: {{error}}"),
),
timeseries.WithPrometheusTarget(`
last_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"responses", gen_name=~"${gen_name:pipe}"}
| json
| unwrap duration [$__interval]) / 1e6`,
prometheus.Legend("{{go_test_name}} {{gen_name}} all groups T: {{timeout}} E: {{error}}"),
),
),
)
}
func WASPDebugDataRow(dataSource string, query map[string]string, collapse bool) dashboard.Option {
queryString := ""
for key, value := range query {
queryString += key + value + ", "
}
defaultRowOpts := []row.Option{}
if collapse {
defaultRowOpts = append(defaultRowOpts, row.Collapse())
}
return dashboard.Row(
"WASP Debug",
append(defaultRowOpts,
row.WithStat(
"Latest segment stats",
stat.Transparent(),
stat.DataSource(dataSource),
stat.Text(stat.TextValueAndName),
stat.SparkLine(),
stat.Span(12),
stat.Height("100px"),
stat.ColorValue(),
stat.WithPrometheusTarget(`
sum(bytes_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", gen_name=~"${gen_name:pipe}"} [$__range]) * 1e-6)
`, prometheus.Legend("Overall logs size"),
),
stat.WithPrometheusTarget(`
sum(bytes_rate({`+queryString+`go_test_name=~"${go_test_name:pipe}", gen_name=~"${gen_name:pipe}"} [$__interval]) * 1e-6)
`, prometheus.Legend("Logs size per second"),
),
),
row.WithTimeSeries(
"CallResult sampling (successful results)",
timeseries.Transparent(),
timeseries.Span(12),
timeseries.Height("200px"),
timeseries.DataSource(dataSource),
timeseries.Axis(
axis.Label("CallResults"),
),
timeseries.WithPrometheusTarget(`
sum(last_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"stats", gen_name=~"${gen_name:pipe}"}
| json
| unwrap samples_recorded [$__interval])) by (go_test_name, gen_name)
`, prometheus.Legend("{{go_test_name}} {{gen_name}} recorded"),
),
timeseries.WithPrometheusTarget(`
sum(last_over_time({`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"stats", gen_name=~"${gen_name:pipe}"}
| json
| unwrap samples_skipped [$__interval])) by (go_test_name, gen_name)
`, prometheus.Legend("{{go_test_name}} {{gen_name}} skipped"),
),
),
row.WithLogs(
"Stats logs",
logs.DataSource(dataSource),
logs.Span(12),
logs.Height("300px"),
logs.Transparent(),
logs.WithLokiTarget(`{`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"stats", gen_name=~"${gen_name:pipe}"}`),
),
row.WithLogs(
"Failed responses",
logs.DataSource(dataSource),
logs.Span(6),
logs.Height("300px"),
logs.Transparent(),
logs.WithLokiTarget(`{`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"responses", gen_name=~"${gen_name:pipe}"} |~ "failed\":true"`),
),
row.WithLogs(
"Timed out responses",
logs.DataSource(dataSource),
logs.Span(6),
logs.Height("300px"),
logs.Transparent(),
logs.WithLokiTarget(`{`+queryString+`go_test_name=~"${go_test_name:pipe}", test_data_type=~"responses", gen_name=~"${gen_name:pipe}"} |~ "timeout\":true"`),
),
)...,
)
}