-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |