-
Notifications
You must be signed in to change notification settings - Fork 2
/
bucket_metrics.go
72 lines (62 loc) · 1.91 KB
/
bucket_metrics.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
package master
import (
"sort"
"github.com/codeuniversity/al-master/metrics"
)
//UpdateBucketsMetrics updates prometheus bucket metrics
func UpdateBucketsMetrics(buckets Buckets) {
minBucketCells, maxBucketCells := minMaxBucketCells(buckets)
metrics.AmountOfBuckets.Set(float64(len(buckets)))
metrics.MinCellsInBuckets.Set(minBucketCells)
metrics.MaxCellsInBuckets.Set(maxBucketCells)
metrics.AverageCellsPerBucket.Set(averageCellsPerBucket(buckets))
metrics.MedianCellsPerBucket.Set(medianCellsPerBucket(buckets))
}
func averageCellsPerBucket(buckets Buckets) (average float64) {
if len(buckets) == 0 {
return
}
var cellsInBuckets []int
var totalAmountOfCells int
for _, bucket := range buckets {
cellsInBuckets = append(cellsInBuckets, len(bucket))
totalAmountOfCells += len(bucket)
}
average = float64(totalAmountOfCells) / float64(len(cellsInBuckets))
return
}
func medianCellsPerBucket(buckets Buckets) (median float64) {
bucketsLength := len(buckets)
if bucketsLength == 0 {
return
}
var cellsInBuckets []int
for _, bucket := range buckets {
cellsInBuckets = append(cellsInBuckets, len(bucket))
}
sort.Ints(cellsInBuckets)
if len(buckets)%2 != 0 {
median = float64(cellsInBuckets[(bucketsLength-1)/2])
} else {
median = float64((cellsInBuckets[bucketsLength/2] + cellsInBuckets[(bucketsLength/2)-1]) / 2)
}
return
}
//minMaxBucketCells returns the amount of cells of the bucket with the most and the least amount of cells
func minMaxBucketCells(buckets Buckets) (minCellsInBucket float64, maxCellsInBucket float64) {
if len(buckets) == 0 {
return
}
var cellsInBucket int
minCellsInBucket = -1
for _, bucket := range buckets {
cellsInBucket = len(bucket)
if float64(cellsInBucket) > maxCellsInBucket {
maxCellsInBucket = float64(cellsInBucket)
}
if float64(cellsInBucket) < minCellsInBucket || minCellsInBucket == -1 {
minCellsInBucket = float64(cellsInBucket)
}
}
return
}