apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v3x/HttpClientFinalizerResponseConnectionInterceptor.java [40:104]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class HttpClientFinalizerResponseConnectionInterceptor implements InstanceMethodsAroundInterceptor {

    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
            MethodInterceptResult result) {
        BiFunction<? super HttpClientResponse, ? super Connection, ? extends Publisher> finalReceiver = (BiFunction<? super HttpClientResponse, ? super Connection, ? extends Publisher>) allArguments[0];
        EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
        allArguments[0] = (BiFunction<HttpClientResponse, Connection, Publisher>) (response, connection) -> {
            Publisher publisher = finalReceiver.apply(response, connection);
            if (cache == null) {
                return publisher;
            }
            // receive the response.
            if (cache.getSpan() != null) {
                if (response.status().code() >= HttpResponseStatus.BAD_REQUEST.code()) {
                    cache.getSpan().errorOccurred();
                }
                Tags.HTTP_RESPONSE_STATUS_CODE.set(cache.getSpan(), response.status().code());
            }
            
            return publisher;
        };
    }

    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                              Object ret) {
        Flux<?> responseFlux = (Flux<?>) ret;

        responseFlux = responseFlux
                .doOnError(e -> {
                    EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
                    if (cache == null) {
                        return;
                    }
                    
                    if (cache.getSpan() != null) {
                        cache.getSpan().errorOccurred();
                        cache.getSpan().log(e);
                    }
                })
                .doFinally(signalType -> {
                    EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
                    if (cache == null) {
                        return;
                    }
                    // do finally. Finish the span.
                    if (cache.getSpan() != null) {
                        if (signalType == SignalType.CANCEL) {
                            cache.getSpan().errorOccurred();
                        }
                        cache.getSpan().asyncFinish();
                    }

                    if (cache.getSpan1() != null) {
                        cache.getSpan1().asyncFinish();
                    }
                });

        return responseFlux;
    }

    @Override
    public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
            Class<?>[] argumentsTypes, Throwable t) {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v4x/HttpClientFinalizerResponseConnectionInterceptor.java [40:104]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class HttpClientFinalizerResponseConnectionInterceptor implements InstanceMethodsAroundInterceptor {

    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
            MethodInterceptResult result) {
        BiFunction<? super HttpClientResponse, ? super Connection, ? extends Publisher> finalReceiver = (BiFunction<? super HttpClientResponse, ? super Connection, ? extends Publisher>) allArguments[0];
        EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
        allArguments[0] = (BiFunction<HttpClientResponse, Connection, Publisher>) (response, connection) -> {
            Publisher publisher = finalReceiver.apply(response, connection);
            if (cache == null) {
                return publisher;
            }
            // receive the response.
            if (cache.getSpan() != null) {
                if (response.status().code() >= HttpResponseStatus.BAD_REQUEST.code()) {
                    cache.getSpan().errorOccurred();
                }
                Tags.HTTP_RESPONSE_STATUS_CODE.set(cache.getSpan(), response.status().code());
            }
            
            return publisher;
        };
    }

    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                              Object ret) {
        Flux<?> responseFlux = (Flux<?>) ret;

        responseFlux = responseFlux
                .doOnError(e -> {
                    EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
                    if (cache == null) {
                        return;
                    }
                    
                    if (cache.getSpan() != null) {
                        cache.getSpan().errorOccurred();
                        cache.getSpan().log(e);
                    }
                })
                .doFinally(signalType -> {
                    EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
                    if (cache == null) {
                        return;
                    }
                    // do finally. Finish the span.
                    if (cache.getSpan() != null) {
                        if (signalType == SignalType.CANCEL) {
                            cache.getSpan().errorOccurred();
                        }
                        cache.getSpan().asyncFinish();
                    }

                    if (cache.getSpan1() != null) {
                        cache.getSpan1().asyncFinish();
                    }
                });

        return responseFlux;
    }

    @Override
    public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
            Class<?>[] argumentsTypes, Throwable t) {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



