'Access URITemplate or RequestLine value in Feign RequestInterceptor / RequestTemplate
I'm developing an app against a cloud application that has hard api rate limits in place. In order to have my team get a feeling for how close we are in regards to those limits I want to count all API calls made from our app in a meaningful way.
We use Feign as access layer, and I was hoping to be able to use the RequestInterceptor to count the different API endpoints we call:
RequestInterceptor ri = rq -> addStatistics(rq.url());
Now this does not work, as the resulting URLs almost always count "1" afterwards, as they already contain all resolved path variables, so I get counts for
1 - /something/id1valueverycryptic/get
1 - /something/anothercrypticidkey/get
and so on.
I was hoping to somehow get access to either the @ResuqestLine mapping value (GET /something/{id}/get) or at least the uri template pre-resolve (/somethine/{id}/get)
Is there a way to do this?
Thanks!
Solution 1:[1]
Maybe you could try using custom feign InvocationHandlerFactory.
I've managed to log RequestInterceptor using code like this:
change EnableFeignClients and add defaultConfiguration
@EnableFeignClients(defaultConfiguration = FeignConfig.class)add default feign config
@Configuration public class FeignConfig { @Bean @ConditionalOnMissingBean public Retryer feignRetryer() { return Retryer.NEVER_RETRY; } @Bean @Scope("prototype") @ConditionalOnMissingBean public Feign.Builder feignBuilder(Retryer retryer) { return Feign.builder() .retryer(retryer) .invocationHandlerFactory((target, dispatch) -> new CountingFeignInvocationHandler(target, dispatch)); } }create your invocation handler (code based on feign.ReflectiveFeign.FeignInvocationHandler)
public class CountingFeignInvocationHandler implements InvocationHandler { private final Target target; private final Map<Method, MethodHandler> dispatch; public CountingFeignInvocationHandler(Target target, Map<Method, MethodHandler> dispatch) { this.target = checkNotNull(target, "target"); this.dispatch = checkNotNull(dispatch, "dispatch for %s", target); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ("equals".equals(method.getName())) { try { Object otherHandler = args.length > 0 && args[0] != null ? Proxy.getInvocationHandler(args[0]) : null; return equals(otherHandler); } catch (IllegalArgumentException e) { return false; } } else if ("hashCode".equals(method.getName())) { return hashCode(); } else if ("toString".equals(method.getName())) { return toString(); } RequestLine requestLine = method.getAnnotation(RequestLine.class); addStatistics(requestLine.value()); return dispatch.get(method).invoke(args); } @Override public boolean equals(Object obj) { if (obj instanceof CountingFeignInvocationHandler) { CountingFeignInvocationHandler other = (CountingFeignInvocationHandler) obj; return target.equals(other.target); } return false; } @Override public int hashCode() { return target.hashCode(); } @Override public String toString() { return target.toString(); } }
Be careful and check if you feign configuration wasn't more complex and in that case extend classes as needed.
Solution 2:[2]
    If you are using spring-cloud-starter-openfeign ,  You could do something like below 
    
    add the a primary contract bean 
    @Bean("YourContract")
    @Primary
        public Contract springpringContract() {
            return (targetType) -> {
    
                List<MethodMetadata> parseAndValidatateMetadata = new SpringMvcContract().parseAndValidatateMetadata(targetType);
                parseAndValidatateMetadata.forEach(metadata -> {
                    RequestTemplate template = metadata.template();
                    template.header("unresolved_uri", template.path().replace("{", "[").replace("}", "]"));
    
                });
                return parseAndValidatateMetadata;
            };
        }
    
    Add the contract to the feign client builder 
    @Bean
     public <T> T feignBuilder(Class<T> feignInterface, String targetURL) {
            return Feign.builder().client(getClient())
                    .contract(contract)
                    .
                    .
    }
    
    Once you are done with the above you should be able to access the unresolved path in the RequestTemplate
@component
public class FeignRequestFilter  implements RequestInterceptor {
    @Override
        public void apply(RequestTemplate template) {
            String unresolvedUri = template.headers().getOrDefault("unresolved_uri", Collections.singleton(template.path()))
                    .iterator().next();
    }
}
    					Solution 3:[3]
Maybe you could try overwriting feign Logger.
Suppose we have a feign client,
@FeignClient(name = "demo-client", url = "http://localhost:8080/api", configuration = FeignConfig.class)
public interface DemoClient {
    @GetMapping(value = "/test/{id}")
    void test(@PathVariable(name = "id") Integer id) {
    }
}
import feign.Logger;
import feign.Request;
import feign.Response;
import java.io.IOException;
public class CustomFeignRequestLogging extends Logger {
    @Override
    protected void logRequest(String configKey, Level logLevel, Request request) {
        super.logRequest(configKey, logLevel, request);
        // targetUrl = http://localhost:8080/api
        String targetUrl = request.requestTemplate().feignTarget().url();
        // path = /test/{id}
        String path = request.requestTemplate().methodMetadata().template().path();
    }
}
    					Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Tomasz Jagie??o | 
| Solution 2 | Pradeep Palaniswamy | 
| Solution 3 | Anil Nivargi | 
