diff --git a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc index 76b05f30b..0e20d4582 100644 --- a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc +++ b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc @@ -880,17 +880,17 @@ spring.zipkin.service.name: myService === Customization of Reported Spans Before reporting spans (for example, to Zipkin) you may want to modify that span in some way. -You can do so by using the `SpanAdjuster` interface. +You can do so by using the `FinishedSpanHandler` interface. In Sleuth, we generate spans with a fixed name. Some users want to modify the name depending on values of tags. -You can implement the `SpanAdjuster` interface to alter that name. +You can implement the `FinishedSpanHandler` interface to alter that name. -The following example shows how to register two beans that implement `SpanAdjuster`: +The following example shows how to register two beans that implement `FinishedSpanHandler`: [source,java] ---- -include::../../../..//spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanAdjusterTests.java[tags=adjuster,indent=0] +include::../../../..//spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/FinishedSpanHandlerTests.java[tags=finishedSpanHandler,indent=0] ---- The preceding example results in changing the name of the reported span to `foo bar`, just before it gets reported (for example, to Zipkin). diff --git a/pom.xml b/pom.xml index 9ffe72069..86cb69b40 100644 --- a/pom.xml +++ b/pom.xml @@ -273,7 +273,7 @@ Fishtown.BUILD-SNAPSHOT 2.1.0.BUILD-SNAPSHOT 2.1.0.BUILD-SNAPSHOT - 5.3.3 + 5.4.0 2.0.4.RELEASE diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/SpanAdjuster.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/SpanAdjuster.java index e9acee433..e3a833a5b 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/SpanAdjuster.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/SpanAdjuster.java @@ -19,16 +19,9 @@ package org.springframework.cloud.sleuth; import zipkin2.Span; /** - * Adds ability to adjust a span before reporting it. - * - * IMPORTANT - if you override the default {@link brave.Tracing} implementation, - * remember to ensure that you pass to it an adjusted version of the {@link zipkin2.reporter.Reporter} - * bean. In other words you must reuse the list of available {@link SpanAdjuster}s and - * wrap the provided {@link zipkin2.reporter.Reporter} interface with it. - * - * @author Marcin Grzejszczak - * @since 1.1.4 + * @deprecated use {@link brave.handler.FinishedSpanHandler} */ +@Deprecated public interface SpanAdjuster { /** * You can adjust the {@link zipkin2.Span} by creating a new one using the {@link Span#toBuilder()} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java index 6046afe72..de43d1788 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.cloud.sleuth.autoconfig; +import brave.handler.FinishedSpanHandler; import java.util.ArrayList; import java.util.List; @@ -58,6 +59,7 @@ public class TraceAutoConfiguration { public static final String TRACER_BEAN_NAME = "tracer"; @Autowired(required = false) List spanAdjusters = new ArrayList<>(); + @Autowired(required = false) List finishedSpanHandlers = new ArrayList<>(); @Autowired(required = false) List scopeDecorators = new ArrayList<>(); @Bean @@ -71,7 +73,7 @@ public class TraceAutoConfiguration { ErrorParser errorParser, SleuthProperties sleuthProperties ) { - return Tracing.newBuilder() + Tracing.Builder builder = Tracing.newBuilder() .sampler(sampler) .errorParser(errorParser) .localServiceName(serviceName) @@ -79,8 +81,11 @@ public class TraceAutoConfiguration { .currentTraceContext(currentTraceContext) .spanReporter(adjustedReporter(reporter)) .traceId128Bit(sleuthProperties.isTraceId128()) - .supportsJoin(sleuthProperties.isSupportsJoin()) - .build(); + .supportsJoin(sleuthProperties.isSupportsJoin()); + for (FinishedSpanHandler finishedSpanHandlerFactory : this.finishedSpanHandlers) { + builder.addFinishedSpanHandler(finishedSpanHandlerFactory); + } + return builder.build(); } private Reporter adjustedReporter(Reporter delegate) { diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/FinishedSpanHandlerTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/FinishedSpanHandlerTests.java new file mode 100644 index 000000000..757daba00 --- /dev/null +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/FinishedSpanHandlerTests.java @@ -0,0 +1,90 @@ +/* + * 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; + +import brave.Span; +import brave.Tracer; +import brave.handler.FinishedSpanHandler; +import brave.handler.MutableSpan; +import brave.propagation.TraceContext; +import brave.sampler.Sampler; +import org.assertj.core.api.BDDAssertions; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.sleuth.util.ArrayListSpanReporter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; +import zipkin2.reporter.Reporter; + +/** + * @author Marcin Grzejszczak + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = FinishedSpanHandlerTests.FinishedSpanHandlerAspectTestsConfig.class, + webEnvironment = SpringBootTest.WebEnvironment.NONE) +public class FinishedSpanHandlerTests { + + @Autowired ArrayListSpanReporter reporter; + @Autowired Tracer tracer; + + @Test + public void should_adjust_span_twice_before_reporting() { + Span hello = this.tracer.nextSpan().name("hello").start(); + + hello.finish(); + + BDDAssertions.then(this.reporter.getSpans()).hasSize(1); + BDDAssertions.then(this.reporter.getSpans().get(0).name()).isEqualTo("foo bar"); + } + + @Configuration + @EnableAutoConfiguration(exclude = IntegrationAutoConfiguration.class) + static class FinishedSpanHandlerAspectTestsConfig { + @Bean Sampler sampler() { + return Sampler.ALWAYS_SAMPLE; + } + + @Bean Reporter reporter() { + return new ArrayListSpanReporter(); + } + + // tag::finishedSpanHandler[] + @Bean FinishedSpanHandler handlerOne() { + return new FinishedSpanHandler() { + @Override public boolean handle(TraceContext traceContext, MutableSpan span) { + span.name("foo"); + return true; // keep this span + } + }; + } + + @Bean FinishedSpanHandler handlerTwo() { + return new FinishedSpanHandler() { + @Override public boolean handle(TraceContext traceContext, MutableSpan span) { + span.name(span.name() + " bar"); + return true; // keep this span + } + }; + } + // end::finishedSpanHandler[] + } +} \ No newline at end of file diff --git a/spring-cloud-sleuth-dependencies/pom.xml b/spring-cloud-sleuth-dependencies/pom.xml index 0c7dbde8c..50741fbb1 100644 --- a/spring-cloud-sleuth-dependencies/pom.xml +++ b/spring-cloud-sleuth-dependencies/pom.xml @@ -30,7 +30,7 @@ spring-cloud-sleuth-dependencies Spring Cloud Sleuth Dependencies - 0.33.0 + 0.33.1 diff --git a/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin2/ZipkinAutoConfigurationTests.java b/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin2/ZipkinAutoConfigurationTests.java index 3d4110c51..cbf19bc1d 100644 --- a/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin2/ZipkinAutoConfigurationTests.java +++ b/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin2/ZipkinAutoConfigurationTests.java @@ -18,6 +18,9 @@ package org.springframework.cloud.sleuth.zipkin2; import brave.Span; import brave.Tracing; +import brave.handler.FinishedSpanHandler; +import brave.handler.MutableSpan; +import brave.propagation.TraceContext; import brave.sampler.Sampler; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; @@ -30,7 +33,6 @@ import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.sleuth.SpanAdjuster; import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -198,13 +200,23 @@ public class ZipkinAutoConfigurationTests { } @Configuration - protected static class AdjustersConfig { - @Bean SpanAdjuster adjusterOne() { - return span -> span.toBuilder().name("foo").build(); + protected static class HandlerHanldersConfig { + @Bean FinishedSpanHandler handlerOne() { + return new FinishedSpanHandler() { + @Override public boolean handle(TraceContext traceContext, MutableSpan span) { + span.name("foo"); + return true; // keep this span + } + }; } - @Bean SpanAdjuster adjusterTwo() { - return span -> span.toBuilder().name(span.name() + " bar").build(); + @Bean FinishedSpanHandler handlerTwo() { + return new FinishedSpanHandler() { + @Override public boolean handle(TraceContext traceContext, MutableSpan span) { + span.name(span.name() + " bar"); + return true; // keep this span + } + }; } } }