|
| 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 org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 21 | +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 23 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 24 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 25 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 26 | +import org.springframework.web.server.ServerWebExchange; |
| 27 | +import org.springframework.web.server.ServerWebExchangeDecorator; |
| 28 | +import org.springframework.web.server.adapter.DefaultServerWebExchange; |
| 29 | + |
| 30 | +import java.lang.reflect.Method; |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
| 32 | + |
| 33 | +import static org.apache.skywalking.apm.network.trace.component.ComponentsDefine.SPRING_CLOUD_GATEWAY; |
| 34 | + |
| 35 | +public class GatewayFilterV412Interceptor implements InstanceMethodsAroundInterceptor { |
| 36 | + |
| 37 | + private static final ThreadLocal<AtomicInteger> STACK_DEEP = ThreadLocal.withInitial(() -> new AtomicInteger(0)); |
| 38 | + |
| 39 | + @Override |
| 40 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 41 | + MethodInterceptResult result) throws Throwable { |
| 42 | + if (isEntry()) { |
| 43 | + ServerWebExchange exchange = (ServerWebExchange) allArguments[0]; |
| 44 | + |
| 45 | + EnhancedInstance enhancedInstance = getInstance(exchange); |
| 46 | + |
| 47 | + AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/GatewayFilter"); |
| 48 | + if (enhancedInstance != null && enhancedInstance.getSkyWalkingDynamicField() != null) { |
| 49 | + ContextManager.continued((ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField()); |
| 50 | + } |
| 51 | + span.setComponent(SPRING_CLOUD_GATEWAY); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static EnhancedInstance getInstance(Object o) { |
| 56 | + EnhancedInstance instance = null; |
| 57 | + if (o instanceof DefaultServerWebExchange) { |
| 58 | + instance = (EnhancedInstance) o; |
| 59 | + } else if (o instanceof ServerWebExchangeDecorator) { |
| 60 | + ServerWebExchange delegate = ((ServerWebExchangeDecorator) o).getDelegate(); |
| 61 | + return getInstance(delegate); |
| 62 | + } |
| 63 | + return instance; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 68 | + Object ret) throws Throwable { |
| 69 | + if (isExit()) { |
| 70 | + if (ContextManager.isActive()) { |
| 71 | + ContextManager.stopSpan(); |
| 72 | + } |
| 73 | + } |
| 74 | + return ret; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 79 | + Class<?>[] argumentsTypes, Throwable t) { |
| 80 | + ContextManager.activeSpan().log(t); |
| 81 | + } |
| 82 | + |
| 83 | + private boolean isEntry() { |
| 84 | + return STACK_DEEP.get().getAndIncrement() == 0; |
| 85 | + } |
| 86 | + |
| 87 | + private boolean isExit() { |
| 88 | + boolean isExit = STACK_DEEP.get().decrementAndGet() == 0; |
| 89 | + if (isExit) { |
| 90 | + STACK_DEEP.remove(); |
| 91 | + } |
| 92 | + return isExit; |
| 93 | + } |
| 94 | +} |
0 commit comments