kezhenxu94 commented on a change in pull request #5493:
URL: https://github.com/apache/skywalking/pull/5493#discussion_r490665685



##########
File path: 
apm-sniffer/apm-sdk-plugin/spring-plugins/spring-webflux-5.x-webclient-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/webclient/WebFluxWebClientInterceptor.java
##########
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.plugin.spring.webflux.v5.webclient;
+
+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.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.HttpStatus;
+import org.springframework.web.reactive.function.client.ClientRequest;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import reactor.core.publisher.Mono;
+
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.util.function.BiConsumer;
+
+public class WebFluxWebClientInterceptor implements 
InstanceMethodsAroundInterceptor {
+
+    @Override
+    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] 
allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws 
Throwable {
+        if (allArguments[0] == null) {
+            //illegal args,can't trace ignore
+            return;
+        }
+
+        ClientRequest request = (ClientRequest) allArguments[0];
+        final ContextCarrier contextCarrier = new ContextCarrier();
+
+        URI uri = request.url();
+        final String requestURIString = getRequestURIString(uri);
+        final String operationName = requestURIString;
+        final String remotePeer = getIPAndPort(uri);
+        AbstractSpan span = ContextManager.createExitSpan(operationName, 
contextCarrier, remotePeer);
+
+        //set componet name
+        span.setComponent(ComponentsDefine.SPRING_WEBCLIENT);
+        Tags.URL.set(span, uri.toString());
+        Tags.HTTP.METHOD.set(span, request.method().toString());
+        SpanLayer.asHttp(span);
+
+        //user async interface
+        span.prepareForAsync();
+        ContextManager.stopSpan();
+
+        objInst.setSkyWalkingDynamicField(span);
+
+        CarrierItem next = contextCarrier.items();
+        while (next.hasNext()) {
+            next = next.next();
+            request.headers().add(next.getHeadKey(), next.getHeadValue());
+        }
+    }
+
+    @Override
+    public Object afterMethod(EnhancedInstance objInst, Method method, 
Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
+        Mono<ClientResponse> ret1 = (Mono<ClientResponse>) ret;
+        AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();

Review comment:
       `span` may be `null` `if (allArguments[0] == null)`

##########
File path: test/plugin/scenarios/webflux-scenario/support-version.list
##########
@@ -23,3 +23,11 @@
 2.1.5.RELEASE
 2.1.6.RELEASE
 2.1.7.RELEASE
+2.1.8.RELEASE
+2.1.9.RELEASE
+2.1.10.RELEASE
+2.1.11.RELEASE
+2.1.12.RELEASE
+2.1.13.RELEASE
+2.1.14.RELEASE
+2.1.15.RELEASE

Review comment:
       Only pick up some of them to test, testing all versions requires many 
resources and makes the test slow

##########
File path: 
apm-sniffer/apm-sdk-plugin/spring-plugins/spring-webflux-5.x-webclient-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/webclient/WebFluxWebClientInterceptor.java
##########
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.plugin.spring.webflux.v5.webclient;
+
+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.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.HttpStatus;
+import org.springframework.web.reactive.function.client.ClientRequest;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import reactor.core.publisher.Mono;
+
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.util.function.BiConsumer;
+
+public class WebFluxWebClientInterceptor implements 
InstanceMethodsAroundInterceptor {
+
+    @Override
+    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] 
allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws 
Throwable {
+        if (allArguments[0] == null) {
+            //illegal args,can't trace ignore
+            return;
+        }
+
+        ClientRequest request = (ClientRequest) allArguments[0];
+        final ContextCarrier contextCarrier = new ContextCarrier();
+
+        URI uri = request.url();
+        final String requestURIString = getRequestURIString(uri);
+        final String operationName = requestURIString;
+        final String remotePeer = getIPAndPort(uri);
+        AbstractSpan span = ContextManager.createExitSpan(operationName, 
contextCarrier, remotePeer);
+
+        //set componet name
+        span.setComponent(ComponentsDefine.SPRING_WEBCLIENT);
+        Tags.URL.set(span, uri.toString());
+        Tags.HTTP.METHOD.set(span, request.method().toString());
+        SpanLayer.asHttp(span);
+
+        //user async interface
+        span.prepareForAsync();
+        ContextManager.stopSpan();
+
+        objInst.setSkyWalkingDynamicField(span);
+
+        CarrierItem next = contextCarrier.items();
+        while (next.hasNext()) {
+            next = next.next();
+            request.headers().add(next.getHeadKey(), next.getHeadValue());
+        }
+    }
+
+    @Override
+    public Object afterMethod(EnhancedInstance objInst, Method method, 
Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
+        Mono<ClientResponse> ret1 = (Mono<ClientResponse>) ret;
+        AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
+        return ret1.doAfterSuccessOrError(new BiConsumer<ClientResponse, 
Throwable>() {
+            @Override
+            public void accept(ClientResponse clientResponse, Throwable 
throwable) {
+                HttpStatus httpStatus = clientResponse.statusCode();
+                if (httpStatus != null) {
+                    Tags.STATUS_CODE.set(span, 
Integer.toString(httpStatus.value()));
+                    if (httpStatus.isError()) {
+                        span.errorOccurred();
+                    }
+                }
+            }
+        }).doOnError(error -> {
+            span.errorOccurred();
+            span.log(error);

Review comment:
       You don't need `span.errorOccurred();` now
   ```suggestion
               span.log(error);
   ```




----------------------------------------------------------------
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:
[email protected]


Reply via email to