1.增加异常拦截与阻塞,防止@trentsky 提出的问题 #47

2.暂时弃用多线程埋点api,暂不提供更多支持。
This commit is contained in:
wusheng 2016-04-20 09:39:56 +08:00
parent b8cfec0f1a
commit 31a4e28bf0
5 changed files with 121 additions and 68 deletions

View File

@ -5,31 +5,42 @@ import static com.ai.cloud.skywalking.conf.Config.BuriedPoint.EXCLUSIVE_EXCEPTIO
import java.util.HashSet;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.ai.cloud.skywalking.api.IExceptionHandler;
import com.ai.cloud.skywalking.conf.Config;
import com.ai.cloud.skywalking.context.Context;
import com.ai.cloud.skywalking.protocol.Span;
public class ApplicationExceptionHandler implements IExceptionHandler {
private static Logger logger = LogManager
.getLogger(ApplicationExceptionHandler.class);
private static String EXCEPTION_SPLIT = ",";
private static Set<String> exclusiveExceptionSet = null;
@Override
public void handleException(Throwable th) {
if (exclusiveExceptionSet == null) {
Set<String> exclusiveExceptions = new HashSet<String>();
String[] exceptions = EXCLUSIVE_EXCEPTIONS.split(EXCEPTION_SPLIT);
for(String exception : exceptions){
exclusiveExceptions.add(exception);
}
exclusiveExceptionSet = exclusiveExceptions;
}
try {
if (exclusiveExceptionSet == null) {
Set<String> exclusiveExceptions = new HashSet<String>();
Span span = Context.getLastSpan();
span.handleException(th, exclusiveExceptionSet,
Config.BuriedPoint.MAX_EXCEPTION_STACK_LENGTH);
String[] exceptions = EXCLUSIVE_EXCEPTIONS
.split(EXCEPTION_SPLIT);
for (String exception : exceptions) {
exclusiveExceptions.add(exception);
}
exclusiveExceptionSet = exclusiveExceptions;
}
Span span = Context.getLastSpan();
span.handleException(th, exclusiveExceptionSet,
Config.BuriedPoint.MAX_EXCEPTION_STACK_LENGTH);
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
}
}

View File

@ -14,42 +14,57 @@ import com.ai.cloud.skywalking.model.Identification;
import com.ai.cloud.skywalking.protocol.Span;
import com.ai.cloud.skywalking.util.ContextGenerator;
public class LocalBuriedPointSender extends ApplicationExceptionHandler implements IBuriedPointSender {
public class LocalBuriedPointSender extends ApplicationExceptionHandler
implements IBuriedPointSender {
private static Logger logger = LogManager.getLogger(LocalBuriedPointSender.class);
private static Logger logger = LogManager
.getLogger(LocalBuriedPointSender.class);
public ContextData beforeSend(Identification id) {
if (!AuthDesc.isAuth())
return new EmptyContextData();
public ContextData beforeSend(Identification id) {
try {
if (!AuthDesc.isAuth())
return new EmptyContextData();
Span spanData = ContextGenerator.generateSpanFromThreadLocal(id);
// 将新创建的Context存放到ThreadLocal栈中
Context.append(spanData);
// 并将当前的Context返回回去
return new ContextData(spanData);
}
Span spanData = ContextGenerator.generateSpanFromThreadLocal(id);
// 将新创建的Context存放到ThreadLocal栈中
Context.append(spanData);
// 并将当前的Context返回回去
return new ContextData(spanData);
} catch (Throwable t) {
logger.error(t.getMessage(), t);
return new EmptyContextData();
}
}
public void afterSend() {
if (!AuthDesc.isAuth())
return;
public void afterSend() {
try {
if (!AuthDesc.isAuth())
return;
// 弹出上下文的栈顶中的元素
Span spanData = Context.removeLastSpan();
if (spanData == null) {
return;
}
// 弹出上下文的栈顶中的元素
Span spanData = Context.removeLastSpan();
if (spanData == null) {
return;
}
// 加上花费时间
spanData.setCost(System.currentTimeMillis() - spanData.getStartDate());
// 加上花费时间
spanData.setCost(System.currentTimeMillis()
- spanData.getStartDate());
if (Config.BuriedPoint.PRINTF) {
logger.debug("TraceId:" + spanData.getTraceId() + "\tviewpointId:" + spanData.getViewPointId() + "\tParentLevelId:" + spanData.
getParentLevel() + "\tLevelId:" + spanData.getLevelId() + "\tbusinessKey:" + spanData.getBusinessKey());
}
if (Config.BuriedPoint.PRINTF) {
logger.debug("TraceId:" + spanData.getTraceId()
+ "\tviewpointId:" + spanData.getViewPointId()
+ "\tParentLevelId:" + spanData.getParentLevel()
+ "\tLevelId:" + spanData.getLevelId()
+ "\tbusinessKey:" + spanData.getBusinessKey());
}
// 存放到本地发送进程中
if (!Config.Sender.IS_OFF) {
ContextBuffer.save(spanData);
}
}
// 存放到本地发送进程中
if (!Config.Sender.IS_OFF) {
ContextBuffer.save(spanData);
}
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
}
}

View File

@ -13,35 +13,49 @@ import com.ai.cloud.skywalking.model.Identification;
import com.ai.cloud.skywalking.protocol.Span;
import com.ai.cloud.skywalking.util.ContextGenerator;
public class RPCBuriedPointReceiver extends ApplicationExceptionHandler implements IBuriedPointReceiver {
public class RPCBuriedPointReceiver extends ApplicationExceptionHandler
implements IBuriedPointReceiver {
private static Logger logger = LogManager.getLogger(RPCBuriedPointReceiver.class);
private static Logger logger = LogManager
.getLogger(RPCBuriedPointReceiver.class);
public void afterReceived() {
if (!AuthDesc.isAuth())
return;
public void afterReceived() {
try {
if (!AuthDesc.isAuth())
return;
// 获取上下文的栈顶中的元素
Span spanData = Context.removeLastSpan();
// 填上必要信息
spanData.setCost(System.currentTimeMillis() - spanData.getStartDate());
// 存放到本地发送进程中
ContextBuffer.save(spanData);
}
// 获取上下文的栈顶中的元素
Span spanData = Context.removeLastSpan();
// 填上必要信息
spanData.setCost(System.currentTimeMillis()
- spanData.getStartDate());
// 存放到本地发送进程中
ContextBuffer.save(spanData);
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
}
public void beforeReceived(ContextData context, Identification id) {
if (!AuthDesc.isAuth())
return;
public void beforeReceived(ContextData context, Identification id) {
try {
if (!AuthDesc.isAuth())
return;
Span spanData = ContextGenerator.generateSpanFromContextData(context, id);
//设置是否为接收端
spanData.setReceiver(true);
Span spanData = ContextGenerator.generateSpanFromContextData(
context, id);
// 设置是否为接收端
spanData.setReceiver(true);
if (Config.BuriedPoint.PRINTF) {
logger.debug("TraceId:" + spanData.getTraceId() + "\tviewpointId:" + spanData.getViewPointId() + "\tParentLevelId:" + spanData.
getParentLevel() + "\tLevelId:" + spanData.getLevelId());
}
if (Config.BuriedPoint.PRINTF) {
logger.debug("TraceId:" + spanData.getTraceId()
+ "\tviewpointId:" + spanData.getViewPointId()
+ "\tParentLevelId:" + spanData.getParentLevel()
+ "\tLevelId:" + spanData.getLevelId());
}
Context.append(spanData);
}
Context.append(spanData);
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
}
}

View File

@ -15,7 +15,13 @@ import com.ai.cloud.skywalking.protocol.Span;
import com.ai.cloud.skywalking.util.BuriedPointMachineUtil;
import com.ai.cloud.skywalking.util.TraceIdGenerator;
/**
* 暂不确定多线程的实现方式
*
* @author wusheng
*
*/
@Deprecated
public class ThreadBuriedPointSender extends ApplicationExceptionHandler implements IBuriedPointSender {
private static Logger logger = LogManager.getLogger(ThreadBuriedPointSender.class);

View File

@ -14,6 +14,13 @@ import com.ai.cloud.skywalking.model.Identification;
import com.ai.cloud.skywalking.protocol.Span;
import com.ai.cloud.skywalking.util.ContextGenerator;
/**
* 暂不确定多线程的实现方式
*
* @author wusheng
*
*/
@Deprecated
public class ThreadFactoryBuriedPointSender extends ApplicationExceptionHandler implements IBuriedPointSender {
private static Logger logger = LogManager.getLogger(ThreadBuriedPointSender.class);