-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaggregations_test.go
189 lines (155 loc) · 5.1 KB
/
aggregations_test.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
package main
import (
"fmt"
"testing"
"time"
prom "github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
)
func TestCounterAggregation(t *testing.T) {
registry := prom.NewRegistry()
actx := newAggregatorContext(registry)
firstMinfo := makeMetricInfo("sum_metric", "counter", 1.0)
doAggregateMetric(t, actx, firstMinfo)
secondMinfo := makeMetricInfo("sum_metric", "counter", 2.0)
doAggregateMetric(t, actx, secondMinfo)
resMetrics, err := registry.Gather()
if err != nil {
t.Errorf("Gather() failed: %v", err)
}
assert.Equal(t, 1, len(resMetrics), "there should be one metric available")
m := resMetrics[0]
assert.Equal(t, "sum_metric", *m.Name)
assert.Equal(t, dto.MetricType_COUNTER, *m.Type)
assert.Equal(t, 1, len(m.Metric))
assert.Equal(t, 3.0, *m.Metric[0].Counter.Value)
}
func TestGaugeAggregation(t *testing.T) {
registry := prom.NewRegistry()
actx := newAggregatorContext(registry)
firstMinfo := makeMetricInfo("gauge_metric", "gauge", 1.0)
doAggregateMetric(t, actx, firstMinfo)
secondMinfo := makeMetricInfo("gauge_metric", "gauge", 100.0)
doAggregateMetric(t, actx, secondMinfo)
resMetrics, err := registry.Gather()
if err != nil {
t.Errorf("Gather() failed: %v", err)
}
assert.Equal(t, 1, len(resMetrics), "there should be one metric available")
m := resMetrics[0]
assert.Equal(t, "gauge_metric", *m.Name)
assert.Equal(t, dto.MetricType_GAUGE, *m.Type)
assert.Equal(t, 1, len(m.Metric))
assert.Equal(t, 100.0, *m.Metric[0].Gauge.Value)
// now make sure that the gague aggregation won't be overwritten if
// a timestamp of a new metric is older than the one of the aggregation
thirdMinfo := makeMetricInfoWithTs("gauge_metric", "gauge", 100500.0,
time.Now().Unix()-100500)
doAggregateMetric(t, actx, thirdMinfo)
resMetrics, err = registry.Gather()
if err != nil {
t.Errorf("Gather() failed: %v", err)
}
m = resMetrics[0]
assert.Equal(t, 100.0, *m.Metric[0].Gauge.Value)
}
func TestAggregationLabels(t *testing.T) {
registry := prom.NewRegistry()
actx := newAggregatorContext(registry)
firstMinfo := makeMetricInfo("sum_metric", "counter", 2.0)
firstMinfo.labelNames = []string{"one", "two", "three"}
firstMinfo.labels["one"] = "1"
firstMinfo.labels["two"] = "2"
firstMinfo.labels["three"] = "3"
doAggregateMetric(t, actx, firstMinfo)
secondMinfo := makeMetricInfo("sum_metric", "counter", 4.0)
secondMinfo.labelNames = []string{"one", "two", "three"}
secondMinfo.labels["one"] = "3"
secondMinfo.labels["two"] = "2"
secondMinfo.labels["three"] = "1"
doAggregateMetric(t, actx, secondMinfo)
resMetrics, err := registry.Gather()
if err != nil {
t.Errorf("Gather() failed: %v", err)
}
assert.Equal(t, 1, len(resMetrics))
m := resMetrics[0]
assert.Equal(t, 2, len(m.Metric))
assert.Equal(t, 2.0, *m.Metric[0].Counter.Value)
assert.Equal(t, 4.0, *m.Metric[1].Counter.Value)
assertThatLabelsAreEqual(t, []string{"one:1", "two:2", "three:3"},
m.Metric[0].Label)
assertThatLabelsAreEqual(t, []string{"one:3", "two:2", "three:1"},
m.Metric[1].Label)
}
func TestAggregationSameNameDifferentMetricTypes(t *testing.T) {
registry := prom.NewRegistry()
actx := newAggregatorContext(registry)
firstMinfo := makeMetricInfo("same_name", "counter", 1.0)
doAggregateMetric(t, actx, firstMinfo)
secondMinfo := makeMetricInfo("same_name", "gauge", 2.0)
err := aggregateMetric(actx, secondMinfo)
if err == nil {
t.Log("It should not be possible to aggregate two metrics with the same" +
" name but different types")
t.Fail()
}
resMetrics, err := registry.Gather()
if err != nil {
t.Errorf("Gather() failed: %v", err)
}
assert.Equal(t, 1, len(resMetrics))
m := resMetrics[0]
assert.Equal(t, 1, len(m.Metric))
assert.Equal(t, dto.MetricType_COUNTER, *m.Type)
assert.Equal(t, 1.0, *m.Metric[0].Counter.Value)
}
func TestAggregationWithUnknownType(t *testing.T) {
registry := prom.NewRegistry()
actx := newAggregatorContext(registry)
minfo := makeMetricInfo("same_name", "some_unknown_type", 1.0)
err := aggregateMetric(actx, minfo)
if err == nil {
t.Log("It should not be possible to aggregate metric with unknown type")
t.Fail()
}
}
func makeMetricInfo(name string, aggrType string, value float64) *metricInfo {
return makeMetricInfoWithTs(name, aggrType, value, time.Now().Unix())
}
func makeMetricInfoWithTs(name string, aggrType string,
value float64, ts int64) *metricInfo {
return &metricInfo{
name: name,
aggrType: aggrType,
value: value,
timestamp: ts,
labels: make(map[string]string),
}
}
func doAggregateMetric(t *testing.T, actx *aggregatorContext,
mInfo *metricInfo) {
err := aggregateMetric(actx, mInfo)
if err != nil {
t.Errorf("aggregateMetric: %v", err)
}
}
func assertThatLabelsAreEqual(t *testing.T, expected []string,
labels []*dto.LabelPair) {
assert.Equal(t, len(expected), len(labels))
for _, pair := range labels {
l := fmt.Sprintf("%s:%s", *pair.Name, *pair.Value)
found := false
for _, el := range expected {
if el == l {
found = true
break
}
}
if !found {
t.Logf("Unexpected label value: \"%v\"", l)
t.Fail()
}
}
}