Remove @Bean RestTemplate
(If users need one they can add it themselves) Fixes gh-239
This commit is contained in:
@@ -38,8 +38,9 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
|
||||
* enables span information propagation when using {@link RestTemplate}
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||
* Auto-configuration} enables span information propagation when using
|
||||
* {@link RestTemplate}
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
@@ -58,12 +59,6 @@ public class TraceWebClientAutoConfiguration {
|
||||
return new TraceRestTemplateInterceptor(tracer, spanInjector);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpanInjector<HttpRequest> httpRequestSpanInjector() {
|
||||
return new HttpRequestInjector();
|
||||
@@ -82,7 +77,8 @@ public class TraceWebClientAutoConfiguration {
|
||||
public void init() {
|
||||
if (this.restTemplates != null) {
|
||||
for (RestTemplate restTemplate : this.restTemplates) {
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(restTemplate.getInterceptors());
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
|
||||
restTemplate.getInterceptors());
|
||||
interceptors.add(this.traceRestTemplateInterceptor);
|
||||
restTemplate.setInterceptors(interceptors);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
package org.springframework.cloud.sleuth.instrument.web;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static junitparams.JUnitParamsRunner.$;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@@ -14,6 +26,7 @@ import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.instrument.DefaultTestAutoConfiguration;
|
||||
import org.springframework.cloud.sleuth.instrument.web.common.AbstractMvcWiremockIntegrationTest;
|
||||
import org.springframework.cloud.sleuth.instrument.web.common.HttpMockServer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
@@ -31,38 +44,33 @@ import org.springframework.web.context.request.async.WebAsyncTask;
|
||||
import junitparams.JUnitParamsRunner;
|
||||
import junitparams.Parameters;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static junitparams.JUnitParamsRunner.$;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringApplicationConfiguration(classes = {RestTemplateTraceAspectIntegrationTests.CorrelationIdAspectTestConfiguration.class})
|
||||
@SpringApplicationConfiguration(classes = {
|
||||
RestTemplateTraceAspectIntegrationTests.CorrelationIdAspectTestConfiguration.class })
|
||||
@RunWith(JUnitParamsRunner.class)
|
||||
public class RestTemplateTraceAspectIntegrationTests extends AbstractMvcWiremockIntegrationTest {
|
||||
public class RestTemplateTraceAspectIntegrationTests
|
||||
extends AbstractMvcWiremockIntegrationTest {
|
||||
|
||||
@ClassRule public static final SpringClassRule SCR = new SpringClassRule();
|
||||
@Rule public final SpringMethodRule springMethodRule = new SpringMethodRule();
|
||||
@ClassRule
|
||||
public static final SpringClassRule SCR = new SpringClassRule();
|
||||
@Rule
|
||||
public final SpringMethodRule springMethodRule = new SpringMethodRule();
|
||||
|
||||
@Before public void setupDefaultWireMockStubbing() {
|
||||
@Before
|
||||
public void setupDefaultWireMockStubbing() {
|
||||
stubInteraction(get(urlMatching(".*")), aResponse().withStatus(200));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_set_span_data_on_headers_via_aspect_in_synchronous_call() throws Exception {
|
||||
public void should_set_span_data_on_headers_via_aspect_in_synchronous_call()
|
||||
throws Exception {
|
||||
whenARequestIsSentToASyncEndpoint();
|
||||
|
||||
thenTraceIdHasBeenSetOnARequestHeader();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_set_span_data_on_headers_when_sending_a_request_via_async_rest_template() throws Exception {
|
||||
public void should_set_span_data_on_headers_when_sending_a_request_via_async_rest_template()
|
||||
throws Exception {
|
||||
whenARequestIsSentToAAsyncRestTemplateEndpoint();
|
||||
|
||||
thenTraceIdHasBeenSetOnARequestHeader();
|
||||
@@ -70,7 +78,8 @@ public class RestTemplateTraceAspectIntegrationTests extends AbstractMvcWiremock
|
||||
|
||||
@Test
|
||||
@Parameters
|
||||
public void should_set_span_data_on_headers_via_aspect_in_asynchronous_call(String url) throws Exception {
|
||||
public void should_set_span_data_on_headers_via_aspect_in_asynchronous_call(
|
||||
String url) throws Exception {
|
||||
whenARequestIsSentToAnAsyncEndpoint(url);
|
||||
|
||||
thenTraceIdHasBeenSetOnARequestHeader();
|
||||
@@ -81,39 +90,49 @@ public class RestTemplateTraceAspectIntegrationTests extends AbstractMvcWiremock
|
||||
}
|
||||
|
||||
private void whenARequestIsSentToAAsyncRestTemplateEndpoint() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/asyncRestTemplate").accept(MediaType.TEXT_PLAIN)).andReturn();
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/asyncRestTemplate")
|
||||
.accept(MediaType.TEXT_PLAIN)).andReturn();
|
||||
}
|
||||
|
||||
private void whenARequestIsSentToASyncEndpoint() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/syncPing").accept(MediaType.TEXT_PLAIN)).andReturn();
|
||||
this.mockMvc.perform(
|
||||
MockMvcRequestBuilders.get("/syncPing").accept(MediaType.TEXT_PLAIN))
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
private void thenTraceIdHasBeenSetOnARequestHeader() {
|
||||
this.wireMock.verifyThat(getRequestedFor(urlMatching(".*")).withHeader(Span.TRACE_ID_NAME, matching("^(?!\\s*$).+")));
|
||||
this.wireMock.verifyThat(getRequestedFor(urlMatching(".*"))
|
||||
.withHeader(Span.TRACE_ID_NAME, matching("^(?!\\s*$).+")));
|
||||
}
|
||||
|
||||
private void whenARequestIsSentToAnAsyncEndpoint(String url) throws Exception {
|
||||
MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get(url).accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(request().asyncStarted())
|
||||
.andReturn();
|
||||
MvcResult mvcResult = this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get(url).accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(request().asyncStarted()).andReturn();
|
||||
mvcResult.getAsyncResult(SECONDS.toMillis(2));
|
||||
this.mockMvc.perform(asyncDispatch(mvcResult)).
|
||||
andDo(print()).
|
||||
andExpect(status().isOk());
|
||||
this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@EnableAsync
|
||||
@DefaultTestAutoConfiguration
|
||||
@Import(AspectTestingController.class)
|
||||
public static class CorrelationIdAspectTestConfiguration {
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
|
||||
@RestController
|
||||
public static class AspectTestingController {
|
||||
|
||||
@Autowired HttpMockServer httpMockServer;
|
||||
@Autowired RestTemplate restTemplate;
|
||||
@Autowired AsyncRestTemplate asyncRestTemplate;
|
||||
@Autowired
|
||||
HttpMockServer httpMockServer;
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
@Autowired
|
||||
AsyncRestTemplate asyncRestTemplate;
|
||||
|
||||
@RequestMapping(value = "/asyncRestTemplate", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
|
||||
public String asyncRestTemplate()
|
||||
@@ -147,13 +166,15 @@ public class RestTemplateTraceAspectIntegrationTests extends AbstractMvcWiremock
|
||||
};
|
||||
|
||||
private String callWiremockAndReturnOk() {
|
||||
this.restTemplate.getForObject("http://localhost:" + this.httpMockServer.port(), String.class);
|
||||
this.restTemplate.getForObject(
|
||||
"http://localhost:" + this.httpMockServer.port(), String.class);
|
||||
return "OK";
|
||||
}
|
||||
|
||||
private String callWiremockViaAsyncRestTemplateAndReturnOk()
|
||||
throws ExecutionException, InterruptedException {
|
||||
this.asyncRestTemplate.getForEntity("http://localhost:" + this.httpMockServer.port(), String.class).get();
|
||||
this.asyncRestTemplate.getForEntity(
|
||||
"http://localhost:" + this.httpMockServer.port(), String.class).get();
|
||||
return "OK";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +115,11 @@ public class TraceFilterCustomExtractorTests {
|
||||
this.port = event.getEmbeddedServletContainer().getPort();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean
|
||||
CustomRestController customRestController() {
|
||||
return new CustomRestController();
|
||||
|
||||
Reference in New Issue
Block a user