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

Optimize constants #9

Merged
merged 5 commits into from
Apr 12, 2019
Merged
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 @@ -23,7 +23,7 @@
import com.alipay.common.tracer.core.middleware.parent.AbstractDigestSpanEncoder;
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import java.io.IOException;

import java.util.Map;

/**
Expand All @@ -32,10 +32,11 @@
* @since 2.2.0
*/
public class DataSourceClientDigestJsonEncoder extends AbstractDigestSpanEncoder {

@Override
public String encode(SofaTracerSpan span) throws IOException {
public String encode(SofaTracerSpan span) {
JsonStringBuilder jsonStringBuilder = new JsonStringBuilder();
jsonStringBuilder.appendBegin("time", Timestamp.format(span.getEndTime()));
jsonStringBuilder.appendBegin(CommonSpanTags.TIME, Timestamp.format(span.getEndTime()));
appendSlot(jsonStringBuilder, span);
return jsonStringBuilder.toString();
}
Expand All @@ -48,9 +49,9 @@ private void appendSlot(JsonStringBuilder jsonStringBuilder, SofaTracerSpan sofa
jsonStringBuilder
.append(CommonSpanTags.LOCAL_APP, tagWithStr.get(CommonSpanTags.LOCAL_APP));
//TraceId
jsonStringBuilder.append("traceId", context.getTraceId());
jsonStringBuilder.append(CommonSpanTags.TRACE_ID, context.getTraceId());
//SpanId
jsonStringBuilder.append("spanId", context.getSpanId());
jsonStringBuilder.append(CommonSpanTags.SPAN_ID, context.getSpanId());
//schema
jsonStringBuilder.append(DataSourceTracerKeys.DATABASE_NAME,
tagWithStr.get(DataSourceTracerKeys.DATABASE_NAME));
Expand Down Expand Up @@ -86,6 +87,7 @@ private void appendSlot(JsonStringBuilder jsonStringBuilder, SofaTracerSpan sofa
private void appendBaggage(JsonStringBuilder jsonStringBuilder,
SofaTracerSpanContext sofaTracerSpanContext) {
//baggage
jsonStringBuilder.appendEnd("baggage", baggageSerialized(sofaTracerSpanContext));
jsonStringBuilder.appendEnd(CommonSpanTags.BAGGAGE,
baggageSerialized(sofaTracerSpanContext));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alipay.common.tracer.core.appender.file.LoadTestAwareAppender;
import com.alipay.common.tracer.core.appender.self.SelfLog;
import com.alipay.common.tracer.core.appender.self.Timestamp;
import com.alipay.common.tracer.core.constants.SofaTracerConstant;
import com.alipay.common.tracer.core.reporter.stat.AbstractSofaTracerStatisticReporter;
import com.alipay.common.tracer.core.reporter.stat.model.StatKey;
import com.alipay.common.tracer.core.reporter.stat.model.StatMapKey;
Expand Down Expand Up @@ -50,8 +51,9 @@ public void doReportStat(SofaTracerSpan sofaTracerSpan) {
tagsWithStr.get(DataSourceTracerKeys.DATABASE_NAME));
statKey.addKey(DataSourceTracerKeys.SQL, tagsWithStr.get(DataSourceTracerKeys.SQL));
//result
String result = DataSourceClientTracer.RESULT_CODE_SUCCESS.equals(tagsWithStr
.get(DataSourceTracerKeys.RESULT_CODE)) ? "true" : "false";
String result = SofaTracerConstant.RESULT_CODE_SUCCESS.equals(tagsWithStr
.get(DataSourceTracerKeys.RESULT_CODE)) ? SofaTracerConstant.STAT_FLAG_SUCCESS
: SofaTracerConstant.STAT_FLAG_FAILS;
statKey.setResult(result);
//pressure mark
statKey.setLoadTest(TracerUtils.isLoadTest(sofaTracerSpan));
Expand All @@ -77,13 +79,13 @@ public void print(StatKey statKey, long[] values) {
try {
jsonBuffer.reset();
jsonBuffer.appendBegin();
jsonBuffer.append("time", Timestamp.currentTime());
jsonBuffer.append("stat.key", this.statKeySplit(statMapKey));
jsonBuffer.append("count", values[0]);
jsonBuffer.append("total.cost.milliseconds", values[1]);
jsonBuffer.append("success", statMapKey.getResult());
jsonBuffer.append(CommonSpanTags.TIME, Timestamp.currentTime());
jsonBuffer.append(CommonSpanTags.STAT_KEY, this.statKeySplit(statMapKey));
jsonBuffer.append(CommonSpanTags.COUNT, values[0]);
jsonBuffer.append(CommonSpanTags.TOTAL_COST_MILLISECONDS, values[1]);
jsonBuffer.append(CommonSpanTags.SUCCESS, statMapKey.getResult());
//pressure
jsonBuffer.appendEnd("load.test", statMapKey.getEnd());
jsonBuffer.appendEnd(CommonSpanTags.LOAD_TEST, statMapKey.getEnd());

if (appender instanceof LoadTestAwareAppender) {
((LoadTestAwareAppender) appender).append(jsonBuffer.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -41,13 +42,8 @@ protected DataSourceTracerState initialValue() {
return null;
}
};

private volatile static DataSourceClientTracer dataSourceClientTracer = null;

public static final String RESULT_CODE_SUCCESS = "success";

public static final String RESULT_CODE_FAILED = "failed";

/***
* DataSource Client Tracer Singleton
* @return singleton
Expand All @@ -64,7 +60,7 @@ public static DataSourceClientTracer getDataSourceClientTracer() {
}

private DataSourceClientTracer() {
super("dataSource");
super(ComponentNameConstants.DATA_SOURCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.tracer.plugins.datasource.tracer;

import com.alipay.common.tracer.core.constants.SofaTracerConstant;
import com.alipay.sofa.tracer.plugins.datasource.Interceptor;

/**
Expand All @@ -34,12 +35,12 @@ public void setClientTracer(DataSourceClientTracer clientTracer) {
@Override
public Object intercept(Chain chain) throws Exception {
long start = System.currentTimeMillis();
String resultCode = DataSourceClientTracer.RESULT_CODE_SUCCESS;
String resultCode = SofaTracerConstant.RESULT_SUCCESS;
try {
clientTracer.startTrace(chain.getOriginalSql());
return chain.proceed();
} catch (Exception e) {
resultCode = DataSourceClientTracer.RESULT_CODE_FAILED;
resultCode = SofaTracerConstant.RESULT_FAILED;
throw e;
} finally {
clientTracer.endTrace(System.currentTimeMillis() - start, resultCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alipay.common.tracer.core.appender.self.SelfLog;
import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.constants.SofaTracerConstant;
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;
Expand Down Expand Up @@ -46,23 +47,17 @@
@Activate(group = { Constants.PROVIDER, Constants.CONSUMER }, value = "dubboSofaTracerFilter", order = 1)
public class DubboSofaTracerFilter implements Filter {

private String appName = StringUtils.EMPTY_STRING;
private String appName = StringUtils.EMPTY_STRING;

private static final String BLANK = StringUtils.EMPTY_STRING;
private static final String BLANK = StringUtils.EMPTY_STRING;

private static final String SUCCESS_CODE = "00";

private static final String FAILED_CODE = "99";

private static final String TIME_OUT_ERROR_CODE = "03";

private static final String SPAN_INVOKE_KEY = "sofa.current.span.key";
private static final String SPAN_INVOKE_KEY = "sofa.current.span.key";

private DubboConsumerSofaTracer dubboConsumerSofaTracer;

private DubboProviderSofaTracer dubboProviderSofaTracer;

private static Map<String, SofaTracerSpan> TracerSpanMap = new ConcurrentHashMap<String, SofaTracerSpan>();
private static Map<String, SofaTracerSpan> TracerSpanMap = new ConcurrentHashMap<String, SofaTracerSpan>();

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
Expand Down Expand Up @@ -104,14 +99,14 @@ public Result onResponse(Result result, Invoker<?> invoker, Invocation invocatio
this.dubboConsumerSofaTracer = DubboConsumerSofaTracer
.getDubboConsumerSofaTracerSingleton();
}
String resultCode = SUCCESS_CODE;
String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
if (result.hasException()) {
if (result.getException() instanceof RpcException) {
resultCode = Integer.toString(((RpcException) result.getException())
.getCode());
sofaTracerSpan.setTag(CommonSpanTags.RESULT_CODE, resultCode);
} else {
resultCode = FAILED_CODE;
resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
}
}
// add elapsed time
Expand Down Expand Up @@ -168,7 +163,7 @@ private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
}
Result result;
Throwable exception = null;
String resultCode = SUCCESS_CODE;
String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
try {
// do invoke
result = invoker.invoke(invocation);
Expand All @@ -195,7 +190,7 @@ private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
RpcException rpcException = (RpcException) exception;
resultCode = String.valueOf(rpcException.getCode());
} else {
resultCode = FAILED_CODE;
resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
}
}

Expand All @@ -218,7 +213,7 @@ private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
future.whenComplete((object, throwable)-> {
if (throwable instanceof TimeoutException) {
sofaTracerSpan.setTag(Tags.ERROR.getKey(),throwable.getMessage());
dubboConsumerSofaTracer.clientReceiveTagFinish(sofaTracerSpan, TIME_OUT_ERROR_CODE);
dubboConsumerSofaTracer.clientReceiveTagFinish(sofaTracerSpan, SofaTracerConstant.RESULT_CODE_TIME_OUT);
}
});
}
Expand Down Expand Up @@ -260,7 +255,7 @@ private Result doServerFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
exception = t;
throw new RpcException(t);
} finally {
String resultCode = SUCCESS_CODE;
String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
if (exception != null) {
if (exception instanceof RpcException) {
sofaTracerSpan.setTag(Tags.ERROR.getKey(), exception.getMessage());
Expand All @@ -269,7 +264,7 @@ private Result doServerFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
resultCode = String.valueOf(rpcException.getCode());
}
} else {
resultCode = FAILED_CODE;
resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
}
}
dubboProviderSofaTracer.serverSend(resultCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public String encode(SofaTracerSpan sofaTracerSpan) throws IOException {

JsonStringBuilder data = new JsonStringBuilder();
//span end time
data.appendBegin("time", Timestamp.format(sofaTracerSpan.getEndTime()));
data.appendBegin(CommonSpanTags.TIME, Timestamp.format(sofaTracerSpan.getEndTime()));

Map<String, String> tagStr = sofaTracerSpan.getTagsWithStr();
Map<String, Number> tagNum = sofaTracerSpan.getTagsWithNumber();
SofaTracerSpanContext context = sofaTracerSpan.getSofaTracerSpanContext();

// TraceId
data.append("traceId", context.getTraceId());
data.append(CommonSpanTags.TRACE_ID, context.getTraceId());
// SpanId
data.append("spanId", context.getSpanId());
data.append(CommonSpanTags.SPAN_ID, context.getSpanId());
// Span Type
data.append(Tags.SPAN_KIND.getKey(), tagStr.get(Tags.SPAN_KIND.getKey()));
// app name
Expand Down Expand Up @@ -89,7 +89,7 @@ public String encode(SofaTracerSpan sofaTracerSpan) throws IOException {
data.append(CommonSpanTags.CURRENT_THREAD_NAME,
tagStr.get(CommonSpanTags.CURRENT_THREAD_NAME));
//time-consuming ms
data.append("time.cost.milliseconds",
data.append(CommonSpanTags.TIME_COST_MILLISECONDS,
(sofaTracerSpan.getEndTime() - sofaTracerSpan.getStartTime()));
this.appendBaggage(data, context);
return data.toString();
Expand All @@ -98,7 +98,8 @@ public String encode(SofaTracerSpan sofaTracerSpan) throws IOException {
private void appendBaggage(JsonStringBuilder jsonStringBuilder,
SofaTracerSpanContext sofaTracerSpanContext) {
//baggage
jsonStringBuilder.appendEnd("baggage", baggageSerialized(sofaTracerSpanContext));
jsonStringBuilder.appendEnd(CommonSpanTags.BAGGAGE,
baggageSerialized(sofaTracerSpanContext));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public class DubboServerDigestJsonEncoder extends AbstractDigestSpanEncoder {
public String encode(SofaTracerSpan sofaTracerSpan) throws IOException {
JsonStringBuilder data = new JsonStringBuilder();
//span end time
data.appendBegin("time", Timestamp.format(sofaTracerSpan.getEndTime()));
data.appendBegin(CommonSpanTags.TIME, Timestamp.format(sofaTracerSpan.getEndTime()));
Map<String, String> tagStr = sofaTracerSpan.getTagsWithStr();
Map<String, Number> tagNum = sofaTracerSpan.getTagsWithNumber();
SofaTracerSpanContext context = sofaTracerSpan.getSofaTracerSpanContext();
//TraceId
data.append("traceId", context.getTraceId());
data.append(CommonSpanTags.TRACE_ID, context.getTraceId());
//SpanId
data.append("spanId", context.getSpanId());
data.append(CommonSpanTags.SPAN_ID, context.getSpanId());
//Span Type
data.append(Tags.SPAN_KIND.getKey(), tagStr.get(Tags.SPAN_KIND.getKey()));
//local appName
Expand Down Expand Up @@ -74,7 +74,7 @@ public String encode(SofaTracerSpan sofaTracerSpan) throws IOException {
data.append(CommonSpanTags.CURRENT_THREAD_NAME,
tagStr.get(CommonSpanTags.CURRENT_THREAD_NAME));
//time-consuming ms
data.append("time.cost.milliseconds",
data.append(CommonSpanTags.TIME_COST_MILLISECONDS,
(sofaTracerSpan.getEndTime() - sofaTracerSpan.getStartTime()));
this.appendBaggage(data, context);
return data.toString();
Expand All @@ -90,6 +90,7 @@ private long getTime(Number number) {
private void appendBaggage(JsonStringBuilder jsonStringBuilder,
SofaTracerSpanContext sofaTracerSpanContext) {
//baggage
jsonStringBuilder.appendEnd("baggage", baggageSerialized(sofaTracerSpanContext));
jsonStringBuilder.appendEnd(CommonSpanTags.BAGGAGE,
baggageSerialized(sofaTracerSpanContext));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alipay.common.tracer.core.appender.file.LoadTestAwareAppender;
import com.alipay.common.tracer.core.appender.self.SelfLog;
import com.alipay.common.tracer.core.appender.self.Timestamp;
import com.alipay.common.tracer.core.constants.SofaTracerConstant;
import com.alipay.common.tracer.core.reporter.stat.AbstractSofaTracerStatisticReporter;
import com.alipay.common.tracer.core.reporter.stat.model.StatKey;
import com.alipay.common.tracer.core.reporter.stat.model.StatMapKey;
Expand Down Expand Up @@ -58,7 +59,9 @@ public void doReportStat(SofaTracerSpan sofaTracerSpan) {
String methodName = tagsWithStr.get(CommonSpanTags.METHOD);
statKey.setKey(buildString(new String[] { fromApp, toApp, serviceName, methodName }));
String resultCode = tagsWithStr.get(CommonSpanTags.RESULT_CODE);
statKey.setResult("00".equals(resultCode) ? "Y" : "N");
statKey
.setResult(SofaTracerConstant.RESULT_CODE_SUCCESS.equals(resultCode) ? SofaTracerConstant.STAT_FLAG_SUCCESS
: SofaTracerConstant.STAT_FLAG_FAILS);
statKey.setEnd(buildString(new String[] { getLoadTestMark(sofaTracerSpan) }));
statKey.setLoadTest(TracerUtils.isLoadTest(sofaTracerSpan));
statKey.addKey(CommonSpanTags.LOCAL_APP, fromApp);
Expand Down Expand Up @@ -88,11 +91,11 @@ public void print(StatKey statKey, long[] values) {
StatMapKey statMapKey = (StatMapKey) statKey;

jsonBuffer.reset();
jsonBuffer.appendBegin("time", Timestamp.currentTime());
jsonBuffer.append("stat.key", this.statKeySplit(statMapKey));
jsonBuffer.append("count", values[0]);
jsonBuffer.append("total.cost.milliseconds", values[1]);
jsonBuffer.append("success", statMapKey.getResult());
jsonBuffer.appendBegin(CommonSpanTags.TIME, Timestamp.currentTime());
jsonBuffer.append(CommonSpanTags.STAT_KEY, this.statKeySplit(statMapKey));
jsonBuffer.append(CommonSpanTags.COUNT, values[0]);
jsonBuffer.append(CommonSpanTags.TIME_COST_MILLISECONDS, values[1]);
jsonBuffer.append(CommonSpanTags.SUCCESS, statMapKey.getResult());
jsonBuffer.appendEnd();
try {
if (appender instanceof LoadTestAwareAppender) {
Expand Down
Loading