forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboards.go
109 lines (98 loc) · 3.23 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
package models
import (
"fmt"
"sort"
"github.com/kiali/kiali/kubernetes/kiali_monitoring/v1alpha1"
"github.com/kiali/kiali/prometheus"
)
// MonitoringDashboard is the model representing custom monitoring dashboard, transformed from MonitoringDashboard k8s resource
type MonitoringDashboard struct {
Title string `json:"title"`
Charts []Chart `json:"charts"`
Aggregations []Aggregation `json:"aggregations"`
}
// Chart is the model representing a custom chart, transformed from charts in MonitoringDashboard k8s resource
type Chart struct {
Name string `json:"name"`
Unit string `json:"unit"`
Spans int `json:"spans"`
Metric *prometheus.Metric `json:"metric"`
Histogram prometheus.Histogram `json:"histogram"`
}
// ConvertChart converts a k8s chart (from MonitoringDashboard k8s resource) into this models chart
func ConvertChart(from v1alpha1.MonitoringDashboardChart) Chart {
return Chart{
Name: from.Name,
Unit: from.Unit,
Spans: from.Spans,
}
}
// Aggregation is the model representing label's allowed aggregation, transformed from aggregation in MonitoringDashboard k8s resource
type Aggregation struct {
Label string `json:"label"`
DisplayName string `json:"displayName"`
}
// ConvertAggregations converts a k8s aggregations (from MonitoringDashboard k8s resource) into this models aggregations
// Results are sorted by DisplayName
func ConvertAggregations(from v1alpha1.MonitoringDashboardSpec) []Aggregation {
uniqueAggs := make(map[string]Aggregation)
for _, item := range from.Items {
for _, agg := range item.Chart.Aggregations {
uniqueAggs[agg.DisplayName] = Aggregation{Label: agg.Label, DisplayName: agg.DisplayName}
}
}
aggs := []Aggregation{}
for _, agg := range uniqueAggs {
aggs = append(aggs, agg)
}
sort.Slice(aggs, func(i, j int) bool {
return aggs[i].DisplayName < aggs[j].DisplayName
})
return aggs
}
func buildIstioAggregations(local, remote string) []Aggregation {
aggs := []Aggregation{
{
Label: fmt.Sprintf("%s_version", local),
DisplayName: "Local version",
},
}
if remote == "destination" {
aggs = append(aggs, Aggregation{
Label: "destination_service_name",
DisplayName: "Remote service",
})
}
aggs = append(aggs, []Aggregation{
{
Label: fmt.Sprintf("%s_app", remote),
DisplayName: "Remote app",
},
{
Label: fmt.Sprintf("%s_version", remote),
DisplayName: "Remote version",
},
{
Label: "response_code",
DisplayName: "Response code",
},
}...)
return aggs
}
// PrepareIstioDashboard prepares the Istio dashboard title and aggregations dynamically for input values
func PrepareIstioDashboard(direction, local, remote string) MonitoringDashboard {
return MonitoringDashboard{
Title: fmt.Sprintf("%s Metrics", direction),
Aggregations: buildIstioAggregations(local, remote),
}
}
// Runtime holds the runtime title and associated dashboard template(s)
type Runtime struct {
Name string `json:"name"`
DashboardRefs []DashboardRef `json:"dashboardRefs"`
}
// DashboardRef holds template name and title for a custom dashboard
type DashboardRef struct {
Template string `json:"template"`
Title string `json:"title"`
}