Deprecates SpanAdjuster for Brave's FirehoseHandler

Brave's `FirehoseHandler` has the features adjuster has, more
efficiently and also including the ability to prune spans.

See https://github.com/openzipkin/brave/pull/794
This commit is contained in:
Adrian Cole
2018-09-24 22:33:24 -04:00
committed by Adrian Cole
parent 390473a60e
commit 7b5d530c80
7 changed files with 124 additions and 24 deletions

View File

@@ -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).

View File

@@ -273,7 +273,7 @@
<spring-cloud-stream.version>Fishtown.BUILD-SNAPSHOT</spring-cloud-stream.version>
<spring-cloud-netflix.version>2.1.0.BUILD-SNAPSHOT</spring-cloud-netflix.version>
<spring-cloud-openfeign.version>2.1.0.BUILD-SNAPSHOT</spring-cloud-openfeign.version>
<brave.version>5.3.3</brave.version>
<brave.version>5.4.0</brave.version>
<spring-security-boot-autoconfigure.version>2.0.4.RELEASE</spring-security-boot-autoconfigure.version>
</properties>

View File

@@ -19,16 +19,9 @@ package org.springframework.cloud.sleuth;
import zipkin2.Span;
/**
* Adds ability to adjust a span before reporting it.
*
* <b>IMPORTANT</b> - 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<zipkin2.Span>}
* 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()}

View File

@@ -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<SpanAdjuster> spanAdjusters = new ArrayList<>();
@Autowired(required = false) List<FinishedSpanHandler> finishedSpanHandlers = new ArrayList<>();
@Autowired(required = false) List<CurrentTraceContext.ScopeDecorator> 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<zipkin2.Span> adjustedReporter(Reporter<zipkin2.Span> delegate) {

View File

@@ -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<zipkin2.Span> 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[]
}
}

View File

@@ -30,7 +30,7 @@
<name>spring-cloud-sleuth-dependencies</name>
<description>Spring Cloud Sleuth Dependencies</description>
<properties>
<brave.opentracing.version>0.33.0</brave.opentracing.version>
<brave.opentracing.version>0.33.1</brave.opentracing.version>
</properties>
<dependencyManagement>
<dependencies>

View File

@@ -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
}
};
}
}
}