|
| 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.v4x; |
| 19 | + |
| 20 | +import org.apache.skywalking.apm.agent.core.context.CarrierItem; |
| 21 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 23 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 25 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 26 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 27 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 28 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 29 | +import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v4x.define.EnhanceObjectCache; |
| 30 | +import org.apache.skywalking.apm.util.StringUtil; |
| 31 | +import org.reactivestreams.Publisher; |
| 32 | +import reactor.netty.NettyOutbound; |
| 33 | +import reactor.netty.http.client.HttpClientRequest; |
| 34 | + |
| 35 | +import java.lang.reflect.Method; |
| 36 | +import java.net.URL; |
| 37 | +import java.util.function.BiFunction; |
| 38 | + |
| 39 | +import static org.apache.skywalking.apm.network.trace.component.ComponentsDefine.SPRING_CLOUD_GATEWAY; |
| 40 | + |
| 41 | +/** |
| 42 | + * This class intercept <code>send</code> method. |
| 43 | + * <p> |
| 44 | + * In before method, create a new BiFunction lambda expression for setting <code>ContextCarrier</code> to http header |
| 45 | + * and replace the original lambda in argument |
| 46 | + */ |
| 47 | +public class HttpClientFinalizerSendInterceptor implements InstanceMethodsAroundInterceptor { |
| 48 | + |
| 49 | + @Override |
| 50 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 51 | + MethodInterceptResult result) throws Throwable { |
| 52 | + EnhanceObjectCache enhanceObjectCache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField(); |
| 53 | + if (enhanceObjectCache == null) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + In this plug-in, the HttpClientFinalizerSendInterceptor depends on the NettyRoutingFilterInterceptor |
| 59 | + When the NettyRoutingFilterInterceptor is not executed, the HttpClientFinalizerSendInterceptor has no meaning to be executed independently |
| 60 | + and using ContextManager.activeSpan() method would cause NPE as active span does not exist. |
| 61 | + */ |
| 62 | + if (!ContextManager.isActive()) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + AbstractSpan span = ContextManager.activeSpan(); |
| 67 | + span.prepareForAsync(); |
| 68 | + |
| 69 | + if (StringUtil.isNotEmpty(enhanceObjectCache.getUrl())) { |
| 70 | + URL url = new URL(enhanceObjectCache.getUrl()); |
| 71 | + |
| 72 | + ContextCarrier contextCarrier = new ContextCarrier(); |
| 73 | + AbstractSpan abstractSpan = ContextManager.createExitSpan( |
| 74 | + "SpringCloudGateway/sendRequest", contextCarrier, getPeer(url)); |
| 75 | + Tags.URL.set(abstractSpan, enhanceObjectCache.getUrl()); |
| 76 | + abstractSpan.prepareForAsync(); |
| 77 | + abstractSpan.setComponent(SPRING_CLOUD_GATEWAY); |
| 78 | + abstractSpan.setLayer(SpanLayer.HTTP); |
| 79 | + ContextManager.stopSpan(abstractSpan); |
| 80 | + |
| 81 | + BiFunction<? super HttpClientRequest, ? super NettyOutbound, ? extends Publisher<Void>> finalSender = (BiFunction<? super HttpClientRequest, ? super NettyOutbound, ? extends Publisher<Void>>) allArguments[0]; |
| 82 | + allArguments[0] = (BiFunction<HttpClientRequest, NettyOutbound, Publisher<Void>>) (request, outbound) -> { |
| 83 | + Publisher publisher = finalSender.apply(request, outbound); |
| 84 | + |
| 85 | + CarrierItem next = contextCarrier.items(); |
| 86 | + while (next.hasNext()) { |
| 87 | + next = next.next(); |
| 88 | + request.requestHeaders().remove(next.getHeadKey()); |
| 89 | + request.requestHeaders().set(next.getHeadKey(), next.getHeadValue()); |
| 90 | + } |
| 91 | + return publisher; |
| 92 | + }; |
| 93 | + enhanceObjectCache.setCacheSpan(abstractSpan); |
| 94 | + } |
| 95 | + ContextManager.stopSpan(span); |
| 96 | + enhanceObjectCache.setSpan1(span); |
| 97 | + } |
| 98 | + |
| 99 | + private String getPeer(URL url) { |
| 100 | + return url.getHost() + ":" + url.getPort(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 105 | + Object ret) { |
| 106 | + ((EnhancedInstance) ret).setSkyWalkingDynamicField(objInst.getSkyWalkingDynamicField()); |
| 107 | + return ret; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 112 | + Class<?>[] argumentsTypes, Throwable t) { |
| 113 | + |
| 114 | + } |
| 115 | +} |
0 commit comments