ZipkinExtractor uses SkipPatternProvider; fixes gh-851

This commit is contained in:
Marcin Grzejszczak
2018-02-13 15:55:00 +01:00
parent 48ac636b8c
commit 1c1aca667a
8 changed files with 104 additions and 87 deletions

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2013-2018 the original author or 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 org.springframework.cloud.sleuth.instrument.web;
import java.util.regex.Pattern;
interface SkipPatternProvider {
Pattern skipPattern();
}

View File

@@ -119,8 +119,8 @@ public class TraceFilter extends GenericFilterBean {
private static Pattern skipPattern(BeanFactory beanFactory) {
try {
TraceWebAutoConfiguration.SkipPatternProvider patternProvider = beanFactory
.getBean(TraceWebAutoConfiguration.SkipPatternProvider.class);
SkipPatternProvider patternProvider = beanFactory
.getBean(SkipPatternProvider.class);
// the null value will not happen on production but might happen in tests
if (patternProvider != null) {
return patternProvider.skipPattern();

View File

@@ -17,15 +17,19 @@ package org.springframework.cloud.sleuth.instrument.web;
import java.util.regex.Pattern;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.sleuth.TraceKeys;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
@@ -48,8 +52,8 @@ public class TraceHttpAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public HttpSpanExtractor httpSpanExtractor(SleuthWebProperties sleuthWebProperties) {
return new ZipkinHttpSpanExtractor(Pattern.compile(sleuthWebProperties.getSkipPattern()));
public HttpSpanExtractor httpSpanExtractor(SkipPatternProvider skipPatternProvider) {
return new ZipkinHttpSpanExtractor(skipPatternProvider.skipPattern());
}
@Bean
@@ -57,4 +61,74 @@ public class TraceHttpAutoConfiguration {
public HttpSpanInjector httpSpanInjector() {
return new ZipkinHttpSpanInjector();
}
@Configuration
@ConditionalOnClass(ManagementServerProperties.class)
@ConditionalOnMissingBean(SkipPatternProvider.class)
@EnableConfigurationProperties(SleuthWebProperties.class)
protected static class SkipPatternProviderConfig {
@Bean
@ConditionalOnBean(ManagementServerProperties.class)
public SkipPatternProvider skipPatternForManagementServerProperties(
final ManagementServerProperties managementServerProperties,
final SleuthWebProperties sleuthWebProperties) {
return new SkipPatternProvider() {
@Override
public Pattern skipPattern() {
return getPatternForManagementServerProperties(
managementServerProperties,
sleuthWebProperties);
}
};
}
/**
* Sets or appends {@link ManagementServerProperties#getContextPath()} to the skip
* pattern. If neither is available then sets the default one
*/
static Pattern getPatternForManagementServerProperties(
ManagementServerProperties managementServerProperties,
SleuthWebProperties sleuthWebProperties) {
String skipPattern = sleuthWebProperties.getSkipPattern();
if (StringUtils.hasText(skipPattern)
&& StringUtils.hasText(managementServerProperties.getContextPath())) {
return Pattern.compile(skipPattern + "|"
+ managementServerProperties.getContextPath() + ".*");
}
else if (StringUtils.hasText(managementServerProperties.getContextPath())) {
return Pattern
.compile(managementServerProperties.getContextPath() + ".*");
}
return defaultSkipPattern(skipPattern);
}
@Bean
@ConditionalOnMissingBean(ManagementServerProperties.class)
public SkipPatternProvider defaultSkipPatternBeanIfManagementServerPropsArePresent(SleuthWebProperties sleuthWebProperties) {
return defaultSkipPatternProvider(sleuthWebProperties.getSkipPattern());
}
}
@Bean
@ConditionalOnMissingClass("org.springframework.boot.actuate.autoconfigure.ManagementServerProperties")
@ConditionalOnMissingBean(SkipPatternProvider.class)
public SkipPatternProvider defaultSkipPatternBean(SleuthWebProperties sleuthWebProperties) {
return defaultSkipPatternProvider(sleuthWebProperties.getSkipPattern());
}
private static SkipPatternProvider defaultSkipPatternProvider(
final String skipPattern) {
return new SkipPatternProvider() {
@Override
public Pattern skipPattern() {
return defaultSkipPattern(skipPattern);
}
};
}
private static Pattern defaultSkipPattern(String skipPattern) {
return StringUtils.hasText(skipPattern) ? Pattern.compile(skipPattern)
: Pattern.compile(SleuthWebProperties.DEFAULT_SKIP_PATTERN);
}
}

View File

@@ -15,18 +15,13 @@
*/
package org.springframework.cloud.sleuth.instrument.web;
import java.util.regex.Pattern;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.sleuth.ErrorParser;
import org.springframework.cloud.sleuth.SpanNamer;
@@ -35,7 +30,6 @@ import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import static javax.servlet.DispatcherType.ASYNC;
@@ -101,77 +95,4 @@ public class TraceWebAutoConfiguration {
return new TraceFilter(beanFactory, skipPatternProvider.skipPattern());
}
@Configuration
@ConditionalOnClass(ManagementServerProperties.class)
@ConditionalOnMissingBean(SkipPatternProvider.class)
@EnableConfigurationProperties(SleuthWebProperties.class)
protected static class SkipPatternProviderConfig {
@Bean
@ConditionalOnBean(ManagementServerProperties.class)
public SkipPatternProvider skipPatternForManagementServerProperties(
final ManagementServerProperties managementServerProperties,
final SleuthWebProperties sleuthWebProperties) {
return new SkipPatternProvider() {
@Override
public Pattern skipPattern() {
return getPatternForManagementServerProperties(
managementServerProperties,
sleuthWebProperties);
}
};
}
/**
* Sets or appends {@link ManagementServerProperties#getContextPath()} to the skip
* pattern. If neither is available then sets the default one
*/
static Pattern getPatternForManagementServerProperties(
ManagementServerProperties managementServerProperties,
SleuthWebProperties sleuthWebProperties) {
String skipPattern = sleuthWebProperties.getSkipPattern();
if (StringUtils.hasText(skipPattern)
&& StringUtils.hasText(managementServerProperties.getContextPath())) {
return Pattern.compile(skipPattern + "|"
+ managementServerProperties.getContextPath() + ".*");
}
else if (StringUtils.hasText(managementServerProperties.getContextPath())) {
return Pattern
.compile(managementServerProperties.getContextPath() + ".*");
}
return defaultSkipPattern(skipPattern);
}
@Bean
@ConditionalOnMissingBean(ManagementServerProperties.class)
public SkipPatternProvider defaultSkipPatternBeanIfManagementServerPropsArePresent(SleuthWebProperties sleuthWebProperties) {
return defaultSkipPatternProvider(sleuthWebProperties.getSkipPattern());
}
}
@Bean
@ConditionalOnMissingClass("org.springframework.boot.actuate.autoconfigure.ManagementServerProperties")
@ConditionalOnMissingBean(SkipPatternProvider.class)
public SkipPatternProvider defaultSkipPatternBean(SleuthWebProperties sleuthWebProperties) {
return defaultSkipPatternProvider(sleuthWebProperties.getSkipPattern());
}
private static SkipPatternProvider defaultSkipPatternProvider(
final String skipPattern) {
return new SkipPatternProvider() {
@Override
public Pattern skipPattern() {
return defaultSkipPattern(skipPattern);
}
};
}
private static Pattern defaultSkipPattern(String skipPattern) {
return StringUtils.hasText(skipPattern) ? Pattern.compile(skipPattern)
: Pattern.compile(SleuthWebProperties.DEFAULT_SKIP_PATTERN);
}
interface SkipPatternProvider {
Pattern skipPattern();
}
}

View File

@@ -36,7 +36,6 @@ public class ZipkinHttpSpanExtractor implements HttpSpanExtractor {
@Override
public Span joinTrace(SpanTextMap textMap) {
Map<String, String> carrier = SPAN_CARRIER_MAPPER.convert(textMap);
boolean debug = Span.SPAN_SAMPLED.equals(carrier.get(Span.SPAN_FLAGS));
boolean idToBeGenerated = debug && onlySpanIdIsPresent(carrier);
// we're only generating Trace ID since if there's no Span ID will assume

View File

@@ -20,7 +20,7 @@ import java.util.regex.Pattern;
import org.junit.Test;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration.SkipPatternProviderConfig;
import org.springframework.cloud.sleuth.instrument.web.TraceHttpAutoConfiguration.SkipPatternProviderConfig;
import static org.assertj.core.api.BDDAssertions.then;

View File

@@ -70,7 +70,7 @@ public class TraceFilterAlwaysSamplerIntegrationTests extends AbstractMvcIntegra
private BeanFactory beanFactory() {
BeanFactory beanFactory = Mockito.mock(BeanFactory.class);
BDDMockito.given(beanFactory.getBean(TraceWebAutoConfiguration.SkipPatternProvider.class))
BDDMockito.given(beanFactory.getBean(SkipPatternProvider.class))
.willThrow(new NoSuchBeanDefinitionException("foo"));
BDDMockito.given(beanFactory.getBean(SleuthProperties.class)).willReturn(this.properties);
BDDMockito.given(beanFactory.getBean(Tracer.class)).willReturn(this.tracer);

View File

@@ -561,7 +561,7 @@ public class TraceFilterTests {
}
private BeanFactory beanFactory() {
BDDMockito.given(beanFactory.getBean(TraceWebAutoConfiguration.SkipPatternProvider.class))
BDDMockito.given(beanFactory.getBean(SkipPatternProvider.class))
.willThrow(new NoSuchBeanDefinitionException("foo"));
BDDMockito.given(beanFactory.getBean(SleuthProperties.class)).willReturn(this.properties);
BDDMockito.given(beanFactory.getBean(Tracer.class)).willReturn(this.tracer);