hook.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package redis
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. red "github.com/go-redis/redis/v8"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "github.com/zeromicro/go-zero/core/mapping"
  9. "github.com/zeromicro/go-zero/core/timex"
  10. "github.com/zeromicro/go-zero/core/trace"
  11. "go.opentelemetry.io/otel"
  12. tracestd "go.opentelemetry.io/otel/trace"
  13. )
  14. // spanName is the span name of the redis calls.
  15. const spanName = "redis"
  16. var (
  17. startTimeKey = contextKey("startTime")
  18. spanKey = contextKey("span")
  19. durationHook = hook{tracer: otel.GetTracerProvider().Tracer(trace.TraceName)}
  20. )
  21. type (
  22. contextKey string
  23. hook struct {
  24. tracer tracestd.Tracer
  25. }
  26. )
  27. func (h hook) BeforeProcess(ctx context.Context, _ red.Cmder) (context.Context, error) {
  28. return h.startSpan(context.WithValue(ctx, startTimeKey, timex.Now())), nil
  29. }
  30. func (h hook) AfterProcess(ctx context.Context, cmd red.Cmder) error {
  31. h.endSpan(ctx)
  32. val := ctx.Value(startTimeKey)
  33. if val == nil {
  34. return nil
  35. }
  36. start, ok := val.(time.Duration)
  37. if !ok {
  38. return nil
  39. }
  40. duration := timex.Since(start)
  41. if duration > slowThreshold.Load() {
  42. logDuration(ctx, cmd, duration)
  43. }
  44. return nil
  45. }
  46. func (h hook) BeforeProcessPipeline(ctx context.Context, _ []red.Cmder) (context.Context, error) {
  47. return h.startSpan(context.WithValue(ctx, startTimeKey, timex.Now())), nil
  48. }
  49. func (h hook) AfterProcessPipeline(ctx context.Context, cmds []red.Cmder) error {
  50. h.endSpan(ctx)
  51. if len(cmds) == 0 {
  52. return nil
  53. }
  54. val := ctx.Value(startTimeKey)
  55. if val == nil {
  56. return nil
  57. }
  58. start, ok := val.(time.Duration)
  59. if !ok {
  60. return nil
  61. }
  62. duration := timex.Since(start)
  63. if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
  64. logDuration(ctx, cmds[0], duration)
  65. }
  66. return nil
  67. }
  68. func logDuration(ctx context.Context, cmd red.Cmder, duration time.Duration) {
  69. var buf strings.Builder
  70. for i, arg := range cmd.Args() {
  71. if i > 0 {
  72. buf.WriteByte(' ')
  73. }
  74. buf.WriteString(mapping.Repr(arg))
  75. }
  76. logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
  77. }
  78. func (h hook) startSpan(ctx context.Context) context.Context {
  79. ctx, span := h.tracer.Start(ctx, spanName)
  80. return context.WithValue(ctx, spanKey, span)
  81. }
  82. func (h hook) endSpan(ctx context.Context) {
  83. spanVal := ctx.Value(spanKey)
  84. if spanVal == nil {
  85. return
  86. }
  87. if span, ok := spanVal.(tracestd.Span); ok {
  88. span.End()
  89. }
  90. }