diff --git a/CHANGES.md b/CHANGES.md index 436efa48..88de9245 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ Release Notes. #### Bug Fixes * Fix wrong docker image name and `-version` command. +* Fix redis plugin cannot work in cluster mode. #### Issues and PR - All issues are [here](https://github.com/apache/skywalking/milestone/219?closed=1) diff --git a/plugins/go-redisv9/hook.go b/plugins/go-redisv9/hook.go index 4455733b..cfb73385 100644 --- a/plugins/go-redisv9/hook.go +++ b/plugins/go-redisv9/hook.go @@ -80,35 +80,39 @@ func (r *redisHook) DialHook(next redis.DialHook) redis.DialHook { func (r *redisHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook { return func(ctx context.Context, cmd redis.Cmder) error { - s, err := tracing.CreateExitSpan( - // operationName - GoRedisCacheType+"/"+cmd.FullName(), - - // peer - r.Addr, - - // injector - func(k, v string) error { - return nil - }, - - // opts - tracing.WithComponent(GoRedisComponentID), - tracing.WithLayer(tracing.SpanLayerCache), - tracing.WithTag(tracing.TagCacheType, GoRedisCacheType), - tracing.WithTag(tracing.TagCacheOp, getCacheOp(cmd.FullName())), - tracing.WithTag(tracing.TagCacheCmd, cmd.FullName()), - tracing.WithTag(tracing.TagCacheKey, getKey(cmd.Args())), - tracing.WithTag(tracing.TagCacheArgs, maxString(cmd.String(), config.MaxArgsBytes)), - ) - - if err != nil { - err = fmt.Errorf("go-redis :skyWalking failed to create exit span, got error: %v", err) - return err + var s tracing.Span + var err error + if r.Addr != "" { + s, err = tracing.CreateExitSpan( + // operationName + GoRedisCacheType+"/"+cmd.FullName(), + + // peer + r.Addr, + + // injector + func(k, v string) error { + return nil + }, + + // opts + tracing.WithComponent(GoRedisComponentID), + tracing.WithLayer(tracing.SpanLayerCache), + tracing.WithTag(tracing.TagCacheType, GoRedisCacheType), + tracing.WithTag(tracing.TagCacheOp, getCacheOp(cmd.FullName())), + tracing.WithTag(tracing.TagCacheCmd, cmd.FullName()), + tracing.WithTag(tracing.TagCacheKey, getKey(cmd.Args())), + tracing.WithTag(tracing.TagCacheArgs, maxString(cmd.String(), config.MaxArgsBytes)), + ) + + if err != nil { + err = fmt.Errorf("go-redis :skyWalking failed to create exit span, got error: %v", err) + return err + } + + defer s.End() } - defer s.End() - if err = next(ctx, cmd); err != nil { recordError(s, err) return err @@ -132,31 +136,34 @@ func (r *redisHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.Pr summary += "..." } - s, err := tracing.CreateExitSpan( - // operationName - "redis/pipeline", - - // peer - r.Addr, - - // injector - func(k, v string) error { - return nil - }, - - // opts - tracing.WithComponent(GoRedisComponentID), - tracing.WithLayer(tracing.SpanLayerCache), - tracing.WithTag(tracing.TagCacheType, GoRedisCacheType), - tracing.WithTag(tracing.TagCacheCmd, "pipeline:"+strings.TrimRight(summary, "/")), - ) - if err != nil { - err = fmt.Errorf("go-redis :skyWalking failed to create exit span, got error: %v", err) - return err + var s tracing.Span + var err error + if r.Addr != "" { + s, err = tracing.CreateExitSpan( + // operationName + "redis/pipeline", + + // peer + r.Addr, + + // injector + func(k, v string) error { + return nil + }, + + // opts + tracing.WithComponent(GoRedisComponentID), + tracing.WithLayer(tracing.SpanLayerCache), + tracing.WithTag(tracing.TagCacheType, GoRedisCacheType), + tracing.WithTag(tracing.TagCacheCmd, "pipeline:"+strings.TrimRight(summary, "/")), + ) + if err != nil { + err = fmt.Errorf("go-redis :skyWalking failed to create exit span, got error: %v", err) + return err + } + defer s.End() } - defer s.End() - if err = next(ctx, cmds); err != nil { recordError(s, err) return err @@ -167,7 +174,7 @@ func (r *redisHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.Pr } func recordError(span tracing.Span, err error) { - if err != redis.Nil { + if err != redis.Nil && span != nil { span.Error(err.Error()) } }