gzlicanyi commented on code in PR #555:
URL: https://github.com/apache/skywalking-java/pull/555#discussion_r1232021790


##########
apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v20x/HttpClientRequestInterceptor.java:
##########
@@ -89,27 +97,45 @@ public Object afterMethod(final EnhancedInstance objInst,
                               final Object[] allArguments,
                               final Class<?>[] argumentsTypes,
                               final Object ret) {
-        EnhanceCacheObject enhanceCacheObject = (EnhanceCacheObject) 
objInst.getSkyWalkingDynamicField();
+        EnhanceCacheObject enhanceCacheObject = ENHANCE_CONTEXT.get();
+        ENHANCE_CONTEXT.remove();
         Mono<HttpClientResponse> responseMono = (Mono<HttpClientResponse>) ret;
         return responseMono.doAfterSuccessOrError(new 
BiConsumer<HttpClientResponse, Throwable>() {
             @Override
             public void accept(final HttpClientResponse httpClientResponse, 
final Throwable throwable) {
+                doAfterSuccessOrError(httpClientResponse, throwable, 
enhanceCacheObject);
+            }
+        });
+    }
 
-                AbstractSpan abstractSpan = enhanceCacheObject.getSendSpan();
-                if (abstractSpan != null) {
-                    if (throwable != null) {
-                        abstractSpan.log(throwable);
-                    } else if (httpClientResponse.status().code() > 400) {
-                        abstractSpan.errorOccurred();
-                    }
-                    Tags.HTTP_RESPONSE_STATUS_CODE.set(abstractSpan, 
httpClientResponse.status().code());
-                    abstractSpan.asyncFinish();
-                }
+    void doAfterSuccessOrError(HttpClientResponse httpClientResponse, 
Throwable throwable, EnhanceCacheObject enhanceCacheObject) {
+        try {
+            //When executing the beforeMethod method, if the ContextManager is 
inactive, the enhanceCacheObject will be null.
+            if (enhanceCacheObject == null) {
+                return;
+            }
 
-                objInst.setSkyWalkingDynamicField(null);
-                enhanceCacheObject.getFilterSpan().asyncFinish();
+            //The doAfterSuccessOrError method may be executed multiple times.
+            if (enhanceCacheObject.isSpanFinish()) {
+                return;
             }
-        });
+
+            AbstractSpan abstractSpan = enhanceCacheObject.getSendSpan();
+            if (throwable != null) {
+                abstractSpan.log(throwable);
+            } else if (httpClientResponse.status().code() > 400) {
+                abstractSpan.errorOccurred();
+            }
+            Tags.HTTP_RESPONSE_STATUS_CODE.set(abstractSpan, 
httpClientResponse.status().code());
+
+            abstractSpan.asyncFinish();
+            enhanceCacheObject.getFilterSpan().asyncFinish();
+
+            enhanceCacheObject.setSpanFinish(true);
+        } catch (Throwable e) {
+            //Catch unknown exceptions to avoid interrupting business 
processes.
+            LOGGER.error("Failed to finish the Span of 
reactor.ipc.netty.http.client.HttpClient.", e);
+        }

Review Comment:
   Used to discover if there are any other unknown exceptions.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to