redis/example_instrumentation_tes...

81 lines
1.8 KiB
Go
Raw Normal View History

package redis_test
import (
"context"
"fmt"
2019-08-08 14:29:44 +03:00
"github.com/go-redis/redis/v7"
)
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) error {
fmt.Printf("finished processing: <%s>\n", cmd)
return 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) error {
fmt.Printf("pipeline finished processing: %v\n", cmds)
return nil
}
func Example_instrumentation() {
rdb := redis.NewClient(&redis.Options{
2018-01-20 13:26:33 +03:00
Addr: ":6379",
})
rdb.AddHook(redisHook{})
rdb.Ping()
2018-01-20 13:26:33 +03:00
// Output: starting processing: <ping: >
// finished processing: <ping: PONG>
}
2018-03-07 12:56:24 +03:00
func ExamplePipeline_instrumentation() {
rdb := redis.NewClient(&redis.Options{
2018-01-20 13:26:33 +03:00
Addr: ":6379",
})
rdb.AddHook(redisHook{})
rdb.Pipelined(func(pipe redis.Pipeliner) error {
2018-01-20 13:26:33 +03:00
pipe.Ping()
pipe.Ping()
return nil
})
// Output: pipeline starting processing: [ping: ping: ]
// pipeline finished processing: [ping: PONG ping: PONG]
}
2019-12-29 13:06:43 +03:00
func ExampleWatch_instrumentation() {
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
})
rdb.AddHook(redisHook{})
rdb.Watch(func(tx *redis.Tx) error {
tx.Ping()
tx.Ping()
return nil
}, "foo")
// Output:
// starting processing: <watch foo: >
// finished processing: <watch foo: OK>
// starting processing: <ping: >
// finished processing: <ping: PONG>
// starting processing: <ping: >
// finished processing: <ping: PONG>
// starting processing: <unwatch: >
// finished processing: <unwatch: OK>
}