...
1 package redis_test
2
3 import (
4 "context"
5 "fmt"
6 "net"
7
8 "github.com/redis/go-redis/v9"
9 )
10
11 type redisHook struct{}
12
13 var _ redis.Hook = redisHook{}
14
15 func (redisHook) DialHook(hook redis.DialHook) redis.DialHook {
16 return func(ctx context.Context, network, addr string) (net.Conn, error) {
17 fmt.Printf("dialing %s %s\n", network, addr)
18 conn, err := hook(ctx, network, addr)
19 fmt.Printf("finished dialing %s %s\n", network, addr)
20 return conn, err
21 }
22 }
23
24 func (redisHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
25 return func(ctx context.Context, cmd redis.Cmder) error {
26 fmt.Printf("starting processing: <%v>\n", cmd.Args())
27 err := hook(ctx, cmd)
28 fmt.Printf("finished processing: <%v>\n", cmd.Args())
29 return err
30 }
31 }
32
33 func (redisHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook {
34 return func(ctx context.Context, cmds []redis.Cmder) error {
35 names := make([]string, 0, len(cmds))
36 for _, cmd := range cmds {
37 names = append(names, fmt.Sprintf("%v", cmd.Args()))
38 }
39 fmt.Printf("pipeline starting processing: %v\n", names)
40 err := hook(ctx, cmds)
41 fmt.Printf("pipeline finished processing: %v\n", names)
42 return err
43 }
44 }
45
46 func Example_instrumentation() {
47 rdb := redis.NewClient(&redis.Options{
48 Addr: ":6379",
49 DisableIdentity: true,
50 })
51 rdb.AddHook(redisHook{})
52
53 rdb.Ping(ctx)
54
55
56
57
58
59
60
61 }
62
63 func ExamplePipeline_instrumentation() {
64 rdb := redis.NewClient(&redis.Options{
65 Addr: ":6379",
66 DisableIdentity: true,
67 })
68 rdb.AddHook(redisHook{})
69
70 rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
71 pipe.Ping(ctx)
72 pipe.Ping(ctx)
73 return nil
74 })
75
76
77
78
79
80
81
82 }
83
84 func ExampleClient_Watch_instrumentation() {
85 rdb := redis.NewClient(&redis.Options{
86 Addr: ":6379",
87 DisableIdentity: true,
88 })
89 rdb.AddHook(redisHook{})
90
91 rdb.Watch(ctx, func(tx *redis.Tx) error {
92 tx.Ping(ctx)
93 tx.Ping(ctx)
94 return nil
95 }, "foo")
96
97
98
99
100
101
102
103
104
105
106
107
108
109 }
110
View as plain text