Added an option to filter out outbound requests (#1048)
* Added an option to filter out outbound requests without this change there's no way to filter outbound requests basing on the path via a property. with this change we're introducing that fixes gh-1047
This commit is contained in:
committed by
GitHub
parent
b87b152173
commit
978e2df343
@@ -82,6 +82,10 @@ public class SleuthWebProperties {
|
||||
}
|
||||
|
||||
public static class Client {
|
||||
/**
|
||||
* Pattern for URLs that should be skipped in client side tracing
|
||||
*/
|
||||
private String skipPattern = "";
|
||||
|
||||
/**
|
||||
* Enable interceptor injecting into {@link org.springframework.web.client.RestTemplate}
|
||||
@@ -95,6 +99,14 @@ public class SleuthWebProperties {
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getSkipPattern() {
|
||||
return this.skipPattern;
|
||||
}
|
||||
|
||||
public void setSkipPattern(String skipPattern) {
|
||||
this.skipPattern = skipPattern;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Async {
|
||||
|
||||
@@ -111,8 +111,8 @@ public class TraceHttpAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = ClientSampler.NAME)
|
||||
HttpSampler sleuthClientSampler() {
|
||||
return HttpSampler.TRACE_ID;
|
||||
HttpSampler sleuthClientSampler(SleuthWebProperties sleuthWebProperties) {
|
||||
return new PathMatchingHttpSampler(sleuthWebProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,3 +138,19 @@ class CompositeHttpSampler extends HttpSampler {
|
||||
return leftDecision && rightDecision;
|
||||
}
|
||||
}
|
||||
|
||||
class PathMatchingHttpSampler extends HttpSampler {
|
||||
private final SleuthWebProperties properties;
|
||||
|
||||
PathMatchingHttpSampler(SleuthWebProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override public <Req> Boolean trySample(HttpAdapter<Req, ?> adapter, Req request) {
|
||||
String path = adapter.path(request);
|
||||
if (path == null) {
|
||||
return null;
|
||||
}
|
||||
return path.matches(this.properties.getClient().getSkipPattern()) ? false : null;
|
||||
}
|
||||
}
|
||||
@@ -62,19 +62,23 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = RestTemplateTraceAspectIntegrationTests.Config.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @DirtiesContext
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
properties = "spring.sleuth.web.client.skipPattern=/issue.*")
|
||||
@DirtiesContext
|
||||
public class RestTemplateTraceAspectIntegrationTests {
|
||||
|
||||
@Autowired WebApplicationContext context;
|
||||
@Autowired AspectTestingController controller;
|
||||
@Autowired Tracing tracer;
|
||||
@Autowired ArrayListSpanReporter reporter;
|
||||
@Autowired RestTemplate restTemplate;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before public void init() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
|
||||
this.controller.reset();
|
||||
this.reporter.clear();
|
||||
}
|
||||
|
||||
@Before @After public void verify() {
|
||||
@@ -116,6 +120,16 @@ public class RestTemplateTraceAspectIntegrationTests {
|
||||
thenClientKindIsReported();
|
||||
}
|
||||
|
||||
// issue #1047
|
||||
@Test
|
||||
public void should_not_create_a_client_span_for_filtered_out_paths()
|
||||
throws Exception {
|
||||
whenARequestIsSentToASyncEndpointThatShouldBeFilteredOut();
|
||||
|
||||
then(Tracing.current().tracer().currentSpan()).isNull();
|
||||
then(this.reporter.getSpans()).isEmpty();
|
||||
}
|
||||
|
||||
private void whenARequestIsSentToAnAsyncRestTemplateEndpoint() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/asyncRestTemplate")
|
||||
.accept(MediaType.TEXT_PLAIN)).andReturn();
|
||||
@@ -127,6 +141,12 @@ public class RestTemplateTraceAspectIntegrationTests {
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
private void whenARequestIsSentToASyncEndpointThatShouldBeFilteredOut() throws Exception {
|
||||
this.mockMvc.perform(
|
||||
MockMvcRequestBuilders.get("/issue1047_start").accept(MediaType.TEXT_PLAIN))
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
private void thenTraceIdHasBeenSetOnARequestHeader() {
|
||||
assertThat(this.controller.getTraceId()).matches("^(?!\\s*$).+");
|
||||
}
|
||||
@@ -183,6 +203,16 @@ public class RestTemplateTraceAspectIntegrationTests {
|
||||
this.traceId = null;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/issue1047_end", method = RequestMethod.GET,
|
||||
produces = MediaType.TEXT_PLAIN_VALUE) public String issue1047() {
|
||||
return "should_filter_out_this_endpoint";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/issue1047_start", method = RequestMethod.GET,
|
||||
produces = MediaType.TEXT_PLAIN_VALUE) public String issue1047Start() {
|
||||
return callAndReturnIssue1047();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET,
|
||||
produces = MediaType.TEXT_PLAIN_VALUE) public String home(
|
||||
@RequestHeader(value = "X-B3-SpanId", required = false) String traceId) {
|
||||
@@ -228,7 +258,10 @@ public class RestTemplateTraceAspectIntegrationTests {
|
||||
});
|
||||
}
|
||||
|
||||
;
|
||||
private String callAndReturnIssue1047() {
|
||||
this.restTemplate.getForObject("http://localhost:" + port() + "/issue1047_end", String.class);
|
||||
return "OK";
|
||||
}
|
||||
|
||||
private String callAndReturnOk() {
|
||||
this.restTemplate.getForObject("http://localhost:" + port(), String.class);
|
||||
|
||||
Reference in New Issue
Block a user