This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dashboards.go
213 lines (191 loc) · 6.8 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
package model
import (
"fmt"
"sort"
"strconv"
pmod "github.com/prometheus/common/model"
"github.com/kiali/k-charted/kubernetes/v1alpha1"
"github.com/kiali/k-charted/prometheus"
)
const (
statLabel = "__stat__"
nameLabel = "__name__"
)
// 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"`
ExternalLinks []ExternalLink `json:"externalLinks"`
}
// 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"`
StartCollapsed bool `json:"startCollapsed"`
ChartType *string `json:"chartType,omitempty"`
Min *int `json:"min,omitempty"`
Max *int `json:"max,omitempty"`
Metrics []*SampleStream `json:"metrics"`
XAxis *string `json:"xAxis"`
Error string `json:"error"`
}
type ConversionParams struct {
Scale float64
SortLabel string
SortLabelParseAs string
RemoveSortLabel bool
}
// BuildLabelsMap initiates a labels map out of a given metric name and optionally histogram stat
// Exported for external usage (Kiali)
func BuildLabelsMap(name, stat string) map[string]string {
labels := map[string]string{
nameLabel: name,
}
if stat != "" {
labels[statLabel] = stat
}
return labels
}
func (chart *Chart) FillHistogram(ref v1alpha1.MonitoringDashboardMetric, from prometheus.Histogram, conversionParams ConversionParams) {
// Extract and sort keys for consistent ordering
stats := []string{}
for k := range from {
stats = append(stats, k)
}
sort.Strings(stats)
for _, stat := range stats {
promMetric := from[stat]
if promMetric.Err != nil {
chart.Error = fmt.Sprintf("error in metric %s/%s: %v", ref.MetricName, stat, promMetric.Err)
return
}
metric := ConvertMatrix(promMetric.Matrix, BuildLabelsMap(ref.DisplayName, stat), conversionParams)
chart.Metrics = append(chart.Metrics, metric...)
}
}
func (chart *Chart) FillMetric(ref v1alpha1.MonitoringDashboardMetric, from prometheus.Metric, conversionParams ConversionParams) {
if from.Err != nil {
chart.Error = fmt.Sprintf("error in metric %s: %v", ref.MetricName, from.Err)
return
}
metric := ConvertMatrix(from.Matrix, BuildLabelsMap(ref.DisplayName, ""), conversionParams)
chart.Metrics = append(chart.Metrics, metric...)
}
func ConvertMatrix(from pmod.Matrix, initialLabels map[string]string, conversionParams ConversionParams) []*SampleStream {
series := make([]*SampleStream, len(from))
if len(conversionParams.SortLabel) > 0 {
sort.Slice(from, func(i, j int) bool {
first := from[i].Metric[pmod.LabelName(conversionParams.SortLabel)]
second := from[j].Metric[pmod.LabelName(conversionParams.SortLabel)]
if conversionParams.SortLabelParseAs == "int" {
// Note: in case of parsing error, 0 will be returned and used for sorting; error silently ignored.
iFirst, _ := strconv.Atoi(string(first))
iSecond, _ := strconv.Atoi(string(second))
return iFirst < iSecond
}
return first < second
})
}
for i, s := range from {
series[i] = convertSampleStream(s, initialLabels, conversionParams)
}
return series
}
type SampleStream struct {
LabelSet map[string]string `json:"labelSet"`
Values []SamplePair `json:"values"`
}
func convertSampleStream(from *pmod.SampleStream, initialLabels map[string]string, conversionParams ConversionParams) *SampleStream {
labelSet := make(map[string]string, len(from.Metric)+len(initialLabels))
for k, v := range initialLabels {
labelSet[k] = v
}
for k, v := range from.Metric {
if conversionParams.SortLabel == string(k) && conversionParams.RemoveSortLabel {
// Do not keep sort label
continue
}
labelSet[string(k)] = string(v)
}
values := make([]SamplePair, len(from.Values))
for i, v := range from.Values {
values[i] = convertSamplePair(&v, conversionParams.Scale)
}
return &SampleStream{
LabelSet: labelSet,
Values: values,
}
}
type SamplePair struct {
Timestamp int64
Value float64
}
// MarshalJSON implements json.Marshaler.
func (s SamplePair) MarshalJSON() ([]byte, error) {
return pmod.SamplePair{
Timestamp: pmod.Time(s.Timestamp),
Value: pmod.SampleValue(s.Value),
}.MarshalJSON()
}
func convertSamplePair(from *pmod.SamplePair, scale float64) SamplePair {
return SamplePair{
Timestamp: int64(from.Timestamp),
Value: scale * float64(from.Value),
}
}
// 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,
StartCollapsed: from.StartCollapsed,
ChartType: from.ChartType,
Min: from.Min,
Max: from.Max,
Metrics: []*SampleStream{},
XAxis: from.XAxis,
}
}
// 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"`
SingleSelection bool `json:"singleSelection"`
}
// 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, SingleSelection: agg.SingleSelection}
}
}
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
}
// ExternalLink provides links to external dashboards (e.g. to Grafana)
type ExternalLink struct {
URL string `json:"url"`
Name string `json:"name"`
Variables v1alpha1.MonitoringDashboardExternalLinkVariables `json:"variables"`
}
// 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"`
}