未验证 提交 b6cc9a59 编写于 作者: Y yoje 提交者: GitHub

Fix npe in afterMethod/handleMethodException of kafka/finagle plugins (#4712)

* Fix npe in afterMethod/handleMethodException of kafka/finagle plugins
上级 071708a4
...@@ -36,7 +36,7 @@ public interface InstanceMethodsAroundInterceptor { ...@@ -36,7 +36,7 @@ public interface InstanceMethodsAroundInterceptor {
/** /**
* called after target method invocation. Even method's invocation triggers an exception. * called after target method invocation. Even method's invocation triggers an exception.
* *
* @param ret the method's original return value. * @param ret the method's original return value. May be null if the method triggers an exception.
* @return the method's actual return value. * @return the method's actual return value.
*/ */
Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
......
...@@ -58,7 +58,13 @@ public class ClientDestTracingFilterInterceptor extends AbstractInterceptor { ...@@ -58,7 +58,13 @@ public class ClientDestTracingFilterInterceptor extends AbstractInterceptor {
@Override @Override
public void handleMethodExceptionImpl(EnhancedInstance enhancedInstance, Method method, Object[] objects, public void handleMethodExceptionImpl(EnhancedInstance enhancedInstance, Method method, Object[] objects,
Class<?>[] classes, Throwable t) { Class<?>[] classes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t); /*
* Current thread may not be the same thread that execute ClientTracingFilterInterceptor, we can not ensure
* there is an active span
*/
if (ContextManager.isActive()) {
ContextManager.activeSpan().errorOccurred().log(t);
}
} }
private String getRemote(Object[] objects) { private String getRemote(Object[] objects) {
......
...@@ -66,22 +66,29 @@ public class ClientTracingFilterInterceptor extends AbstractInterceptor { ...@@ -66,22 +66,29 @@ public class ClientTracingFilterInterceptor extends AbstractInterceptor {
getLocalContextHolder().remove(SW_SPAN); getLocalContextHolder().remove(SW_SPAN);
getMarshalledContextHolder().remove(SWContextCarrier$.MODULE$); getMarshalledContextHolder().remove(SWContextCarrier$.MODULE$);
finagleSpan.prepareForAsync(); /*
ContextManager.stopSpan(finagleSpan); * If the intercepted method throws exception, the ret will be null
*/
((Future<?>) ret).addEventListener(new FutureEventListener<Object>() { if (ret == null) {
@Override ContextManager.stopSpan(finagleSpan);
public void onSuccess(Object value) { } else {
finagleSpan.asyncFinish(); finagleSpan.prepareForAsync();
} ContextManager.stopSpan(finagleSpan);
@Override ((Future<?>) ret).addEventListener(new FutureEventListener<Object>() {
public void onFailure(Throwable cause) { @Override
finagleSpan.errorOccurred(); public void onSuccess(Object value) {
finagleSpan.log(cause); finagleSpan.asyncFinish();
finagleSpan.asyncFinish(); }
}
}); @Override
public void onFailure(Throwable cause) {
finagleSpan.errorOccurred();
finagleSpan.log(cause);
finagleSpan.asyncFinish();
}
});
}
return ret; return ret;
} }
......
...@@ -61,21 +61,29 @@ public class ServerTracingFilterInterceptor extends AbstractInterceptor { ...@@ -61,21 +61,29 @@ public class ServerTracingFilterInterceptor extends AbstractInterceptor {
public Object afterMethodImpl(EnhancedInstance enhancedInstance, Method method, Object[] objects, Class<?>[] classes, Object ret) throws Throwable { public Object afterMethodImpl(EnhancedInstance enhancedInstance, Method method, Object[] objects, Class<?>[] classes, Object ret) throws Throwable {
final AbstractSpan finagleSpan = getSpan(); final AbstractSpan finagleSpan = getSpan();
getLocalContextHolder().remove(FinagleCtxs.SW_SPAN); getLocalContextHolder().remove(FinagleCtxs.SW_SPAN);
finagleSpan.prepareForAsync();
ContextManager.stopSpan(finagleSpan);
((Future<?>) ret).addEventListener(new FutureEventListener<Object>() {
@Override
public void onSuccess(Object value) {
finagleSpan.asyncFinish();
}
@Override /*
public void onFailure(Throwable cause) { * If the intercepted method throws exception, the ret will be null
finagleSpan.errorOccurred(); */
finagleSpan.log(cause); if (ret == null) {
finagleSpan.asyncFinish(); ContextManager.stopSpan(finagleSpan);
} } else {
}); finagleSpan.prepareForAsync();
ContextManager.stopSpan(finagleSpan);
((Future<?>) ret).addEventListener(new FutureEventListener<Object>() {
@Override
public void onSuccess(Object value) {
finagleSpan.asyncFinish();
}
@Override
public void onFailure(Throwable cause) {
finagleSpan.errorOccurred();
finagleSpan.log(cause);
finagleSpan.asyncFinish();
}
});
}
return ret; return ret;
} }
......
...@@ -51,6 +51,12 @@ public class KafkaConsumerInterceptor implements InstanceMethodsAroundIntercepto ...@@ -51,6 +51,12 @@ public class KafkaConsumerInterceptor implements InstanceMethodsAroundIntercepto
@Override @Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable { Object ret) throws Throwable {
/*
* If the intercepted method throws exception, the ret will be null
*/
if (ret == null) {
return ret;
}
Map<TopicPartition, List<ConsumerRecord<?, ?>>> records = (Map<TopicPartition, List<ConsumerRecord<?, ?>>>) ret; Map<TopicPartition, List<ConsumerRecord<?, ?>>> records = (Map<TopicPartition, List<ConsumerRecord<?, ?>>>) ret;
// //
// The entry span will only be created when the consumer received at least one message. // The entry span will only be created when the consumer received at least one message.
...@@ -88,6 +94,12 @@ public class KafkaConsumerInterceptor implements InstanceMethodsAroundIntercepto ...@@ -88,6 +94,12 @@ public class KafkaConsumerInterceptor implements InstanceMethodsAroundIntercepto
@Override @Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) { Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t); /*
* The entry span is created in {@link #afterMethod}, but {@link #handleMethodException} is called before
* {@link #afterMethod}, before the creation of entry span, we can not ensure there is an active span
*/
if (ContextManager.isActive()) {
ContextManager.activeSpan().errorOccurred().log(t);
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册