Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intercept feign URL without parameters (#3854)
* Intercept feign RequestTemplate resolve args method ,get the url without parameters * fix ci warnings * optimize import * fix: miss path in feign target url like "http://localhost:8080/feign-scenario" fix feign-scenario test use pathVariable * Add testcase to DefaultHttpClientInterceptorTest * Clean ThreadLocal * User one ThreadLocal replace two. User try/final remove ThreadLocal . * Code Optimize
- Loading branch information
Showing
9 changed files
with
342 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/FeignResolvedURL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.feign.http.v9; | ||
|
||
/** | ||
* class for {@link PathVarInterceptor} intercept feign url resolved params in url . | ||
* @author qiyang | ||
*/ | ||
public class FeignResolvedURL { | ||
/** | ||
* url before resolved | ||
*/ | ||
private String originUrl; | ||
/** | ||
* url after resolved | ||
*/ | ||
private String url; | ||
|
||
public FeignResolvedURL(String originUrl) { | ||
this.originUrl = originUrl; | ||
} | ||
|
||
public FeignResolvedURL(String originUrl, String url) { | ||
this.originUrl = originUrl; | ||
this.url = url; | ||
} | ||
|
||
public String getOriginUrl() { | ||
return originUrl; | ||
} | ||
|
||
public void setOriginUrl(String originUrl) { | ||
this.originUrl = originUrl; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...ugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/PathVarInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.feign.http.v9; | ||
|
||
import feign.RequestTemplate; | ||
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 java.lang.reflect.Method; | ||
|
||
/** | ||
* {@link PathVarInterceptor} intercept the Feign RequestTemplate args resolve ; | ||
* | ||
* @author qiyang | ||
*/ | ||
public class PathVarInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
||
static final ThreadLocal<FeignResolvedURL> URL_CONTEXT = new ThreadLocal<FeignResolvedURL>(); | ||
|
||
/** | ||
* Get the {@link RequestTemplate#url()} before feign.ReflectiveFeign.BuildTemplateByResolvingArgs#resolve(Object[], RequestTemplate, Map) | ||
* put it into the {@link PathVarInterceptor#URL_CONTEXT} | ||
* | ||
* @param method intercept method | ||
* @param result change this result, if you want to truncate the method. | ||
*/ | ||
@Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, | ||
Class<?>[] argumentsTypes, MethodInterceptResult result) { | ||
RequestTemplate template = (RequestTemplate)allArguments[1]; | ||
URL_CONTEXT.set(new FeignResolvedURL(template.url())); | ||
} | ||
|
||
/** | ||
* Get the resolved {@link RequestTemplate#url()} after feign.ReflectiveFeign.BuildTemplateByResolvingArgs#resolve(Object[], RequestTemplate, Map) | ||
* put it into the {@link PathVarInterceptor#URL_CONTEXT} | ||
* @param method intercept method | ||
* @param ret the method's original return value. | ||
* @return result without change | ||
*/ | ||
@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, | ||
Class<?>[] argumentsTypes, Object ret) { | ||
RequestTemplate resolvedTemplate = (RequestTemplate) ret; | ||
URL_CONTEXT.get().setUrl(resolvedTemplate.url()); | ||
return ret; | ||
} | ||
|
||
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, | ||
Class<?>[] argumentsTypes, Throwable t) { | ||
if (URL_CONTEXT.get() != null) { | ||
URL_CONTEXT.remove(); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...in/java/org/apache/skywalking/apm/plugin/feign/http/v9/define/PathVarInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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.feign.http.v9.define; | ||
|
||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; | ||
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; | ||
|
||
public class PathVarInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { | ||
|
||
/** | ||
* Enhance class. | ||
*/ | ||
private static final String ENHANCE_CLASS = "feign.ReflectiveFeign$BuildTemplateByResolvingArgs"; | ||
|
||
/** | ||
* Intercept class. | ||
*/ | ||
private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.feign.http.v9.PathVarInterceptor"; | ||
|
||
@Override protected ClassMatch enhanceClass() { | ||
return byName(ENHANCE_CLASS); | ||
} | ||
|
||
@Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { | ||
return new ConstructorInterceptPoint[0]; | ||
} | ||
|
||
@Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { | ||
return new InstanceMethodsInterceptPoint[] { | ||
new InstanceMethodsInterceptPoint() { | ||
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
return named("resolve"); | ||
} | ||
|
||
@Override public String getMethodsInterceptor() { | ||
return INTERCEPT_CLASS; | ||
} | ||
|
||
@Override public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.