-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhook_test.go
159 lines (124 loc) · 4.38 KB
/
hook_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
package redisopentracing_test
import (
"context"
"errors"
"testing"
"github.com/go-redis/redis/v8"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
redisopentracing "github.com/globocom/go-redis-opentracing"
)
func TestHook(t *testing.T) {
assert := assert.New(t)
t.Run("create a new hook", func(t *testing.T) {
// act
sut := redisopentracing.NewHook(opentracing.NoopTracer{})
// assert
assert.NotNil(sut)
})
t.Run("starts a span before a command and finishes it after the command", func(t *testing.T) {
// arrange
tracer := mocktracer.New()
sut := redisopentracing.NewHook(tracer)
cmd := redis.NewStringCmd(context.Background(), "get")
// act
ctx, err1 := sut.BeforeProcess(context.Background(), cmd)
// assert
assert.Nil(err1)
assert.Len(tracer.FinishedSpans(), 0)
// act
err2 := sut.AfterProcess(ctx, cmd)
// assert
assert.Nil(err2)
assert.Len(tracer.FinishedSpans(), 1)
span := tracer.FinishedSpans()[0]
assert.Equal("get", span.OperationName)
assert.Len(span.Tags(), 1)
assert.Equal("redis", span.Tags()["db.type"])
assert.Equal(nil, span.Tags()["db.error"])
assert.Equal(nil, span.Tags()[string(ext.Error)])
})
t.Run("starts a span before a command and finishes it after the command, with error", func(t *testing.T) {
// arrange
tracer := mocktracer.New()
sut := redisopentracing.NewHook(tracer)
cmd := redis.NewStringCmd(context.Background(), "get")
cmd.SetErr(errors.New("some error"))
// act
ctx, err1 := sut.BeforeProcess(context.Background(), cmd)
// assert
assert.Nil(err1)
assert.Len(tracer.FinishedSpans(), 0)
// act
err2 := sut.AfterProcess(ctx, cmd)
// assert
assert.Nil(err2)
assert.Len(tracer.FinishedSpans(), 1)
span := tracer.FinishedSpans()[0]
assert.Equal("get", span.OperationName)
assert.Len(span.Tags(), 3)
assert.Equal("redis", span.Tags()["db.type"])
assert.Equal("some error", span.Tags()["db.error"])
assert.Equal(true, span.Tags()[string(ext.Error)])
})
t.Run("starts a span before a pipeline and finishes it after the pipeline", func(t *testing.T) {
// arrange
tracer := mocktracer.New()
sut := redisopentracing.NewHook(tracer)
cmd1 := redis.NewStringCmd(context.Background(), "get")
cmd2 := redis.NewStringCmd(context.Background(), "dbsize")
cmd3 := redis.NewStringCmd(context.Background(), "set x y")
cmds := []redis.Cmder{cmd1, cmd2, cmd3}
// act
ctx, err1 := sut.BeforeProcessPipeline(context.Background(), cmds)
// assert
assert.Nil(err1)
assert.Len(tracer.FinishedSpans(), 0)
// act
err2 := sut.AfterProcessPipeline(ctx, cmds)
// assert
assert.Nil(err2)
assert.Len(tracer.FinishedSpans(), 1)
span := tracer.FinishedSpans()[0]
assert.Equal("pipeline", span.OperationName)
assert.Len(span.Tags(), 2)
assert.Equal("redis", span.Tags()["db.type"])
assert.Equal(3, span.Tags()["db.redis.num_cmd"])
assert.Equal(nil, span.Tags()["db.error0"])
assert.Equal(nil, span.Tags()["db.error1"])
assert.Equal(nil, span.Tags()["db.error2"])
assert.Equal(nil, span.Tags()[string(ext.Error)])
})
t.Run("starts a span before a pipeline and finishes it after the pipeline, with error", func(t *testing.T) {
// arrange
tracer := mocktracer.New()
sut := redisopentracing.NewHook(tracer)
cmd1 := redis.NewStringCmd(context.Background(), "get")
cmd2 := redis.NewStringCmd(context.Background(), "dbsize")
cmd2.SetErr(errors.New("error 1 in pipeline cmd"))
cmd3 := redis.NewStringCmd(context.Background(), "set x y")
cmd3.SetErr(errors.New("error 2 in pipeline cmd"))
cmds := []redis.Cmder{cmd1, cmd2, cmd3}
// act
ctx, err1 := sut.BeforeProcessPipeline(context.Background(), cmds)
// assert
assert.Nil(err1)
assert.Len(tracer.FinishedSpans(), 0)
// act
err2 := sut.AfterProcessPipeline(ctx, cmds)
// assert
assert.Nil(err2)
assert.Len(tracer.FinishedSpans(), 1)
span := tracer.FinishedSpans()[0]
assert.Equal("pipeline", span.OperationName)
assert.Len(span.Tags(), 5)
assert.Equal("redis", span.Tags()["db.type"])
assert.Equal(3, span.Tags()["db.redis.num_cmd"])
assert.Equal(nil, span.Tags()["db.error0"])
assert.Equal("error 1 in pipeline cmd", span.Tags()["db.error1"])
assert.Equal("error 2 in pipeline cmd", span.Tags()["db.error2"])
assert.Equal(true, span.Tags()[string(ext.Error)])
})
}