diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/RuntimeContextConfiguration.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/RuntimeContextConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..f1b95a3ca167470ab4c25eaa87fd205d7d6bf283 --- /dev/null +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/RuntimeContextConfiguration.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ + +package org.apache.skywalking.apm.agent.core.conf; + +public class RuntimeContextConfiguration { + + public static String[] NEED_PROPAGATE_CONTEXT_KEY = new String[] { + "SW_REQUEST", + "SW_RESPONSE" + }; +} diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java index adc6a3f50777e5a873726345cd106d9bdb8641e7..a7c5453b15e85a79ae044dccbacf153a58459f65 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java @@ -105,8 +105,4 @@ public interface AbstractTracerContext { */ void stopSpan(AbstractSpan span); - /** - * @return the runtime context from current tracing context. - */ - RuntimeContext getRuntimeContext(); } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java index e1ce82890fd7786f9ad9c11fa8283a21617e3aa7..a2ea19aaad0936a45fb9e81685ffe12d8bd97a70 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java @@ -42,6 +42,7 @@ import org.apache.skywalking.apm.util.StringUtil; public class ContextManager implements TracingContextListener, BootService, IgnoreTracerContextListener { private static final ILog logger = LogManager.getLogger(ContextManager.class); private static ThreadLocal CONTEXT = new ThreadLocal(); + private static ThreadLocal RUNTIME_CONTEXT = new ThreadLocal(); private static ContextManagerExtendService EXTEND_SERVICE; private static AbstractTracerContext getOrCreate(String operationName, boolean forceSampling) { @@ -198,11 +199,12 @@ public class ContextManager implements TracingContextListener, BootService, Igno } public static RuntimeContext getRuntimeContext() { - if (isActive()) { - return get().getRuntimeContext(); - } else { - throw new IllegalStateException("No active context"); + RuntimeContext runtimeContext = RUNTIME_CONTEXT.get(); + if (runtimeContext == null) { + runtimeContext = new RuntimeContext(RUNTIME_CONTEXT); + RUNTIME_CONTEXT.set(runtimeContext); } - } + return runtimeContext; + } } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java index a52fd2efc88034568f2cc0028187cb359f991b8e..fd115e5662400a2a8c7e1b7afca4c6ce2fc12d6f 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java @@ -37,13 +37,6 @@ public class IgnoredTracerContext implements AbstractTracerContext { private int stackDepth; - /** - * Runtime context of the ignored context - * - * The context should work even no trace, in order to avoid the unexpected status. - */ - private RuntimeContext runtimeContext; - public IgnoredTracerContext() { this.stackDepth = 0; } @@ -102,13 +95,6 @@ public class IgnoredTracerContext implements AbstractTracerContext { } } - @Override public RuntimeContext getRuntimeContext() { - if (runtimeContext == null) { - runtimeContext = new RuntimeContext(); - } - return runtimeContext; - } - public static class ListenerManager { private static List LISTENERS = new LinkedList(); diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContext.java index 3bd79fd62a3370c61d677b023990da41705af58d..a96098fae14ad664817fe687a0e1be043de1ceeb 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContext.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContext.java @@ -18,8 +18,11 @@ package org.apache.skywalking.apm.agent.core.context; +import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.apache.skywalking.apm.agent.core.conf.RuntimeContextConfiguration; /** * RuntimeContext is alive during the tracing context. @@ -27,11 +30,16 @@ import java.util.concurrent.ConcurrentHashMap; * * In most cases, it means it only stays in a single thread for context propagation. * - * @author wusheng + * @author wusheng, ascrutae */ public class RuntimeContext { + private final ThreadLocal contextThreadLocal; private Map context = new ConcurrentHashMap(0); + public RuntimeContext(ThreadLocal contextThreadLocal) { + this.contextThreadLocal = contextThreadLocal; + } + public void put(Object key, Object value) { context.put(key, value); } @@ -43,4 +51,32 @@ public class RuntimeContext { public T get(Object key, Class type) { return (T)context.get(key); } + + public void remove(Object key) { + context.remove(key); + + if (context.isEmpty()) { + contextThreadLocal.remove(); + } + } + + public RuntimeContextSnapshot capture() { + Map runtimeContextMap = new HashMap(); + for (String key : RuntimeContextConfiguration.NEED_PROPAGATE_CONTEXT_KEY) { + Object value = this.get(key); + if (value != null) { + runtimeContextMap.put(key, value); + } + } + + return new RuntimeContextSnapshot(runtimeContextMap); + } + + public void accept(RuntimeContextSnapshot snapshot) { + Iterator iterator = snapshot.iterator(); + while (iterator.hasNext()) { + Map.Entry runtimeContextItem = iterator.next(); + ContextManager.getRuntimeContext().put(runtimeContextItem.getKey(), runtimeContextItem.getValue()); + } + } } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContextSnapshot.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContextSnapshot.java new file mode 100644 index 0000000000000000000000000000000000000000..5bc203776a01f265ca4e826e4c5363753ce63f8d --- /dev/null +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/RuntimeContextSnapshot.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ + +package org.apache.skywalking.apm.agent.core.context; + +import java.util.Iterator; +import java.util.Map; + +public class RuntimeContextSnapshot { + private final Map map; + + public RuntimeContextSnapshot(Map map) { + this.map = map; + } + + public Iterator iterator() { + return map.entrySet().iterator(); + } +} diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java index e3d4f7c559057303d9c5d23993885db4e6928c14..84552bd48cb52d4279d95a5b15d631ad35afcf14 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java @@ -79,11 +79,6 @@ public class TracingContext implements AbstractTracerContext { */ private int spanIdGenerator; - /** - * Runtime context of the tracing context - */ - private RuntimeContext runtimeContext; - /** * Initialize all fields with default value. */ @@ -424,14 +419,6 @@ public class TracingContext implements AbstractTracerContext { } } - @Override - public RuntimeContext getRuntimeContext() { - if (runtimeContext == null) { - runtimeContext = new RuntimeContext(); - } - return runtimeContext; - } - /** * Finish this context, and notify all {@link TracingContextListener}s, managed by {@link * TracingContext.ListenerManager} diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java index 28dce1a39b4cedc1c8fc7262e80ab88dd9da1fdb..1595f81f3c2e37eea90a4035c2ec085050e3f2c9 100644 --- a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java @@ -18,7 +18,6 @@ package org.apache.skywalking.apm.plugin.hystrix.v1; -import com.netflix.hystrix.HystrixCommand; import java.lang.reflect.Method; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; @@ -28,8 +27,6 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; -import static org.apache.skywalking.apm.plugin.hystrix.v1.Constants.ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT; - public class HystrixCommandRunInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, @@ -46,8 +43,6 @@ public class HystrixCommandRunInterceptor implements InstanceMethodsAroundInterc } // Because of `fall back` method running in other thread. so we need capture concurrent span for tracing. enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); - - ContextManager.getRuntimeContext().put(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT, ((HystrixCommand)objInst).getProperties().executionIsolationStrategy().get().name().toUpperCase()); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixConcurrencyStrategyInterceptor.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixConcurrencyStrategyInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..8ccce2ffab3632a36c22cab7e868c9aac009a9e6 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixConcurrencyStrategyInterceptor.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ + +package org.apache.skywalking.apm.plugin.hystrix.v1; + +import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy; +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; + +public class HystrixConcurrencyStrategyInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) throws Throwable { + return new SWHystrixConcurrencyStrategyWrapper((HystrixConcurrencyStrategy)ret); + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/SWHystrixConcurrencyStrategyWrapper.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/SWHystrixConcurrencyStrategyWrapper.java new file mode 100644 index 0000000000000000000000000000000000000000..102dc7c058c56c2bfb4917df83dbbc81b076f48a --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/SWHystrixConcurrencyStrategyWrapper.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ + +package org.apache.skywalking.apm.plugin.hystrix.v1; + +import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy; +import java.util.concurrent.Callable; +import org.apache.skywalking.apm.agent.core.conf.RuntimeContextConfiguration; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.RuntimeContextSnapshot; + +public class SWHystrixConcurrencyStrategyWrapper extends HystrixConcurrencyStrategy { + + private final HystrixConcurrencyStrategy delegate; + + public SWHystrixConcurrencyStrategyWrapper( + HystrixConcurrencyStrategy delegate) { + this.delegate = delegate; + } + + @Override + public Callable wrapCallable(Callable callable) { + return new WrappedCallable(ContextManager.getRuntimeContext().capture(), super.wrapCallable(callable)); + } + + static class WrappedCallable implements Callable { + + private final RuntimeContextSnapshot contextSnapshot; + private final Callable target; + + WrappedCallable(RuntimeContextSnapshot contextSnapshot, Callable target) { + this.contextSnapshot = contextSnapshot; + this.target = target; + } + + @Override public T call() throws Exception { + try { + ContextManager.getRuntimeContext().accept(contextSnapshot); + return target.call(); + } finally { + for (String key : RuntimeContextConfiguration.NEED_PROPAGATE_CONTEXT_KEY) { + ContextManager.getRuntimeContext().remove(key); + } + } + } + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/define/HystrixPluginsInstrumentation.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/define/HystrixPluginsInstrumentation.java index 0c59115d4ce33e5e09c405b87ebd3fa51410291b..886620f7b130afa14a3035ca5e79151aef3b82af 100644 --- a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/define/HystrixPluginsInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/define/HystrixPluginsInstrumentation.java @@ -32,6 +32,8 @@ public class HystrixPluginsInstrumentation extends ClassInstanceMethodsEnhancePl public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.hystrix.v1.HystrixPluginsInterceptor"; public static final String ENHANCE_METHOD = "getCommandExecutionHook"; + public static final String GET_CONCURRENCY_STRATEGY_METHOD = "getConcurrencyStrategy"; + public static final String GET_CONCURRENCY_STRATEGY_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.hystrix.v1.HystrixConcurrencyStrategyInterceptor"; public static final String ENHANCE_CLASS = "com.netflix.hystrix.strategy.HystrixPlugins"; @Override @@ -51,6 +53,19 @@ public class HystrixPluginsInstrumentation extends ClassInstanceMethodsEnhancePl return INTERCEPT_CLASS; } + @Override public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override public ElementMatcher getMethodsMatcher() { + return named(GET_CONCURRENCY_STRATEGY_METHOD); + } + + @Override public String getMethodsInterceptor() { + return GET_CONCURRENCY_STRATEGY_INTERCEPT_CLASS; + } + @Override public boolean isOverrideArgs() { return false; } diff --git a/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/Constants.java b/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/Constants.java new file mode 100644 index 0000000000000000000000000000000000000000..e1e8d84d6506026c205c37c8af7742ed8e892a2e --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/Constants.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ + +package org.apache.skywalking.apm.plugin.jetty.v9.server; + +public class Constants { + public static final String FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG"; +} diff --git a/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/ForwardInterceptor.java b/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/ForwardInterceptor.java index 0bce81b17ac21e25a4c310c9b3db99b361c1ea39..ea2e425b69349a70cd92e4799a5fbb8424794b49 100644 --- a/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/ForwardInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/ForwardInterceptor.java @@ -30,7 +30,6 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; public class ForwardInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { - private static final String FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG"; @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, @@ -40,7 +39,7 @@ public class ForwardInterceptor implements InstanceMethodsAroundInterceptor, Ins Map eventMap = new HashMap(); eventMap.put("forward-url", (String)objInst.getSkyWalkingDynamicField()); abstractTracingSpan.log(System.currentTimeMillis(), eventMap); - ContextManager.getRuntimeContext().put(FORWARD_REQUEST_FLAG, true); + ContextManager.getRuntimeContext().put(Constants.FORWARD_REQUEST_FLAG, true); } } diff --git a/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/HandleInterceptor.java b/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/HandleInterceptor.java index 0ed3ec6f13ca236d08299a798c8063f7209b766c..6700cab1dedc893b0508f00e688ef9354ed69c0c 100644 --- a/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/HandleInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-server-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/server/HandleInterceptor.java @@ -67,6 +67,7 @@ public class HandleInterceptor implements InstanceMethodsAroundInterceptor { Tags.STATUS_CODE.set(span, Integer.toString(servletResponse.getStatus())); } ContextManager.stopSpan(); + ContextManager.getRuntimeContext().remove(Constants.FORWARD_REQUEST_FLAG); return ret; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RequestMappingMethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RequestMappingMethodInterceptorTest.java index bedbae744ac7b28bf39041c5c8843421760cb4cc..db90da400c8912c8a0a169f878c2449f5bcbcbe5 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RequestMappingMethodInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/RequestMappingMethodInterceptorTest.java @@ -174,9 +174,7 @@ public class RequestMappingMethodInterceptorTest { @Override public Object getSkyWalkingDynamicField() { - value.setPathMappingCache(new PathMappingCache("/test")); - return value; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java index 0b166ef049820df866051c7d84c62780ebee2725..1ac245018e7814805fa5fe585f8a927c439b9680 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java @@ -32,13 +32,9 @@ public class Constants { public static final String REST_MAPPING_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.RestMappingMethodInterceptor"; - public static final String HYSTRIX_COMMAND_ANNOTATION = "com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand"; - public static final String REQUEST_KEY_IN_RUNTIME_CONTEXT = "SW_REQUEST"; public static final String RESPONSE_KEY_IN_RUNTIME_CONTEXT = "SW_RESPONSE"; - public static final String ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT = "ISOLATE_STRATEGY"; - public static final String FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG"; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java index 4f9d9f2ba73f5dc67506843ad67d64eaca128e79..b2cb54afe744c3fb5b00e572f483deef6ec8f39a 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java @@ -34,7 +34,6 @@ import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.FORWARD_REQUEST_FLAG; -import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT; @@ -65,12 +64,8 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround requestURL = pathMappingCache.findPathMapping(method); } - String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT); HttpServletRequest request = (HttpServletRequest)ContextManager.getRuntimeContext().get(REQUEST_KEY_IN_RUNTIME_CONTEXT); - - if (hystrixIsolateStrategy != null) { - ContextManager.createLocalSpan(requestURL); - } else if (request != null) { + if (request != null) { ContextCarrier contextCarrier = new ContextCarrier(); CarrierItem next = contextCarrier.items(); while (next.hasNext()) { @@ -98,18 +93,19 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround return ret; } - String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT); HttpServletResponse response = (HttpServletResponse)ContextManager.getRuntimeContext().get(RESPONSE_KEY_IN_RUNTIME_CONTEXT); - - if (hystrixIsolateStrategy != null) { - ContextManager.stopSpan(); - } else if (response != null) { - AbstractSpan span = ContextManager.activeSpan(); - if (response.getStatus() >= 400) { - span.errorOccurred(); - Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus())); + try { + if (response != null) { + AbstractSpan span = ContextManager.activeSpan(); + if (response.getStatus() >= 400) { + span.errorOccurred(); + Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus())); + } + ContextManager.stopSpan(); } - ContextManager.stopSpan(); + } finally { + ContextManager.getRuntimeContext().remove(REQUEST_KEY_IN_RUNTIME_CONTEXT); + ContextManager.getRuntimeContext().remove(RESPONSE_KEY_IN_RUNTIME_CONTEXT); } return ret; diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java index f1762a93483a5fa32cf5f2c46b6040d960756dd4..9ae5150d9f6bb0f396f7e939b5d679eb63d61f60 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeForRequestInterceptor.java @@ -19,14 +19,14 @@ package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; +import java.lang.reflect.Method; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.springframework.web.context.request.NativeWebRequest; -import java.lang.reflect.Method; - +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT; /** @@ -45,6 +45,9 @@ public class InvokeForRequestInterceptor implements InstanceMethodsAroundInterce @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + ContextManager.getRuntimeContext().remove(RESPONSE_KEY_IN_RUNTIME_CONTEXT); + // clear request in this method + ContextManager.getRuntimeContext().remove(REQUEST_KEY_IN_RUNTIME_CONTEXT); return ret; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java index 33fbec273f0cfa520b2970c62ed872ceb8579358..858ee75e3f4e4fec0de31b55d4aca437c0fddc86 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/InvokeHandlerMethodInterceptor.java @@ -40,6 +40,8 @@ public class InvokeHandlerMethodInterceptor implements InstanceMethodsAroundInte @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + ContextManager.getRuntimeContext().remove(RESPONSE_KEY_IN_RUNTIME_CONTEXT); + ContextManager.getRuntimeContext().remove(REQUEST_KEY_IN_RUNTIME_CONTEXT); return ret; } diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/Constants.java b/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/Constants.java new file mode 100644 index 0000000000000000000000000000000000000000..64501083e1f50f0bfaed514205808b51a5df652e --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/Constants.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ + +package org.apache.skywalking.apm.plugin.tomcat78x; + +public class Constants { + public static final String FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG"; +} diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/ForwardInterceptor.java b/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/ForwardInterceptor.java index 0c9ff83d2cc6e5de9ca572d4d2a0ebf6a007c4dd..f7b255acecfbca361c07cd3f17d65b1a6ea145f5 100644 --- a/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/ForwardInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/ForwardInterceptor.java @@ -31,8 +31,6 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInt public class ForwardInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { - private static final String FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG"; - @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { @@ -41,7 +39,7 @@ public class ForwardInterceptor implements InstanceMethodsAroundInterceptor, Ins Map eventMap = new HashMap(); eventMap.put("forward-url", (String)objInst.getSkyWalkingDynamicField()); abstractTracingSpan.log(System.currentTimeMillis(), eventMap); - ContextManager.getRuntimeContext().put(FORWARD_REQUEST_FLAG, true); + ContextManager.getRuntimeContext().put(Constants.FORWARD_REQUEST_FLAG, true); } } diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/TomcatInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/TomcatInvokeInterceptor.java index c8c247b0588527f4ad43931c5f495d231e2b69df..649e5c71268d111d226c226500ed4881087d16c7 100644 --- a/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/TomcatInvokeInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/TomcatInvokeInterceptor.java @@ -81,6 +81,7 @@ public class TomcatInvokeInterceptor implements InstanceMethodsAroundInterceptor Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus())); } ContextManager.stopSpan(); + ContextManager.getRuntimeContext().remove(Constants.FORWARD_REQUEST_FLAG); return ret; }