Skip to content

Commit

Permalink
Intercept feign RequestTemplate resolve args method ,get the url with…
Browse files Browse the repository at this point in the history
…out parameters
  • Loading branch information
亓杨 committed Nov 14, 2019
1 parent 424ec7e commit 42a712e
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@

import feign.Request;
import feign.Response;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.apache.skywalking.apm.agent.core.context.CarrierItem;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
Expand All @@ -40,6 +37,7 @@
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.util.StringUtil;

/**
* {@link DefaultHttpClientInterceptor} intercept the default implementation of http calls by the Feign.
Expand All @@ -48,8 +46,6 @@
*/
public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterceptor {

private static final String COMPONENT_NAME = "FeignDefaultHttp";

/**
* Get the {@link feign.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, port,
* kind, component, url from {@link feign.Request}. Through the reflection of the way, set the http header of
Expand All @@ -59,15 +55,17 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc
* @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 {
Request request = (Request)allArguments[0];

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
Request request = (Request) allArguments[0];
String originUrl = PathVarInterceptor.URL_CONTEXT.get();
URL url = new URL(request.url());
ContextCarrier contextCarrier = new ContextCarrier();
int port = url.getPort() == -1 ? 80 : url.getPort();
String remotePeer = url.getHost() + ":" + port;
String operationName = url.getPath();
String operationName = StringUtil.isEmpty(originUrl) ? url.getPath() : originUrl;
PathVarInterceptor.URL_CONTEXT.remove();
if (operationName == null || operationName.length() == 0) {
operationName = "/";
}
Expand Down Expand Up @@ -101,13 +99,14 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc
* the server. Finish the {@link AbstractSpan}.
*
* @param method
* @param ret the method's original return value.
* @param ret the method's original return value.
* @return
* @throws Throwable
*/
@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Object ret) throws Throwable {
Response response = (Response)ret;
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Object ret) throws Throwable {
Response response = (Response) ret;
if (response != null) {
int statusCode = response.status();

Expand All @@ -123,8 +122,9 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc
return ret;
}

@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
AbstractSpan activeSpan = ContextManager.activeSpan();
activeSpan.log(t);
activeSpan.errorOccurred();
Expand Down
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) {

}
}
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;
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

feign-default-http-9.x=org.apache.skywalking.apm.plugin.feign.http.v9.define.DefaultHttpClientInstrumentation
feign-default-http-9.x=org.apache.skywalking.apm.plugin.feign.http.v9.define.DefaultHttpClientInstrumentation
feign-pathvar-9.x=org.apache.skywalking.apm.plugin.feign.http.v9.define.PathVarInstrumentation
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}"));
}
}

0 comments on commit 42a712e

Please sign in to comment.