diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java index 2b87d9e6f76d9179b70f0ee11ad42e9ff2b44efd..62e89fc7b716f4f28af4386d944d553879f2b484 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestExecuteInterceptor.java @@ -31,9 +31,11 @@ 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 org.apache.skywalking.apm.plugin.spring.commons.EnhanceCacheObjects; +import org.apache.skywalking.apm.plugin.spring.resttemplate.helper.RestTemplateRuntimeContextHelper; import org.springframework.http.HttpMethod; public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor { + @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { @@ -44,30 +46,34 @@ public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor String remotePeer = requestURL.getHost() + ":" + (requestURL.getPort() > 0 ? requestURL.getPort() : "https".equalsIgnoreCase(requestURL .getScheme()) ? 443 : 80); - String formatURIPath = requestURL.getPath(); - AbstractSpan span = ContextManager.createExitSpan(formatURIPath, contextCarrier, remotePeer); + String uri = requestURL.getPath(); + AbstractSpan span = ContextManager.createExitSpan(uri, contextCarrier, remotePeer); span.setComponent(ComponentsDefine.SPRING_REST_TEMPLATE); Tags.URL.set(span, requestURL.getScheme() + "://" + requestURL.getHost() + ":" + requestURL.getPort() + requestURL .getPath()); Tags.HTTP.METHOD.set(span, httpMethod.toString()); SpanLayer.asHttp(span); - Object[] cacheValues = new Object[3]; - cacheValues[0] = formatURIPath; - cacheValues[1] = contextCarrier; - objInst.setSkyWalkingDynamicField(cacheValues); + RestTemplateRuntimeContextHelper.addUri(uri); + RestTemplateRuntimeContextHelper.addContextCarrier(contextCarrier); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { - Object[] cacheValues = (Object[]) objInst.getSkyWalkingDynamicField(); - cacheValues[2] = ContextManager.capture(); - if (ret != null) { - String uri = (String) cacheValues[0]; - ((EnhancedInstance) ret).setSkyWalkingDynamicField(new EnhanceCacheObjects(uri, ComponentsDefine.SPRING_REST_TEMPLATE, SpanLayer.HTTP, (ContextSnapshot) cacheValues[2])); + try { + ContextSnapshot contextSnapshot = ContextManager.capture(); + if (ret != null) { + String uri = RestTemplateRuntimeContextHelper.getUri(); + ((EnhancedInstance) ret).setSkyWalkingDynamicField( + new EnhanceCacheObjects(uri, ComponentsDefine.SPRING_REST_TEMPLATE, SpanLayer.HTTP, contextSnapshot) + ); + } + ContextManager.stopSpan(); + } finally { + RestTemplateRuntimeContextHelper.cleanUri(); + RestTemplateRuntimeContextHelper.cleanContextCarrier(); } - ContextManager.stopSpan(); return ret; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java index c71dfbe3b7096aab7400c277fdf3d39cb2cc69db..2b33c480e760698bceac86bcc7386d0cdbbfcb14 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/async/RestRequestInterceptor.java @@ -24,6 +24,7 @@ import org.apache.skywalking.apm.agent.core.context.ContextCarrier; 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.apache.skywalking.apm.plugin.spring.resttemplate.helper.RestTemplateRuntimeContextHelper; import org.springframework.http.client.AsyncClientHttpRequest; public class RestRequestInterceptor implements InstanceMethodsAroundInterceptor { @@ -39,8 +40,7 @@ public class RestRequestInterceptor implements InstanceMethodsAroundInterceptor Object ret) throws Throwable { AsyncClientHttpRequest clientHttpRequest = (AsyncClientHttpRequest) ret; if (ret != null) { - Object[] cacheValues = (Object[]) objInst.getSkyWalkingDynamicField(); - ContextCarrier contextCarrier = (ContextCarrier) cacheValues[1]; + ContextCarrier contextCarrier = RestTemplateRuntimeContextHelper.getContextCarrier(); CarrierItem next = contextCarrier.items(); while (next.hasNext()) { next = next.next(); diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/helper/RestTemplateRuntimeContextHelper.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/helper/RestTemplateRuntimeContextHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..9016cc776ef5ff1132bae644107128967d7bd884 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/helper/RestTemplateRuntimeContextHelper.java @@ -0,0 +1,53 @@ +/* + * 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.spring.resttemplate.helper; + +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; +import org.apache.skywalking.apm.agent.core.context.ContextManager; + +public class RestTemplateRuntimeContextHelper { + + private static final String REST_TEMPLATE_CONTEXT_CARRIER_KEY_IN_RUNTIME_CONTEXT = "REST_TEMPLATE_CONTEXT_CARRIER"; + + private static final String REST_TEMPLATE_URI_KEY_IN_RUNTIME_CONTEXT = "REST_TEMPLATE_URI"; + + public static void cleanUri() { + ContextManager.getRuntimeContext().remove(REST_TEMPLATE_URI_KEY_IN_RUNTIME_CONTEXT); + } + + public static void cleanContextCarrier() { + ContextManager.getRuntimeContext().remove(REST_TEMPLATE_CONTEXT_CARRIER_KEY_IN_RUNTIME_CONTEXT); + } + + public static void addUri(String uri) { + ContextManager.getRuntimeContext().put(REST_TEMPLATE_URI_KEY_IN_RUNTIME_CONTEXT, uri); + } + + public static void addContextCarrier(ContextCarrier contextCarrier) { + ContextManager.getRuntimeContext().put(REST_TEMPLATE_CONTEXT_CARRIER_KEY_IN_RUNTIME_CONTEXT, contextCarrier); + } + + public static String getUri() { + return (String) ContextManager.getRuntimeContext().get(REST_TEMPLATE_URI_KEY_IN_RUNTIME_CONTEXT); + } + + public static ContextCarrier getContextCarrier() { + return (ContextCarrier) ContextManager.getRuntimeContext().get(REST_TEMPLATE_CONTEXT_CARRIER_KEY_IN_RUNTIME_CONTEXT); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestExecuteInterceptor.java index 294e9f01d40cb7b72b5353545623035be5c8231f..b8b2f98f4302e718fb7fe64bb9b428e938fe9e94 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestExecuteInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestExecuteInterceptor.java @@ -29,6 +29,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedI import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import org.apache.skywalking.apm.plugin.spring.resttemplate.helper.RestTemplateRuntimeContextHelper; import org.springframework.http.HttpMethod; public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor { @@ -50,12 +51,13 @@ public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor Tags.HTTP.METHOD.set(span, httpMethod.toString()); SpanLayer.asHttp(span); - objInst.setSkyWalkingDynamicField(contextCarrier); + RestTemplateRuntimeContextHelper.addContextCarrier(contextCarrier); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + RestTemplateRuntimeContextHelper.cleanContextCarrier(); ContextManager.stopSpan(); return ret; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestRequestInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestRequestInterceptor.java index c45a951a49e9ceb55c2b6a210b0ae3d1b9bc64de..490608036f557e6e9d87e7cc1bebb94c22ba543e 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestRequestInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/resttemplate-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/resttemplate/sync/RestRequestInterceptor.java @@ -24,6 +24,7 @@ import org.apache.skywalking.apm.agent.core.context.ContextCarrier; 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.apache.skywalking.apm.plugin.spring.resttemplate.helper.RestTemplateRuntimeContextHelper; import org.springframework.http.client.AbstractClientHttpRequest; import org.springframework.http.client.ClientHttpRequest; @@ -41,7 +42,7 @@ public class RestRequestInterceptor implements InstanceMethodsAroundInterceptor ClientHttpRequest clientHttpRequest = (ClientHttpRequest) ret; if (clientHttpRequest instanceof AbstractClientHttpRequest) { AbstractClientHttpRequest httpRequest = (AbstractClientHttpRequest) clientHttpRequest; - ContextCarrier contextCarrier = (ContextCarrier) objInst.getSkyWalkingDynamicField(); + ContextCarrier contextCarrier = RestTemplateRuntimeContextHelper.getContextCarrier(); CarrierItem next = contextCarrier.items(); while (next.hasNext()) { next = next.next();