-
Notifications
You must be signed in to change notification settings - Fork 32
/
prometheusmetrics_test.go
220 lines (198 loc) · 7.68 KB
/
prometheusmetrics_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package prometheusmetrics
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/rcrowley/go-metrics"
"math"
"reflect"
"testing"
"time"
)
func TestPrometheusRegistration(t *testing.T) {
defaultRegistry := prometheus.DefaultRegisterer
pClient := NewPrometheusProvider(metrics.DefaultRegistry, "test", "subsys", defaultRegistry, 1*time.Second)
if pClient.promRegistry != defaultRegistry {
t.Fatalf("Failed to pass prometheus registry to go-metrics provider")
}
}
func TestUpdatePrometheusMetricsOnce(t *testing.T) {
prometheusRegistry := prometheus.NewRegistry()
metricsRegistry := metrics.NewRegistry()
pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)
metricsRegistry.Register("counter", metrics.NewCounter())
pClient.UpdatePrometheusMetricsOnce()
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "test",
Subsystem: "subsys",
Name: "counter",
Help: "counter",
})
err := prometheusRegistry.Register(gauge)
if err == nil {
t.Fatalf("Go-metrics registry didn't get registered to prometheus registry")
}
}
func TestUpdatePrometheusMetrics(t *testing.T) {
prometheusRegistry := prometheus.NewRegistry()
metricsRegistry := metrics.NewRegistry()
pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)
metricsRegistry.Register("counter", metrics.NewCounter())
go pClient.UpdatePrometheusMetrics()
time.Sleep(2 * time.Second)
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "test",
Subsystem: "subsys",
Name: "counter",
Help: "counter",
})
err := prometheusRegistry.Register(gauge)
if err == nil {
t.Fatalf("Go-metrics registry didn't get registered to prometheus registry")
}
}
func TestPrometheusCounterGetUpdated(t *testing.T) {
prometheusRegistry := prometheus.NewRegistry()
metricsRegistry := metrics.NewRegistry()
pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)
cntr := metrics.NewCounter()
metricsRegistry.Register("counter", cntr)
cntr.Inc(2)
go pClient.UpdatePrometheusMetrics()
cntr.Inc(13)
time.Sleep(5 * time.Second)
metrics, _ := prometheusRegistry.Gather()
serialized := fmt.Sprint(metrics[0])
expected := fmt.Sprintf("name:\"test_subsys_counter\" help:\"counter\" type:GAUGE metric:<gauge:<value:%d > > ", cntr.Count())
if serialized != expected {
t.Fatalf("Go-metrics value and prometheus metrics value do not match")
}
}
func TestPrometheusGaugeGetUpdated(t *testing.T) {
prometheusRegistry := prometheus.NewRegistry()
metricsRegistry := metrics.NewRegistry()
pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)
gm := metrics.NewGauge()
metricsRegistry.Register("gauge", gm)
gm.Update(2)
go pClient.UpdatePrometheusMetrics()
gm.Update(13)
time.Sleep(5 * time.Second)
metrics, _ := prometheusRegistry.Gather()
if len(metrics) == 0 {
t.Fatalf("prometheus was unable to register the metric")
}
serialized := fmt.Sprint(metrics[0])
expected := fmt.Sprintf("name:\"test_subsys_gauge\" help:\"gauge\" type:GAUGE metric:<gauge:<value:%d > > ", gm.Value())
if serialized != expected {
t.Fatalf("Go-metrics value and prometheus metrics value do not match")
}
}
func TestPrometheusMeterGetUpdated(t *testing.T) {
prometheusRegistry := prometheus.NewRegistry()
metricsRegistry := metrics.NewRegistry()
pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)
gm := metrics.NewMeter()
metricsRegistry.Register("meter", gm)
gm.Mark(2)
go pClient.UpdatePrometheusMetrics()
gm.Mark(13)
time.Sleep(5 * time.Second)
metrics, _ := prometheusRegistry.Gather()
if len(metrics) == 0 {
t.Fatalf("prometheus was unable to register the metric")
}
metricValues := make(map[string]interface{})
for _, metric := range metrics {
metricValues[metric.GetName()] = math.Round(metric.GetMetric()[0].Gauge.GetValue())
}
snapshot := gm.Snapshot()
expectedValues := map[string]interface{}{
"test_subsys_meter_count": float64(snapshot.Count()),
"test_subsys_meter_rate1": math.Round(snapshot.Rate1()),
"test_subsys_meter_rate15": math.Round(snapshot.Rate15()),
"test_subsys_meter_rate5": math.Round(snapshot.Rate5()),
"test_subsys_meter_rate_mean": math.Round(snapshot.RateMean()),
}
if !reflect.DeepEqual(metricValues, expectedValues) {
t.Fatalf(
"Go-metrics value and prometheus metrics value do not match. Expected: %v, actual: %v",
expectedValues,
metricValues,
)
}
}
func TestPrometheusHistogramGetUpdated(t *testing.T) {
prometheusRegistry := prometheus.NewRegistry()
metricsRegistry := metrics.NewRegistry()
pClient := NewPrometheusProvider(
metricsRegistry, "test", "subsys",
prometheusRegistry, 1*time.Second,
)
// values := make([]int64, 0)
//sample := metrics.HistogramSnapshot{metrics.NewSampleSnapshot(int64(len(values)), values)}
gm := metrics.NewHistogram(metrics.NewUniformSample(1028))
metricsRegistry.Register("metric", gm)
for ii := 0; ii < 94; ii++ {
gm.Update(1)
}
for ii := 0; ii < 5; ii++ {
gm.Update(5)
}
gm.Update(10)
go pClient.UpdatePrometheusMetrics()
time.Sleep(5 * time.Second)
metrics, _ := prometheusRegistry.Gather()
if len(metrics) < 2 {
t.Fatalf("prometheus was unable to register the metric")
}
serialized := fmt.Sprint(metrics[1])
expected := `name:"test_subsys_metric_histogram" help:"metric" type:HISTOGRAM metric:<histogram:<sample_count:100 sample_sum:129 bucket:<cumulative_count:1 upper_bound:0.05 > bucket:<cumulative_count:1 upper_bound:0.1 > bucket:<cumulative_count:1 upper_bound:0.25 > bucket:<cumulative_count:1 upper_bound:0.5 > bucket:<cumulative_count:1 upper_bound:0.75 > bucket:<cumulative_count:1 upper_bound:0.9 > bucket:<cumulative_count:5 upper_bound:0.95 > bucket:<cumulative_count:9 upper_bound:0.99 > > > `
if serialized != expected {
t.Fatalf("Go-metrics value and prometheus metrics value for max do not match:\n+ %s\n- %s", serialized, expected)
}
}
func TestPrometheusTimerGetUpdated(t *testing.T) {
prometheusRegistry := prometheus.NewRegistry()
metricsRegistry := metrics.NewRegistry()
pClient := NewPrometheusProvider(
metricsRegistry, "test", "subsys",
prometheusRegistry, 1*time.Second,
)
timer := metrics.NewTimer()
metricsRegistry.Register("timer", timer)
timer.Update(2)
go pClient.UpdatePrometheusMetrics()
timer.Update(13)
timer.Update(56)
time.Sleep(5 * time.Second)
metrics, _ := prometheusRegistry.Gather()
if len(metrics) == 0 {
t.Fatalf("prometheus was unable to register the metric")
}
metricValues := make(map[string]interface{})
for _, metric := range metrics {
metricValues[metric.GetName()] = math.Round(metric.GetMetric()[0].Gauge.GetValue())
}
snapshot := timer.Snapshot()
expectedValues := map[string]interface{}{
"test_subsys_timer_count": float64(snapshot.Count()),
"test_subsys_timer_max": float64(snapshot.Max()),
"test_subsys_timer_mean": math.Round(snapshot.Mean()),
"test_subsys_timer_min": float64(snapshot.Min()),
"test_subsys_timer_rate1": math.Round(snapshot.Rate1()),
"test_subsys_timer_rate15": math.Round(snapshot.Rate15()),
"test_subsys_timer_rate5": math.Round(snapshot.Rate5()),
"test_subsys_timer_rate_mean": math.Round(snapshot.RateMean()),
"test_subsys_timer_std_dev": math.Round(snapshot.StdDev()),
"test_subsys_timer_sum": float64(snapshot.Sum()),
"test_subsys_timer_variance": math.Round(snapshot.Variance()),
"test_subsys_timer_timer": float64(0),
}
if !reflect.DeepEqual(metricValues, expectedValues) {
t.Fatalf(
"Go-metrics value and prometheus metrics value do not match. Expected: %v, actual: %v",
expectedValues,
metricValues,
)
}
}