Skip to content

Commit a1832f6

Browse files
shakuzenmarcingrzejszczak
authored andcommitted
Skip only Actuator endpoints when context path set (#1150)
Previously, all endpoints under the set context path would be skipped. Instead, this skips only Actuator endpoints under the set context-path. Resolves #1146
1 parent 3a618e6 commit a1832f6

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import brave.Tracing;
2525
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2627
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
2728
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2829
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -92,27 +93,31 @@ public SingleSkipPattern skipPatternForManagementServerProperties(
9293
}
9394

9495
@Configuration
95-
@ConditionalOnClass(ServerProperties.class)
96+
@ConditionalOnClass({ServerProperties.class, WebEndpointProperties.class})
9697
protected static class ServerSkipPatternProviderConfig {
9798

9899
/**
99-
* Sets or appends {@link ServerProperties#getServlet()#getContextPath()} to the
100-
* skip pattern. If neither is available then sets the default one
100+
* Uses {@link ServerProperties#getServlet()#getContextPath()} and
101+
* {@link WebEndpointProperties#getBasePath()} to skip Actuator endpoints.
101102
*/
102103
static Optional<Pattern> getPatternForServerProperties(
103-
ServerProperties serverProperties) {
104+
ServerProperties serverProperties,
105+
WebEndpointProperties webEndpointProperties) {
104106
String contextPath = serverProperties.getServlet().getContextPath();
105107
if (StringUtils.hasText(contextPath)) {
106-
return Optional.of(Pattern.compile(contextPath + ".*"));
108+
return Optional.of(Pattern.compile(
109+
contextPath + webEndpointProperties.getBasePath() + ".*"));
107110
}
108111
return Optional.empty();
109112
}
110113

111114
@Bean
112-
@ConditionalOnBean(ServerProperties.class)
115+
@ConditionalOnBean({ ServerProperties.class, WebEndpointProperties.class })
113116
public SingleSkipPattern skipPatternForServerProperties(
114-
final ServerProperties serverProperties) {
115-
return () -> getPatternForServerProperties(serverProperties);
117+
final ServerProperties serverProperties,
118+
final WebEndpointProperties webEndpointProperties) {
119+
return () -> getPatternForServerProperties(serverProperties,
120+
webEndpointProperties);
116121
}
117122

118123
}

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/SkipPatternProviderConfigTest.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.regex.Pattern;
2222

2323
import org.junit.Test;
24+
25+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2426
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
2527
import org.springframework.boot.autoconfigure.web.ServerProperties;
2628

@@ -80,7 +82,9 @@ public void should_return_management_context_with_context_path() throws Exceptio
8082
public void should_return_empty_when_server_props_have_no_context_path()
8183
throws Exception {
8284
Optional<Pattern> pattern = new TraceWebAutoConfiguration.ServerSkipPatternProviderConfig()
83-
.skipPatternForServerProperties(new ServerProperties()).skipPattern();
85+
.skipPatternForServerProperties(new ServerProperties(),
86+
new WebEndpointProperties())
87+
.skipPattern();
8488

8589
then(pattern).isEmpty();
8690
}
@@ -91,10 +95,11 @@ public void should_return_server_props_with_context_path() throws Exception {
9195
properties.getServlet().setContextPath("foo");
9296

9397
Optional<Pattern> pattern = new TraceWebAutoConfiguration.ServerSkipPatternProviderConfig()
94-
.skipPatternForServerProperties(properties).skipPattern();
98+
.skipPatternForServerProperties(properties, new WebEndpointProperties())
99+
.skipPattern();
95100

96101
then(pattern).isNotEmpty();
97-
then(pattern.get().pattern()).isEqualTo("foo.*");
102+
then(pattern.get().pattern()).isEqualTo("foo/actuator.*");
98103
}
99104

100105
@Test

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/issues/issue971/DemoSleuthSkipApplicationTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.context.annotation.Bean;
3131
import org.springframework.context.annotation.Configuration;
3232
import org.springframework.test.context.junit4.SpringRunner;
33+
import org.springframework.web.bind.annotation.GetMapping;
34+
import org.springframework.web.bind.annotation.RestController;
3335
import org.springframework.web.client.RestTemplate;
3436

3537
import static org.assertj.core.api.BDDAssertions.then;
@@ -60,11 +62,26 @@ public void should_not_sample_skipped_endpoint_with_context_path() {
6062
then(this.accumulator.getSpans()).hasSize(0);
6163
}
6264

65+
@Test
66+
public void should_sample_non_actuator_endpoint_with_context_path() {
67+
new RestTemplate().getForObject(
68+
"http://localhost:" + this.port + "/context-path/something",
69+
String.class);
70+
71+
then(this.tracer.currentSpan()).isNull();
72+
then(this.accumulator.getSpans()).hasSize(1);
73+
}
74+
6375
@EnableAutoConfiguration(exclude = RabbitAutoConfiguration.class)
6476
@Configuration
6577
@DisableSecurity
78+
@RestController
6679
public static class Config {
6780

81+
@GetMapping("something")
82+
void doNothing() {
83+
}
84+
6885
@Bean
6986
ArrayListSpanReporter reporter() {
7087
return new ArrayListSpanReporter();

0 commit comments

Comments
 (0)