example_instrumentation_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package redis_test
  2. import (
  3. "fmt"
  4. "github.com/go-redis/redis"
  5. )
  6. func Example_instrumentation() {
  7. cl := redis.NewClient(&redis.Options{
  8. Addr: ":6379",
  9. })
  10. cl.WrapProcess(func(old func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
  11. return func(cmd redis.Cmder) error {
  12. fmt.Printf("starting processing: <%s>\n", cmd)
  13. err := old(cmd)
  14. fmt.Printf("finished processing: <%s>\n", cmd)
  15. return err
  16. }
  17. })
  18. cl.Ping()
  19. // Output: starting processing: <ping: >
  20. // finished processing: <ping: PONG>
  21. }
  22. func ExamplePipeline_instrumentation() {
  23. client := redis.NewClient(&redis.Options{
  24. Addr: ":6379",
  25. })
  26. client.WrapProcessPipeline(func(old func([]redis.Cmder) error) func([]redis.Cmder) error {
  27. return func(cmds []redis.Cmder) error {
  28. fmt.Printf("pipeline starting processing: %v\n", cmds)
  29. err := old(cmds)
  30. fmt.Printf("pipeline finished processing: %v\n", cmds)
  31. return err
  32. }
  33. })
  34. client.Pipelined(func(pipe redis.Pipeliner) error {
  35. pipe.Ping()
  36. pipe.Ping()
  37. return nil
  38. })
  39. // Output: pipeline starting processing: [ping: ping: ]
  40. // pipeline finished processing: [ping: PONG ping: PONG]
  41. }