Updates to Brave 5.12 and introduces SpanHandler (#1632)

`SpanHandler` is the base type for the now deprecated `FinishedSpanHandler`.

Notable, it can not just handle things at the end of a recording, but also the
beginning.

For example, this permits set-once baggage without the HTTP abstraction:
```java
static final BaggageField EPOCH_SECONDS = BaggageField.create("epoch_seconds");

static final class RootOnlyBaggage extends SpanHandler {
  @Override
  public boolean begin(TraceContext context, MutableSpan span, @Nullable TraceContext parent) {
    if (EPOCH_SECONDS.getValue(context) == null) { // only set at the first span
      long epochSeconds = System.currentTimeMillis() / 1000;
      EPOCH_SECONDS.updateValue(context, String.valueOf(epochSeconds));
    }
    return true;
  }

  @Override public boolean end(TraceContext context, MutableSpan span, Cause cause) {
    Tags.BAGGAGE_FIELD.tag(EPOCH_SECONDS, context, span);
    return true;
  }
}
```

As the parent is available, it can also facilitate advanced tasks like counting
children, or summarizing entire local roots.

See https://github.com/openzipkin/brave/tree/master/brave/src/test/java/brave/features/handler
and https://github.com/openzipkin/brave/blob/master/brave/src/main/java/brave/handler/SpanHandler.java for more
This commit is contained in:
Adrian Cole
2020-05-16 14:01:38 +08:00
committed by Adrian Cole
parent 511c54da81
commit a9128dcd32
18 changed files with 74 additions and 64 deletions

View File

@@ -34,7 +34,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<spring-boot.version>2.1.10.RELEASE</spring-boot.version>
<brave.version>5.11.2</brave.version>
<brave.version>5.12.0</brave.version>
<okhttp.version>3.11.0</okhttp.version>
</properties>

View File

@@ -923,17 +923,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 `FinishedSpanHandler` interface.
You can do so by implementing a `SpanHandler`.
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 `FinishedSpanHandler` interface to alter that name.
You can implement the `SpanHandler` interface to alter that name.
The following example shows how to register two beans that implement `FinishedSpanHandler`:
The following example shows how to register two beans that implement `SpanHandler`:
[source,java]
----
include::../../../..//spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/FinishedSpanHandlerTests.java[tags=finishedSpanHandler,indent=0]
include::{project-root}//spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanHandlerTests.java[tags=spanHandler,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

@@ -264,7 +264,7 @@
<spring-cloud-stream.version>Fishtown.SR4</spring-cloud-stream.version>
<spring-cloud-netflix.version>2.1.6.BUILD-SNAPSHOT</spring-cloud-netflix.version>
<spring-cloud-openfeign.version>2.1.6.BUILD-SNAPSHOT</spring-cloud-openfeign.version>
<brave.version>5.11.2</brave.version>
<brave.version>5.12.0</brave.version>
<spring-security-boot-autoconfigure.version>2.1.2.RELEASE
</spring-security-boot-autoconfigure.version>

View File

@@ -21,7 +21,7 @@ import zipkin2.Span;
/**
* Deprecated Span Adjuster.
*
* @deprecated use {@link brave.handler.FinishedSpanHandler}
* @deprecated use {@link brave.handler.SpanHandler}
* @author Marcin Grzejszczak
*/
@Deprecated

View File

@@ -25,7 +25,7 @@ import brave.ErrorParser;
import brave.Tracer;
import brave.Tracing;
import brave.TracingCustomizer;
import brave.handler.FinishedSpanHandler;
import brave.handler.SpanHandler;
import brave.propagation.B3Propagation;
import brave.propagation.CurrentTraceContext;
import brave.propagation.CurrentTraceContextCustomizer;
@@ -85,7 +85,7 @@ public class TraceAutoConfiguration {
List<SpanAdjuster> spanAdjusters = new ArrayList<>();
@Autowired(required = false)
List<FinishedSpanHandler> finishedSpanHandlers = new ArrayList<>();
List<SpanHandler> spanHandlers = new ArrayList<>();
@Autowired(required = false)
List<CurrentTraceContext.ScopeDecorator> scopeDecorators = new ArrayList<>();
@@ -119,8 +119,8 @@ public class TraceAutoConfiguration {
spanReporters != null ? spanReporters : Collections.emptyList()))
.traceId128Bit(sleuthProperties.isTraceId128())
.supportsJoin(sleuthProperties.isSupportsJoin());
for (FinishedSpanHandler finishedSpanHandlerFactory : this.finishedSpanHandlers) {
builder.addFinishedSpanHandler(finishedSpanHandlerFactory);
for (SpanHandler spanHandlerFactory : this.spanHandlers) {
builder.addSpanHandler(spanHandlerFactory);
}
for (TracingCustomizer customizer : this.tracingCustomizers) {
customizer.customize(builder);

View File

@@ -16,8 +16,8 @@
package org.springframework.cloud.sleuth.log;
import brave.internal.HexCodec;
import brave.internal.Nullable;
import brave.internal.codec.HexCodec;
import brave.propagation.CurrentTraceContext;
import brave.propagation.TraceContext;
import org.slf4j.Logger;

View File

@@ -16,7 +16,7 @@
package org.springframework.cloud.sleuth.propagation;
import brave.handler.FinishedSpanHandler;
import brave.handler.SpanHandler;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -41,8 +41,7 @@ public class SleuthTagPropagationAutoConfiguration {
protected static class TagPropagationConfiguration {
@Bean
public FinishedSpanHandler sleuthFinishedSpanHandler(
SleuthProperties sleuthProperties,
static SpanHandler tagPropagationSpanHandler(SleuthProperties sleuthProperties,
SleuthTagPropagationProperties tagPropagationProperties) {
return new TagPropagationFinishedSpanHandler(sleuthProperties,
tagPropagationProperties);

View File

@@ -31,8 +31,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
public class SleuthTagPropagationProperties {
/**
* Enables a {@link TagPropagationFinishedSpanHandler} that adds extra propagated
* fields to span tags.
* Enables a {@link TagPropagationSpanHandler} that adds extra propagated fields to
* span tags.
*/
private boolean enabled = true;

View File

@@ -17,7 +17,7 @@
package org.springframework.cloud.sleuth.sampler;
import brave.TracingCustomizer;
import brave.handler.FinishedSpanHandler;
import brave.handler.SpanHandler;
import brave.sampler.Sampler;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
@@ -48,7 +48,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
* <ul>
* <li>{@code zipkin2.reporter.Reporter} - what's used by Zipkin or others like
* Stackdriver</li>
* <li>{@link FinishedSpanHandler} - only accepts sampled data</li>
* <li>{@link SpanHandler} - only accepts sampled data</li>
* <li>{@link TracingCustomizer} - can configure one of the above</li>
* </ul>
*
@@ -69,8 +69,8 @@ final class SamplerCondition extends AnyNestedCondition {
}
@ConditionalOnBean(FinishedSpanHandler.class)
static final class FinishedSpanHandlerAvailable {
@ConditionalOnBean(SpanHandler.class)
static final class SpanHandlerAvailable {
}

View File

@@ -18,8 +18,8 @@ package org.springframework.cloud.sleuth;
import brave.Span;
import brave.Tracer;
import brave.handler.FinishedSpanHandler;
import brave.handler.MutableSpan;
import brave.handler.SpanHandler;
import brave.propagation.TraceContext;
import brave.sampler.Sampler;
import org.assertj.core.api.BDDAssertions;
@@ -42,8 +42,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Marcin Grzejszczak
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FinishedSpanHandlerTests.FinishedSpanHandlerAspectTestsConfig.class, webEnvironment = NONE)
public class FinishedSpanHandlerTests {
@SpringBootTest(classes = SpanHandlerTests.SpanHandlerAspectTestsConfig.class, webEnvironment = NONE)
public class SpanHandlerTests {
@Autowired
ArrayListSpanReporter reporter;
@@ -63,7 +63,7 @@ public class FinishedSpanHandlerTests {
@Configuration
@EnableAutoConfiguration(exclude = IntegrationAutoConfiguration.class)
static class FinishedSpanHandlerAspectTestsConfig {
static class SpanHandlerAspectTestsConfig {
@Bean
Sampler sampler() {
@@ -75,12 +75,13 @@ public class FinishedSpanHandlerTests {
return new ArrayListSpanReporter();
}
// tag::finishedSpanHandler[]
// tag::spanHandler[]
@Bean
FinishedSpanHandler handlerOne() {
return new FinishedSpanHandler() {
SpanHandler handlerOne() {
return new SpanHandler() {
@Override
public boolean handle(TraceContext traceContext, MutableSpan span) {
public boolean end(TraceContext traceContext, MutableSpan span,
Cause cause) {
span.name("foo");
return true; // keep this span
}
@@ -88,16 +89,17 @@ public class FinishedSpanHandlerTests {
}
@Bean
FinishedSpanHandler handlerTwo() {
return new FinishedSpanHandler() {
SpanHandler handlerTwo() {
return new SpanHandler() {
@Override
public boolean handle(TraceContext traceContext, MutableSpan span) {
public boolean end(TraceContext traceContext, MutableSpan span,
Cause cause) {
span.name(span.name() + " bar");
return true; // keep this span
}
};
}
// end::finishedSpanHandler[]
// end::spanHandler[]
}

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.sleuth.autoconfig;
import brave.propagation.B3Propagation;
import brave.propagation.B3Propagation.Format;
import brave.propagation.B3SinglePropagation;
import brave.propagation.ExtraFieldPropagation;
import brave.propagation.Propagation;
@@ -33,6 +34,12 @@ import org.springframework.context.support.GenericApplicationContext;
public class TraceAutoConfigurationPropagationCustomizationTests {
// Default for spring-messaging is on 2.2.x is MULTI, though 3.x it is
// SINGLE_NO_PARENT
// spring-cloud/spring-cloud-sleuth#1607
Propagation.Factory defaultB3Propagation = B3Propagation.newFactoryBuilder()
.injectFormat(Format.MULTI).build();
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TraceAutoConfiguration.class));
@@ -40,7 +47,7 @@ public class TraceAutoConfigurationPropagationCustomizationTests {
public void stillCreatesDefault() {
this.contextRunner.run((context) -> {
BDDAssertions.then(context.getBean(Propagation.Factory.class))
.isEqualTo(B3Propagation.FACTORY);
.isEqualTo(defaultB3Propagation);
});
}

View File

@@ -18,8 +18,8 @@ package org.springframework.cloud.sleuth.sampler;
import brave.Tracing;
import brave.TracingCustomizer;
import brave.handler.FinishedSpanHandler;
import brave.handler.MutableSpan;
import brave.handler.SpanHandler;
import brave.propagation.TraceContext;
import brave.sampler.CountingSampler;
import brave.sampler.Sampler;
@@ -52,12 +52,11 @@ public class SamplerAutoConfigurationTests {
}
@Test
public void should_use_CountingSampler_withFinishedSpanHandler() {
this.contextRunner.withUserConfiguration(WithFinishedSpanHandler.class)
.run((context -> {
final Sampler bean = context.getBean(Sampler.class);
BDDAssertions.then(bean).isInstanceOf(CountingSampler.class);
}));
public void should_use_CountingSampler_withSpanHandler() {
this.contextRunner.withUserConfiguration(WithSpanHandler.class).run((context -> {
final Sampler bean = context.getBean(Sampler.class);
BDDAssertions.then(bean).isInstanceOf(CountingSampler.class);
}));
}
@Test
@@ -127,13 +126,13 @@ public class SamplerAutoConfigurationTests {
}
@Configuration
static class WithFinishedSpanHandler {
static class WithSpanHandler {
@Bean
FinishedSpanHandler finishedSpanHandler() {
return new FinishedSpanHandler() {
SpanHandler spanHandler() {
return new SpanHandler() {
@Override
public boolean handle(TraceContext context, MutableSpan span) {
public boolean end(TraceContext context, MutableSpan span, Cause cause) {
return true;
}
};

View File

@@ -31,7 +31,7 @@
<name>spring-cloud-sleuth-dependencies</name>
<description>Spring Cloud Sleuth Dependencies</description>
<properties>
<brave.version>5.11.2</brave.version>
<brave.version>5.12.0</brave.version>
<brave.opentracing.version>0.33.13</brave.opentracing.version>
<grpc.spring.boot.version>3.0.1</grpc.spring.boot.version>
</properties>
@@ -77,6 +77,12 @@
<groupId>io.opentracing.brave</groupId>
<artifactId>brave-opentracing</artifactId>
<version>${brave.opentracing.version}</version>
<exclusions>
<exclusion>
<groupId>io.zipkin.brave</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- GRPC -->
<dependency>

View File

@@ -70,11 +70,6 @@
<artifactId>spring-cloud-sleuth-sample-test-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.zipkin2</groupId>
<artifactId>zipkin</artifactId>
<version>2.19.3</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@@ -105,10 +105,6 @@
<artifactId>awaitility</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.zipkin.zipkin2</groupId>
<artifactId>zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>

View File

@@ -78,6 +78,10 @@
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-kafka</artifactId>

View File

@@ -20,8 +20,8 @@ import java.util.List;
import brave.Span;
import brave.Tracing;
import brave.handler.FinishedSpanHandler;
import brave.handler.MutableSpan;
import brave.handler.SpanHandler;
import brave.propagation.TraceContext;
import brave.sampler.Sampler;
import okhttp3.mockwebserver.MockWebServer;
@@ -299,10 +299,11 @@ public class ZipkinAutoConfigurationTests {
protected static class HandlerHanldersConfig {
@Bean
FinishedSpanHandler handlerOne() {
return new FinishedSpanHandler() {
SpanHandler handlerOne() {
return new SpanHandler() {
@Override
public boolean handle(TraceContext traceContext, MutableSpan span) {
public boolean end(TraceContext traceContext, MutableSpan span,
Cause cause) {
span.name("foo");
return true; // keep this span
}
@@ -310,10 +311,11 @@ public class ZipkinAutoConfigurationTests {
}
@Bean
FinishedSpanHandler handlerTwo() {
return new FinishedSpanHandler() {
SpanHandler handlerTwo() {
return new SpanHandler() {
@Override
public boolean handle(TraceContext traceContext, MutableSpan span) {
public boolean end(TraceContext traceContext, MutableSpan span,
Cause cause) {
span.name(span.name() + " bar");
return true; // keep this span
}

View File

@@ -4,7 +4,7 @@
"https://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress files=".*/test/.*" checks="JavadocVariable"/>
<suppress files=".*FinishedSpanHandlerTests.*" checks="LineLengthCheck"/>
<suppress files=".*SpanHandlerTests.*" checks="LineLengthCheck"/>
<suppress files=".*GrpcTracingIntegrationTests.*" checks="LineLengthCheck"/>
<suppress files=".*IgnoreAutoConfiguredSkipPatternsIntegrationTests.*" checks="LineLengthCheck"/>
<suppress files=".*RestTemplateTraceAspectIntegrationTests.*" checks="LineLengthCheck"/>
@@ -15,7 +15,7 @@
<suppress files=".*SleuthNewSpanParserAnnotationDisableTests.*" checks="LineLengthCheck"/>
<suppress files=".*SpanAdjusterTests.*" checks="LineLengthCheck"/>
<suppress files=".*SpringDataInstrumentationTests.*" checks="LineLengthCheck"/>
<suppress files=".*TagPropagationFinishedSpanHandlerTest.*" checks="LineLengthCheck"/>
<suppress files=".*TagPropagationSpanHandlerTest.*" checks="LineLengthCheck"/>
<suppress files=".*TraceAsyncIntegrationTests.*" checks="LineLengthCheck"/>
<suppress files=".*TraceAutoConfigurationWithDisabledSleuthTests.*" checks="LineLengthCheck"/>
<suppress files=".*TraceFilterIntegrationTests.*" checks="LineLengthCheck"/>