Skip to content

Commit

Permalink
fix redis trace plugin (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
Just-CJ authored Dec 20, 2024
1 parent d8053ce commit 1163b46
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 94 deletions.
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 @@ -87,99 +78,68 @@ <T> T[] limitKeys(T[] keys) {
return keys;
}

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) {
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);
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
}
handleTraceCompletion(candidateThrowable);
}
}

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);
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
}
handleTraceCompletion(candidateThrowable);
}
}

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);
} else {
redisSofaTracer.clientReceive(SofaTracerConstant.RESULT_CODE_SUCCESS);
}
handleTraceCompletion(candidateThrowable);
}
}

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();
} 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);
}
handleTraceCompletion(candidateThrowable);
}
}

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 @@ -188,12 +148,7 @@ private void activateAndCloseSpan(Span span, Runnable runnable) {
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);
}
handleTraceCompletion(candidateThrowable);
}
}

Expand All @@ -211,12 +166,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 +177,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,40 @@ 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 sofaTracerSpanContext = sofaTracerSpan.getSofaTracerSpanContext();
if (sofaTracerSpanContext != 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);
}
}
}

}
}

0 comments on commit 1163b46

Please sign in to comment.