-
Notifications
You must be signed in to change notification settings - Fork 63
/
aggregate.go
169 lines (140 loc) · 4.36 KB
/
aggregate.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
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package metrics
import (
"encoding/json"
"fmt"
"regexp"
"github.com/nginx/agent/sdk/v2/checksum"
"github.com/nginx/agent/sdk/v2/proto"
)
var (
failsRegex = regexp.MustCompile(`slab.slots.*.fails`)
freeRegex = regexp.MustCompile(`slab.slots.*.free`)
reqsRegex = regexp.MustCompile(`slab.slots.*.reqs`)
usedRegex = regexp.MustCompile(`slab.slots.*.used`)
)
type PerDimension struct {
Dimensions []*proto.Dimension
RunningSumMap map[string]float64
}
type MetricsHandler func(float64, int) float64
type Collections struct {
Count int // this is the number of collections run. Will use this to calculate the average.
Data map[string]PerDimension
}
func dimChecksum(stats *proto.StatsEntity) string {
dims := stats.GetDimensions()
data, err := json.Marshal(dims)
if err == nil {
return checksum.HexChecksum(data)
}
return checksum.HexChecksum([]byte(fmt.Sprintf("%#v", dims)))
}
// SaveCollections loops through one or more reports and get all the raw metrics for the Collections
// Note this function operate on the Collections struct data directly.
func SaveCollections(metricsCollections Collections, reports ...*proto.MetricsReport) Collections {
// could be multiple reports, however they must all be of the same type.
for _, report := range reports {
metricsCollections.Count++
for _, stats := range report.GetData() {
dimensionsChecksum := dimChecksum(stats)
if _, ok := metricsCollections.Data[dimensionsChecksum]; !ok {
metricsCollections.Data[dimensionsChecksum] = PerDimension{
Dimensions: stats.GetDimensions(),
RunningSumMap: make(map[string]float64),
}
}
simpleMetrics := stats.GetSimplemetrics()
if simpleMetrics != nil {
for _, simpleMetric := range simpleMetrics {
if metrics, ok := metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name]; ok {
metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name] = metrics + simpleMetric.GetValue()
} else {
metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name] = simpleMetric.GetValue()
}
}
}
}
}
return metricsCollections
}
func GenerateMetrics(metricsCollections Collections) []*proto.StatsEntity {
results := make([]*proto.StatsEntity, 0, 200)
for _, metricsPerDimension := range metricsCollections.Data {
simpleMetrics := getAggregatedSimpleMetric(metricsCollections.Count, metricsPerDimension.RunningSumMap)
results = append(results, NewStatsEntity(
metricsPerDimension.Dimensions,
simpleMetrics,
))
}
return results
}
func getAggregatedSimpleMetric(count int, internalMap map[string]float64) (simpleMetrics []*proto.SimpleMetric) {
variableMetrics := map[*regexp.Regexp]MetricsHandler{
failsRegex: sum,
freeRegex: avg,
reqsRegex: sum,
usedRegex: avg,
}
calMap := GetCalculationMap()
for name, value := range internalMap {
if valueType, ok := calMap[name]; ok {
var aggregatedValue float64
switch valueType {
case "sum":
aggregatedValue = sum(value, count)
case "avg":
aggregatedValue = avg(value, count)
case "boolean":
aggregatedValue = boolean(value, count)
}
// Only aggregate metrics when the aggregation method is defined
simpleMetrics = append(simpleMetrics, &proto.SimpleMetric{
Name: name,
Value: aggregatedValue,
})
} else {
for reg, calculation := range variableMetrics {
if reg.MatchString(name) {
result := calculation(value, count)
simpleMetrics = append(simpleMetrics, &proto.SimpleMetric{
Name: name,
Value: result,
})
}
}
}
}
return simpleMetrics
}
func sum(value float64, count int) float64 {
// the value is already summed in collection
return value
}
func avg(value float64, count int) float64 {
if count > 0 {
// the value is already summed in collection
return value / float64(count)
} else {
return value
}
}
// the return value is boolean in 1 or 0.
func boolean(value float64, count int) float64 {
const ZERO, TEST, ONE float64 = 0.0, 0.5, 1.0
floatCount := float64(count)
if floatCount == ZERO {
return value
}
// the value is already summed in collection
average := value / floatCount
if average > TEST {
return ONE
}
return ZERO
}