forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboards.go
623 lines (573 loc) · 18.6 KB
/
dashboards.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package business
import (
"fmt"
"sort"
"strings"
"sync"
"k8s.io/client-go/tools/clientcmd/api"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes/monitoringdashboards"
"github.com/kiali/kiali/kubernetes/monitoringdashboards/v1alpha1"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
"github.com/kiali/kiali/prometheus"
"github.com/kiali/kiali/prometheus/internalmetrics"
)
const defaultNamespaceLabel = "namespace"
// DashboardsService deals with fetching dashboards from k8s client
type DashboardsService struct {
promClient prometheus.ClientInterface
k8sClient monitoringdashboards.ClientInterface
promConfig config.PrometheusConfig
globalNamespace string
namespaceLabel string
CustomEnabled bool
}
// NewDashboardsService initializes this business service
func NewDashboardsService() *DashboardsService {
cfg := config.Get()
customEnabled := cfg.ExternalServices.CustomDashboards.Enabled
prom := cfg.ExternalServices.Prometheus
if customEnabled && cfg.ExternalServices.CustomDashboards.Prometheus.URL != "" {
prom = cfg.ExternalServices.CustomDashboards.Prometheus
}
nsLabel := cfg.ExternalServices.CustomDashboards.NamespaceLabel
if nsLabel == "" {
nsLabel = "kubernetes_namespace"
}
return &DashboardsService{
CustomEnabled: customEnabled,
promConfig: prom,
globalNamespace: cfg.Deployment.Namespace,
namespaceLabel: nsLabel,
}
}
func (in *DashboardsService) prom() (prometheus.ClientInterface, error) {
// Lazy init
if in.promClient == nil {
client, err := prometheus.NewClientForConfig(in.promConfig)
if err != nil {
return nil, fmt.Errorf("cannot initialize Prometheus Client: %v", err)
}
in.promClient = client
}
return in.promClient, nil
}
func (in *DashboardsService) k8s() (monitoringdashboards.ClientInterface, error) {
// Lazy init
if in.k8sClient == nil {
client, err := monitoringdashboards.NewClient()
if err != nil {
return nil, fmt.Errorf("cannot initialize Kubernetes Client: %v", err)
}
in.k8sClient = client
}
return in.k8sClient, nil
}
// k8sGetDashboard wraps KialiMonitoringInterface.GetDashboard with client creation & error handling
func (in *DashboardsService) k8sGetDashboard(namespace, template string) (*v1alpha1.MonitoringDashboard, error) {
client, err := in.k8s()
if err != nil {
return nil, err
}
log.Tracef("load k8s dashboard '%s' in namespace '%s'", template, namespace)
return client.GetDashboard(namespace, template)
}
// k8sGetDashboards wraps KialiMonitoringInterface.GetDashboards with client creation & error handling
func (in *DashboardsService) k8sGetDashboards(namespace string) ([]v1alpha1.MonitoringDashboard, error) {
client, err := in.k8s()
if err != nil {
return nil, err
}
log.Tracef("load all k8s dashboards in namespace '%s'", namespace)
return client.GetDashboards(namespace)
}
func (in *DashboardsService) loadRawDashboardResource(namespace, template string) (*v1alpha1.MonitoringDashboard, error) {
// There is an override mechanism with dashboards: default dashboards can be provided in Kiali namespace,
// and can be overriden in app namespace.
// So we look for the one in app namespace first, and only if not found fallback to the one in istio-system.
dashboard, err := in.k8sGetDashboard(namespace, template)
if err != nil && in.globalNamespace != "" {
dashboard, err = in.k8sGetDashboard(in.globalNamespace, template)
if err != nil {
return nil, err
}
}
return dashboard, err
}
func (in *DashboardsService) loadRawDashboardResources(namespace string) (map[string]v1alpha1.MonitoringDashboard, error) {
all := make(map[string]v1alpha1.MonitoringDashboard)
// From global namespace
if in.globalNamespace != "" {
dashboards, err := in.k8sGetDashboards(in.globalNamespace)
if err != nil {
return nil, err
}
for _, d := range dashboards {
all[d.Name] = d
}
}
// From specific namespace
if namespace != in.globalNamespace {
dashboards, err := in.k8sGetDashboards(namespace)
if err != nil {
return nil, err
}
for _, d := range dashboards {
all[d.Name] = d
}
}
return all, nil
}
func (in *DashboardsService) loadAndResolveDashboardResource(namespace, template string, loaded map[string]bool) (*v1alpha1.MonitoringDashboard, error) {
// Circular dependency check
if _, ok := loaded[template]; ok {
return nil, fmt.Errorf("cannot load dashboard %s due to circular dependency detected. Already loaded dependencies: %v", template, loaded)
}
loaded[template] = true
dashboard, err := in.loadRawDashboardResource(namespace, template)
if err != nil {
return nil, err
}
err = in.resolveReferences(namespace, dashboard, loaded)
return dashboard, err
}
// resolveReferences resolves the composition mechanism that allows to reference a dashboard from another one
func (in *DashboardsService) resolveReferences(namespace string, dashboard *v1alpha1.MonitoringDashboard, loaded map[string]bool) error {
resolved := []v1alpha1.MonitoringDashboardItem{}
for _, item := range dashboard.Spec.Items {
reference := strings.TrimSpace(item.Include)
if reference != "" {
// reference can point to a whole dashboard (ex: microprofile-1.0) or a chart within a dashboard (ex: microprofile-1.0$Thread count)
parts := strings.Split(reference, "$")
dashboardRefName := parts[0]
composedDashboard, err := in.loadAndResolveDashboardResource(namespace, dashboardRefName, loaded)
if err != nil {
return err
}
for _, item2 := range composedDashboard.Spec.Items {
if len(parts) > 1 {
// Reference a specific chart
if item2.Chart.Name == parts[1] {
resolved = append(resolved, item2)
break
}
} else {
// Reference the whole dashboard
resolved = append(resolved, item2)
}
}
} else {
resolved = append(resolved, item)
}
}
dashboard.Spec.Items = resolved
return nil
}
// GetDashboard returns a dashboard filled-in with target data
func (in *DashboardsService) GetDashboard(authInfo *api.AuthInfo, params models.DashboardQuery, template string) (*models.MonitoringDashboard, error) {
promClient, err := in.prom()
if err != nil {
return nil, err
}
dashboard, err := in.loadAndResolveDashboardResource(params.Namespace, template, map[string]bool{})
if err != nil {
return nil, err
}
filters := in.buildLabels(params.Namespace, params.LabelsFilters)
aggLabels := append(params.AdditionalLabels, models.ConvertAggregations(dashboard.Spec)...)
if len(aggLabels) == 0 {
// Prevent null in json
aggLabels = []models.Aggregation{}
}
wg := sync.WaitGroup{}
wg.Add(len(dashboard.Spec.Items) + 1)
filledCharts := make([]models.Chart, len(dashboard.Spec.Items))
for i, item := range dashboard.Spec.Items {
go func(idx int, chart v1alpha1.MonitoringDashboardChart) {
defer wg.Done()
conversionParams := models.ConversionParams{Scale: 1.0, SortLabel: chart.SortLabel, SortLabelParseAs: chart.SortLabelParseAs}
if chart.UnitScale != 0.0 {
conversionParams.Scale = chart.UnitScale
}
// Group by labels is concat of what is defined in CR + what is passed as parameters
byLabels := append(chart.GroupLabels, params.ByLabels...)
if len(chart.SortLabel) > 0 {
// We also need to group by the label used for sorting, if not explicitly present
present := false
for _, lbl := range byLabels {
if lbl == chart.SortLabel {
present = true
break
}
}
if !present {
byLabels = append(byLabels, chart.SortLabel)
// Mark the sort label to not be kept during conversion
conversionParams.RemoveSortLabel = true
}
}
grouping := strings.Join(byLabels, ",")
filledCharts[idx] = models.ConvertChart(chart)
metrics := chart.GetMetrics()
for _, ref := range metrics {
var converted []models.Metric
var err error
if chart.DataType == v1alpha1.Raw {
aggregator := params.RawDataAggregator
if chart.Aggregator != "" {
aggregator = chart.Aggregator
}
metric := promClient.FetchRange(ref.MetricName, filters, grouping, aggregator, ¶ms.RangeQuery)
converted, err = models.ConvertMetric(ref.DisplayName, metric, conversionParams)
} else if chart.DataType == v1alpha1.Rate {
metric := promClient.FetchRateRange(ref.MetricName, []string{filters}, grouping, ¶ms.RangeQuery)
converted, err = models.ConvertMetric(ref.DisplayName, metric, conversionParams)
} else {
histo := promClient.FetchHistogramRange(ref.MetricName, filters, grouping, ¶ms.RangeQuery)
converted, err = models.ConvertHistogram(ref.DisplayName, histo, conversionParams)
}
// Fill in chart
if err != nil {
filledCharts[idx].Error = err.Error()
} else {
filledCharts[idx].Metrics = append(filledCharts[idx].Metrics, converted...)
}
}
}(i, item.Chart)
}
var externalLinks []models.ExternalLink
go func() {
defer wg.Done()
links, _, err := GetGrafanaLinks(authInfo, dashboard.Spec.ExternalLinks)
if err != nil {
log.Errorf("Error while getting Grafana links: %v", err)
}
if links != nil {
externalLinks = links
} else {
externalLinks = []models.ExternalLink{}
}
}()
wg.Wait()
return &models.MonitoringDashboard{
Title: dashboard.Spec.Title,
Charts: filledCharts,
Aggregations: aggLabels,
ExternalLinks: externalLinks,
}, nil
}
// SearchExplicitDashboards will check annotations of all supplied pods to extract a unique list of dashboards
// Accepted annotations are "kiali.io/runtimes" and "kiali.io/dashboards"
func (in *DashboardsService) SearchExplicitDashboards(namespace string, pods []models.Pod) []models.Runtime {
uniqueRefsList := extractUniqueDashboards(pods)
if len(uniqueRefsList) > 0 {
log.Tracef("getting dashboards from refs list: %v", uniqueRefsList)
return in.buildRuntimesList(namespace, uniqueRefsList)
}
return []models.Runtime{}
}
func (in *DashboardsService) buildRuntimesList(namespace string, templatesNames []string) []models.Runtime {
dashboards := make([]*v1alpha1.MonitoringDashboard, len(templatesNames))
wg := sync.WaitGroup{}
wg.Add(len(templatesNames))
for idx, template := range templatesNames {
go func(i int, tpl string) {
defer wg.Done()
dashboard, err := in.loadRawDashboardResource(namespace, tpl)
if err != nil {
log.Errorf("cannot get dashboard %s in namespace %s. Error was: %v", tpl, namespace, err)
} else {
dashboards[i] = dashboard
}
}(idx, template)
}
wg.Wait()
runtimes := []models.Runtime{}
for _, dashboard := range dashboards {
if dashboard == nil {
continue
}
runtimes = addDashboardToRuntimes(dashboard, runtimes)
}
return runtimes
}
func (in *DashboardsService) fetchMetricNames(namespace string, labelsFilters map[string]string) []string {
promClient, err := in.prom()
if err != nil {
return []string{}
}
labels := in.buildLabels(namespace, labelsFilters)
metrics, err := promClient.GetMetricsForLabels([]string{labels})
if err != nil {
log.Errorf("runtimes discovery failed, cannot load metrics for labels: %s. Error was: %v", labels, err)
}
return metrics
}
// discoverDashboards tries to discover dashboards based on existing metrics
func (in *DashboardsService) discoverDashboards(namespace string, labelsFilters map[string]string) []models.Runtime {
log.Tracef("starting runtimes discovery on namespace %s with filters [%v]", namespace, labelsFilters)
var metrics []string
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
metrics = in.fetchMetricNames(namespace, labelsFilters)
}()
allDashboards, err := in.loadRawDashboardResources(namespace)
if err != nil {
log.Errorf("runtimes discovery failed, cannot load dashboards in namespace %s. Error was: %v", namespace, err)
return []models.Runtime{}
}
wg.Wait()
return runDiscoveryMatcher(metrics, allDashboards)
}
func runDiscoveryMatcher(metrics []string, allDashboards map[string]v1alpha1.MonitoringDashboard) []models.Runtime {
// In all dashboards, finds the ones that match the metrics set
// We must exclude from the results included dashboards when both the including and the included dashboards are matching
runtimesMap := make(map[string]*v1alpha1.MonitoringDashboard)
for _, d := range allDashboards {
dashboard := d // sticky reference
matchReference := strings.TrimSpace(dashboard.Spec.DiscoverOn)
if matchReference != "" {
for _, metric := range metrics {
if matchReference == strings.TrimSpace(metric) {
if _, exists := runtimesMap[dashboard.Name]; !exists {
runtimesMap[dashboard.Name] = &dashboard
}
// Mark included dashboards as already found
// and set them "nil" to not show them as standalone dashboards even if they match
for _, item := range dashboard.Spec.Items {
if item.Include != "" {
runtimesMap[item.Include] = nil
}
}
break
}
}
}
}
runtimes := []models.Runtime{}
for _, dashboard := range runtimesMap {
if dashboard != nil {
runtimes = addDashboardToRuntimes(dashboard, runtimes)
}
}
sort.Slice(runtimes, func(i, j int) bool { return runtimes[i].Name < runtimes[j].Name })
return runtimes
}
func addDashboardToRuntimes(dashboard *v1alpha1.MonitoringDashboard, runtimes []models.Runtime) []models.Runtime {
runtime := dashboard.Spec.Runtime
ref := models.DashboardRef{
Template: dashboard.Name,
Title: dashboard.Spec.Title,
}
found := false
for i := range runtimes {
rtObj := &runtimes[i]
if rtObj.Name == runtime {
rtObj.DashboardRefs = append(rtObj.DashboardRefs, ref)
found = true
break
}
}
if !found {
runtimes = append(runtimes, models.Runtime{
Name: runtime,
DashboardRefs: []models.DashboardRef{ref},
})
}
return runtimes
}
func (in *DashboardsService) buildLabels(namespace string, labelsFilters map[string]string) string {
namespaceLabel := in.namespaceLabel
if namespaceLabel == "" {
namespaceLabel = defaultNamespaceLabel
}
labels := fmt.Sprintf(`{%s="%s"`, namespaceLabel, namespace)
for k, v := range labelsFilters {
labels += fmt.Sprintf(`,%s="%s"`, k, v)
}
labels += "}"
return labels
}
type istioChart struct {
models.Chart
refName string
scale float64
}
func getIstioCharts() []istioChart {
istioCharts := []istioChart{
{
Chart: models.Chart{
Name: "Request volume",
Unit: "ops",
Spans: 6,
},
refName: "request_count",
},
{
Chart: models.Chart{
Name: "Request duration",
Unit: "seconds",
Spans: 6,
},
refName: "request_duration_millis",
scale: 0.001,
},
{
Chart: models.Chart{
Name: "Request throughput",
Unit: "bitrate",
Spans: 6,
},
refName: "request_throughput",
scale: 8, // Bps to bps
},
{
Chart: models.Chart{
Name: "Request size",
Unit: "bytes",
Spans: 6,
},
refName: "request_size",
},
{
Chart: models.Chart{
Name: "Response throughput",
Unit: "bitrate",
Spans: 6,
},
refName: "response_throughput",
scale: 8, // Bps to bps
},
{
Chart: models.Chart{
Name: "Response size",
Unit: "bytes",
Spans: 6,
},
refName: "response_size",
},
{
Chart: models.Chart{
Name: "TCP received",
Unit: "bitrate",
Spans: 6,
},
refName: "tcp_received",
},
{
Chart: models.Chart{
Name: "TCP sent",
Unit: "bitrate",
Spans: 6,
},
refName: "tcp_sent",
},
}
return istioCharts
}
func GetIstioScaler() func(name string) float64 {
charts := getIstioCharts()
return func(name string) float64 {
for _, c := range charts {
if c.refName == name {
return c.scale
}
}
return 1.0
}
}
// GetIstioDashboard returns Istio dashboard (currently hard-coded) filled-in with metrics
func (in *DashboardsService) BuildIstioDashboard(metrics models.MetricsMap, direction string) *models.MonitoringDashboard {
var dashboard models.MonitoringDashboard
// Copy dashboard
if direction == "inbound" {
dashboard = models.PrepareIstioDashboard("Inbound", "destination", "source")
} else {
dashboard = models.PrepareIstioDashboard("Outbound", "source", "destination")
}
istioCharts := getIstioCharts()
for _, chartTpl := range istioCharts {
newChart := chartTpl.Chart
conversionParams := models.ConversionParams{Scale: 1.0}
if chartTpl.scale != 0.0 {
conversionParams.Scale = chartTpl.scale
}
if metrics := metrics[chartTpl.refName]; metrics != nil {
newChart.Metrics = metrics
} else {
newChart.Metrics = []models.Metric{}
}
dashboard.Charts = append(dashboard.Charts, newChart)
}
return &dashboard
}
// GetCustomDashboardRefs finds all dashboard IDs and Titles associated to this app and add them to the model
func (in *DashboardsService) GetCustomDashboardRefs(namespace, app, version string, pods []*models.Pod) []models.Runtime {
if !in.CustomEnabled {
// Custom dashboards are disabled
return []models.Runtime{}
}
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "DashboardsService", "GetCustomDashboardRefs")
defer promtimer.ObserveNow(&err)
// A better way to do?
var podsCast []models.Pod
for _, p := range pods {
podsCast = append(podsCast, *p)
}
runtimes := in.SearchExplicitDashboards(namespace, podsCast)
if len(runtimes) == 0 {
cfg := config.Get()
discoveryEnabled := cfg.ExternalServices.CustomDashboards.DiscoveryEnabled
if discoveryEnabled == config.DashboardsDiscoveryEnabled ||
(discoveryEnabled == config.DashboardsDiscoveryAuto &&
len(pods) <= cfg.ExternalServices.CustomDashboards.DiscoveryAutoThreshold) {
filters := make(map[string]string)
if app != "" {
filters[cfg.IstioLabels.AppLabelName] = app
}
if version != "" {
filters[cfg.IstioLabels.VersionLabelName] = version
}
runtimes = in.discoverDashboards(namespace, filters)
}
}
return runtimes
}
func extractUniqueDashboards(pods []models.Pod) []string {
// Get uniqueness from plain list rather than map to preserve ordering; anyway, very low amount of objects is expected
uniqueRefs := []string{}
for _, pod := range pods {
// Check for custom dashboards annotation
dashboards := extractDashboardsFromAnnotation(pod, "kiali.io/runtimes")
dashboards = append(dashboards, extractDashboardsFromAnnotation(pod, "kiali.io/dashboards")...)
for _, ref := range dashboards {
if ref != "" {
exists := false
for _, existingRef := range uniqueRefs {
if ref == existingRef {
exists = true
break
}
}
if !exists {
uniqueRefs = append(uniqueRefs, ref)
}
}
}
}
return uniqueRefs
}
func extractDashboardsFromAnnotation(pod models.Pod, annotation string) []string {
dashboards := []string{}
if rawDashboards, ok := pod.Annotations[annotation]; ok {
rawDashboardsSlice := strings.Split(rawDashboards, ",")
for _, dashboard := range rawDashboardsSlice {
dashboards = append(dashboards, strings.TrimSpace(dashboard))
}
}
return dashboards
}