org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer#brave.spring.web.TracingClientHttpRequestInterceptor源码实例Demo

下面列出了org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer#brave.spring.web.TracingClientHttpRequestInterceptor 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Test
public void notSampledHeaderAddedWhenNotSampled() {
	this.tracing.close();
	this.tracing = Tracing.newBuilder().currentTraceContext(this.currentTraceContext)
			.addSpanHandler(this.spans).sampler(Sampler.NEVER_SAMPLE).build();
	this.template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(
			TracingClientHttpRequestInterceptor.create(HttpTracing.create(tracing))));

	Span span = tracing.tracer().nextSpan().name("new trace");
	Map<String, String> headers;

	try (Tracer.SpanInScope ws = tracing.tracer().withSpanInScope(span.start())) {
		headers = this.template.getForEntity("/", Map.class).getBody();
	}
	finally {
		span.finish();
	}

	then(this.spans).isEmpty();
}
 
private void assertInterceptorsOrder(
		List<ClientHttpRequestInterceptor> interceptors) {
	int traceInterceptorIndex = -1;
	int myInterceptorIndex = -1;
	int mySecondInterceptorIndex = -1;
	Map<Class, Integer> numberOfInstances = new HashMap<>();
	for (int i = 0; i < interceptors.size(); i++) {
		ClientHttpRequestInterceptor interceptor = interceptors.get(i);
		incrementNumberOfInstances(numberOfInstances, interceptor);
		if (interceptor instanceof TracingClientHttpRequestInterceptor
				|| interceptor instanceof LazyTracingClientHttpRequestInterceptor) {
			traceInterceptorIndex = i;
		}
		else if (interceptor instanceof MyClientHttpRequestInterceptor) {
			myInterceptorIndex = i;
		}
		else if (interceptor instanceof MySecondClientHttpRequestInterceptor) {
			mySecondInterceptorIndex = i;
		}
	}
	then(traceInterceptorIndex).isGreaterThanOrEqualTo(0)
			.isLessThan(myInterceptorIndex).isLessThan(mySecondInterceptorIndex);
	then(numberOfInstances.values())
			.as("Can't have duplicate entries for interceptors")
			.containsOnlyElementsOf(Collections.singletonList(1));
}
 
源代码3 项目: txle   文件: RestTemplateConfig.java
@Bean
public RestTemplate restTemplate(@Autowired(required = false) OmegaContext context, @Autowired Tracing tracing) {
  RestTemplate template = new RestTemplate();
  List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors();
  interceptors.add(new TransactionClientHttpRequestInterceptor(context));
  // add interceptor for rest's request server By Gannalyo
  interceptors.add(TracingClientHttpRequestInterceptor.create(tracing));
  template.setInterceptors(interceptors);
  return template;
}
 
private boolean hasTraceInterceptor(RestTemplate restTemplate) {
	for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
		if (interceptor instanceof TracingClientHttpRequestInterceptor
				|| interceptor instanceof LazyTracingClientHttpRequestInterceptor) {
			return true;
		}
	}
	return false;
}
 
private TracingClientHttpRequestInterceptor interceptor() {
	if (this.interceptor == null) {
		this.interceptor = this.beanFactory
				.getBean(TracingClientHttpRequestInterceptor.class);
	}
	return this.interceptor;
}
 
@Override
public void customize(OAuth2RestTemplate template) {
	final TracingClientHttpRequestInterceptor interceptor = this.beanFactory
			.getBean(TracingClientHttpRequestInterceptor.class);
	new RestTemplateInterceptorInjector(interceptor).inject(template);
	if (this.delegate != null) {
		((UserInfoRestTemplateCustomizer) this.delegate).customize(template);
	}
}
 
@Setup
public void setup() {
	new SpringApplication(SleuthBenchmarkingSpringApp.class).run(
			"--spring.jmx.enabled=false", "--spring.application.name=withSleuth");
	this.mockMvc = MockMvcBuilders
			.standaloneSetup(
					this.withSleuth.getBean(SleuthBenchmarkingSpringApp.class))
			.build();
	this.tracedTemplate = new RestTemplate(
			new MockMvcClientHttpRequestFactory(this.mockMvc));
	this.tracedTemplate.setInterceptors(Collections.singletonList(
			this.withSleuth.getBean(TracingClientHttpRequestInterceptor.class)));
	this.untracedTemplate = new RestTemplate(
			new MockMvcClientHttpRequestFactory(this.mockMvc));
}
 
@Bean
public TracingClientHttpRequestInterceptor tracingClientHttpRequestInterceptor(
		HttpTracing httpTracing) {
	return (TracingClientHttpRequestInterceptor) TracingClientHttpRequestInterceptor
			.create(httpTracing);
}
 
private void setInterceptors(HttpTracing httpTracing) {
	this.template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(
			TracingClientHttpRequestInterceptor.create(httpTracing)));
}
 
@BeforeEach
public void setup() {
	this.template.setInterceptors(Arrays
			.<ClientHttpRequestInterceptor>asList(TracingClientHttpRequestInterceptor
					.create(HttpTracing.create(this.tracing))));
}
 
@Override protected ClientHttpRequestFactory newClient(int port) {
  return configureClient(TracingClientHttpRequestInterceptor.create(httpTracing));
}