resttemplate plugin concurrency scenario data mistake problem fix (#5658)
* resttemplate plugin concurrency scenario data mistake problem
This commit is contained in:
parent
f48e4961ec
commit
f2e83bad30
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue