mirror of
https://github.com/redis/go-redis.git
synced 2025-06-14 01:21:30 +03:00
Replace Wrap* with hooks that support context
This commit is contained in:
@ -1,44 +1,54 @@
|
||||
package redis_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
type redisHook struct{}
|
||||
|
||||
var _ redis.Hook = redisHook{}
|
||||
|
||||
func (redisHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
|
||||
fmt.Printf("starting processing: <%s>\n", cmd)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
|
||||
fmt.Printf("finished processing: <%s>\n", cmd)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (redisHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
|
||||
fmt.Printf("pipeline starting processing: %v\n", cmds)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (redisHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
|
||||
fmt.Printf("pipeline finished processing: %v\n", cmds)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func Example_instrumentation() {
|
||||
redisdb := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
redisdb.WrapProcess(func(old func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
|
||||
return func(cmd redis.Cmder) error {
|
||||
fmt.Printf("starting processing: <%s>\n", cmd)
|
||||
err := old(cmd)
|
||||
fmt.Printf("finished processing: <%s>\n", cmd)
|
||||
return err
|
||||
}
|
||||
})
|
||||
rdb.AddHook(redisHook{})
|
||||
|
||||
redisdb.Ping()
|
||||
rdb.Ping()
|
||||
// Output: starting processing: <ping: >
|
||||
// finished processing: <ping: PONG>
|
||||
}
|
||||
|
||||
func ExamplePipeline_instrumentation() {
|
||||
redisdb := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
rdb.AddHook(redisHook{})
|
||||
|
||||
redisdb.WrapProcessPipeline(func(old func([]redis.Cmder) error) func([]redis.Cmder) error {
|
||||
return func(cmds []redis.Cmder) error {
|
||||
fmt.Printf("pipeline starting processing: %v\n", cmds)
|
||||
err := old(cmds)
|
||||
fmt.Printf("pipeline finished processing: %v\n", cmds)
|
||||
return err
|
||||
}
|
||||
})
|
||||
|
||||
redisdb.Pipelined(func(pipe redis.Pipeliner) error {
|
||||
rdb.Pipelined(func(pipe redis.Pipeliner) error {
|
||||
pipe.Ping()
|
||||
pipe.Ping()
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user