You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2024/09/05 15:27:07 UTC

(skywalking-go) branch main updated: Fix redis plugin cannot work in cluster mode (#201)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 3ac7e9a  Fix redis plugin cannot work in cluster mode (#201)
3ac7e9a is described below

commit 3ac7e9a71b54724ba60b1086a9ff24a6af18d2fc
Author: mrproliu <74...@qq.com>
AuthorDate: Thu Sep 5 23:27:01 2024 +0800

    Fix redis plugin cannot work in cluster mode (#201)
---
 CHANGES.md                 |   1 +
 plugins/go-redisv9/hook.go | 109 ++++++++++++++++++++++++---------------------
 2 files changed, 59 insertions(+), 51 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 436efa4..88de924 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 4455733..cfb7338 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())
 	}
 }