...

Source file src/github.com/redis/go-redis/v9/example_instrumentation_test.go

Documentation: github.com/redis/go-redis/v9

     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  	// Output:
    55  	// starting processing: <[ping]>
    56  	// dialing tcp :6379
    57  	// finished dialing tcp :6379
    58  	// starting processing: <[hello 3]>
    59  	// finished processing: <[hello 3]>
    60  	// finished processing: <[ping]>
    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  	// Output:
    76  	// pipeline starting processing: [[ping] [ping]]
    77  	// dialing tcp :6379
    78  	// finished dialing tcp :6379
    79  	// starting processing: <[hello 3]>
    80  	// finished processing: <[hello 3]>
    81  	// pipeline finished processing: [[ping] [ping]]
    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  	// Output:
    97  	// starting processing: <[watch foo]>
    98  	// dialing tcp :6379
    99  	// finished dialing tcp :6379
   100  	// starting processing: <[hello 3]>
   101  	// finished processing: <[hello 3]>
   102  	// finished processing: <[watch foo]>
   103  	// starting processing: <[ping]>
   104  	// finished processing: <[ping]>
   105  	// starting processing: <[ping]>
   106  	// finished processing: <[ping]>
   107  	// starting processing: <[unwatch]>
   108  	// finished processing: <[unwatch]>
   109  }
   110  

View as plain text