-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register skipPattern for SB actuator endpoints dynamically. (#100)
- Loading branch information
1 parent
e043077
commit 6cae6b8
Showing
16 changed files
with
1,132 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
...src/main/java/io/opentracing/contrib/spring/web/starter/SkipPatternAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* Copyright 2016-2019 The OpenTracing Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package io.opentracing.contrib.spring.web.starter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; | ||
import org.springframework.boot.actuate.autoconfigure.web.server.ConditionalOnManagementPort; | ||
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType; | ||
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties; | ||
import org.springframework.boot.actuate.endpoint.EndpointsSupplier; | ||
import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint; | ||
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoint; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.autoconfigure.web.ServerProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.util.StringUtils; | ||
|
||
import io.opentracing.contrib.spring.web.webfilter.SkipPattern; | ||
|
||
/** | ||
* @author Gilles Robert | ||
*/ | ||
@Configuration | ||
@ConditionalOnProperty(name = "opentracing.spring.web.enabled", havingValue = "true", matchIfMissing = true) | ||
public class SkipPatternAutoConfiguration { | ||
|
||
@Autowired(required = false) | ||
private List<SkipPattern> patterns = new ArrayList<>(); | ||
|
||
@Bean(name = "skipPattern") | ||
public Pattern skipPattern() { | ||
return Pattern.compile(this.patterns | ||
.stream() | ||
.map(SkipPattern::pattern) | ||
.filter(Optional::isPresent).map(Optional::get) | ||
.map(Pattern::pattern) | ||
.collect(Collectors.joining("|"))); | ||
} | ||
|
||
@Configuration | ||
@ConditionalOnClass(ManagementServerProperties.class) | ||
@ConditionalOnProperty(value = "opentracing.spring.web.ignoreAutoConfiguredSkipPatterns", havingValue = "false", matchIfMissing = true) | ||
protected static class ManagementSkipPatternProviderConfig { | ||
|
||
static Optional<Pattern> getPatternForManagementServerProperties( | ||
ManagementServerProperties managementServerProperties) { | ||
String contextPath = managementServerProperties.getServlet().getContextPath(); | ||
if (StringUtils.hasText(contextPath)) { | ||
return Optional.of(Pattern.compile(contextPath + ".*")); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnBean(ManagementServerProperties.class) | ||
public SkipPattern skipPatternForManagementServerProperties( | ||
final ManagementServerProperties managementServerProperties) { | ||
return () -> getPatternForManagementServerProperties(managementServerProperties); | ||
} | ||
} | ||
|
||
@Configuration | ||
@ConditionalOnClass( {ServerProperties.class, EndpointsSupplier.class, ExposableWebEndpoint.class}) | ||
@ConditionalOnBean(ServerProperties.class) | ||
@ConditionalOnProperty(value = "opentracing.spring.web.ignoreAutoConfiguredSkipPatterns", havingValue = "false", matchIfMissing = true) | ||
protected static class ActuatorSkipPatternProviderConfig { | ||
|
||
static Optional<Pattern> getEndpointsPatterns(String contextPath, | ||
WebEndpointProperties webEndpointProperties, | ||
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) { | ||
Collection<ExposableWebEndpoint> endpoints = endpointsSupplier.getEndpoints(); | ||
|
||
if (endpoints.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
|
||
String pattern = endpoints.stream().map(PathMappedEndpoint::getRootPath) | ||
.map(path -> path + "|" + path + "/.*").collect( | ||
Collectors.joining("|", | ||
getPathPrefix(contextPath, | ||
webEndpointProperties.getBasePath()) + "/(", | ||
")")); | ||
if (StringUtils.hasText(pattern)) { | ||
return Optional.of(Pattern.compile(pattern)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private static String getPathPrefix(String contextPath, String actuatorBasePath) { | ||
String result = ""; | ||
if (StringUtils.hasText(contextPath)) { | ||
result += contextPath; | ||
} | ||
if (!actuatorBasePath.equals("/")) { | ||
result += actuatorBasePath; | ||
} | ||
return result; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnManagementPort(ManagementPortType.SAME) | ||
public SkipPattern skipPatternForActuatorEndpointsSamePort( | ||
final ServerProperties serverProperties, | ||
final WebEndpointProperties webEndpointProperties, | ||
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) { | ||
return () -> getEndpointsPatterns( | ||
serverProperties.getServlet().getContextPath(), webEndpointProperties, | ||
endpointsSupplier); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT) | ||
@ConditionalOnProperty(name = "management.server.servlet.context-path", havingValue = "/", matchIfMissing = true) | ||
public SkipPattern skipPatternForActuatorEndpointsDifferentPort( | ||
final WebEndpointProperties webEndpointProperties, | ||
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) { | ||
return () -> getEndpointsPatterns(null, webEndpointProperties, | ||
endpointsSupplier); | ||
} | ||
} | ||
|
||
@Configuration | ||
protected static class DefaultSkipPatternConfig { | ||
|
||
private static String combinedPatterns(String skipPattern) { | ||
String pattern = skipPattern; | ||
if (!StringUtils.hasText(skipPattern)) { | ||
pattern = WebTracingProperties.DEFAULT_SKIP_PATTERN; | ||
} | ||
return pattern; | ||
} | ||
|
||
@Bean | ||
SkipPattern defaultSkipPatternBean(WebTracingProperties webTracingProperties) { | ||
return () -> Optional.of(Pattern.compile(combinedPatterns(webTracingProperties.getSkipPattern()))); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
opentracing-spring-web-starter/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
io.opentracing.contrib.spring.web.starter.ServerTracingAutoConfiguration,\ | ||
io.opentracing.contrib.spring.web.starter.SkipPatternAutoConfiguration,\ | ||
io.opentracing.contrib.spring.web.starter.RestTemplateTracingAutoConfiguration,\ | ||
io.opentracing.contrib.spring.web.starter.WebClientTracingAutoConfiguration,\ | ||
io.opentracing.contrib.spring.web.starter.WebFluxTracingAutoConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.