Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 710
Jan Avatar asked Apr 05 '19 08:04

Jan


2 Answers

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.

like image 154
Tomasz Jagiełło Avatar answered Nov 15 '22 05:11

Tomasz Jagiełło


    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();
    }
}
like image 43
Pradeep Palaniswamy Avatar answered Nov 15 '22 05:11

Pradeep Palaniswamy