|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v412x; |
| 19 | + |
| 20 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 21 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 22 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 23 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 24 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 25 | +import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v4x.define.EnhanceObjectCache; |
| 26 | +import org.reactivestreams.Publisher; |
| 27 | +import reactor.core.publisher.Flux; |
| 28 | +import reactor.core.publisher.SignalType; |
| 29 | +import reactor.netty.Connection; |
| 30 | +import reactor.netty.http.client.HttpClientResponse; |
| 31 | + |
| 32 | +import java.lang.reflect.Method; |
| 33 | +import java.util.function.BiFunction; |
| 34 | + |
| 35 | +/** |
| 36 | + * This class intercept <code>responseConnection</code> method. |
| 37 | + * <p> |
| 38 | + * After downstream service response, finish the span in the {@link EnhanceObjectCache}. |
| 39 | + */ |
| 40 | +public class HttpClientFinalizerResponseConnectionV412Interceptor implements InstanceMethodsAroundInterceptor { |
| 41 | + |
| 42 | + @Override |
| 43 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 44 | + MethodInterceptResult result) { |
| 45 | + BiFunction<? super HttpClientResponse, ? super Connection, ? extends Publisher> finalReceiver = (BiFunction<? super HttpClientResponse, ? super Connection, ? extends Publisher>) allArguments[0]; |
| 46 | + EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField(); |
| 47 | + allArguments[0] = (BiFunction<HttpClientResponse, Connection, Publisher>) (response, connection) -> { |
| 48 | + Publisher publisher = finalReceiver.apply(response, connection); |
| 49 | + if (cache == null) { |
| 50 | + return publisher; |
| 51 | + } |
| 52 | + // receive the response. |
| 53 | + if (cache.getSpan() != null) { |
| 54 | + if (response.status().code() >= HttpResponseStatus.BAD_REQUEST.code()) { |
| 55 | + cache.getSpan().errorOccurred(); |
| 56 | + } |
| 57 | + Tags.HTTP_RESPONSE_STATUS_CODE.set(cache.getSpan(), response.status().code()); |
| 58 | + } |
| 59 | + |
| 60 | + return publisher; |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 66 | + Object ret) { |
| 67 | + Flux<?> responseFlux = (Flux<?>) ret; |
| 68 | + |
| 69 | + responseFlux = responseFlux |
| 70 | + .doOnError(e -> { |
| 71 | + EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField(); |
| 72 | + if (cache == null) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (cache.getSpan() != null) { |
| 77 | + cache.getSpan().errorOccurred(); |
| 78 | + cache.getSpan().log(e); |
| 79 | + } |
| 80 | + }) |
| 81 | + .doFinally(signalType -> { |
| 82 | + EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField(); |
| 83 | + if (cache == null) { |
| 84 | + return; |
| 85 | + } |
| 86 | + // do finally. Finish the span. |
| 87 | + if (cache.getSpan() != null) { |
| 88 | + if (signalType == SignalType.CANCEL) { |
| 89 | + cache.getSpan().errorOccurred(); |
| 90 | + } |
| 91 | + cache.getSpan().asyncFinish(); |
| 92 | + } |
| 93 | + |
| 94 | + if (cache.getSpan1() != null) { |
| 95 | + cache.getSpan1().asyncFinish(); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + return responseFlux; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 104 | + Class<?>[] argumentsTypes, Throwable t) { |
| 105 | + |
| 106 | + } |
| 107 | +} |
0 commit comments