/
sli.go
149 lines (137 loc) Β· 3.37 KB
/
sli.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
package model
import (
"fmt"
"github.com/coroot/coroot/timeseries"
"github.com/coroot/coroot/utils"
"github.com/dustin/go-humanize"
)
type AvailabilitySLI struct {
Config CheckConfigSLOAvailability
TotalRequests *timeseries.TimeSeries
FailedRequests *timeseries.TimeSeries
TotalRequestsRaw *timeseries.TimeSeries
FailedRequestsRaw *timeseries.TimeSeries
}
type HistogramBucket struct {
Le float32
TimeSeries *timeseries.TimeSeries
}
type LatencySLI struct {
Config CheckConfigSLOLatency
Histogram []HistogramBucket
HistogramRaw []HistogramBucket
}
func (sli *LatencySLI) GetTotalAndFast(raw bool) (*timeseries.TimeSeries, *timeseries.TimeSeries) {
var total, fast *timeseries.TimeSeries
histogram := sli.Histogram
if raw {
histogram = sli.HistogramRaw
}
for _, b := range histogram {
if b.Le <= sli.Config.ObjectiveBucket {
fast = b.TimeSeries
}
if timeseries.IsInf(b.Le, 1) {
total = b.TimeSeries
}
}
return total, fast
}
func HistogramSeries(buckets []HistogramBucket, objectiveBucket, objectivePercentage float32) []Series {
res := make([]Series, 0, len(buckets))
var from, to float32
thresholdIdx := -1
for i, b := range buckets {
var h Series
to = b.Le
if i == 0 {
from = 0
h.Data = b.TimeSeries
} else {
from = buckets[i-1].Le
h.Data = timeseries.Sub(b.TimeSeries, buckets[i-1].TimeSeries)
}
h.Color = "green"
if objectiveBucket > 0 && objectivePercentage > 0 {
if to > objectiveBucket {
h.Color = "red"
} else {
thresholdIdx = i
}
}
h.Value = fmt.Sprint(to)
switch {
case timeseries.IsInf(to, 0):
h.Name = fmt.Sprintf(">%ss", humanize.Ftoa(float64(from)))
h.Title = fmt.Sprintf(">%s s", humanize.Ftoa(float64(from)))
h.Value = "inf"
case from < 0.1:
h.Name = fmt.Sprintf("%.0fms", to*1000)
h.Title = fmt.Sprintf("%.0f-%.0f ms", from*1000, to*1000)
default:
h.Name = fmt.Sprintf("%ss", humanize.Ftoa(float64(to)))
h.Title = fmt.Sprintf("%s-%s s", humanize.Ftoa(float64(from)), humanize.Ftoa(float64(to)))
}
res = append(res, h)
}
if thresholdIdx > -1 {
res[thresholdIdx].Threshold = fmt.Sprintf(
"<b>Latency objective</b><br> %s of requests should be served faster than %s",
utils.FormatPercentage(objectivePercentage), utils.FormatLatency(objectiveBucket))
}
return res
}
func Quantile(histogram []HistogramBucket, q float32) *timeseries.TimeSeries {
if len(histogram) == 0 {
return nil
}
total := histogram[len(histogram)-1]
type bucket struct {
iter *timeseries.Iterator
le float32
}
var buckets []bucket
for _, b := range histogram {
buckets = append(buckets, bucket{
iter: b.TimeSeries.Iter(),
le: b.Le,
})
}
res := make([]float32, total.TimeSeries.Len())
idx := 0
totalIter := total.TimeSeries.Iter()
var t, c, rank float32
var i int
var b bucket
for totalIter.Next() {
_, t = totalIter.Value()
rank = t * q
for _, b = range buckets {
b.iter.Next()
}
var prev, lower, upper, bc float32
for i, b = range buckets {
upper = b.le
if i > 0 {
_, prev = buckets[i-1].iter.Value()
lower = buckets[i-1].le
}
_, c = b.iter.Value()
if timeseries.IsNaN(c) {
c = 0.
}
if c < rank {
continue
}
bc = c - prev
if timeseries.IsInf(upper, 1) {
res[idx] = lower
} else {
res[idx] = lower + (upper-lower)*((rank-prev)/bc)
}
break
}
idx++
}
return total.TimeSeries.NewWithData(res)
}