wu-sheng commented on a change in pull request #3945: Enhance webflux plugin, 
related to Spring Gateway plugin too.
URL: https://github.com/apache/skywalking/pull/3945#discussion_r358115858
 
 

 ##########
 File path: 
apm-sniffer/optional-plugins/optional-spring-plugins/spring-webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/DispatcherHandlerHandleMethodInterceptor.java
 ##########
 @@ -18,60 +18,87 @@
 
 package org.apache.skywalking.apm.plugin.spring.webflux.v5;
 
-/**
- * @author zhaoyuguang
- */
-
+import java.lang.reflect.Method;
+import java.util.List;
 import org.apache.skywalking.apm.agent.core.context.CarrierItem;
 import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
 import org.apache.skywalking.apm.agent.core.context.ContextManager;
 import org.apache.skywalking.apm.agent.core.context.tag.Tags;
+import org.apache.skywalking.apm.agent.core.context.tag.Tags.HTTP;
 import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
 import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
 import 
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
 import 
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
 import 
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
 import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
 import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.reactive.HandlerMapping;
 import org.springframework.web.server.ServerWebExchange;
 import org.springframework.web.server.ServerWebExchangeDecorator;
 import org.springframework.web.server.adapter.DefaultServerWebExchange;
+import org.springframework.web.util.pattern.PathPattern;
+import reactor.core.publisher.Mono;
 
-import java.lang.reflect.Method;
-import java.util.List;
-
+/**
+ * @author Born
+ */
 public class DispatcherHandlerHandleMethodInterceptor implements 
InstanceMethodsAroundInterceptor {
-
     private static final String WIP_OPERATION_NAME = "WEBFLUX.handle";
 
     @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];
-            HttpHeaders headers = exchange.getRequest().getHeaders();
-            while (next.hasNext()) {
-                next = next.next();
-                List<String> header = headers.get(next.getHeadKey());
-                if (header != null && header.size() > 0) {
-                    next.setHeadValue(header.get(0));
-                }
-            }
-            AbstractSpan span = 
ContextManager.createEntrySpan(WIP_OPERATION_NAME, contextCarrier);
-            span.setComponent(ComponentsDefine.SPRING_WEBFLUX);
-            SpanLayer.asHttp(span);
-            Tags.URL.set(span, exchange.getRequest().getURI().toString());
-            instance.setSkyWalkingDynamicField(span);
-        }
+
     }
 
     @Override
     public Object afterMethod(EnhancedInstance objInst, Method method, 
Object[] allArguments, Class<?>[] argumentsTypes,
                               Object ret) throws Throwable {
-        return ret;
+        EnhancedInstance instance = getInstance(allArguments[0]);
+
+        ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
+
+        ContextCarrier carrier = new ContextCarrier();
+        CarrierItem next = carrier.items();
+        HttpHeaders headers = exchange.getRequest().getHeaders();
+        while (next.hasNext()) {
+            next = next.next();
+            List<String> header = headers.get(next.getHeadKey());
+            if (header != null && header.size() > 0) {
+                next.setHeadValue(header.get(0));
+            }
+        }
+
+        AbstractSpan span = ContextManager.createEntrySpan(WIP_OPERATION_NAME, 
carrier);
+        span.setComponent(ComponentsDefine.SPRING_WEBFLUX);
+        SpanLayer.asHttp(span);
+        Tags.URL.set(span, exchange.getRequest().getURI().toString());
+        HTTP.METHOD.set(span, exchange.getRequest().getMethodValue());
+        instance.setSkyWalkingDynamicField(ContextManager.capture());
+        span.prepareForAsync();
+        ContextManager.stopSpan(span);
+
+        return ((Mono) ret).doFinally(s -> {
+            try {
+                HttpStatus httpStatus = exchange.getResponse().getStatusCode();
+                if 
(exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE) != null) 
{
+                    String urlPattern = ((PathPattern) exchange
+                            
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)).getPatternString();
+                    span.setOperationName(urlPattern);
+                }
+                // fix webflux-2.0.0-2.1.0 version have bug. httpStatus is 
null. not support
+                if (httpStatus != null) {
+                    Tags.STATUS_CODE.set(span, 
Integer.toString(httpStatus.value()));
+                    if (httpStatus.isError()) {
+                        span.errorOccurred();
+                    }
+                }
+            } finally {
 
 Review comment:
   I think this is not for exception catch, this is making sure span finished.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to