Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix redis trace plugin #550

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@
package com.sofa.alipay.tracer.plugins.spring.redis.common;

import com.alipay.common.tracer.core.SofaTracer;
import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.constants.SofaTracerConstant;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.sofa.alipay.tracer.plugins.spring.redis.tracer.RedisSofaTracer;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.tag.Tags;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -38,9 +32,6 @@
* @since:
**/
public class RedisActionWrapperHelper {
public static final String COMMAND = "command";
public static final String COMPONENT_NAME = "java-redis";
public static final String DB_TYPE = "redis";
protected final SofaTracer tracer;
private final RedisSofaTracer redisSofaTracer;
private String appName;
Expand All @@ -55,29 +46,29 @@ private static String deserialize(byte[] bytes) {
}

public <T> T doInScope(String command, byte[] key, Supplier<T> supplier) {
Span span = buildSpan(command, deserialize(key));
return activateAndCloseSpan(span, supplier);
buildSpan(command, deserialize(key));
return activateAndCloseSpan(supplier);
}

public <T> T doInScope(String command, Supplier<T> supplier) {
Span span = buildSpan(command);
return activateAndCloseSpan(span, supplier);
redisSofaTracer.startTrace(command);
return activateAndCloseSpan(supplier);
}

public void doInScope(String command, byte[] key, Runnable runnable) {
Span span = buildSpan(command, deserialize(key));
activateAndCloseSpan(span, runnable);
buildSpan(command, deserialize(key));
activateAndCloseSpan(runnable);
}

public void doInScope(String command, Runnable runnable) {
Span span = buildSpan(command);
activateAndCloseSpan(span, runnable);
redisSofaTracer.startTrace(command);
activateAndCloseSpan(runnable);
}

public <T> T doInScope(String command, byte[][] keys, Supplier<T> supplier) {
Span span = buildSpan(command);
SofaTracerSpan span = redisSofaTracer.startTrace(command);
span.setTag("keys", toStringWithDeserialization(limitKeys(keys)));
return activateAndCloseSpan(span, supplier);
return activateAndCloseSpan(supplier);
}

<T> T[] limitKeys(T[] keys) {
Expand All @@ -88,64 +79,61 @@ <T> T[] limitKeys(T[] keys) {
}

public <T> T decorate(Supplier<T> supplier, String operateName) {
Span span = buildSpan(operateName);
Throwable candidateThrowable = null;
try {
redisSofaTracer.startTrace(operateName);
return supplier.get();
} catch (Throwable t) {
candidateThrowable = t;
throw t;
} finally {
if (candidateThrowable != null) {
span.setTag(Tags.ERROR.getKey(), candidateThrowable.getMessage());
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_ERROR);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_ERROR,
candidateThrowable.getMessage());
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_SUCCESS, null);
Comment on lines +84 to +94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider extracting duplicate error handling logic.

The error handling pattern is duplicated across decorate, decorateThrowing, and other methods. Consider extracting this into a reusable method to improve maintainability.

+ private void handleTraceCompletion(Throwable candidateThrowable) {
+     if (candidateThrowable != null) {
+         redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_ERROR,
+             candidateThrowable.getMessage());
+     } else {
+         redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_SUCCESS, null);
+     }
+ }

  public <T> T decorate(Supplier<T> supplier, String operateName) {
      Throwable candidateThrowable = null;
      try {
          redisSofaTracer.startTrace(operateName);
          return supplier.get();
      } catch (Throwable t) {
          candidateThrowable = t;
          throw t;
      } finally {
-         if (candidateThrowable != null) {
-             redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_ERROR,
-                 candidateThrowable.getMessage());
-         } else {
-             redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_SUCCESS, null);
-         }
+         handleTraceCompletion(candidateThrowable);
      }
  }

Also applies to: 102-112, 121-131

}
}
}

public void decorate(Action action, String operateName) {
Span span = buildSpan(operateName);
Throwable candidateThrowable = null;
try {
redisSofaTracer.startTrace(operateName);
action.execute();
} catch (Throwable t) {
candidateThrowable = t;
throw t;
} finally {
if (candidateThrowable != null) {
span.setTag(Tags.ERROR.getKey(), candidateThrowable.getMessage());
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_ERROR);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_ERROR,
candidateThrowable.getMessage());
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_SUCCESS, null);
}
}
}

public <T extends Exception> void decorateThrowing(ThrowingAction<T> action, String operateName)
throws T {
Span span = buildSpan(operateName);
Throwable candidateThrowable = null;
try {
redisSofaTracer.startTrace(operateName);
action.execute();
} catch (Throwable t) {
candidateThrowable = t;
throw t;
} finally {
if (candidateThrowable != null) {
span.setTag(Tags.ERROR.getKey(), candidateThrowable.getMessage());
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_ERROR);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_ERROR,
candidateThrowable.getMessage());
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_SUCCESS, null);
}
}
}

public <T extends Exception, V> V decorateThrowing(ThrowingSupplier<T, V> supplier,
String operateName) throws T {

Span span = buildSpan(operateName);
private <T> T activateAndCloseSpan(Supplier<T> supplier) {
Throwable candidateThrowable = null;
try {
return supplier.get();
Expand All @@ -154,32 +142,15 @@ public <T extends Exception, V> V decorateThrowing(ThrowingSupplier<T, V> suppli
throw t;
} finally {
if (candidateThrowable != null) {
span.setTag(Tags.ERROR.getKey(), candidateThrowable.getMessage());
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_ERROR);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_ERROR,
candidateThrowable.getMessage());
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_SUCCESS, null);
}
}
}

private <T> T activateAndCloseSpan(Span span, Supplier<T> supplier) {
Throwable candidateThrowable = null;
try {
return supplier.get();
} catch (Throwable t) {
candidateThrowable = t;
throw t;
} finally {
if (candidateThrowable != null) {
span.setTag(Tags.ERROR.getKey(), candidateThrowable.getMessage());
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_ERROR);
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
}
}
}

private void activateAndCloseSpan(Span span, Runnable runnable) {
private void activateAndCloseSpan(Runnable runnable) {

Throwable candidateThrowable = null;
try {
Expand All @@ -189,10 +160,10 @@ private void activateAndCloseSpan(Span span, Runnable runnable) {
throw t;
} finally {
if (candidateThrowable != null) {
span.setTag(Tags.ERROR.getKey(), candidateThrowable.getMessage());
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_ERROR);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_ERROR,
candidateThrowable.getMessage());
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
redisSofaTracer.endTrace(SofaTracerConstant.RESULT_CODE_SUCCESS, null);
}
}
}
Expand All @@ -211,12 +182,9 @@ private static String toStringWithDeserialization(byte[][] array) {
return "[" + String.join(", ", list) + "]";
}

public Span buildSpan(String operationName) {
return builder(operationName).start();
}

public Span buildSpan(String operationName, Object key) {
return buildSpan(operationName).setTag("key", nullable(key));
public void buildSpan(String operationName, Object key) {
SofaTracerSpan span = redisSofaTracer.startTrace(operationName);
span.setTag("key", nullable(key));
}

public static String nullable(Object object) {
Expand All @@ -225,18 +193,4 @@ public static String nullable(Object object) {
}
return object.toString();
}

private Tracer.SpanBuilder builder(String operationName) {
SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext().getCurrentSpan();
if (this.appName == null) {
this.appName = SofaTracerConfiguration
.getProperty(SofaTracerConfiguration.TRACER_APPNAME_KEY);
}
Tracer.SpanBuilder sb = tracer.buildSpan(operationName).asChildOf(currentSpan)
.withTag(CommonSpanTags.LOCAL_APP, appName).withTag(COMMAND, operationName)
.withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME)
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.withTag(Tags.DB_TYPE.getKey(), DB_TYPE);
return sb;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,38 @@
package com.sofa.alipay.tracer.plugins.spring.redis.tracer;

import com.alipay.common.tracer.core.appender.encoder.SpanEncoder;
import com.alipay.common.tracer.core.appender.self.SelfLog;
import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.constants.ComponentNameConstants;
import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext;
import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
import com.alipay.common.tracer.core.reporter.stat.AbstractSofaTracerStatisticReporter;
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.tracer.AbstractClientTracer;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.sofa.alipay.tracer.plugins.spring.redis.encoder.RedisDigestEncoder;
import com.sofa.alipay.tracer.plugins.spring.redis.encoder.RedisDigestJsonEncoder;
import com.sofa.alipay.tracer.plugins.spring.redis.enums.RedisLogEnum;
import com.sofa.alipay.tracer.plugins.spring.redis.reporter.RedisStatJsonReporter;
import com.sofa.alipay.tracer.plugins.spring.redis.reporter.RedisStatReporter;
import io.opentracing.tag.Tags;

/**
* @author: guolei.sgl ([email protected]) 2019/11/18 9:03 PM
* @since:
**/
public class RedisSofaTracer extends AbstractClientTracer {

public static final String COMMAND = "command";
public static final String COMPONENT_NAME = "java-redis";
public static final String DB_TYPE = "redis";

private volatile static RedisSofaTracer redisSofaTracer = null;

private String appName;

public static RedisSofaTracer getRedisSofaTracerSingleton() {
if (redisSofaTracer == null) {
synchronized (RedisSofaTracer.class) {
Expand Down Expand Up @@ -100,4 +113,41 @@ protected AbstractSofaTracerStatisticReporter getRedisClientStatReporter(String
}

}

public SofaTracerSpan startTrace(String operationName) {
SofaTracerSpan sofaTracerSpan = clientSend(operationName);
if (this.appName == null) {
this.appName = SofaTracerConfiguration
.getProperty(SofaTracerConfiguration.TRACER_APPNAME_KEY);
}
SofaTracerSpanContext ctx = sofaTracerSpan.getSofaTracerSpanContext();
if (ctx != null) {
sofaTracerSpan.setTag(CommonSpanTags.LOCAL_APP, appName);
sofaTracerSpan.setTag(COMMAND, operationName);
sofaTracerSpan.setTag(Tags.COMPONENT.getKey(), COMPONENT_NAME);
sofaTracerSpan.setTag(Tags.DB_TYPE.getKey(), DB_TYPE);
;
}
return sofaTracerSpan;
}

public void endTrace(String resultCode, String errorMsg) {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
if (sofaTraceContext != null) {
SofaTracerSpan sofaTracerSpan = sofaTraceContext.getCurrentSpan();
if (sofaTracerSpan != null) {
try {
sofaTracerSpan.setTag(CommonSpanTags.RESULT_CODE, resultCode);
if (StringUtils.isNotBlank(errorMsg)) {
sofaTracerSpan.setTag(Tags.ERROR.getKey(), errorMsg);
}
sofaTracerSpan.setEndTime(System.currentTimeMillis());
clientReceive(resultCode);
} catch (Throwable throwable) {
SelfLog.errorWithTraceId("redis processed", throwable);
}
}
}

}
}