-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intercept feign RequestTemplate resolve args method ,get the url with…
…out parameters
- Loading branch information
亓杨
committed
Nov 14, 2019
1 parent
424ec7e
commit 42a712e
Showing
5 changed files
with
229 additions
and
20 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
67 changes: 67 additions & 0 deletions
67
...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,67 @@ | ||
/* | ||
* 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; | ||
import java.util.Map; | ||
|
||
/** | ||
* {@link PathVarInterceptor} intercept the Feign RequestTemplate args resolve ; | ||
* | ||
* @author qiyang | ||
*/ | ||
public class PathVarInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
||
static final ThreadLocal<String> URL_CONTEXT = new ThreadLocal<String>(); | ||
|
||
/** | ||
* Get the {@link RequestTemplate#url()} before {@link feign.ReflectiveFeign.BuildTemplateByResolvingArgs#resolve(Object[], RequestTemplate, Map)} | ||
* put it into the {@link PathVarInterceptor#URL_CONTEXT} | ||
* | ||
* @param method | ||
* @param result change this result, if you want to truncate the method. | ||
* @throws Throwable | ||
*/ | ||
@Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, | ||
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { | ||
RequestTemplate template = (RequestTemplate)allArguments[1]; | ||
URL_CONTEXT.set(template.url()); | ||
} | ||
|
||
/** | ||
* do nothing | ||
* @param method | ||
* @param ret the method's original return value. | ||
* @return | ||
*/ | ||
@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, | ||
Class<?>[] argumentsTypes, Object ret) { | ||
return ret; | ||
} | ||
|
||
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, | ||
Class<?>[] argumentsTypes, Throwable t) { | ||
|
||
} | ||
} |
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
73 changes: 73 additions & 0 deletions
73
.../src/test/java/org/apache/skywalking/apm/plugin/feign/http/v9/PathVarInterceptorTest.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,73 @@ | ||
/* | ||
* 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.MethodInterceptResult; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* @author qiyang | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
public class PathVarInterceptorTest { | ||
|
||
private PathVarInterceptor pathVarInterceptor; | ||
|
||
@Mock | ||
private EnhancedInstance enhancedInstance; | ||
|
||
@Mock | ||
private MethodInterceptResult result; | ||
|
||
private Object[] allArguments; | ||
private Class[] argumentTypes; | ||
|
||
@Before | ||
public void setUp() { | ||
|
||
RequestTemplate template = new RequestTemplate(); | ||
template.append("http://skywalking.org/{pathVar}"); | ||
|
||
Map<String, Object> variables = new HashMap<String, Object>(); | ||
variables.put("pathVar","value"); | ||
allArguments = new Object[] {new Object[]{}, template,variables}; | ||
argumentTypes = new Class[] {Object[].class, RequestTemplate.class,Map.class}; | ||
pathVarInterceptor = new PathVarInterceptor(); | ||
|
||
} | ||
|
||
@Test | ||
public void testMethodsAround() throws Throwable { | ||
pathVarInterceptor.beforeMethod(enhancedInstance,null,allArguments,argumentTypes,result); | ||
assertThat(PathVarInterceptor.URL_CONTEXT.get(),is("http://skywalking.org/{pathVar}")); | ||
} | ||
} |