Commit 44556ae0 authored by 赵禹光's avatar 赵禹光 Committed by 吴晟
Browse files

gateway & webflux compatible with these scene (#3419)

parent d6a84619
Loading
Loading
Loading
Loading
+17 −15
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ import org.springframework.web.server.ServerWebExchange;

import java.lang.reflect.Method;

public class BodyInserterResponseMethodInterceptor implements InstanceMethodsAroundInterceptor {
public class AbstractServerResponseMethodInterceptor implements InstanceMethodsAroundInterceptor {

    /**
     * The error reason
@@ -43,7 +43,8 @@ public class BodyInserterResponseMethodInterceptor implements InstanceMethodsAro

    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
        EnhancedInstance instance = (EnhancedInstance) allArguments[0];
        EnhancedInstance instance = DispatcherHandlerHandleMethodInterceptor.getInstance(allArguments[0]);
        if (instance != null) {
            AbstractSpan span = (AbstractSpan) instance.getSkyWalkingDynamicField();
            if (span == null) {
                return;
@@ -60,6 +61,7 @@ public class BodyInserterResponseMethodInterceptor implements InstanceMethodsAro
            ContextManager.stopSpan(span);
            ((EnhancedInstance) allArguments[0]).setSkyWalkingDynamicField(null);
        }
    }

    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
+32 −15
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInt
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.springframework.http.HttpHeaders;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebExchangeDecorator;
import org.springframework.web.server.adapter.DefaultServerWebExchange;

import java.lang.reflect.Method;
import java.util.List;
@@ -45,10 +47,11 @@ public class DispatcherHandlerHandleMethodInterceptor implements InstanceMethods
    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                             MethodInterceptResult result) throws Throwable {
        EnhancedInstance instance = DispatcherHandlerHandleMethodInterceptor.getInstance(allArguments[0]);
        if (instance != null) {
            ContextCarrier contextCarrier = new ContextCarrier();
            CarrierItem next = contextCarrier.items();
            ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
        EnhancedInstance instance = (EnhancedInstance) allArguments[0];
            HttpHeaders headers = exchange.getRequest().getHeaders();
            while (next.hasNext()) {
                next = next.next();
@@ -63,6 +66,7 @@ public class DispatcherHandlerHandleMethodInterceptor implements InstanceMethods
            Tags.URL.set(span, exchange.getRequest().getURI().toString());
            instance.setSkyWalkingDynamicField(span);
        }
    }

    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
@@ -74,4 +78,17 @@ public class DispatcherHandlerHandleMethodInterceptor implements InstanceMethods
    public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
                                      Class<?>[] argumentsTypes, Throwable t) {
    }

    public static EnhancedInstance getInstance(Object o) {
        EnhancedInstance instance = null;
        if (o instanceof ServerWebExchangeDecorator) {
            ServerWebExchange delegate = ((ServerWebExchangeDecorator) o).getDelegate();
            if (delegate instanceof DefaultServerWebExchange) {
                instance = (EnhancedInstance) delegate;
            }
        } else if (o instanceof DefaultServerWebExchange) {
            instance = (EnhancedInstance) o;
        }
        return instance;
    }
}
+7 −5
Original line number Diff line number Diff line
@@ -40,12 +40,14 @@ public class DispatcherHandlerHandleResultMethodInterceptor implements InstanceM
    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                              Object ret) throws Throwable {
        EnhancedInstance instance = (EnhancedInstance) allArguments[0];
        EnhancedInstance instance = DispatcherHandlerHandleMethodInterceptor.getInstance(allArguments[0]);
        if (instance != null) {
            AbstractSpan span = (AbstractSpan) instance.getSkyWalkingDynamicField();
            if (span != null) {
                ContextManager.stopSpan(span);
                instance.setSkyWalkingDynamicField(null);
            }
        }
        return ret;
    }

+20 −18
Original line number Diff line number Diff line
@@ -41,7 +41,8 @@ public class DispatcherHandlerInvokeHandlerMethodInterceptor implements Instance
    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                             MethodInterceptResult result) throws Throwable {
        EnhancedInstance instance = (EnhancedInstance) allArguments[0];
        EnhancedInstance instance = DispatcherHandlerHandleMethodInterceptor.getInstance(allArguments[0]);
        if (instance != null) {
            AbstractSpan span = (AbstractSpan) instance.getSkyWalkingDynamicField();
            if (span == null) {
                return;
@@ -62,6 +63,7 @@ public class DispatcherHandlerInvokeHandlerMethodInterceptor implements Instance
                span.setOperationName(getHandlerMethodOperationName(handler));
            }
        }
    }

    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceC
 *
 * @author zhaoyuguang
 */
public class DefaultServerWebExchangeConstructorInterceptor implements InstanceConstructorInterceptor {
public class ServerWebExchangeConstructorInterceptor implements InstanceConstructorInterceptor {
    @Override
    public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    }
Loading