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 GitHub
parent d4088dec9c
commit b1d39ce291
21 changed files with 82 additions and 74 deletions

View File

@@ -33,7 +33,7 @@
|spring.sleuth.messaging.rabbit.remote-service-name | rabbitmq |
|spring.sleuth.opentracing.enabled | true |
|spring.sleuth.propagation-keys | | List of fields that are referenced the same in-process as it is on the wire. For example, the name "x-vcap-request-id" would be set as-is including the prefix. <p> Note: {@code fieldName} will be implicitly lower-cased. @see brave.propagation.ExtraFieldPropagation.FactoryBuilder#addField(String)
|spring.sleuth.propagation.tag.enabled | true | Enables a {@link TagPropagationFinishedSpanHandler} that adds extra propagated fields to span tags.
|spring.sleuth.propagation.tag.enabled | true | Enables a {@link TagPropagationSpanHandler} that adds extra propagated fields to span tags.
|spring.sleuth.propagation.tag.whitelisted-keys | | A list of keys to be put from extra propagation fields to span tags.
|spring.sleuth.reactor.decorate-on-each | true | When true decorates on each operator, will be less performing, but logging will always contain the tracing entries in each operator. When false decorates on last operator, will be more performing, but logging might not always contain the tracing entries.
|spring.sleuth.reactor.enabled | true | When true enables instrumentation for reactor.

View File

@@ -971,17 +971,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::{project-root}//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).