Fixed disabling of Sleuth

without this change it's impossible to disable Sleuth when you have registered a Random bean
with this change the conditions are fixed

fixes #462
This commit is contained in:
Marcin Grzejszczak
2016-12-08 15:05:23 +01:00
parent 398d26186e
commit 0f4427bbf3
7 changed files with 144 additions and 88 deletions

View File

@@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.sleuth.SpanExtractor;
import org.springframework.cloud.sleuth.SpanInjector;
import org.springframework.cloud.sleuth.TraceKeys;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
@@ -37,7 +38,7 @@ import org.springframework.messaging.support.MessageBuilder;
*/
@Configuration
@ConditionalOnClass(Message.class)
@ConditionalOnBean({ TraceKeys.class, Random.class })
@ConditionalOnBean(Tracer.class)
public class TraceSpanMessagingAutoConfiguration {
@Bean

View File

@@ -23,7 +23,7 @@ import static org.assertj.core.api.BDDAssertions.then;
/**
* @author Marcin Grzejszczak
*/
public class DefaultSpanNamerTest {
public class DefaultSpanNamerTests {
DefaultSpanNamer defaultSpanNamer = new DefaultSpanNamer();

View File

@@ -30,7 +30,7 @@ import static org.assertj.core.api.BDDAssertions.then;
/**
* @author Adrian Cole
*/
public class LogTest {
public class LogTests {
@Rule
public ExpectedException thrown = ExpectedException.none();

View File

@@ -1,82 +0,0 @@
/**
* Copyright 2015-2016 The OpenZipkin 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.autoconfig;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration;
import org.springframework.cloud.sleuth.sampler.NeverSampler;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
public class TraceAutoConfigurationTest {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@After
public void close() {
context.close();
}
@Test
public void defaultsTo64BitTraceId() {
context = new AnnotationConfigApplicationContext();
context.register(
PropertyPlaceholderAutoConfiguration.class,
SleuthLogAutoConfiguration.class,
TraceAutoConfiguration.class
);
context.refresh();
Tracer tracer = context.getBean(Tracer.class);
Span span = null;
try {
span = tracer.createSpan("foo", NeverSampler.INSTANCE);
assertThat(span.getTraceIdHigh()).isEqualTo(0L);
assertThat(span.getTraceId()).isNotEqualTo(0L);
} finally {
if (span != null){
tracer.close(span);
}
}
}
@Test
public void optInto128BitTraceId() {
addEnvironment(context, "spring.sleuth.traceId128:true");
context.register(
PropertyPlaceholderAutoConfiguration.class,
SleuthLogAutoConfiguration.class,
TraceAutoConfiguration.class
);
context.refresh();
Tracer tracer = context.getBean(Tracer.class);
Span span = null;
try {
span = tracer.createSpan("foo", NeverSampler.INSTANCE);
assertThat(span.getTraceIdHigh()).isNotEqualTo(0L);
assertThat(span.getTraceId()).isNotEqualTo(0L);
} finally {
if (span != null){
tracer.close(span);
}
}
}
}

View File

@@ -0,0 +1,75 @@
/**
* Copyright 2015-2016 The OpenZipkin Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.autoconfig;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration;
import org.springframework.cloud.sleuth.sampler.NeverSampler;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
public class TraceAutoConfigurationTests {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@After public void close() {
context.close();
}
@Test public void defaultsTo64BitTraceId() {
context = new AnnotationConfigApplicationContext();
context.register(PropertyPlaceholderAutoConfiguration.class,
SleuthLogAutoConfiguration.class, TraceAutoConfiguration.class);
context.refresh();
Tracer tracer = context.getBean(Tracer.class);
Span span = null;
try {
span = tracer.createSpan("foo", NeverSampler.INSTANCE);
assertThat(span.getTraceIdHigh()).isEqualTo(0L);
assertThat(span.getTraceId()).isNotEqualTo(0L);
}
finally {
if (span != null) {
tracer.close(span);
}
}
}
@Test public void optInto128BitTraceId() {
addEnvironment(context, "spring.sleuth.traceId128:true");
context.register(PropertyPlaceholderAutoConfiguration.class,
SleuthLogAutoConfiguration.class, TraceAutoConfiguration.class);
context.refresh();
Tracer tracer = context.getBean(Tracer.class);
Span span = null;
try {
span = tracer.createSpan("foo", NeverSampler.INSTANCE);
assertThat(span.getTraceIdHigh()).isNotEqualTo(0L);
assertThat(span.getTraceId()).isNotEqualTo(0L);
}
finally {
if (span != null) {
tracer.close(span);
}
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2013-2016 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.autoconfig;
import java.security.SecureRandom;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TraceAutoConfigurationWithDisabledSleuthTests.Config.class)
@TestPropertySource(properties = "spring.sleuth.enabled=false")
public class TraceAutoConfigurationWithDisabledSleuthTests {
@Test
public void shouldStartContext(){
}
@EnableAutoConfiguration
static class Config {
@Bean
public FactoryBean<SecureRandom> secureRandom() {
return new FactoryBean<SecureRandom>() {
@Override public SecureRandom getObject() throws Exception {
return new SecureRandom();
}
@Override public Class<?> getObjectType() {
return SecureRandom.class;
}
@Override public boolean isSingleton() {
return true;
}
};
}
}
}

View File

@@ -4,18 +4,20 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.SpanReporter;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.util.ArrayListSpanAccumulator;
import org.springframework.cloud.sleuth.TraceKeys;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.instrument.web.TraceFilter;
import org.springframework.cloud.sleuth.instrument.web.common.AbstractMvcIntegrationTest;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.util.ArrayListSpanAccumulator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
@@ -28,6 +30,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MultipleHopsIntegrationTests.Config.class)
@TestPropertySource(properties = "spring.application.name=multiplehopsintegrationtests")
public class MultipleHopsIntegrationTests extends AbstractMvcIntegrationTest {
@Autowired Tracer tracer;
@@ -55,7 +58,7 @@ public class MultipleHopsIntegrationTests extends AbstractMvcIntegrationTest {
}
@Configuration
@SpringBootApplication
@SpringBootApplication(exclude = JmxAutoConfiguration.class)
public static class Config {
@Bean ArrayListSpanAccumulator arrayListSpanAccumulator() {