-
Notifications
You must be signed in to change notification settings - Fork 11
/
metrics_test.go
172 lines (156 loc) · 4.47 KB
/
metrics_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
package metrics
import (
"bytes"
"context"
"math"
"strings"
"testing"
"time"
"github.com/luraproject/lura/v2/logging"
"github.com/rcrowley/go-metrics"
)
func TestConfigGetter(t *testing.T) {
sampleCfg := map[string]interface{}{
Namespace: map[string]interface{}{
"proxy_disabled": true,
"router_disabled": true,
"collection_time": "100ms",
"listen_address": "192.168.1.1:8888",
},
}
testCfg := ConfigGetter(sampleCfg).(*Config)
if testCfg.BackendDisabled {
t.Error("Backend should be enabled.")
}
if !testCfg.ProxyDisabled {
t.Error("Proxy should be disabled.")
}
if !testCfg.RouterDisabled {
t.Error("Router should be disabled.")
}
if testCfg.CollectionTime != 100*time.Millisecond {
t.Errorf("Unexpected collection time: %v", testCfg.CollectionTime)
}
if testCfg.ListenAddr != "192.168.1.1:8888" {
t.Errorf("Unexpected addr: %s", testCfg.ListenAddr)
}
}
func TestDefaultConfiguration(t *testing.T) {
errorCfg := map[string]interface{}{
Namespace: map[string]interface{}{
"proxy_disabled": "bad_value",
},
}
testCfg := ConfigGetter(errorCfg).(*Config)
if testCfg.BackendDisabled {
t.Error("The backend should be enabled by default.")
}
if testCfg.ProxyDisabled {
t.Error("The proxy should be enabled by default.")
}
if testCfg.RouterDisabled {
t.Error("The router should be enabled by default.")
}
if testCfg.CollectionTime != time.Minute {
t.Errorf("Collection time should be 1 minute not %v", testCfg.CollectionTime)
}
}
func TestNoConfiguration(t *testing.T) {
noCfg := map[string]interface{}{}
testCfg := ConfigGetter(noCfg)
if nil != testCfg {
t.Error("The config should be nil (disabled middleware).")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
buf := bytes.NewBuffer(make([]byte, 1024))
l, _ := logging.NewLogger("DEBUG", buf, "")
m := New(ctx, nil, l)
stats1 := m.Snapshot()
time.Sleep(100 * time.Millisecond)
stats2 := m.Snapshot()
if stats1.Time > stats2.Time {
t.Error("the later stat must have a higher timestamp")
return
}
// sleep some time so the producer is able to collect some logs
time.Sleep(200 * time.Millisecond)
lines := len(strings.Split(buf.String(), "\n"))
if lines != 1 {
t.Error("unexpected log size. got:", lines)
}
}
func TestBadConfiguration(t *testing.T) {
invalidExtra := map[string]interface{}{Namespace: true}
testCfg := ConfigGetter(invalidExtra)
if nil != testCfg {
t.Error("The config should be nil (invalid ExtraConfig).")
}
badCfg := map[string]interface{}{
Namespace: map[string]interface{}{"test": math.Inf},
}
if testCfg := ConfigGetter(badCfg); testCfg != nil {
tmp := testCfg.(*Config)
if tmp.BackendDisabled {
t.Error("The backend should be enabled by default.")
}
if tmp.ProxyDisabled {
t.Error("The proxy should be enabled by default.")
}
if tmp.RouterDisabled {
t.Error("The router should be enabled by default.")
}
if tmp.CollectionTime != time.Minute {
t.Errorf("Collection time should be 1 minute not %v", tmp.CollectionTime)
}
}
}
func TestMetrics(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l, _ := logging.NewLogger("DEBUG", new(bytes.Buffer), "")
cfg := map[string]interface{}{Namespace: map[string]interface{}{"collection_time": "100ms"}}
m := New(ctx, cfg, l)
stats1 := m.Snapshot()
time.Sleep(100 * time.Millisecond)
stats2 := m.Snapshot()
if stats1.Time > stats2.Time {
t.Error("the later stat must have a higher timestamp")
return
}
}
func TestMetrics_process(t *testing.T) {
p := metrics.NewRegistry()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
called := false
m := Metrics{Registry: &p, Router: NewRouterMetrics(&p), latestSnapshot: NewStats()}
m.processMetrics(ctx, time.Millisecond, customLogger{&called})
time.Sleep(50 * time.Millisecond)
totalMetrics := 0
tm := &totalMetrics
expected := map[string]bool{
"router.disconnected-total": true,
"router.disconnected": true,
"router.disconnected-gauge": true,
"router.connected-total": true,
"router.connected": true,
"router.connected-gauge": true,
}
p.Each(func(name string, _ interface{}) {
_, isRouterMetric := expected[name]
if !strings.HasPrefix(name, "service.") && !isRouterMetric {
t.Errorf("Unexpected metric: %s", name)
}
*tm = *tm + 1
})
if totalMetrics < 31 {
t.Error("Not enough metrics")
}
}
type customLogger struct {
called *bool
}
func (l customLogger) Printf(_ string, _ ...interface{}) {
*(l.called) = true
}