2016-11-30 13:39:14 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2019-05-31 16:36:57 +03:00
|
|
|
"context"
|
2016-11-30 13:39:14 +03:00
|
|
|
"fmt"
|
2022-10-11 15:37:34 +03:00
|
|
|
"net"
|
2016-11-30 13:39:14 +03:00
|
|
|
|
2023-01-23 09:48:54 +03:00
|
|
|
"github.com/redis/go-redis/v9"
|
2016-11-30 13:39:14 +03:00
|
|
|
)
|
|
|
|
|
2019-05-31 16:36:57 +03:00
|
|
|
type redisHook struct{}
|
|
|
|
|
|
|
|
var _ redis.Hook = redisHook{}
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
func (redisHook) DialHook(hook redis.DialHook) redis.DialHook {
|
|
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2022-10-12 17:18:55 +03:00
|
|
|
fmt.Printf("dialing %s %s\n", network, addr)
|
|
|
|
conn, err := hook(ctx, network, addr)
|
|
|
|
fmt.Printf("finished dialing %s %s\n", network, addr)
|
|
|
|
return conn, err
|
2022-10-11 15:37:34 +03:00
|
|
|
}
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
func (redisHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
|
|
|
return func(ctx context.Context, cmd redis.Cmder) error {
|
|
|
|
fmt.Printf("starting processing: <%s>\n", cmd)
|
|
|
|
err := hook(ctx, cmd)
|
|
|
|
fmt.Printf("finished processing: <%s>\n", cmd)
|
|
|
|
return err
|
|
|
|
}
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
func (redisHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
|
|
|
return func(ctx context.Context, cmds []redis.Cmder) error {
|
|
|
|
fmt.Printf("pipeline starting processing: %v\n", cmds)
|
|
|
|
err := hook(ctx, cmds)
|
|
|
|
fmt.Printf("pipeline finished processing: %v\n", cmds)
|
|
|
|
return err
|
|
|
|
}
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
|
2016-11-30 13:39:14 +03:00
|
|
|
func Example_instrumentation() {
|
2019-05-31 16:36:57 +03:00
|
|
|
rdb := redis.NewClient(&redis.Options{
|
2018-01-20 13:26:33 +03:00
|
|
|
Addr: ":6379",
|
2016-11-30 13:39:14 +03:00
|
|
|
})
|
2019-05-31 16:36:57 +03:00
|
|
|
rdb.AddHook(redisHook{})
|
2016-11-30 13:39:14 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
rdb.Ping(ctx)
|
2018-01-20 13:26:33 +03:00
|
|
|
// Output: starting processing: <ping: >
|
2022-10-12 17:18:55 +03:00
|
|
|
// dialing tcp :6379
|
|
|
|
// finished dialing tcp :6379
|
2018-01-20 13:26:33 +03:00
|
|
|
// finished processing: <ping: PONG>
|
2016-11-30 13:39:14 +03:00
|
|
|
}
|
|
|
|
|
2018-03-07 12:56:24 +03:00
|
|
|
func ExamplePipeline_instrumentation() {
|
2019-05-31 16:36:57 +03:00
|
|
|
rdb := redis.NewClient(&redis.Options{
|
2018-01-20 13:26:33 +03:00
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2019-05-31 16:36:57 +03:00
|
|
|
rdb.AddHook(redisHook{})
|
2016-11-30 13:39:14 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
pipe.Ping(ctx)
|
|
|
|
pipe.Ping(ctx)
|
2018-01-20 13:26:33 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Output: pipeline starting processing: [ping: ping: ]
|
2022-10-13 09:12:02 +03:00
|
|
|
// dialing tcp :6379
|
|
|
|
// finished dialing tcp :6379
|
2018-01-20 13:26:33 +03:00
|
|
|
// pipeline finished processing: [ping: PONG ping: PONG]
|
2016-11-30 13:39:14 +03:00
|
|
|
}
|
2019-12-29 13:06:43 +03:00
|
|
|
|
2020-09-17 16:13:43 +03:00
|
|
|
func ExampleClient_Watch_instrumentation() {
|
2019-12-29 13:06:43 +03:00
|
|
|
rdb := redis.NewClient(&redis.Options{
|
|
|
|
Addr: ":6379",
|
|
|
|
})
|
|
|
|
rdb.AddHook(redisHook{})
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
rdb.Watch(ctx, func(tx *redis.Tx) error {
|
|
|
|
tx.Ping(ctx)
|
|
|
|
tx.Ping(ctx)
|
2019-12-29 13:06:43 +03:00
|
|
|
return nil
|
|
|
|
}, "foo")
|
|
|
|
// Output:
|
|
|
|
// starting processing: <watch foo: >
|
2022-10-13 09:12:02 +03:00
|
|
|
// dialing tcp :6379
|
|
|
|
// finished dialing tcp :6379
|
2019-12-29 13:06:43 +03:00
|
|
|
// 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>
|
|
|
|
}
|