1package redis_test 2 3import ( 4 "context" 5 "fmt" 6 7 "github.com/go-redis/redis/v7" 8) 9 10type redisHook struct{} 11 12var _ redis.Hook = redisHook{} 13 14func (redisHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) { 15 fmt.Printf("starting processing: <%s>\n", cmd) 16 return ctx, nil 17} 18 19func (redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error { 20 fmt.Printf("finished processing: <%s>\n", cmd) 21 return nil 22} 23 24func (redisHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) { 25 fmt.Printf("pipeline starting processing: %v\n", cmds) 26 return ctx, nil 27} 28 29func (redisHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error { 30 fmt.Printf("pipeline finished processing: %v\n", cmds) 31 return nil 32} 33 34func Example_instrumentation() { 35 rdb := redis.NewClient(&redis.Options{ 36 Addr: ":6379", 37 }) 38 rdb.AddHook(redisHook{}) 39 40 rdb.Ping() 41 // Output: starting processing: <ping: > 42 // finished processing: <ping: PONG> 43} 44 45func ExamplePipeline_instrumentation() { 46 rdb := redis.NewClient(&redis.Options{ 47 Addr: ":6379", 48 }) 49 rdb.AddHook(redisHook{}) 50 51 rdb.Pipelined(func(pipe redis.Pipeliner) error { 52 pipe.Ping() 53 pipe.Ping() 54 return nil 55 }) 56 // Output: pipeline starting processing: [ping: ping: ] 57 // pipeline finished processing: [ping: PONG ping: PONG] 58} 59 60func ExampleWatch_instrumentation() { 61 rdb := redis.NewClient(&redis.Options{ 62 Addr: ":6379", 63 }) 64 rdb.AddHook(redisHook{}) 65 66 rdb.Watch(func(tx *redis.Tx) error { 67 tx.Ping() 68 tx.Ping() 69 return nil 70 }, "foo") 71 // Output: 72 // starting processing: <watch foo: > 73 // finished processing: <watch foo: OK> 74 // starting processing: <ping: > 75 // finished processing: <ping: PONG> 76 // starting processing: <ping: > 77 // finished processing: <ping: PONG> 78 // starting processing: <unwatch: > 79 // finished processing: <unwatch: OK> 80} 81