From 978e2df3433b67fd304400e5c1bebb6dd5bd40d7 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 1 Aug 2018 16:43:41 +0200 Subject: [PATCH] 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 --- .../instrument/web/SleuthWebProperties.java | 12 ++++++ .../web/TraceHttpAutoConfiguration.java | 20 +++++++++- ...stTemplateTraceAspectIntegrationTests.java | 37 ++++++++++++++++++- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/SleuthWebProperties.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/SleuthWebProperties.java index ea2319b73..801be3642 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/SleuthWebProperties.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/SleuthWebProperties.java @@ -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 { diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java index 619092549..ab1272751 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java @@ -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 Boolean trySample(HttpAdapter adapter, Req request) { + String path = adapter.path(request); + if (path == null) { + return null; + } + return path.matches(this.properties.getClient().getSkipPattern()) ? false : null; + } +} \ No newline at end of file diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/RestTemplateTraceAspectIntegrationTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/RestTemplateTraceAspectIntegrationTests.java index 9f423260e..2a43df03e 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/RestTemplateTraceAspectIntegrationTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/RestTemplateTraceAspectIntegrationTests.java @@ -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);