-
Notifications
You must be signed in to change notification settings - Fork 6
/
metrics_test.go
142 lines (113 loc) · 3.75 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
package metrics
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func func1() string {
return func2()
}
func func2() string {
return func3()
}
func func3() string {
return CallerFuncName(2)
}
func TestCallerFuncName(t *testing.T) {
func1Name := func1()
assert.Equal(t, func1Name, "func1")
}
func TestGetFuncName(t *testing.T) {
func1Name := GetFuncName(func1)
assert.Equal(t, func1Name, "func1")
}
func Test_ReportTimedFuncWithError(t *testing.T) {
var rec statterRecorder
_ = Init("", "", &StatterConfig{StuckFunctionTimeout: time.Minute, MockingEnabled: true})
oldClient := client
client = &rec
defer func() {
client = oldClient
}()
t.Run("can be deferred and report the error", func(t *testing.T) {
rec.reset()
exec := func() (err error) {
defer ReportFuncCallAndTimingWithErr(Tags{"foo": "bar"})(&err, Tags{"stop": "error"})
time.Sleep(5 * time.Millisecond)
return errors.New("this error should be recorder")
}
require.Error(t, exec())
require.Len(t, rec.calls, 3)
expectedTags := []string{"foo=bar", "func_name=1"}
assert.Equal(t, "Incr", rec.calls[0][0])
assert.Equal(t, "func.called", rec.calls[0][1])
assert.ElementsMatch(t, expectedTags, rec.calls[0][2])
expectedTags = []string{"foo=bar", "stop=error", "func_name=1"}
assert.Equal(t, "Count", rec.calls[1][0])
assert.Equal(t, "func.timing", rec.calls[1][1])
assert.ElementsMatch(t, expectedTags, rec.calls[1][3])
assert.Equal(t, "Incr", rec.calls[2][0])
assert.Equal(t, "func.error", rec.calls[2][1])
assert.ElementsMatch(t, expectedTags, rec.calls[2][2])
})
t.Run("can be deferred and skip error reporting if nil", func(t *testing.T) {
rec.reset()
exec := func() {
var err error
defer ReportFuncCallAndTimingWithErr(Tags{"foo": "bar"})(&err)
}
exec()
require.Len(t, rec.calls, 2)
assert.Equal(t, "func.called", rec.calls[0][1])
assert.Equal(t, "func.timing", rec.calls[1][1])
})
t.Run("can be deferred with new tags and skip error reporting", func(t *testing.T) {
rec.reset()
exec := func() {
var err error
defer ReportFuncCallAndTimingWithErr(Tags{"foo": "bar"})(&err, Tags{"something": "new"})
}
exec()
require.Len(t, rec.calls, 2)
expectedTags := []string{"foo=bar", "something=new", "func_name=1"}
assert.Equal(t, "func.called", rec.calls[0][1])
assert.Equal(t, "func.timing", rec.calls[1][1])
assert.ElementsMatch(t, expectedTags, rec.calls[1][3])
})
}
type statterRecorder struct {
calls [][]interface{}
}
func (r *statterRecorder) reset() {
r.calls = make([][]interface{}, 0)
}
func (r *statterRecorder) Count(name string, value int64, tags []string, rate float64) error {
r.calls = append(r.calls, []interface{}{"Count", name, value, tags, rate})
return nil
}
func (r *statterRecorder) Incr(name string, tags []string, rate float64) error {
r.calls = append(r.calls, []interface{}{"Incr", name, tags, rate})
return nil
}
func (r *statterRecorder) Decr(name string, tags []string, rate float64) error {
r.calls = append(r.calls, []interface{}{"Count", name, tags, rate})
return nil
}
func (r *statterRecorder) Gauge(name string, value float64, tags []string, rate float64) error {
r.calls = append(r.calls, []interface{}{"Count", name, value, tags, rate})
return nil
}
func (r *statterRecorder) Timing(name string, value time.Duration, tags []string, rate float64) error {
r.calls = append(r.calls, []interface{}{"Count", name, value, tags, rate})
return nil
}
func (r *statterRecorder) Histogram(name string, value float64, tags []string, rate float64) error {
r.calls = append(r.calls, []interface{}{"Count", name, value, tags, rate})
return nil
}
func (r *statterRecorder) Close() error {
r.calls = append(r.calls, []interface{}{"Close"})
return nil
}