io.opentracing.contrib.spring.cloud.feign.TraceFeignContext Maven / Gradle / Ivy
/**
* Copyright 2017-2018 The OpenTracing Authors
*
* Licensed 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 io.opentracing.contrib.spring.cloud.feign;
import feign.Client;
import feign.opentracing.FeignSpanDecorator;
import feign.opentracing.TracingClient;
import io.opentracing.Tracer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
/**
* @author Pavol Loffay
*/
public class TraceFeignContext extends FeignContext {
private FeignContext delegate;
private Tracer tracer;
private BeanFactory beanFactory;
private List spanDecorators;
TraceFeignContext(Tracer tracer, FeignContext delegate, BeanFactory beanFactory,
List spanDecorators) {
this.delegate = delegate;
this.tracer = tracer;
this.beanFactory = beanFactory;
this.spanDecorators = spanDecorators;
}
@Override
public T getInstance(String name, Class type) {
T object = this.delegate.getInstance(name, type);
return (T) this.addTracingClient(object);
}
@Override
public Map getInstances(String name, Class type) {
Map instances = this.delegate.getInstances(name, type);
if (instances == null) {
return null;
}
Map tracedInstances = new HashMap<>();
for (Map.Entry instanceEntry : instances.entrySet()) {
tracedInstances
.put(instanceEntry.getKey(), (T) this.addTracingClient(instanceEntry.getValue()));
}
return tracedInstances;
}
private Object addTracingClient(Object bean) {
if (bean instanceof TracingClient || bean instanceof LoadBalancedTracedFeign) {
return bean;
}
if (bean instanceof Client) {
if (bean instanceof LoadBalancerFeignClient && !(bean instanceof LoadBalancedTracedFeign)) {
return new LoadBalancedTracedFeign(
buildTracingClient(((LoadBalancerFeignClient) bean).getDelegate(), tracer),
beanFactory.getBean(CachingSpringLoadBalancerFactory.class),
beanFactory.getBean(SpringClientFactory.class));
}
return buildTracingClient((Client) bean, tracer);
}
return bean;
}
private TracingClient buildTracingClient(Client delegate, Tracer tracer) {
return new TracingClientBuilder(delegate, tracer)
.withFeignSpanDecorators(spanDecorators)
.build();
}
/**
* Needed for cast in {@link org.springframework.cloud.openfeign.FeignClientFactoryBean}
*/
static class LoadBalancedTracedFeign extends LoadBalancerFeignClient {
public LoadBalancedTracedFeign(Client delegate,
CachingSpringLoadBalancerFactory lbClientFactory,
SpringClientFactory clientFactory) {
super(delegate, lbClientFactory, clientFactory);
}
}
}