diff --git a/sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java b/sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java index b0f182aa8..7242e4991 100644 --- a/sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java +++ b/sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java @@ -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; @@ -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; @@ -55,29 +46,29 @@ private static String deserialize(byte[] bytes) { } public T doInScope(String command, byte[] key, Supplier supplier) { - Span span = buildSpan(command, deserialize(key)); - return activateAndCloseSpan(span, supplier); + buildSpan(command, deserialize(key)); + return activateAndCloseSpan(supplier); } public T doInScope(String command, Supplier 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 doInScope(String command, byte[][] keys, Supplier supplier) { - Span span = buildSpan(command); + SofaTracerSpan span = redisSofaTracer.startTrace(command); span.setTag("keys", toStringWithDeserialization(limitKeys(keys))); - return activateAndCloseSpan(span, supplier); + return activateAndCloseSpan(supplier); } T[] limitKeys(T[] keys) { @@ -88,64 +79,61 @@ T[] limitKeys(T[] keys) { } public T decorate(Supplier 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); } } } 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 void decorateThrowing(ThrowingAction 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 V decorateThrowing(ThrowingSupplier supplier, - String operateName) throws T { - - Span span = buildSpan(operateName); + private T activateAndCloseSpan(Supplier supplier) { Throwable candidateThrowable = null; try { return supplier.get(); @@ -154,32 +142,15 @@ public V decorateThrowing(ThrowingSupplier 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 activateAndCloseSpan(Span span, Supplier 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 { @@ -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); } } } @@ -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) { @@ -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; - } } diff --git a/sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java b/sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java index 38249b4bb..b932ced2d 100644 --- a/sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java +++ b/sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java @@ -17,16 +17,23 @@ 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 (guolei.sgl@antfin.com) 2019/11/18 9:03 PM @@ -34,8 +41,14 @@ **/ 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) { @@ -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); + } + } + } + + } }