From 136ddc23e6cd701ee0163b5218af75d8b92d6024 Mon Sep 17 00:00:00 2001 From: buildmaster Date: Wed, 28 Feb 2018 18:25:20 +0000 Subject: [PATCH] Sync docs from master to gh-pages --- multi/multi__additional_resources.html | 2 +- multi/multi__current_span.html | 18 +- multi/multi__current_tracing_component.html | 12 +- multi/multi__features.html | 101 +- multi/multi__instrumentation.html | 17 +- multi/multi__integrations.html | 125 +- multi/multi__introduction.html | 130 +- ...ulti__managing_spans_with_annotations.html | 92 +- multi/multi__naming_spans.html | 16 +- multi/multi__propagation.html | 79 +- multi/multi__running_examples.html | 3 +- multi/multi__sampling.html | 39 +- multi/multi__sending_spans_to_zipkin.html | 13 +- multi/multi__span_lifecycle.html | 30 +- multi/multi__zipkin_stream_span_consumer.html | 9 +- multi/multi_spring-cloud-sleuth.html | 2 +- single/spring-cloud-sleuth.html | 653 ++++----- spring-cloud-sleuth.xml | 1238 ++++++++--------- 18 files changed, 1175 insertions(+), 1404 deletions(-) diff --git a/multi/multi__additional_resources.html b/multi/multi__additional_resources.html index 657916079..ec3f1930c 100644 --- a/multi/multi__additional_resources.html +++ b/multi/multi__additional_resources.html @@ -1,3 +1,3 @@ - 2. Additional resources

2. Additional resources

Marcin Grzejszczak talking about Spring Cloud Sleuth and Zipkin

click here to see the video

\ No newline at end of file + 2. Additional Resources

2. Additional Resources

You can watch a video of Marcin Grzejszczak talking about Spring Cloud Sleuth and Zipkin:

click here to see the video

\ No newline at end of file diff --git a/multi/multi__current_span.html b/multi/multi__current_span.html index 9b52c3f27..53cf03a39 100644 --- a/multi/multi__current_span.html +++ b/multi/multi__current_span.html @@ -1,20 +1,12 @@ - 7. Current Span

7. Current Span

Brave supports a "current span" concept which represents the in-flight -operation. Tracer.currentSpan() can be used to add custom tags to a -span and Tracer.nextSpan() can be used to create a child of whatever -is in-flight.

7.1 Setting a span in scope manually

When writing new instrumentation, it is important to place a span you -created in scope as the current span. Not only does this allow users to -access it with Tracer.currentSpan(), but it also allows customizations -like SLF4J MDC to see the current trace IDs.

Tracer.withSpanInScope(Span) facilitates this and is most conveniently -employed via the try-with-resources idiom. Whenever external code might -be invoked (such as proceeding an interceptor or otherwise), place the -span in scope like this.

try (SpanInScope ws = tracer.withSpanInScope(span)) {
+   7. Current Span

7. Current Span

Brave supports a "current span" concept which represents the in-flight operation. +You can use Tracer.currentSpan() to add custom tags to a span and Tracer.nextSpan() to create a child of whatever is in-flight.

7.1 Setting a span in scope manually

When writing new instrumentation, it is important to place a span you created in scope as the current span. +Not only does doing so let users access it with Tracer.currentSpan(), but it also allows customizations such as SLF4J MDC to see the current trace IDs.

Tracer.withSpanInScope(Span) facilitates this and is most conveniently employed by using the try-with-resources idiom. +Whenever external code might be invoked (such as proceeding an interceptor or otherwise), place the span in scope, as shown in the following example:

try (SpanInScope ws = tracer.withSpanInScope(span)) {
   return inboundRequest.invoke();
 } finally { // note the scope is independent of the span
   span.finish();
-}

In edge cases, you may need to clear the current span temporarily. For -example, launching a task that should not be associated with the current -request. To do this, simply pass null to withSpanInScope.

try (SpanInScope cleared = tracer.withSpanInScope(null)) {
+}

In edge cases, you may need to clear the current span temporarily (for example, launching a task that should not be associated with the current request). To do tso, pass null to withSpanInScope, as shown in the following example:

try (SpanInScope cleared = tracer.withSpanInScope(null)) {
   startBackgroundThread();
 }
\ No newline at end of file diff --git a/multi/multi__current_tracing_component.html b/multi/multi__current_tracing_component.html index 7b5a68b66..75511b6ac 100644 --- a/multi/multi__current_tracing_component.html +++ b/multi/multi__current_tracing_component.html @@ -1,9 +1,7 @@ - 6. Current Tracing Component

6. Current Tracing Component

Brave supports a "current tracing component" concept which should only -be used when you have no other means to get a reference. This was made -for JDBC connections, as they often initialize prior to the tracing -component.

The most recent tracing component instantiated is available via -Tracing.current(). You there’s also a shortcut to get only the tracer -via Tracing.currentTracer(). If you use either of these methods, do -noot cache the result. Instead, look them up each time you need them.

\ No newline at end of file + 6. Current Tracing Component

6. Current Tracing Component

Brave supports a "current tracing component" concept, which should only be used when you have no other way to get a reference. +This was made for JDBC connections, as they often initialize prior to the tracing component.

The most recent tracing component instantiated is available through Tracing.current(). +You can also use Tracing.currentTracer() to get only the tracer. +If you use either of these methods, do not cache the result. +Instead, look them up each time you need them.

\ No newline at end of file diff --git a/multi/multi__features.html b/multi/multi__features.html index aa66022d0..014458a93 100644 --- a/multi/multi__features.html +++ b/multi/multi__features.html @@ -1,32 +1,30 @@ - 3. Features

3. Features

  • Adds trace and span ids to the Slf4J MDC, so you can extract all the logs from a given trace or span in a log aggregator. Example logs:

    2016-02-02 15:30:57.902  INFO [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ...
    +   3. Features

    3. Features

    • Adds trace and span IDs to the Slf4J MDC, so you can extract all the logs from a given trace or span in a log aggregator, as shown in the following example logs:

      2016-02-02 15:30:57.902  INFO [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ...
       2016-02-02 15:30:58.372 ERROR [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ...
      -2016-02-02 15:31:01.936  INFO [bar,46ab0d418373cbc9,46ab0d418373cbc9,false] 23030 --- [nio-8081-exec-4] ...

      notice the [appname,traceId,spanId,exportable] entries from the MDC:

      • spanId - the id of a specific operation that took place
      • appname - the name of the application that logged the span
      • traceId - the id of the latency graph that contains the span
      • exportable - whether the log should be exported to Zipkin or not. When would you like the span not to be -exportable? In the case in which you want to wrap some operation in a Span and have it written to the logs -only.
    • Provides an abstraction over common distributed tracing data models: traces, spans (forming a DAG), annotations, -key-value annotations. Loosely based on HTrace, but Zipkin (Dapper) compatible.
    • Sleuth records timing information to aid in latency analysis. Using sleuth, you can pinpoint causes of -latency in your applications. Sleuth is written to not log too much, and to not cause your production application to crash.

      • propagates structural data about your call-graph in-band, and the rest out-of-band.
      • includes opinionated instrumentation of layers such as HTTP
      • includes sampling policy to manage volume
      • can report to a Zipkin system for query and visualization
    • Instruments common ingress and egress points from Spring applications (servlet filter, async endpoints, -rest template, scheduled actions, message channels, zuul filters, feign client).
    • Sleuth includes default logic to join a trace across http or messaging boundaries. For example, http propagation -works via Zipkin-compatible request headers. This propagation logic is defined and customized via -SpanInjector and SpanExtractor implementations.
    • Sleuth gives you the possibility to propagate context (also known as baggage) between processes. That means that if you set on a Span -a baggage element then it will be sent downstream either via HTTP or messaging to other processes.
    • Provides a way to create / continue spans and add tags and logs via annotations.
    • If spring-cloud-sleuth-zipkin is on the classpath then the app will generate and collect Zipkin-compatible traces. -By default it sends them via HTTP to a Zipkin server on localhost (port 9411). -Configure the location of the service using spring.zipkin.baseUrl.

      • If you depend on spring-rabbit or spring-kafka your app will send traces to a broker instead of http.
      • Note: spring-cloud-sleuth-stream is deprecated and should no longer be used.
    • Spring Cloud Sleuth is OpenTracing compatible
    [Important]Important

    If using Zipkin, configure the percentage of spans exported using spring.sleuth.sampler.percentage -(default 0.1, i.e. 10%). Otherwise you might think that Sleuth is not working cause it’s omitting some spans.

    [Note]Note

    the SLF4J MDC is always set and logback users will immediately see the trace and span ids in logs per the example - above. Other logging systems have to configure their own formatter to get the same result. The default is - logging.pattern.level set to %5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] - (this is a Spring Boot feature for logback users). - This means that if you’re not using SLF4J this pattern WILL NOT be automatically applied.

    3.1 Introduction to Brave

    [Important]Important

    Starting with version 2.0.0 Spring Cloud Sleuth uses +2016-02-02 15:31:01.936 INFO [bar,46ab0d418373cbc9,46ab0d418373cbc9,false] 23030 --- [nio-8081-exec-4] ...

    Notice the [appname,traceId,spanId,exportable] entries from the MDC:

    • spanId: The ID of a specific operation that took place.
    • appname: The name of the application that logged the span.
    • traceId: The ID of the latency graph that contains the span.
    • exportable: Whether the log should be exported to Zipkin. +When would you like the span not to be exportable? +When you want to wrap some operation in a Span and have it written to the logs only.
  • Provides an abstraction over common distributed tracing data models: traces, spans (forming a DAG), annotations, and key-value annotations. +Spring Cloud Slwuth is loosely based on HTrace but is compatible with Zipkin (Dapper).
  • Sleuth records timing information to aid in latency analysis. +By using sleuth, you can pinpoint causes of latency in your applications.
  • Sleuth is written to not log too much and to not cause your production application to crash. +To that end, Sleuth:

    • Propagates structural data about your call graph in-band and the rest out-of-band.
    • Includes opinionated instrumentation of layers such as HTTP.
    • Includes a sampling policy to manage volume.
    • Can report to a Zipkin system for query and visualization.
  • Instruments common ingress and egress points from Spring applications (servlet filter, async endpoints, rest template, scheduled actions, message channels, Zuul filters, and Feign client).
  • Sleuth includes default logic to join a trace across HTTP or messaging boundaries. +For example, HTTP propagation works over Zipkin-compatible request headers. +This propagation logic is defined and customized through SpanInjector and SpanExtractor implementations.
  • Sleuth can propagate context (also known as baggage) between processes. +Consequently, if you set a baggage element on a Span, it is sent downstream to other processes over either HTTP or messaging.
  • Provides a way to create or continue spans and add tags and logs through annotations.
  • If spring-cloud-sleuth-zipkin is on the classpath, the app generates and collects Zipkin-compatible traces. +By default, it sends them over HTTP to a Zipkin server on localhost (port 9411). +You can configure the location of the service by setting spring.zipkin.baseUrl.

    • If you depend on spring-rabbit or spring-kafka, your app sends traces to a broker instead of HTTP. +**
  • [Caution]Caution

    spring-cloud-sleuth-stream is deprecated and should no longer be used.

    [Important]Important

    If you use Zipkin, configure the percentage of spans exported by setting spring.sleuth.sampler.percentage +(default: 0.1, which is 10 percent). Otherwise, you might think that Sleuth is not working be cause it omits some spans.

    [Note]Note

    The SLF4J MDC is always set and logback users immediately see the trace and span IDs in logs per the example +shown earlier. +Other logging systems have to configure their own formatter to get the same result. +The default is as follows: +logging.pattern.level set to %5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] +(this is a Spring Boot feature for logback users). +If you do not use SLF4J, this pattern is NOT automatically applied.

    3.1 Introduction to Brave

    [Important]Important

    Starting with version 2.0.0, Spring Cloud Sleuth uses Brave as the tracing library. -For your convenience we’re embedding part of the Brave’s docs here.

    Brave is a library used to capture and report latency information about -distributed operations to Zipkin. Most users won’t use Brave directly, -rather libraries or frameworks than employ Brave on their behalf.

    This module includes tracer creates and joins spans that model the -latency of potentially distributed work. It also includes libraries to -propagate the trace context over network boundaries, for example, via -http headers.

    3.1.1 Tracing

    Most importantly, you need a brave.Tracer, configured to [report to Zipkin] -(https://github.com/openzipkin/zipkin-reporter-java).

    Here’s an example setup that sends trace data (spans) to Zipkin over -http (as opposed to Kafka).

    class MyClass {
    +For your convenience, we embed part of the Brave’s docs here.

    Brave is a library used to capture and report latency information about distributed operations to Zipkin. +Most users do not use Brave directly. They use libraries or frameworks rather than employ Brave on their behalf.

    This module includes a tracer that creates and joins spans that model the latency of potentially distributed work. +It also includes libraries to propagate the trace context over network boundaries (for example, with HTTP headers).

    3.1.1 Tracing

    Most importantly, you need a brave.Tracer, configured to report to Zipkin.

    The following example setup sends trace data (spans) to Zipkin over HTTP (as opposed to Kafka):

    class MyClass {
     
         private final Tracer tracer;
     
    @@ -39,46 +37,37 @@ http (as opposed to Kafka).

    "encode").start();
             // ...
         }
    -}
    [Important]Important

    If your span contains a name greater than 50 chars, then that name will -be truncated to 50 chars. Your names have to be explicit and concrete. Big names lead to -latency issues and sometimes even thrown exceptions.

    3.1.2 Tracing

    The tracer creates and joins spans that model the latency of potentially -distributed work. It can employ sampling to reduce overhead in process -or to reduce the amount of data sent to Zipkin.

    Spans returned by a tracer report data to Zipkin when finished, or do -nothing if unsampled. After starting a span, you can annotate events of -interest or add tags containing details or lookup keys.

    Spans have a context which includes trace identifiers that place it at -the correct spot in the tree representing the distributed operation.

    3.1.3 Local Tracing

    When tracing local code, just run it inside a span.

    Span span = tracer.newTrace().name("encode").start();
    +}
    [Important]Important

    If your span contains a name longer than 50 chars, then that name is truncated to 50 chars. +Your names have to be explicit and concrete. +Big names lead to latency issues and sometimes even thrown exceptions.

    The tracer creates and joins spans that model the latency of potentially distributed work. +It can employ sampling to reduce overhead during the process, to reduce the amount of data sent to Zipkin, or both.

    Spans returned by a tracer report data to Zipkin when finished or do nothing if unsampled. +After starting a span, you can annotate events of interest or add tags containing details or lookup keys.

    Spans have a context that includes trace identifiers that place the span at the correct spot in the tree representing the distributed operation.

    3.1.2 Local Tracing

    When tracing local code, you can run it inside a span, as shown in the following example:

    Span span = tracer.newTrace().name("encode").start();
     try {
       doSomethingExpensive();
     } finally {
       span.finish();
    -}

    In the above example, the span is the root of the trace. In many cases, -you will be a part of an existing trace. When this is the case, call -newChild instead of newTrace

    Span span = tracer.newChild(root.context()).name("encode").start();
    +}

    In the preceding example, the span is the root of the trace. +In many cases, the span is part of an existing trace. +When this is the case, call newChild instead of newTrace, as shown in the following example:

    Span span = tracer.newChild(root.context()).name("encode").start();
     try {
       doSomethingExpensive();
     } finally {
       span.finish();
    -}

    3.1.4 Customizing spans

    Once you have a span, you can add tags to it, which can be used as lookup -keys or details. For example, you might add a tag with your runtime -version.

    span.tag("clnt/finagle.version", "6.36.0");

    When exposing the ability to customize spans to third parties, prefer -brave.SpanCustomizer as opposed to brave.Span. The former is simpler to -understand and test, and doesn’t tempt users with span lifecycle hooks.

    interface MyTraceCallback {
    +}

    3.1.3 Customizing Spans

    Once you have a span, you can add tags to it. +The tags can be used as lookup keys or details. +For example, you might add a tag with your runtime version, as shown in the following example:

    span.tag("clnt/finagle.version", "6.36.0");

    When exposing the ability to customize spans to third parties, prefer brave.SpanCustomizer as opposed to brave.Span. +The former is simpler to understand and test and does not tempt users with span lifecycle hooks.

    interface MyTraceCallback {
       void request(Request request, SpanCustomizer customizer);
    -}

    Since brave.Span implements brave.SpanCustomizer, it is just as easy for you -to pass to users.

    Ex.

    for (MyTraceCallback callback : userCallbacks) {
    +}

    Since brave.Span implements brave.SpanCustomizer, you can pass it to users, as shown in the following example:

    for (MyTraceCallback callback : userCallbacks) {
       callback.request(request, span);
    -}

    3.1.5 Implicitly looking up the current span

    Sometimes you won’t know if a trace is in progress or not, and you don’t -want users to do null checks. brave.CurrentSpanCustomizer adds to any -span that’s in progress or drops data accordingly.

    Ex.

    // user code can then inject this without a chance of it being null.
    +}

    3.1.4 Implicitly Looking up the Current Span

    Sometimes, you do not know if a trace is in progress or not, and you do not want users to do null checks. +brave.CurrentSpanCustomizer handles this problem by adding data to any span that’s in progress or drops, as shown in the following example:

    Ex.

    // The user code can then inject this without a chance of it being null.
     @Autowire SpanCustomizer span;
     
     void userCode() {
       span.annotate("tx.started");
       ...
    -}

    3.1.6 RPC tracing

    Check for instrumentation written here -and Zipkin’s list -before rolling your own RPC instrumentation!

    RPC tracing is often done automatically by interceptors. Under the scenes, -they add tags and events that relate to their role in an RPC operation.

    Here’s an example of a client span:

    // before you send a request, add metadata that describes the operation
    +}

    3.1.5 RPC tracing

    [Tip]Tip

    Check for instrumentation written here and Zipkin’s list before rolling your own RPC instrumentation.

    RPC tracing is often done automatically by interceptors. Behind the scenes, they add tags and events that relate to their role in an RPC operation.

    The following example shows how to add a client span:

    // before you send a request, add metadata that describes the operation
     span = tracer.newTrace().name("get").type(CLIENT);
     span.tag("clnt/finagle.version", "6.36.0");
     span.tag(TraceKeys.HTTP_PATH, "/api");
    @@ -95,10 +84,10 @@ span.annotate(Constants.WIRE_SEND);
     span.annotate(Constants.WIRE_RECV);
     
     // when the response is complete, finish the span
    -span.finish();

    One-Way tracing

    Sometimes you need to model an asynchronous operation, where there is a -request, but no response. In normal RPC tracing, you use span.finish() -which indicates the response was received. In one-way tracing, you use -span.flush() instead, as you don’t expect a response.

    Here’s how a client might model a one-way operation

    // start a new span representing a client request
    +span.finish();

    One-Way tracing

    Sometimes, you need to model an asynchronous operation where there is a +request but no response. In normal RPC tracing, you use span.finish() +to indicate that the response was received. In one-way tracing, you use +span.flush() instead, as you do not expect a response.

    The following example shows how a client might model a one-way operation:

    // start a new span representing a client request
     oneWaySend = tracer.newSpan(parent).kind(Span.Kind.CLIENT);
     
     // Add the trace context to the request, so it can be propagated in-band
    @@ -109,7 +98,7 @@ tracing.propagation().injector(Request::addHeader)
     request.execute();
     
     // start the client side and flush instead of finish
    -oneWaySend.start().flush();

    And here’s how a server might handle this..

    // pull the context out of the incoming request
    +oneWaySend.start().flush();

    The following example shows how a server might handle a one-way operation:

    // pull the context out of the incoming request
     extractor = tracing.propagation().extractor(Request::getHeader);
     
     // convert that context to a span which you can name and add tags to
    @@ -123,4 +112,4 @@ oneWayReceive.start().flush();
     
     // you should not modify this span anymore as it is complete. However,
     // you can create children to represent follow-up work.
    -next = tracer.newSpan(oneWayReceive.context()).name("step2").start();

    Note The above propagation logic is a simplified version of our [http handlers](https://github.com/openzipkin/sleuth/tree/master/instrumentation/http#http-server).

    There’s a working example of a one-way span [here](src/test/java/sleuth/features/async/OneWaySpanTest.java).

    \ No newline at end of file +next = tracer.newSpan(oneWayReceive.context()).name("step2").start();
    [Note]Note

    The propagation logic shown in the preceding example is a simplified version of our [http handlers](https://github.com/openzipkin/sleuth/tree/master/instrumentation/http#http-server).

    You can find a working example of a one-way span [here](src/test/java/sleuth/features/async/OneWaySpanTest.java).

\ No newline at end of file diff --git a/multi/multi__instrumentation.html b/multi/multi__instrumentation.html index 8d476ff1e..82c1d45de 100644 --- a/multi/multi__instrumentation.html +++ b/multi/multi__instrumentation.html @@ -1,15 +1,6 @@ - 8. Instrumentation

8. Instrumentation

Spring Cloud Sleuth instruments all your Spring application -automatically, so you shouldn’t have to do anything to activate -it. The instrumentation is added using a variety of technologies -according to the stack that is available, e.g. for a servlet web -application we use a Filter, and for Spring Integration we use -ChannelInterceptors.

You can customize the keys used in span tags. To limit the volume of -span data, by default an HTTP request will be tagged only with a -handful of metadata like the status code, host and URL. You can add -request headers by configuring spring.sleuth.keys.http.headers (a -list of header names).

[Note]Note

Remember that tags are only collected and exported if there is a -Sampler that allows it (by default there is not, so there is no -danger of accidentally collecting too much data without configuring -something).

\ No newline at end of file + 8. Instrumentation

8. Instrumentation

Spring Cloud Sleuth automatically instruments all your Spring applications, so you should not have to do anything to activate it. +The instrumentation is added by using a variety of technologies according to the stack that is available. For example, for a servlet web application, we use a Filter, and, for Spring Integration, we use ChannelInterceptors.

You can customize the keys used in span tags. +To limit the volume of span data, an HTTP request is, by default, tagged only with a handful of metadata, such as the status code, the host, and the URL. +You can add request headers by configuring spring.sleuth.keys.http.headers (a list of header names).

[Note]Note

Tags are collected and exported only if there is a Sampler that allows it. By default, there is no such Sampler, to ensure that there is no danger of accidentally collecting too much data without configuring something).

\ No newline at end of file diff --git a/multi/multi__integrations.html b/multi/multi__integrations.html index 5a872ef7d..af7895a33 100644 --- a/multi/multi__integrations.html +++ b/multi/multi__integrations.html @@ -1,8 +1,8 @@ - 15. Integrations

15. Integrations

15.1 OpenTracing

Spring Cloud Sleuth is OpenTracing compatible. If you have -OpenTracing on the classpath we will automatically register the OpenTracing -Tracer bean. If you wish to disable this just set spring.sleuth.opentracing.enabled to false

15.2 Runnable and Callable

If you’re wrapping your logic in Runnable or Callable it’s enough to wrap those classes in their Sleuth representative.

Example for Runnable:

Runnable runnable = new Runnable() {
+   14. Integrations

14. Integrations

14.1 OpenTracing

Spring Cloud Sleuth is compatible with OpenTracing. +If you have OpenTracing on the classpath, we automatically register the OpenTracing Tracer bean. +If you wish to disable this, set spring.sleuth.opentracing.enabled to false

14.2 Runnable and Callable

If you wrap your logic in Runnable or Callable, you can wrap those classes in their Sleuth representative, as shown in the following example for Runnable:

Runnable runnable = new Runnable() {
 	@Override
 	public void run() {
 		// do some work
@@ -18,7 +18,7 @@ Runnable traceRunnable = "calculateTax");
 // Wrapping `Runnable` with `Tracing`. That way the current span will be available
 // in the thread of `Runnable`
-Runnable traceRunnableFromTracer = tracing.currentTraceContext().wrap(runnable);

Example for Callable:

Callable<String> callable = new Callable<String>() {
+Runnable traceRunnableFromTracer = tracing.currentTraceContext().wrap(runnable);

The following example shows how to do so for Callable:

Callable<String> callable = new Callable<String>() {
 	@Override
 	public String call() throws Exception {
 		return someLogic();
@@ -34,48 +34,47 @@ Callable<String> traceCallable = "calculateTax");
 // Wrapping `Callable` with `Tracing`. That way the current span will be available
 // in the thread of `Callable`
-Callable<String> traceCallableFromTracer = tracing.currentTraceContext().wrap(callable);

That way you will ensure that a new Span is created and closed for each execution.

15.3 Hystrix

15.3.1 Custom Concurrency Strategy

We’re registering a custom HystrixConcurrencyStrategy -that wraps all Callable instances into their Sleuth representative - -the TraceCallable. The strategy either starts or continues a span depending on the fact whether tracing was already going -on before the Hystrix command was called. To disable the custom Hystrix Concurrency Strategy set the spring.sleuth.hystrix.strategy.enabled to false.

15.3.2 Manual Command setting

Assuming that you have the following HystrixCommand:

HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {
+Callable<String> traceCallableFromTracer = tracing.currentTraceContext().wrap(callable);

That way, you ensure that a new span is created and closed for each execution.

14.3 Hystrix

14.3.1 Custom Concurrency Strategy

We register a custom HystrixConcurrencyStrategy called TraceCallable that wraps all Callable instances in their Sleuth representative. +The strategy either starts or continues a span, depending on whether tracing was already going on before the Hystrix command was called. +To disable the custom Hystrix Concurrency Strategy, set the spring.sleuth.hystrix.strategy.enabled to false.

14.3.2 Manual Command setting

Assume that you have the following HystrixCommand:

HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {
 	@Override
 	protected String run() throws Exception {
 		return someLogic();
 	}
-};

In order to pass the tracing information you have to wrap the same logic in the Sleuth version of the HystrixCommand which is the -TraceCommand:

TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, traceKeys, setter) {
+};

To pass the tracing information, you have to wrap the same logic in the Sleuth version of the HystrixCommand, which is called +TraceCommand, as shown in the following example:

TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, traceKeys, setter) {
 	@Override
 	public String doRun() throws Exception {
 		return someLogic();
 	}
-};

15.4 RxJava

We’re registering a custom RxJavaSchedulersHook -that wraps all Action0 instances into their Sleuth representative - -the TraceAction. The hook either starts or continues a span depending on the fact whether tracing was already going -on before the Action was scheduled. To disable the custom RxJavaSchedulersHook set the spring.sleuth.rxjava.schedulers.hook.enabled to false.

You can define a list of regular expressions for thread names, for which you don’t want a Span to be created. Just provide a comma separated list -of regular expressions in the spring.sleuth.rxjava.schedulers.ignoredthreads property.

15.5 HTTP integration

Features from this section can be disabled by providing the spring.sleuth.web.enabled property with value equal to false.

15.5.1 HTTP Filter

Via the TraceFilter all sampled incoming requests result in creation of a Span. That Span’s name is http: + the path to which - the request was sent. E.g. if the request was sent to /foo/bar then the name will be http:/foo/bar. You can configure which URIs you would - like to skip via the spring.sleuth.web.skipPattern property. If you have ManagementServerProperties on classpath then - its value of contextPath gets appended to the provided skip pattern. If you want to reuse the - Sleuth’s default skip patterns and just append your own, pass those patterns via - the spring.sleuth.web.additionalSkipPattern.

15.5.2 HandlerInterceptor

Since we want the span names to be precise we’re using a TraceHandlerInterceptor that either wraps an - existing HandlerInterceptor or is added directly to the list of existing HandlerInterceptors. The - TraceHandlerInterceptor adds a special request attribute to the given HttpServletRequest. If the - the TraceFilter doesn’t see this attribute set it will create a "fallback" span which is an additional - span created on the server side so that the trace is presented properly in the UI. Seeing that most likely - signifies that there is a missing instrumentation. In that case please file an issue in Spring Cloud Sleuth.

15.5.3 Async Servlet support

If your controller returns a Callable or a WebAsyncTask Spring Cloud Sleuth will continue the existing span instead of creating a new one.

15.5.4 WebFlux support

Via the TraceWebFilter all sampled incoming requests result in creation of a Span. That Span’s name is http: + the path to which - the request was sent. E.g. if the request was sent to /foo/bar then the name will be http:/foo/bar. You can configure which URIs you would - like to skip via the spring.sleuth.web.skipPattern property. If you have ManagementServerProperties on classpath then - its value of contextPath gets appended to the provided skip pattern. If you want to reuse the - Sleuth’s default skip patterns and just append your own, pass those patterns via - the spring.sleuth.web.additionalSkipPattern.

15.6 HTTP client integration

15.6.1 Synchronous Rest Template

We’re injecting a RestTemplate interceptor that ensures that all the tracing information is passed to the requests. Each time a -call is made a new Span is created. It gets closed upon receiving the response. In order to block the synchronous RestTemplate features -just set spring.sleuth.web.client.enabled to false.

[Important]Important

You have to register RestTemplate as a bean so that the interceptors will get injected. -If you create a RestTemplate instance with a new keyword then the instrumentation WILL NOT work.

15.6.2 Asynchronous Rest Template

[Important]Important

Starting with Sleuth 2.0.0 we no longer register -a bean of AsyncRestTemplate type. It’s up to you to create such -a bean. Then we will instrument it.

To block the AsyncRestTemplate features set spring.sleuth.web.async.client.enabled to false. -To disable creation of the default TraceAsyncClientHttpRequestFactoryWrapper set spring.sleuth.web.async.client.factory.enabled -to false. If you don’t want to create AsyncRestClient at all set spring.sleuth.web.async.client.template.enabled to false.

Multiple Asynchronous Rest Templates

Sometimes you need to use multiple implementations of Asynchronous Rest Template. In the following snippet you -can see an example of how to set up such a custom AsyncRestTemplate.

@Configuration
+};

14.4 RxJava

We registering a custom RxJavaSchedulersHook that wraps all Action0 instances in their Sleuth representative, which is called TraceAction. +The hook either starts or continues a span, depending on whether tracing was already going on before the Action was scheduled. +To disable the custom RxJavaSchedulersHook, set the spring.sleuth.rxjava.schedulers.hook.enabled to false.

You can define a list of regular expressions for thread names for which you do not want spans to be created. +To do so, provide a comma-separated list of regular expressions in the spring.sleuth.rxjava.schedulers.ignoredthreads property.

14.5 HTTP integration

Features from this section can be disabled by setting the spring.sleuth.web.enabled property with value equal to false.

14.5.1 HTTP Filter

Through the TraceFilter, all sampled incoming requests result in creation of a Span. +That Span’s name is http: + the path to which the request was sent. +For example, if the request was sent to /this/this then the name will be http:/this/that. +You can configure which URIs you would like to skip by setting the spring.sleuth.web.skipPattern property. +If you have ManagementServerProperties on classpath, its value of contextPath gets appended to the provided skip pattern. +If you want to reuse the Sleuth’s default skip patterns and just append your own, pass those patterns by using the spring.sleuth.web.additionalSkipPattern.

14.5.2 HandlerInterceptor

Since we want the span names to be precise, we use a TraceHandlerInterceptor that either wraps an existing HandlerInterceptor or is added directly to the list of existing HandlerInterceptors. +The TraceHandlerInterceptor adds a special request attribute to the given HttpServletRequest. +If the the TraceFilter does not see this attribute, it creates a "fallback" span, which is an additional span created on the server side so that the trace is presented properly in the UI. +If that happens, there is probably missing instrumentation. +In that case, please file an issue in Spring Cloud Sleuth.

14.5.3 Async Servlet support

If your controller returns a Callable or a WebAsyncTask, Spring Cloud Sleuth continues the existing span instead of creating a new one.

14.5.4 WebFlux support

Through TraceWebFilter, all sampled incoming requests result in creation of a Span. +That Span’s name is http: + the path to which the request was sent. +For example, if the request was sent to /this/that, the name is http:/this/that. +You can configure which URIs you would like to skip by using the spring.sleuth.web.skipPattern property. +If you have ManagementServerProperties on the classpath, its value of contextPath gets appended to the provided skip pattern. +If you want to reuse Sleuth’s default skip patterns and append your own, pass those patterns by using the spring.sleuth.web.additionalSkipPattern.

14.6 HTTP Client Integration

14.6.1 Synchronous Rest Template

We inject a RestTemplate interceptor to ensure that all the tracing information is passed to the requests. +Each time a call is made, a new Span is created. +It gets closed upon receiving the response. +To block the synchronous RestTemplate features, set spring.sleuth.web.client.enabled to false.

[Important]Important

You have to register RestTemplate as a bean so that the interceptors get injected. +If you create a RestTemplate instance with a new keyword, the instrumentation does NOT work.

14.6.2 Asynchronous Rest Template

[Important]Important

Starting with Sleuth 2.0.0, we no longer register a bean of AsyncRestTemplate type. +It is up to you to create such a bean. +Then we instrument it.

To block the AsyncRestTemplate features, set spring.sleuth.web.async.client.enabled to false. +To disable creation of the default TraceAsyncClientHttpRequestFactoryWrapper, set spring.sleuth.web.async.client.factory.enabled +to false. +If you do not want to create AsyncRestClient at all, set spring.sleuth.web.async.client.template.enabled to false.

Multiple Asynchronous Rest Templates

Sometimes you need to use multiple implementations of the Asynchronous Rest Template. +In the following snippet, you can see an example of how to set up such a custom AsyncRestTemplate:

@Configuration
 @EnableAutoConfiguration
 static class Config {
 
@@ -95,34 +94,30 @@ can see an example of how to set up such a custom AsyncRes
 		//CUSTOMIZE HERE
 		return factory;
 	}
-}

15.6.3 WebClient

We inject a ExchangeFilterFunction implementation that creates a span and via on success and on -error callbacks takes care of closing client side spans.

[Important]Important

You have to register WebClient as a bean so that the tracing instrumention gets applied. -If you create a WebClient instance with a new keyword then the instrumentation WILL NOT work.

15.6.4 Traverson

If you’re using the Traverson library -it’s enough for you to inject a RestTemplate as a bean into your Traverson object. Since RestTemplate -is already intercepted, you will get full support of tracing in your client. Below you can find a pseudo code -of how to do that:

@Autowired RestTemplate restTemplate;
+}

14.6.3 WebClient

We inject a ExchangeFilterFunction implementation that creates a span and, through on-success and on-error callbacks, takes care of closing client-side spans.

[Important]Important

You have to register WebClient as a bean so that the tracing instrumentation gets applied. +If you create a WebClient instance with a new keyword, the instrumentation does NOT work.

14.6.4 Traverson

If you use the Traverson library, you can inject a RestTemplate as a bean into your Traverson object. +Since RestTemplate is already intercepted, you get full support for tracing in your client. The following pseudo code +shows how to do that:

@Autowired RestTemplate restTemplate;
 
 Traverson traverson = new Traverson(URI.create("http://some/address"),
     MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8).setRestOperations(restTemplate);
-// use Traverson

15.7 Feign

By default Spring Cloud Sleuth provides integration with feign via the TraceFeignClientAutoConfiguration. You can disable it entirely -by setting spring.sleuth.feign.enabled to false. If you do so then no Feign related instrumentation will take place.

Part of Feign instrumentation is done via a FeignBeanPostProcessor. You can disable it by providing the spring.sleuth.feign.processor.enabled equal to false. -If you set it like this then Spring Cloud Sleuth will not instrument any of your custom Feign components. All the default instrumentation -however will be still there.

15.8 Asynchronous communication

15.8.1 @Async annotated methods

In Spring Cloud Sleuth we’re instrumenting async related components so that the tracing information is passed between threads. -You can disable this behaviour by setting the value of spring.sleuth.async.enabled to false.

If you annotate your method with @Async then we’ll automatically create a new Span with the following characteristics:

  • if the method is annotated with @SpanName then the value of the annotation will be the Span’s name
  • if the method is not annotated with @SpanName the Span name will be the annotated method name
  • the Span will be tagged with that method’s class name and the method name too

15.8.2 @Scheduled annotated methods

In Spring Cloud Sleuth we’re instrumenting scheduled method execution so that the tracing information is passed between threads. You can disable this behaviour -by setting the value of spring.sleuth.scheduled.enabled to false.

If you annotate your method with @Scheduled then we’ll automatically create a new Span with the following characteristics:

  • the Span name will be the annotated method name
  • the Span will be tagged with that method’s class name and the method name too

If you want to skip Span creation for some @Scheduled annotated classes you can set the -spring.sleuth.scheduled.skipPattern with a regular expression that will match the fully qualified name of the -@Scheduled annotated class.

[Tip]Tip

If you are using spring-cloud-sleuth-stream and spring-cloud-netflix-hystrix-stream together, Span will be created for -each Hystrix metrics and sent to Zipkin. This may be annoying. You can prevent this by setting -spring.sleuth.scheduled.skipPattern=org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask

15.8.3 Executor, ExecutorService and ScheduledExecutorService

We’re providing LazyTraceExecutor, TraceableExecutorService and TraceableScheduledExecutorService. Those implementations -are creating Spans each time a new task is submitted, invoked or scheduled.

Here you can see an example of how to pass tracing information with TraceableExecutorService when working with CompletableFuture:

CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
+// use Traverson

14.7 Feign

By default, Spring Cloud Sleuth provides integration with Feign through TraceFeignClientAutoConfiguration. +You can disable it entirely by setting spring.sleuth.feign.enabled to false. +If you do so, no Feign-related instrumentation take place.

Part of Feign instrumentation is done through a FeignBeanPostProcessor. +You can disable it by setting spring.sleuth.feign.processor.enabled to false. +If you set it to false, Spring Cloud Sleuth does not instrument any of your custom Feign components. +However, all the default instrumentation is still there.

14.8 Asynchronous Communication

14.8.1 @Async Annotated methods

In Spring Cloud Sleuth, we instrument async-related components so that the tracing information is passed between threads. +You can disable this behavior by setting the value of spring.sleuth.async.enabled to false.

If you annotate your method with @Async, we automatically create a new Span with the following characteristics:

  • If the method is annotated with @SpanName, the value of the annotation is the Span’s name.
  • If the method is not annotated with @SpanName, the Span name is the annotated method name.
  • The span is tagged with the method’s class name and method name.

14.8.2 @Scheduled Annotated Methods

In Spring Cloud Sleuth, we instrument scheduled method execution so that the tracing information is passed between threads. +You can disable this behavior by setting the value of spring.sleuth.scheduled.enabled to false.

If you annotate your method with @Scheduled, we automatically create a new span with the following characteristics:

  • The span name is the annotated method name.
  • The span is tagged with the method’s class name and method name.

If you want to skip span creation for some @Scheduled annotated classes, you can set the spring.sleuth.scheduled.skipPattern with a regular expression that matches the fully qualified name of the @Scheduled annotated class.

[Tip]Tip

If you use spring-cloud-sleuth-stream and spring-cloud-netflix-hystrix-stream together, a span is created for each Hystrix metrics and sent to Zipkin. +This behavior may be annoying. +You can prevent it by setting spring.sleuth.scheduled.skipPattern=org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask.

14.8.3 Executor, ExecutorService, and ScheduledExecutorService

We provide LazyTraceExecutor, TraceableExecutorService, and TraceableScheduledExecutorService. Those implementations create spans each time a new task is submitted, invoked, or scheduled.

The following example shows how to pass tracing information with TraceableExecutorService when working with CompletableFuture:

CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
 	// perform some logic
 	return 1_000_000L;
 }, new TraceableExecutorService(beanFactory, executorService,
 		// 'calculateTax' explicitly names the span - this param is optional
-		"calculateTax"));
[Important]Important

Sleuth doesn’t work with parallelStream() out of the box. If you want -to have the tracing information propagated through the stream you have to use the -approach with supplyAsync(...) as presented above.

Customization of Executors

Sometimes you need to set up a custom instance of the AsyncExecutor. In the following snippet you -can see an example of how to set up such a custom Executor.

@Configuration
+		"calculateTax"));
[Important]Important

Sleuth does not work with parallelStream() out of the box. +If you want to have the tracing information propagated through the stream, you have to use the approach with supplyAsync(...), as shown earlier.

Customization of Executors

Sometimes, you need to set up a custom instance of the AsyncExecutor. +The following example shows how to set up such a custom Executor:

@Configuration
 @EnableAutoConfiguration
 @EnableAsync
 static class CustomExecutorConfig extends AsyncConfigurerSupport {
@@ -140,9 +135,9 @@ can see an example of how to set up such a custom Executor
 		executor.initialize();
 		return new LazyTraceExecutor(this.beanFactory, executor);
 	}
-}

15.9 Messaging

Spring Cloud Sleuth integrates with Spring Integration. It creates spans for publish and -subscribe events. To disable Spring Integration instrumentation, set spring.sleuth.integration.enabled to false.

You can provide the spring.sleuth.integration.patterns pattern to explicitly -provide the names of channels that you want to include for tracing. By default all channels -are included.

[Important]Important

When using the Executor to build a Spring Integration IntegrationFlow remember to use the untraced version of the Executor. -Decorating Spring Integration Executor Channel with TraceableExecutorService will cause the spans to be improperly closed.

15.10 Zuul

We’re instrumenting the Zuul Ribbon integration by enriching the Ribbon requests with tracing information. -To disable Zuul support set the spring.sleuth.zuul.enabled property to false.

\ No newline at end of file +}

14.9 Messaging

Spring Cloud Sleuth integrates with Spring Integration. +It creates spans for publish and subscribe events. +To disable Spring Integration instrumentation, set spring.sleuth.integration.enabled to false.

You can provide the spring.sleuth.integration.patterns pattern to explicitly provide the names of channels that you want to include for tracing. +By default, all channels are included.

[Important]Important

When using the Executor to build a Spring Integration IntegrationFlow, you must use the untraced version of the Executor. +Decorating the Spring Integration Executor Channel with TraceableExecutorService causes the spans to be improperly closed.

14.10 Zuul

We instrument the Zuul Ribbon integration by enriching the Ribbon requests with tracing information. +To disable Zuul support, set the spring.sleuth.zuul.enabled property to false.

\ No newline at end of file diff --git a/multi/multi__introduction.html b/multi/multi__introduction.html index 45a955fd8..dfaedc9ad 100644 --- a/multi/multi__introduction.html +++ b/multi/multi__introduction.html @@ -1,63 +1,59 @@ - 1. Introduction

1. Introduction

Spring Cloud Sleuth implements a distributed tracing solution for Spring Cloud.

1.1 Terminology

Spring Cloud Sleuth borrows Dapper’s terminology.

Span: The basic unit of work. For example, sending an RPC is a new span, as is sending a response to an -RPC. Span’s are identified by a unique 64-bit ID for the span and another 64-bit ID for the trace the span -is a part of. Spans also have other data, such as descriptions, timestamped events, key-value -annotations (tags), the ID of the span that caused them, and process ID’s (normally IP address).

Spans are started and stopped, and they keep track of their timing information. Once you create a -span, you must stop it at some point in the future.

[Tip]Tip

The initial span that starts a trace is called a root span. The value of span id -of that span is equal to trace id.

Trace: A set of spans forming a tree-like structure. For example, if you are running a distributed -big-data store, a trace might be formed by a put request.

Annotation: is used to record existence of an event in time. With -Brave instrumentation we no longer need to set special events -for Zipkin to understand who the client and server are and where -the request started and where it has ended. For learning purposes -however we will mark these events to highlight what kind -of an action took place.

  • cs - Client Sent - The client has made a request. This annotation depicts the start of the span.
  • sr - Server Received - The server side got the request and will start processing it. -If one subtracts the cs timestamp from this timestamp one will receive the network latency.
  • ss - Server Sent - Annotated upon completion of request processing (when the response -got sent back to the client). If one subtracts the sr timestamp from this timestamp one -will receive the time needed by the server side to process the request.
  • cr - Client Received - Signifies the end of the span. The client has successfully received the -response from the server side. If one subtracts the cs timestamp from this timestamp one -will receive the whole time needed by the client to receive the response from the server.

Visualization of what Span and Trace will look in a system together with the Zipkin annotations:

Trace Info propagation

Each color of a note signifies a span (7 spans - from A to G). If you have such information in the note:

Trace Id = X
+   1. Introduction

1. Introduction

Spring Cloud Sleuth implements a distributed tracing solution for Spring Cloud.

1.1 Terminology

Spring Cloud Sleuth borrows Dapper’s terminology.

Span: The basic unit of work. For example, sending an RPC is a new span, as is sending a response to an RPC. +Spans are identified by a unique 64-bit ID for the span and another 64-bit ID for the trace the span is a part of. +Spans also have other data, such as descriptions, timestamped events, key-value annotations (tags), the ID of the span that caused them, and process IDs (normally IP addresses).

Spans can be started and stopped, and they keep track of their timing information. +Once you create a span, you must stop it at some point in the future.

[Tip]Tip

The initial span that starts a trace is called a root span. The value of the ID +of that span is equal to the trace ID.

Trace: A set of spans forming a tree-like structure. +For example, if you run a distributed big-data store, a trace might be formed by a PUT request.

Annotation: Used to record the existence of an event in time. With +Brave instrumentation, we no longer need to set special events +for Zipkin to understand who the client and server are, where +the request started, and where it ended. For learning purposes, +however, we mark these events to highlight what kind +of an action took place.

  • cs: Client Sent. The client has made a request. This annotation indicates the start of the span.
  • sr: Server Received: The server side got the request and started processing it. +Subtracting the cs timestamp from this timestamp reveals the network latency.
  • ss: Server Sent. Annotated upon completion of request processing (when the response got sent back to the client). +Subtracting the sr timestamp from this timestamp reveals the time needed by the server side to process the request.
  • cr> Client Received. Signifies the end of the span. +The client has successfully received the response from the server side. +Subtracting the cs timestamp from this timestamp reveals the whole time needed by the client to receive the response from the server.

The following image shows how Span and Trace look in a system, together with the Zipkin annotations:

Trace Info propagation

Each color of a note signifies a span (there are seven spans - from A to G). +Consider the following note:

Trace Id = X
 Span Id = D
-Client Sent

That means that the current span has Trace-Id set to X, Span-Id set to D. Also, the - Client Sent event took place.

This is how the visualization of the parent / child relationship of spans would look like:

Parent child relationship

1.2 Purpose

In the following sections the example from the image above will be taken into consideration.

1.2.1 Distributed tracing with Zipkin

Altogether there are 7 spans . If you go to traces in Zipkin you will see this number in the second trace:

Traces

However if you pick a particular trace then you will see 4 spans:

Traces Info propagation
[Note]Note

When picking a particular trace you will see merged spans. That means that if there were 2 spans sent to -Zipkin with Server Received and Server Sent / Client Received and Client Sent -annotations then they will presented as a single span.

Why is there a difference between the 7 and 4 spans in this case?

  • 2 spans come from http:/start span. It has the Server Received (SR) and Server Sent (SS) annotations.
  • 2 spans come from the RPC call from service1 to service2 to the http:/foo endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service1 side. Server Received (SR) and Server Sent (SS) events took place -on the service2 side. Physically there are 2 spans but they form 1 logical span related to an RPC call.
  • 2 spans come from the RPC call from service2 to service3 to the http:/bar endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service2 side. Server Received (SR) and Server Sent (SS) events took place -on the service3 side. Physically there are 2 spans but they form 1 logical span related to an RPC call.
  • 2 spans come from the RPC call from service2 to service4 to the http:/baz endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service2 side. Server Received (SR) and Server Sent (SS) events took place -on the service4 side. Physically there are 2 spans but they form 1 logical span related to an RPC call.

So if we count the physical spans we have 1 from http:/start, 2 from service1 calling service2, 2 form service2 -calling service3 and 2 from service2 calling service4. Altogether 7 spans.

Logically we see the information of Total Spans: 4 because we have 1 span related to the incoming request -to service1 and 3 spans related to RPC calls.

1.2.2 Visualizing errors

Zipkin allows you to visualize errors in your trace. When an exception was thrown and wasn’t caught then we’re -setting proper tags on the span which Zipkin can properly colorize. You could see in the list of traces one - trace that was in red color. That’s because there was an exception thrown.

If you click that trace then you’ll see a similar picture

Error Traces

Then if you click on one of the spans you’ll see the following

Error Traces Info propagation

As you can see you can easily see the reason for an error and the whole stacktrace related to it.

1.2.3 Distributed tracing with Brave

Starting with version 2.0.0, Spring Cloud Sleuth uses -Brave as the tracing library. That means -that Sleuth no longer takes care of storing the context but it delegates -that work to Brave.

Due to the fact that Sleuth had different naming / tagging -conventions than Brave, we’ve decided to follow the Brave’s -conventions from now on. However, if you want to use the legacy -Sleuth approaches, it’s enough to set the spring.sleuth.http.legacy.enabled property -to true.

1.2.4 Live examples

Figure 1.1. Click Pivotal Web Services icon to see it live!

Zipkin deployed on Pivotal Web Services

Click here to see it live!

The dependency graph in Zipkin would look like this:

Dependencies

Figure 1.2. Click Pivotal Web Services icon to see it live!

Zipkin deployed on Pivotal Web Services

Click here to see it live!

1.2.5 Log correlation

When grepping the logs of those four applications by trace id equal to e.g. 2485ec27856c56f4 one would get the following:

service1.log:2016-02-26 11:15:47.561  INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application   : Hello from service1. Calling service2
+Client Sent

This note indicats thatthe current span has Trace Id set to X and Span Id set to D. +Also, the Client Sent event took place.

The following image shows how parent-child relationships of spans look:

Parent child relationship

1.2 Purpose

The following sections refer to the example shown in the preceding image.

1.2.1 Distributed Tracing with Zipkin

This example has seven spans. +If you go to traces in Zipkin, you can see this number in the second trace, as shown in the following image:

Traces

However, if you pick a particular trace, you can see four spans, as shown in the following image:

Traces Info propagation
[Note]Note

When you pick a particular trace, you see merged spans. +That means that, if there were two spans sent to Zipkin with Server Received and Server Sent or Client Received and Client Sent annotations, they are presented as a single span.

Why is there a difference between the seven and four spans in this case?

  • Two spans come from the http:/start span. It has the Server Received (sr) and Server Sent (ss) annotations.
  • Two spans come from the RPC call from service1 to service2 to the http:/foo endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service1 side. +Server Received (sr) and Server Sent (ss) events took place on the service2 side. +These two spans form one logical span related to an RPC call.
  • Two spans come from the RPC call from service2 to service3 to the http:/bar endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service2 side. +The Server Received (sr) and Server Sent (ss) events took place on the service3 side. +These two spans form one logical span related to an RPC call.
  • Two spans come from the RPC call from service2 to service4 to the http:/baz endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service2 side. +Server Received (sr) and Server Sent (ss) events took place on the service4 side. +These two spans form one logical span related to an RPC call.

So, if we count the physical spans, we have one from http:/start, two from service1 calling service2, two from service2 +calling service3, and two from service2 calling service4. In sum, we have a total of seven spans.

Logically, we see the information of four total Spans because we have one span related to the incoming request +to service1 and three spans related to RPC calls.

1.2.2 Visualizing errors

Zipkin lets you visualize errors in your trace. +When an exception was thrown and was not caught, we set proper tags on the span, which Zipkin can then properly colorize. +You could see in the list of traces one trace that is red. That appears because an exception was thrown.

If you click that trace, you see a similar picture, as follows:

Error Traces

If you then click on one of the spans, you see the following

Error Traces Info propagation

The span shows the reason for the error and the whole stack trace related to it.

1.2.3 Distributed Tracing with Brave

Starting with version 2.0.0, Spring Cloud Sleuth uses Brave as the tracing library. +Consequently, Sleuth no longer takes care of storing the context but delegates that work to Brave.

Due to the fact that Sleuth had different naming and tagging conventions than Brave, we decided to follow Brave’s conventions from now on. +However, if you want to use the legacy Sleuth approaches, you can set the spring.sleuth.http.legacy.enabled property to true.

1.2.4 Live examples

Figure 1.1. Click the Pivotal Web Services icon to see it live!

Zipkin deployed on Pivotal Web Services

Click here to see it live!

The dependency graph in Zipkin should resemble the following image:

Dependencies

Figure 1.2. Click the Pivotal Web Services icon to see it live!

Zipkin deployed on Pivotal Web Services

Click here to see it live!

1.2.5 Log correlation

When using grep to read the logs of those four applications by scanning for a trace ID equal to (for example) 2485ec27856c56f4, you get output resembling the following:

service1.log:2016-02-26 11:15:47.561  INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application   : Hello from service1. Calling service2
 service2.log:2016-02-26 11:15:47.710  INFO [service2,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.service2.Application   : Hello from service2. Calling service3 and then service4
 service3.log:2016-02-26 11:15:47.895  INFO [service3,2485ec27856c56f4,1210be13194bfe5,true] 68060 --- [nio-8083-exec-1] i.s.c.sleuth.docs.service3.Application   : Hello from service3
 service2.log:2016-02-26 11:15:47.924  INFO [service2,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.service2.Application   : Got response from service3 [Hello from service3]
 service4.log:2016-02-26 11:15:48.134  INFO [service4,2485ec27856c56f4,1b1845262ffba49d,true] 68061 --- [nio-8084-exec-1] i.s.c.sleuth.docs.service4.Application   : Hello from service4
 service2.log:2016-02-26 11:15:48.156  INFO [service2,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.service2.Application   : Got response from service4 [Hello from service4]
-service1.log:2016-02-26 11:15:48.182  INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application   : Got response from service2 [Hello from service2, response from service3 [Hello from service3] and from service4 [Hello from service4]]

If you’re using a log aggregating tool like Kibana, -Splunk etc. you can order the events that took place. An example of -Kibana would look like this:

Log correlation with Kibana

If you want to use Logstash here is the Grok pattern for Logstash:

filter {
+service1.log:2016-02-26 11:15:48.182  INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application   : Got response from service2 [Hello from service2, response from service3 [Hello from service3] and from service4 [Hello from service4]]

If you use a log aggregating tool (such as Kibana, Splunk, and others), you can order the events that took place. +An example from Kibana would resemble the following image:

Log correlation with Kibana

If you want to use Logstash, the following listing shows the Grok pattern for Logstash:

filter {
        # pattern matching logback pattern
        grok {
               match => { "message" => "%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:severity}\s+\[%{DATA:service},%{DATA:trace},%{DATA:span},%{DATA:exportable}\]\s+%{DATA:pid}\s+---\s+\[%{DATA:thread}\]\s+%{DATA:class}\s+:\s+%{GREEDYDATA:rest}" }
        }
-}
[Note]Note

If you want to use Grok together with the logs from Cloud Foundry you have to use this pattern:

filter {
+}
[Note]Note

If you want to use Grok together with the logs from Cloud Foundry, you have to use the following pattern:

filter {
        # pattern matching logback pattern
        grok {
               match => { "message" => "(?m)OUT\s+%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:severity}\s+\[%{DATA:service},%{DATA:trace},%{DATA:span},%{DATA:exportable}\]\s+%{DATA:pid}\s+---\s+\[%{DATA:thread}\]\s+%{DATA:class}\s+:\s+%{GREEDYDATA:rest}" }
        }
-}

JSON Logback with Logstash

Often you do not want to store your logs in a text file but in a JSON file that Logstash can immediately pick. To do that you have to do the following (for readability -we’re passing the dependencies in the groupId:artifactId:version notation.

Dependencies setup

  • Ensure that Logback is on the classpath (ch.qos.logback:logback-core)
  • Add Logstash Logback encode - example for version 4.6 : net.logstash.logback:logstash-logback-encoder:4.6

Logback setup

Below you can find an example of a Logback configuration (file named logback-spring.xml) that:

  • logs information from the application in a JSON format to a build/${spring.application.name}.json file
  • has commented out two additional appenders - console and standard log file
  • has the same logging pattern as the one presented in the previous section
<?xml version="1.0" encoding="UTF-8"?>
+}

JSON Logback with Logstash

Often, you do not want to store your logs in a text file but in a JSON file that Logstash can immediately pick. +To do so, you have to do the following (for readability, we pass the dependencies in the groupId:artifactId:version notation).

Dependencies Setup

  1. Ensure that Logback is on the classpath (ch.qos.logback:logback-core).
  2. Add Logstash Logback encode. For example, to use version 4.6, add net.logstash.logback:logstash-logback-encoder:4.6.

Logback Setup

Consider the following example of a Logback configuration file (named logback-spring.xml).

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
 	<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
 	​
@@ -132,25 +128,23 @@ we’re passing the dependencies in the groupId:artifa
 		<!--<appender-ref ref="logstash"/>-->
 		<!--<appender-ref ref="flatfile"/>-->
 	</root>
-</configuration>
[Note]Note

If you’re using a custom logback-spring.xml then you have to pass the spring.application.name in -bootstrap instead of application property file. Otherwise your custom logback file won’t read the property properly.

1.2.6 Propagating Span Context

The span context is the state that must get propagated to any child Spans across process boundaries. +</configuration>

That Logback configuration file:

  • Logs information from the application in a JSON format to a build/${spring.application.name}.json file.
  • Has commented out two additional appenders: console and standard log file.
  • Has the same logging pattern as the one presented in the previous section.
[Note]Note

If you use a custom logback-spring.xml, you must pass the spring.application.name in the bootstrap rather than the application property file. +Otherwise, your custom logback file does not properly read the property.

1.2.6 Propagating Span Context

The span context is the state that must get propagated to any child spans across process boundaries. Part of the Span Context is the Baggage. The trace and span IDs are a required part of the span context. -Baggage is an optional part.

Baggage is a set of key:value pairs stored in the span context. Baggage travels together with the trace -and is attached to every span. Spring Cloud Sleuth will understand that a header is baggage related if the HTTP - header is prefixed with baggage- and for messaging it starts with baggage_.

[Important]Important

There’s currently no limitation of the count or size of baggage items. However, keep in mind that -too many can decrease system throughput or increase RPC latency. In extreme cases, it could crash the app due -to exceeding transport-level message or header capacity.

Example of setting baggage on a span:

Span initialSpan = this.tracer.nextSpan().name("span").start();
+Baggage is an optional part.

Baggage is a set of key:value pairs stored in the span context. +Baggage travels together with the trace and is attached to every span. +Spring Cloud Sleuth understands that a header is baggage-related if the HTTP header is prefixed with baggage- and, for messaging, it starts with baggage_.

[Important]Important

There is currently no limitation of the count or size of baggage items. +However, keep in mind that too many can decrease system throughput or increase RPC latency. +In extreme cases, too much baggage can crash the application, due to exceeding transport-level message or header capacity.

The following example shows setting baggage on a span:

Span initialSpan = this.tracer.nextSpan().name("span").start();
 try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(initialSpan)) {
 	ExtraFieldPropagation.set("foo", "bar");
 	ExtraFieldPropagation.set("UPPER_CASE", "someValue");
-}

Baggage vs. Span Tags

Baggage travels with the trace (i.e. every child span contains the baggage of its parent). Zipkin has no knowledge of -baggage and will not even receive that information.

Tags are attached to a specific span - they are presented for that particular span only. However you -can search by tag to find the trace, where there exists a span having the searched tag value.

If you want to be able to lookup a span based on baggage, you should add corresponding entry as a tag in the root span.

[Important]Important

Remember that the span needs to be in scope!

initialSpan.tag("foo",
+}

Baggage versus Span Tags

Baggage travels with the trace (every child span contains the baggage of its parent). +Zipkin has no knowledge of baggage and does not receive that information.

Tags are attached to a specific span. In other words, they are presented only for that particular span. +However, you can search by tag to find the trace, assuming a span having the searched tag value exists.

If you want to be able to lookup a span based on baggage, you should add a corresponding entry as a tag in the root span.

[Important]Important

The span must be in scope.

The following listing shows integration tests that use baggage:

initialSpan.tag("foo",
 		ExtraFieldPropagation.get(initialSpan.context(), "foo"));
 initialSpan.tag("UPPER_CASE",
-		ExtraFieldPropagation.get(initialSpan.context(), "UPPER_CASE"));

1.3 Adding to the project

[Important]Important

To ensure that your application name is properly displayed in Zipkin - set the spring.application.name property in bootstrap.yml.

1.3.1 Only Sleuth (log correlation)

If you want to profit only from Spring Cloud Sleuth without the Zipkin integration just add -the spring-cloud-starter-sleuth module to your project.

Maven.  + ExtraFieldPropagation.get(initialSpan.context(), "UPPER_CASE"));

1.3 Adding Sleuth to the Project

This section addresses how to add Sleuth to your project with either Maven or Gradle.

[Important]Important

To ensure that your application name is properly displayed in Zipkin, set the spring.application.name property in bootstrap.yml.

1.3.1 Only Sleuth (log correlation)

If you want to use only Spring Cloud Sleuth without the Zipkin integration, add the spring-cloud-starter-sleuth module to your project.

The following example shows how to add Sleuth with Maven:

Maven. 

<dependencyManagement> 1
       <dependencies>
           <dependency>
@@ -167,8 +161,7 @@ the spring-cloud-starter-sleuth module to your proj
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-sleuth</artifactId>
 </dependency>

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-sleuth

Gradle.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-sleuth.

The following example shows how to add Sleuth with Gradle:

Gradle. 

dependencyManagement { 1
     imports {
         mavenBom "org.springframework.cloud:spring-cloud-dependencies:${releaseTrainVersion}"
@@ -178,8 +171,7 @@ the Spring BOM

2 compile "org.springframework.cloud:spring-cloud-starter-sleuth" }

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-sleuth

1.3.2 Sleuth with Zipkin via HTTP

If you want both Sleuth and Zipkin just add the spring-cloud-starter-zipkin dependency.

Maven.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-sleuth.

1.3.2 Sleuth with Zipkin via HTTP

If you want both Sleuth and Zipkin, add the spring-cloud-starter-zipkin dependency.

The following example shows how to do so for Maven:

Maven. 

<dependencyManagement> 1
       <dependencies>
           <dependency>
@@ -196,8 +188,7 @@ the Spring BOM

<groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-zipkin

Gradle.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-zipkin.

The following example shows how to do so for Gradle:

Gradle. 

dependencyManagement { 1
     imports {
         mavenBom "org.springframework.cloud:spring-cloud-dependencies:${releaseTrainVersion}"
@@ -207,10 +198,9 @@ the Spring BOM

2 compile "org.springframework.cloud:spring-cloud-starter-zipkin" }

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-zipkin

1.3.3 Sleuth with Zipkin via RabbitMQ or Kafka

If you want to use RabbitMQ or Kafka instead of http, add the spring-rabbit or spring-kafka -dependencies. The default destination name is zipkin.

Note: spring-cloud-sleuth-stream is deprecated and incompatible with these destinations

If you want Sleuth over RabbitMQ add the spring-cloud-starter-zipkin and spring-rabbit -dependencies.

Maven.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-zipkin.

1.3.3 Sleuth with Zipkin over RabbitMQ or Kafka

If you want to use RabbitMQ or Kafka instead of HTTP, add the spring-rabbit or spring-kafka dependency. +The default destination name is zipkin.

[Caution]Caution

spring-cloud-sleuth-stream is deprecated and incompatible with these destinations.

If you want Sleuth over RabbitMQ, add the spring-cloud-starter-zipkin and spring-rabbit +dependencies.

The following example shows how to do so for Gradle:

Maven. 

<dependencyManagement> 1
       <dependencies>
           <dependency>
@@ -231,8 +221,7 @@ dependencies.

Maven.  <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> </dependency>

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-zipkin - that way all dependent dependencies will be downloaded

3

To automatically configure rabbit, simply add the spring-rabbit dependency

Gradle.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-zipkin. That way, all nested dependencies get downloaded.

3

To automatically configure RabbitMQ, add the spring-rabbit dependency.

Gradle. 

dependencyManagement { 1
     imports {
         mavenBom "org.springframework.cloud:spring-cloud-dependencies:${releaseTrainVersion}"
@@ -243,5 +232,4 @@ dependencies {
     compile "org.springframework.cloud:spring-cloud-starter-zipkin" 2
     compile "org.springframework.amqp:spring-rabbit" 3
 }

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-zipkin - that way all dependent dependencies will be downloaded

3

To automatically configure rabbit, simply add the spring-rabbit dependency

\ No newline at end of file +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-zipkin. That way, all nested dependencies get downloaded.

3

To automatically configure RabbitMQ, add the spring-rabbit dependency.

\ No newline at end of file diff --git a/multi/multi__managing_spans_with_annotations.html b/multi/multi__managing_spans_with_annotations.html index f59561851..a84bb595f 100644 --- a/multi/multi__managing_spans_with_annotations.html +++ b/multi/multi__managing_spans_with_annotations.html @@ -1,48 +1,80 @@ - 11. Managing spans with annotations

11. Managing spans with annotations

11.1 Rationale

The main arguments for this features are

  • api-agnostic means to collaborate with a span

    • use of annotations allows users to add to a span with no library dependency on a span api. -This allows Sleuth to change its core api less impact to user code.
  • reduced surface area for basic span operations.

    • without this feature one has to use the span api, which has lifecycle commands that -could be used incorrectly. By only exposing scope, tag and log functionality, users can -collaborate without accidentally breaking span lifecycle.
  • collaboration with runtime generated code

    • with libraries such as Spring Data / Feign the implementations of interfaces are generated -at runtime thus span wrapping of objects was tedious. Now you can provide annotations - over interfaces and arguments of those interfaces

11.2 Creating new spans

If you really don’t want to take care of creating local spans manually you can profit from the -@NewSpan annotation. Also we give you the @SpanTag annotation to add tags in an automated -fashion.

Let’s look at some examples of usage.

@NewSpan
-void testMethod();

Annotating the method without any parameter will lead to a creation of a new span whose name -will be equal to annotated method name.

@NewSpan("customNameOnTestMethod4")
-void testMethod4();

If you provide the value in the annotation (either directly or via the name parameter) then -the created span will have the name as the provided value.

// method declaration
+   11. Managing Spans with Annotations

11. Managing Spans with Annotations

You can manage spans with a variety of annotations.

11.1 Rationale

There are a number of good reasons to manage spans with annotations, including:

  • API-agnostic means to collaborate with a span. Use of annotations lets users add to a span with no library dependency on a span api. +Doing so lets Sleuth change its core API to create less impact to user code.
  • Reduced surface area for basic span operations. Without this feature, you must use the span api, which has lifecycle commands that could be used incorrectly. +By only exposing scope, tag, and log functionality, you can collaborate without accidentally breaking span lifecycle.
  • Collaboration with runtime generated code. With libraries such as Spring Data and Feign, the implementations of interfaces are generated at runtime. +Consequently, span wrapping of objects was tedious. +Now you can provide annotations over interfaces and the arguments of those interfaces.

11.2 Creating New Spans

If you do not want to create local spans manually, you can use the @NewSpan annotation. +Also, we provide the @SpanTag annotation to add tags in an automated fashion.

Now we can consider some examples of usage.

@NewSpan
+void testMethod();

Annotating the method without any parameter leads to creating a new span whose name equals the annotated method name.

@NewSpan("customNameOnTestMethod4")
+void testMethod4();

If you provide the value in the annotation (either directly or by setting the name parameter), the created span has the provided value as the name.

// method declaration
 @NewSpan(name = "customNameOnTestMethod5")
 void testMethod5(@SpanTag("testTag") String param);
 
 // and method execution
-this.testBean.testMethod5("test");

You can combine both the name and a tag. Let’s focus on the latter. In this case whatever the value of -the annotated method’s parameter runtime value will be - that will be the value of the tag. In our sample -the tag key will be testTag and the tag value will be test.

@NewSpan(name = "customNameOnTestMethod3")
+this.testBean.testMethod5("test");

You can combine both the name and a tag. Let’s focus on the latter. +In this case, the value of the annotated method’s parameter runtime value becomes the value of the tag. +In our sample, the tag key is testTag, and the tag value is test.

@NewSpan(name = "customNameOnTestMethod3")
 @Override
 public void testMethod3() {
-}

You can place the @NewSpan annotation on both the class and an interface. If you override the -interface’s method and provide a different value of the @NewSpan annotation then the most -concrete one wins (in this case customNameOnTestMethod3 will be set).

11.3 Continuing spans

If you want to just add tags and annotations to an existing span it’s enough -to use the @ContinueSpan annotation as presented below. Note that in contrast -with the @NewSpan annotation you can also add logs via the log parameter:

// method declaration
+}

You can place the @NewSpan annotation on both the class and an interface. +If you override the interface’s method and provide a different value for the @NewSpan annotation, the most +concrete one wins (in this case customNameOnTestMethod3 is set).

11.3 Continuing Spans

If you want to add tags and annotations to an existing span, you can use the @ContinueSpan annotation, as shown in the following example:

// method declaration
 @ContinueSpan(log = "testMethod11")
 void testMethod11(@SpanTag("testTag11") String param);
 
 // method execution
 this.testBean.testMethod11("test");
-this.testBean.testMethod13();

That way the span will get continued and:

  • logs with name testMethod11.before and testMethod11.after will be created
  • if an exception will be thrown a log testMethod11.afterFailure will also be created
  • tag with key testTag11 and value test will be created

11.4 More advanced tag setting

There are 3 different ways to add tags to a span. All of them are controlled by the SpanTag annotation. -Precedence is:

  • try with the bean of TagValueResolver type and provided name
  • if one hasn’t provided the bean name, try to evaluate an expression. We’re searching for a TagValueExpressionResolver bean. -The default implementation uses SPEL expression resolution.
  • if one hasn’t provided any expression to evaluate just return a toString() value of the parameter

11.4.1 Custom extractor

The value of the tag for following method will be computed by an implementation of TagValueResolver interface. -Its class name has to be passed as the value of the resolver attribute.

Having such an annotated method:

@NewSpan
+this.testBean.testMethod13();

(Note that, in contrast with the @NewSpan annotation ,you can also add logs with the log parameter.)

That way, the span gets continued and:

  • Log entries named testMethod11.before and testMethod11.after are created.
  • If an exception is thrown, a log entry named testMethod11.afterFailure is also created.
  • A tag with a key of testTag11 and a value of test is created.

11.4 Advanced Tag Setting

There are 3 different ways to add tags to a span. All of them are controlled by the SpanTag annotation. +The precedence is as follows:

  1. Try with a bean of TagValueResolver type and a provided name.
  2. If the bean name has not been provided, try to evaluate an expression. +We search for a TagValueExpressionResolver bean. +The default implementation uses SPEL expression resolution.
  3. If we do not find any expression to evaluate, return the toString() value of the parameter.

11.4.1 Custom extractor

The value of the tag for the following method is computed by an implementation of TagValueResolver interface. +Its class name has to be passed as the value of the resolver attribute.

Consider the following annotated method:

@NewSpan
 public void getAnnotationForTagValueResolver(@SpanTag(key = "test", resolver = TagValueResolver.class) String test) {
-}

and such a TagValueResolver bean implementation

@Bean(name = "myCustomTagValueResolver")
+}

Now further consider the following TagValueResolver bean implementation:

@Bean(name = "myCustomTagValueResolver")
 public TagValueResolver tagValueResolver() {
 	return parameter -> "Value from myCustomTagValueResolver";
-}

Will lead to setting of a tag value equal to Value from myCustomTagValueResolver.

11.4.2 Resolving expressions for value

Having such an annotated method:

@NewSpan
+}

The two preceding examples lead to setting a tag value equal to Value from myCustomTagValueResolver.

11.4.2 Resolving Expressions for a Value

Consider the following annotated method:

@NewSpan
 public void getAnnotationForTagValueExpression(@SpanTag(key = "test", expression = "length() + ' characters'") String test) {
-}

and no custom implementation of a TagValueExpressionResolver will lead to evaluation of the SPEL expression and a tag with value 4 characters will be set on the span. -If you want to use some other expression resolution mechanism you can create your own implementation -of the bean.

11.4.3 Using toString method

Having such an annotated method:

@NewSpan
+}

No custom implementation of a TagValueExpressionResolver leads to evaluation of the SPEL expression, and a tag with a value of 4 characters is set on the span. +If you want to use some other expression resolution mechanism, you can create your own implementation of the bean.

11.4.3 Using the toString() method

Consider the following annotated method:

@NewSpan
 public void getAnnotationForArgumentToString(@SpanTag("test") Long param) {
-}

if executed with a value of 15 will lead to setting of a tag with a String value of "15".

\ No newline at end of file +}

Running the preceding method with a value of 15 leads to setting a tag with a String value of "15".

11.5 TraceFilter

You can also modify the behavior of the TraceFilter, which is the component that is responsible for processing the input HTTP request and adding tags basing on the HTTP response. +You can customize the tags or modify the response headers by registering your own instance of the TraceFilter bean.

In the following example, we register the TraceFilter bean, add the ZIPKIN-TRACE-ID response header containing the current Span’s trace id, and add a tag with key custom and a value tag to the span.

@Component
+@Order(TraceFilter.ORDER + 1)
+class MyFilter extends GenericFilterBean {
+
+	private final Tracer tracer;
+
+	MyFilter(Tracer tracer) {
+		this.tracer = tracer;
+	}
+
+	@Override public void doFilter(ServletRequest request, ServletResponse response,
+			FilterChain chain) throws IOException, ServletException {
+		Span currentSpan = this.tracer.currentSpan();
+		then(currentSpan).isNotNull();
+		// for readability we're returning trace id in a hex form
+		((HttpServletResponse) response)
+				.addHeader("ZIPKIN-TRACE-ID",
+						currentSpan.context().traceIdString());
+		// we can also add some custom tags
+		currentSpan.tag("custom", "tag");
+		chain.doFilter(request, response);
+	}
+}

11.6 Custom service name

By default, Sleuth assumes that, when you send a span to Zipkin, you want the span’s service name to be equal to the value of the spring.application.name property. +That is not always the case, though. +There are situations in which you want to explicitly provide a different service name for all spans coming from your application. +To achieve that, you can pass the following property to your application to override that value (the example is for a service named myService):

spring.zipkin.service.name: myService

11.7 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.

In Sleuth, we generat 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.

The following example shows how to register two beans that implement SpanAdjuster:

@Bean SpanAdjuster adjusterOne() {
+	return span -> span.toBuilder().name("foo").build();
+}
+
+@Bean SpanAdjuster adjusterTwo() {
+	return span -> span.toBuilder().name(span.name() + " bar").build();
+}

The preceding example results in changing the name of the reported span to foo bar, just before it gets reported (for example, to Zipkin).

11.8 Host Locator

[Important]Important

This section is about defining host from service discovery. +It is NOT about finding Zipkin through service discovery.

To define the host that corresponds to a particular span, we need to resolve the host name and port. +The default approach is to take these values from server properties. +If those are not set, we try to retrieve the host name from the network interfaces.

If you have the discovery client enabled and prefer to retrieve the host address from the registered instance in a service registry, you have to set the spring.zipkin.locator.discovery.enabled property (it is applicable for both HTTP-based and Stream-based span reporting), as follows:

spring.zipkin.locator.discovery.enabled: true
\ No newline at end of file diff --git a/multi/multi__naming_spans.html b/multi/multi__naming_spans.html index 6e9394ad7..893861c8e 100644 --- a/multi/multi__naming_spans.html +++ b/multi/multi__naming_spans.html @@ -1,20 +1,20 @@ - 10. Naming spans

10. Naming spans

Picking a span name is not a trivial task. Span name should depict an operation name. The name should -be low cardinality (e.g. not include identifiers).

Since there is a lot of instrumentation going on some of the span names will be -artificial like:

  • controller-method-name when received by a Controller with a method name conrollerMethodName
  • async for asynchronous operations done via wrapped Callable and Runnable.
  • @Scheduled annotated methods will return the simple name of the class.

Fortunately, for the asynchronous processing you can provide explicit naming.

10.1 @SpanName annotation

You can name the span explicitly via the @SpanName annotation.

@SpanName("calculateTax")
+   10. Naming spans

10. Naming spans

Picking a span name is not a trivial task. A span name should depict an operation name. +The name should be low cardinality, so it should not include identifiers.

Since there is a lot of instrumentation going on, some span names are artificial:

  • controller-method-name when received by a Controller with a method name of conrollerMethodName
  • async for asynchronous operations done with wrapped Callable and Runnable interfaces.
  • Methods annotated with @Scheduled return the simple name of the class.

Fortunately, for asynchronous processing, you can provide explicit naming.

10.1 @SpanName Annotation

You can name the span explicitly by using the @SpanName annotation, as shown in the followwng example:

@SpanName("calculateTax")
 class TaxCountingRunnable implements Runnable {
 
 	@Override public void run() {
 		// perform logic
 	}
-}

In this case, when processed in the following manner:

Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser,
+}

In this case, when processed in the following manner, the span is named calculateTax:

Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser,
 		new TaxCountingRunnable());
 Future<?> future = executorService.submit(runnable);
 // ... some additional logic ...
-future.get();

The span will be named calculateTax.

10.2 toString() method

It’s pretty rare to create separate classes for Runnable or Callable. Typically one creates an anonymous -instance of those classes. You can’t annotate such classes thus to override that, if there is no @SpanName annotation present, -we’re checking if the class has a custom implementation of the toString() method.

So executing such code:

Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser, new Runnable() {
+future.get();

10.2 toString() method

It is pretty rare to create separate classes for Runnable or Callable. +Typically, one creates an anonymous instance of those classes. +You cannot annotate such classes. +To overcome that limitation, if there is no @SpanName annotation present, we check whether the class has a custom implementation of the toString() method.

Running such code leads to creating a span named calculateTax, as shown in the following example:

Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser, new Runnable() {
 	@Override public void run() {
 		// perform logic
 	}
@@ -25,4 +25,4 @@ we’re checking if the class has a custom implementation of the // ... some additional logic ...
-future.get();

will lead in creating a span named calculateTax.

\ No newline at end of file +future.get();
\ No newline at end of file diff --git a/multi/multi__propagation.html b/multi/multi__propagation.html index 199323f2d..177412fff 100644 --- a/multi/multi__propagation.html +++ b/multi/multi__propagation.html @@ -1,10 +1,7 @@ - 5. Propagation

5. Propagation

Propagation is needed to ensure activity originating from the same root -are collected together in the same trace. The most common propagation -approach is to copy a trace context from a client sending an RPC request -to a server receiving it.

For example, when an downstream Http call is made, its trace context is -sent along with it, encoded as request headers:

   Client Span                                                Server Span
+   5. Propagation

5. Propagation

Propagation is needed to ensure activities originating from the same root are collected together in the same trace. +The most common propagation approach is to copy a trace context from a client by sending an RPC request to a server receiving it.

For example, when a downstream HTTP call is made, its trace context is encoded as request headers and sent along with it, as shown in the following image:

   Client Span                                                Server Span
 ┌──────────────────┐                                       ┌──────────────────┐
 │                  │                                       │                  │
 │   TraceContext   │           Http Request Headers        │   TraceContext   │
@@ -18,51 +15,43 @@ sent along with it, encoded as request headers:

   Client
 │ │ Sampled      │ │          │ X─B3─Sampled      │        │ │ Sampled      │ │
 │ └──────────────┘ │          └───────────────────┘        │ └──────────────┘ │
 │                  │                                       │                  │
-└──────────────────┘                                       └──────────────────┘

The names above are from B3 Propagation, -which is built-in to Brave and has implementations in many languages and -frameworks.

Most users will use a framework interceptor which automates propagation. -Here’s how they might work internally.

Here’s what client-side propagation might look like

// configure a function that injects a trace context into a request
+└──────────────────┘                                       └──────────────────┘

The names above are from B3 Propagation, which is built-in to Brave and has implementations in many languages and frameworks.

Most users use a framework interceptor to automate propagation. +The next two examples show how that might work for a client and a server.

The following example shows how client-side propagation might work:

// configure a function that injects a trace context into a request
 injector = tracing.propagation().injector(Request.Builder::addHeader);
 
 // before a request is sent, add the current span's context to it
-injector.inject(span.context(), request);

Here’s what server-side propagation might look like

// configure a function that extracts the trace context from a request
+injector.inject(span.context(), request);

The following example shows how server-side propagation might work:

// configure a function that extracts the trace context from a request
 extracted = tracing.propagation().extractor(Request::getHeader);
 
 // when a server receives a request, it joins or starts a new trace
 span = tracer.nextSpan(extracted, request);

5.1 Propagating extra fields

Sometimes you need to propagate extra fields, such as a request ID or an alternate trace context. -For example, if you are in a Cloud Foundry environment, you might want to pass the request ID:

// when you initialize the builder, define the extra field you want to propagate
+For example, if you are in a Cloud Foundry environment, you might want to pass the request ID, as shown in the following example:

// when you initialize the builder, define the extra field you want to propagate
 tracingBuilder.propagationFactory(
   ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-vcap-request-id")
 );
 
 // later, you can tag that request ID or use it in log correlation
-requestId = ExtraFieldPropagation.get("x-vcap-request-id");

You may also need to propagate a trace context you aren’t using. For example, you may be in an -Amazon Web Services environment, but not reporting data to X-Ray. To ensure X-Ray can co-exist -correctly, pass-through its tracing header like so.

tracingBuilder.propagationFactory(
+requestId = ExtraFieldPropagation.get("x-vcap-request-id");

You may also need to propagate a trace context that you are not using. +For example, you may be in an Amazon Web Services environment but not be reporting data to X-Ray. +To ensure X-Ray can co-exist correctly, pass-through its tracing header, as shown in the following example:

tracingBuilder.propagationFactory(
   ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-amzn-trace-id")
-);

5.1.1 Prefixed fields

You can also prefix fields, if they follow a common pattern. For example, the following will -propagate the field "x-vcap-request-id" as-is, but send the fields "country-code" and "user-id" -on the wire as "x-baggage-country-code" and "x-baggage-user-id" respectively.

Setup your tracing instance with allowed fields:

tracingBuilder.propagationFactory(
+);

5.1.1 Prefixed fields

If they follow a common pattern, you can also prefix fields. +The following example shows how to propagate x-vcap-request-id the field as-is but send the country-code and user-id fields on the wire as x-baggage-country-code and x-baggage-user-id, respectively:

tracingBuilder.propagationFactory(
   ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY)
                        .addField("x-vcap-request-id")
                        .addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id"))
                        .build()
-);

Later, you can call below to affect the country code of the current trace context

ExtraFieldPropagation.set("country-code", "FO");
-String countryCode = ExtraFieldPropagation.get("country-code");

Or, if you have a reference to a trace context, use it explicitly

ExtraFieldPropagation.set(span.context(), "country-code", "FO");
-String countryCode = ExtraFieldPropagation.get(span.context(), "country-code");
[Important]Important

In comparison to previous versions of Sleuth, with -Brave it’s required to pass the list of baggage keys. -There are two properties to achieve this. Via the spring.sleuth.baggage-keys you set keys -that will get prefixed with baggage- for http calls and baggage_ for messaging. You can also pass -a list of prefixed keys that will be whitelisted without any prefix via -spring.sleuth.propagation-keys property.

5.1.2 Extracting a propagated context

The TraceContext.Extractor<C> reads trace identifiers and sampling status -from an incoming request or message. The carrier is usually a request object -or headers.

This utility is used in standard instrumentation like [HttpServerHandler](../instrumentation/http/src/main/java/sleuth/http/HttpServerHandler.java), -but can also be used for custom RPC or messaging code.

TraceContextOrSamplingFlags is usually only used with Tracer.nextSpan(extracted), unless you are -sharing span IDs between a client and a server.

5.1.3 Sharing span IDs between client and server

A normal instrumentation pattern is creating a span representing the server -side of an RPC. Extractor.extract might return a complete trace context when -applied to an incoming client request. Tracer.joinSpan attempts to continue -the this trace, using the same span ID if supported, or creating a child span -if not. When span ID is shared, data reported includes a flag saying so.

Here’s an example of B3 propagation:

                              ┌───────────────────┐      ┌───────────────────┐
+);

Later, you can call the following code to affect the country code of the current trace context:

ExtraFieldPropagation.set("country-code", "FO");
+String countryCode = ExtraFieldPropagation.get("country-code");

Alternatively, if you have a reference to a trace context, you can use it explicitly, as shown in the following example:

ExtraFieldPropagation.set(span.context(), "country-code", "FO");
+String countryCode = ExtraFieldPropagation.get(span.context(), "country-code");
[Important]Important

A difference from previous versions of Sleuth is that, with Brave, you must pass the list of baggage keys. +There are two properties to achieve this. +With the spring.sleuth.baggage-keys, you set keys that get prefixed with baggage- for HTTP calls and baggage_ for messaging. +You can also use the spring.sleuth.propagation-keys property to pass a list of prefixed keys that are whitelisted without any prefix.

5.1.2 Extracting a Propagated Context

The TraceContext.Extractor<C> reads trace identifiers and sampling status from an incoming request or message. +The carrier is usually a request object or headers.

This utility is used in standard instrumentation (such as [HttpServerHandler](../instrumentation/http/src/main/java/sleuth/http/HttpServerHandler.java)) but can also be used for custom RPC or messaging code.

TraceContextOrSamplingFlags is usually used only with Tracer.nextSpan(extracted), unless you are +sharing span IDs between a client and a server.

5.1.3 Sharing span IDs between Client and Server

A normal instrumentation pattern is to create a span representing the server side of an RPC. +Extractor.extract might return a complete trace context when applied to an incoming client request. +Tracer.joinSpan attempts to continue this trace, using the same span ID if supported or creating a child span +if not. When the span ID is shared, the reported data includes a flag saying so.

The following image shows an example of B3 propagation:

                              ┌───────────────────┐      ┌───────────────────┐
  Incoming Headers             │   TraceContext    │      │   TraceContext    │
 ┌───────────────────┐(extract)│ ┌───────────────┐ │(join)│ ┌───────────────┐ │
 │ X─B3-TraceId      │─────────┼─┼> TraceId      │ │──────┼─┼> TraceId      │ │
@@ -73,9 +62,8 @@ if not. When span ID is shared, data reported includes a flag saying so.

H └───────────────────┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ Shared: true │ │ │ └───────────────┘ │ │ └───────────────┘ │ - └───────────────────┘ └───────────────────┘

Some propagation systems only forward the parent span ID, detected when -Propagation.Factory.supportsJoin() == false. In this case, a new span ID is -always provisioned and the incoming context determines the parent ID.

Here’s an example of AWS propagation:

                              ┌───────────────────┐      ┌───────────────────┐
+                              └───────────────────┘      └───────────────────┘

Some propagation systems forward only the parent span ID, detected when Propagation.Factory.supportsJoin() == false. +In this case, a new span ID is always provisioned, and the incoming context determines the parent ID.

The following image shows an example of AWS propagation:

                              ┌───────────────────┐      ┌───────────────────┐
  x-amzn-trace-id              │   TraceContext    │      │   TraceContext    │
 ┌───────────────────┐(extract)│ ┌───────────────┐ │(join)│ ┌───────────────┐ │
 │ Root              │─────────┼─┼> TraceId      │ │──────┼─┼> TraceId      │ │
@@ -84,15 +72,14 @@ always provisioned and the incoming context determines the parent ID.

Here └───────────────────┘ │ └───────────────┘ │ │ │ │ │ └───────────────────┘ │ │ SpanId: New │ │ │ └───────────────┘ │ - └───────────────────┘

Note: Some span reporters do not support sharing span IDs. For example, if you -set Tracing.Builder.spanReporter(amazonXrayOrGoogleStackdrive), disable join -via Tracing.Builder.supportsJoin(false). This will force a new child span on -Tracer.joinSpan().

5.1.4 Implementing Propagation

TraceContext.Extractor<C> is implemented by a Propagation.Factory plugin. Internally, this code -will create the union type TraceContextOrSamplingFlags with one of the following: + └───────────────────┘

Note: Some span reporters do not support sharing span IDs. +For example, if you set Tracing.Builder.spanReporter(amazonXrayOrGoogleStackdrive), you should disable join by setting Tracing.Builder.supportsJoin(false). +Doing so forces a new child span on Tracer.joinSpan().

5.1.4 Implementing Propagation

TraceContext.Extractor<C> is implemented by a Propagation.Factory plugin. +Internally, this code creates the union type, TraceContextOrSamplingFlags, with one of the following: * TraceContext if trace and span IDs were present. -* TraceIdContext if a trace ID was present, but not span IDs. -* SamplingFlags if no identifiers were present

Some Propagation implementations carry extra data from point of extraction (ex reading incoming -headers) to injection (ex writing outgoing headers). For example, it might carry a request ID. When -implementations have extra data, here’s how they handle it. -* If a TraceContext was extracted, add the extra data as TraceContext.extra() +* TraceIdContext if a trace ID was present but span IDs were not present. +* SamplingFlags if no identifiers were present.

Some Propagation implementations carry extra data from the point of extraction (for example, reading incoming headers) to injection (for example, writing outgoing headers). +For example, it might carry a request ID. +When implementations have extra data, they handle it as follows: +* If a TraceContext were extracted, add the extra data as TraceContext.extra(). * Otherwise, add it as TraceContextOrSamplingFlags.extra(), which Tracer.nextSpan handles.

\ No newline at end of file diff --git a/multi/multi__running_examples.html b/multi/multi__running_examples.html index d2f6dd861..7038c83a3 100644 --- a/multi/multi__running_examples.html +++ b/multi/multi__running_examples.html @@ -1,3 +1,4 @@ - 16. Running examples

16. Running examples

You can find the running examples deployed in the Pivotal Web Services. Check them out in the following links:

\ No newline at end of file + 15. Running examples

15. Running examples

You can see the running examples deployed in the Pivotal Web Services. +Check them out at the following links:

\ No newline at end of file diff --git a/multi/multi__sampling.html b/multi/multi__sampling.html index 4a0d21819..05af42729 100644 --- a/multi/multi__sampling.html +++ b/multi/multi__sampling.html @@ -1,12 +1,9 @@ - 4. Sampling

4. Sampling

Sampling may be employed to reduce the data collected and reported out -of process. When a span isn’t sampled, it adds no overhead (noop).

Sampling is an up-front decision, meaning that the decision to report -data is made at the first operation in a trace, and that decision is -propagated downstream.

By default, there’s a global sampler that applies a single rate to all -traced operations. Tracer.Builder.sampler is how you indicate this, -and it defaults to trace every request.

4.1 Declarative sampling

Some need to sample based on the type or annotations of a java method.

Most users will use a framework interceptor which automates this sort of -policy. Here’s how they might work internally.

// derives a sample rate from an annotation on a java method
+   4. Sampling

4. Sampling

Sampling may be employed to reduce the data collected and reported out of process. +When a span is not sampled, it adds no overhead (a noop).

Sampling is an up-front decision, meaning that the decision to report data is made at the first operation in a trace and that decision is propagated downstream.

By default, a global sampler applies a single rate to all traced operations. +Tracer.Builder.sampler controls this setting, and it defaults to tracing every request.

4.1 Declarative sampling

Some applications need to sample based on the type or annotations of a java method.

Most users use a framework interceptor to automate this sort of policy. +The following example shows how that might work internally:

// derives a sample rate from an annotation on a java method
 DeclarativeSampler<Traced> sampler = DeclarativeSampler.create(Traced::sampleRate);
 
 @Around("@annotation(traced)")
@@ -17,10 +14,9 @@ DeclarativeSampler<Traced> sampler = DeclarativeSampler.create(Traced::sam
   } finally {
     span.finish();
   }
-}

4.2 Custom sampling

You may want to apply different policies depending on what the operation -is. For example, you might not want to trace requests to static resources -such as images, or you might want to trace all requests to a new api.

Most users will use a framework interceptor which automates this sort of -policy. Here’s how they might work internally.

Span newTrace(Request input) {
+}

4.2 Custom sampling

Depending on what the operation is, you may want to apply different policies. +For example, you might not want to trace requests to static resources such as images, or you might want to trace all requests to a new api.

Most users use a framework interceptor to automate this sort of policy. +The following example shows how that might work internally:

Span newTrace(Request input) {
   SamplingFlags flags = SamplingFlags.NONE;
   if (input.url().startsWith("/experimental")) {
     flags = SamplingFlags.SAMPLED;
@@ -28,18 +24,13 @@ policy. Here’s how they might work internally.

return tracer.newTrace(flags);
-}

Note: the above is the basis for the built-in http sampler

4.3 Sampling in Spring Cloud Sleuth

Spring Cloud Sleuth by default sets all spans to non-exportable. -That means that you will see traces in logs, but not in any remote store. -For testing the default is often enough, and it probably is all you need -if you are only using the logs (e.g. with an ELK aggregator). If you are -exporting span data to Zipkin, there is also an Sampler.ALWAYS_SAMPLE -that exports everything and a ProbabilityBasedSampler that samples a -fixed fraction of spans.

[Note]Note

The ProbabilityBasedSampler is the default if you are using -spring-cloud-sleuth-zipkin. You can -configure the exports using spring.sleuth.sampler.probability. The passed -value needs to be a double from 0.0 to 1.0.

A sampler can be installed just by creating a bean definition, e.g:

@Bean
+}
[Note]Note

The preceding example forms the basis for the built-in http sampler.

4.3 Sampling in Spring Cloud Sleuth

By default Spring Cloud Sleuth sets all spans to non-exportable. +That means that traces appear in logs but not in any remote store. +For testing the default is often enough, and it probably is all you need if you use only the logs (for example, with an ELK aggregator). +If you export span data to Zipkin, there is also an Sampler.ALWAYS_SAMPLE setting that exports everything and a ProbabilityBasedSampler setting that samples a fixed fraction of spans.

[Note]Note

The ProbabilityBasedSampler is the default if you use spring-cloud-sleuth-zipkin. +You can configure the exports by setting spring.sleuth.sampler.probability. +The passed value needs to be a double from 0.0 to 1.0.

A sampler can be installed by creating a bean definition, as shown in the following example:

@Bean
 public Sampler defaultSampler() {
 	return Sampler.ALWAYS_SAMPLE;
-}
[Tip]Tip

You can set the HTTP header X-B3-Flags to 1 or when doing messaging you can -set spanFlags header to 1. Then the current span will be forced to be exportable -regardless of the sampling decision.

\ No newline at end of file +}
[Tip]Tip

You can set the HTTP header X-B3-Flags to 1, or, when doing messaging, you can set the spanFlags header to 1. +Doing so forces the current span to be exportable regardless of the sampling decision.

\ No newline at end of file diff --git a/multi/multi__sending_spans_to_zipkin.html b/multi/multi__sending_spans_to_zipkin.html index 82594b045..452df0325 100644 --- a/multi/multi__sending_spans_to_zipkin.html +++ b/multi/multi__sending_spans_to_zipkin.html @@ -1,10 +1,7 @@ - 13. Sending spans to Zipkin

13. Sending spans to Zipkin

By default if you add spring-cloud-starter-zipkin as a dependency to your project, -when the span is closed, it will be sent to Zipkin over HTTP. The communication -is asynchronous. You can configure the URL by setting the spring.zipkin.baseUrl -property as follows:

spring.zipkin.baseUrl: http://192.168.99.100:9411/

If you want to find Zipkin via service discovery it’s enough to pass the -Zipkin’s service id inside the URL (example for zipkinserver service id)

spring.zipkin.baseUrl: http://zipkinserver/

If you have web, rabbit or kafka together on the classpath, you might need -to pick the means by which you would like to send spans to zipkin. To do that -just set either web, rabbit or kafka to the spring.zipkin.sender.type property. -Example for web:

spring.zipkin.sender.type: web
\ No newline at end of file + 12. Sending Spans to Zipkin

12. Sending Spans to Zipkin

By default, if you add spring-cloud-starter-zipkin as a dependency to your project, when the span is closed, it is sent to Zipkin over HTTP. +The communication is asynchronous. +You can configure the URL by setting the spring.zipkin.baseUrl property, as follows:

spring.zipkin.baseUrl: http://192.168.99.100:9411/

If you want to find Zipkin through service discovery, you can pass the Zipkin’s service ID inside the URL, as shown in the following example for zipkinserver service ID:

spring.zipkin.baseUrl: http://zipkinserver/

If you have web, rabbit, or kafka together on the classpath, you might need to pick the means by which you would like to send spans to zipkin. +To do so, set web, rabbit, or kafka to the spring.zipkin.sender.type property. +The following example shows setting the sender type for web:

spring.zipkin.sender.type: web
\ No newline at end of file diff --git a/multi/multi__span_lifecycle.html b/multi/multi__span_lifecycle.html index c92aa04c6..4f4b7153d 100644 --- a/multi/multi__span_lifecycle.html +++ b/multi/multi__span_lifecycle.html @@ -1,9 +1,8 @@ - 9. Span lifecycle

9. Span lifecycle

You can do the following operations on the Span by means of brave.Tracer:

  • start - when you start a span its name is assigned and start timestamp is recorded.
  • close - the span gets finished (the end time of the span is recorded) and if -the span is sampled then it will be eligible for collection to e.g. Zipkin.
  • continue - a new instance of span will be created whereas it will be a copy of the -one that it continues.
  • detach - the span doesn’t get stopped or closed. It only gets removed from the current thread.
  • create with explicit parent - you can create a new span and set an explicit parent to it
[Tip]Tip

Spring Cloud Sleuth creates the instance of Tracer for you. In order to use it, -all you need is to just autowire it.

9.1 Creating and finishing spans

You can manually create spans by using the Tracer.

// Start a span. If there was a span present in this thread it will become
+   9. Span lifecycle

9. Span lifecycle

You can do the following operations on the Span by means of brave.Tracer:

  • start: When you start a span, its name is assigned and the start timestamp is recorded.
  • close: The span gets finished (the end time of the span is recorded) and, if the span is sampled, it is eligible for collection (for example, to Zipkin).
  • continue: A new instance of span is created. +It is a copy of the one that it continues.
  • detach: The span does not get stopped or closed. +It only gets removed from the current thread.
  • create with explicit parent: You can create a new span and set an explicit parent for it.
[Tip]Tip

Spring Cloud Sleuth creates an instance of Tracer for you. In order to use it, you can autowire it.

9.1 Creating and finishing spans

You can manually create spans by using the Tracer, as shown in the following example:

// Start a span. If there was a span present in this thread it will become
 // the `newSpan`'s parent.
 Span newSpan = this.tracer.nextSpan().name("calculateTax");
 try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(newSpan.start())) {
@@ -17,12 +16,11 @@ Span newSpan = 
 	// Once done remember to finish the span. This will allow collecting
 	// the span to send it to Zipkin
 	newSpan.finish();
-}

In this example we could see how to create a new instance of span. Assuming that there already -was a span present in this thread then it would become the parent of that span.

[Important]Important

Always clean after you create a span! Don’t forget to finish a span if you want to send it to Zipkin.

[Important]Important

If your span contains a name greater than 50 chars, then that name will -be truncated to 50 chars. Your names have to be explicit and concrete. Big names lead to -latency issues and sometimes even thrown exceptions.

9.2 Continuing spans

Sometimes you don’t want to create a new span but you want to continue one. Example of such a -situation might be (of course it all depends on the use-case):

  • AOP - If there was already a span created before an aspect was reached then you might not want to create a new span.
  • Hystrix - executing a Hystrix command is most likely a logical part of the current processing. It’s in fact -only a technical implementation detail that you wouldn’t necessarily want to reflect in tracing as a separate being.

To continue a span you can use brave.Tracer.

// let's assume that we're in a thread Y and we've received
+}

In the preceding example, we could see how to create a new instance of the span. +If there is already a span in this thread, it becomes the parent of the new span.

[Important]Important

Always clean after you create a span. Also, always finish any span that you want to send to Zipkin.

[Important]Important

If your span contains a name greater than 50 chars, that name is truncated to 50 chars. +Your names have to be explicit and concrete. Big names lead to latency issues and sometimes even exceptions.

9.2 Continuing Spans

Sometimes, you do not want to create a new span but you want to continue one. An example of such a +situation might be as follows:

  • AOP: If there was already a span created before an aspect was reached, you might not want to create a new span.
  • Hystrix: Executing a Hystrix command is most likely a logical part of the current processing. +It is in fact merely a technical implementation detail that you would not necessarily want to reflect in tracing as a separate being.

To continue a span, you can use brave.Tracer, as shown in the following example:

// let's assume that we're in a thread Y and we've received
 // the `initialSpan` from thread X
 Span continuedSpan = this.tracer.joinSpan(newSpan.context());
 try {
@@ -36,11 +34,10 @@ Span continuedSpan = // Once done remember to flush the span. That means that
 	// it will get reported but the span itself is not yet finished
 	continuedSpan.flush();
-}

9.3 Creating spans with an explicit parent

There is a possibility that you want to start a new span and provide an explicit parent of that span. -Let’s assume that the parent of a span is in one thread and you want to start a new span in another thread. -In Brave, whenever you call nextSpan(), it’s creating one in reference -to the span being currently in scope. It’s enough to just put -the span in scope and then call nextSpan(), as presented in the example below:

// let's assume that we're in a thread Y and we've received
+}

9.3 Creating a Span with an explicit Parent

You might want to start a new span and provide an explicit parent of that span. +Assume that the parent of a span is in one thread and you want to start a new span in another thread. +In Brave, whenever you call nextSpan(), it creates a span in reference to the span that is currently in scope. +You can put the span in scope and then call nextSpan(), as shown in the following example:

// let's assume that we're in a thread Y and we've received
 // the `initialSpan` from thread X. `initialSpan` will be the parent
 // of the `newSpan`
 Span newSpan = null;
@@ -59,5 +56,4 @@ Span newSpan = null;
 	if (newSpan != null) {
 		newSpan.finish();
 	}
-}
[Important]Important

After having created such a span remember to finish it, otherwise it will not get -reported to e.g. Zipkin

\ No newline at end of file +}
[Important]Important

After creating such a span, you must finish it. Otherwise it is not reported (for example, to Zipkin).

\ No newline at end of file diff --git a/multi/multi__zipkin_stream_span_consumer.html b/multi/multi__zipkin_stream_span_consumer.html index 32a8d2915..27ee6111d 100644 --- a/multi/multi__zipkin_stream_span_consumer.html +++ b/multi/multi__zipkin_stream_span_consumer.html @@ -1,7 +1,6 @@ - 14. Zipkin Stream Span Consumer

14. Zipkin Stream Span Consumer

[Important]Important

The suggested approach is to use the Zipkin’s -native support for message based span sending. Starting from -Edgware Zipkin Stream server is deprecated and in Finchley -it got removed.

Please refer to the Dalston Documentaion -on how to create a Stream Zipkin server.

\ No newline at end of file + 13. Zipkin Stream Span Consumer

13. Zipkin Stream Span Consumer

[Important]Important

We recommend using Zipkin’s native support for message-based span sending. +Starting from the Edgware release, the Zipkin Stream server is deprecated. +In the Finchley release, it got removed.

See the Dalston Documentaion +for how to create a Stream Zipkin server.

\ No newline at end of file diff --git a/multi/multi_spring-cloud-sleuth.html b/multi/multi_spring-cloud-sleuth.html index b6f14c4ee..31a0afd58 100644 --- a/multi/multi_spring-cloud-sleuth.html +++ b/multi/multi_spring-cloud-sleuth.html @@ -1,3 +1,3 @@ - Spring Cloud Sleuth

Spring Cloud Sleuth

Adrian Cole, Spencer Gibb, Marcin Grzejszczak, Dave Syer

Table of Contents

1. Introduction
1.1. Terminology
1.2. Purpose
1.2.1. Distributed tracing with Zipkin
1.2.2. Visualizing errors
1.2.3. Distributed tracing with Brave
1.2.4. Live examples
1.2.5. Log correlation
JSON Logback with Logstash
1.2.6. Propagating Span Context
Baggage vs. Span Tags
1.3. Adding to the project
1.3.1. Only Sleuth (log correlation)
1.3.2. Sleuth with Zipkin via HTTP
1.3.3. Sleuth with Zipkin via RabbitMQ or Kafka
2. Additional resources
3. Features
3.1. Introduction to Brave
3.1.1. Tracing
3.1.2. Tracing
3.1.3. Local Tracing
3.1.4. Customizing spans
3.1.5. Implicitly looking up the current span
3.1.6. RPC tracing
One-Way tracing
4. Sampling
4.1. Declarative sampling
4.2. Custom sampling
4.3. Sampling in Spring Cloud Sleuth
5. Propagation
5.1. Propagating extra fields
5.1.1. Prefixed fields
5.1.2. Extracting a propagated context
5.1.3. Sharing span IDs between client and server
5.1.4. Implementing Propagation
6. Current Tracing Component
7. Current Span
7.1. Setting a span in scope manually
8. Instrumentation
9. Span lifecycle
9.1. Creating and finishing spans
9.2. Continuing spans
9.3. Creating spans with an explicit parent
10. Naming spans
10.1. @SpanName annotation
10.2. toString() method
11. Managing spans with annotations
11.1. Rationale
11.2. Creating new spans
11.3. Continuing spans
11.4. More advanced tag setting
11.4.1. Custom extractor
11.4.2. Resolving expressions for value
11.4.3. Using toString method
12. Customizations
12.1. Spring Integration
12.2. HTTP
12.3. TraceFilter
12.4. Custom service name
12.5. Customization of reported spans
12.6. Host locator
13. Sending spans to Zipkin
14. Zipkin Stream Span Consumer
15. Integrations
15.1. OpenTracing
15.2. Runnable and Callable
15.3. Hystrix
15.3.1. Custom Concurrency Strategy
15.3.2. Manual Command setting
15.4. RxJava
15.5. HTTP integration
15.5.1. HTTP Filter
15.5.2. HandlerInterceptor
15.5.3. Async Servlet support
15.5.4. WebFlux support
15.6. HTTP client integration
15.6.1. Synchronous Rest Template
15.6.2. Asynchronous Rest Template
Multiple Asynchronous Rest Templates
15.6.3. WebClient
15.6.4. Traverson
15.7. Feign
15.8. Asynchronous communication
15.8.1. @Async annotated methods
15.8.2. @Scheduled annotated methods
15.8.3. Executor, ExecutorService and ScheduledExecutorService
Customization of Executors
15.9. Messaging
15.10. Zuul
16. Running examples
\ No newline at end of file + Spring Cloud Sleuth

Spring Cloud Sleuth

Adrian Cole, Spencer Gibb, Marcin Grzejszczak, Dave Syer, Jay Bryant

Table of Contents

1. Introduction
1.1. Terminology
1.2. Purpose
1.2.1. Distributed Tracing with Zipkin
1.2.2. Visualizing errors
1.2.3. Distributed Tracing with Brave
1.2.4. Live examples
1.2.5. Log correlation
JSON Logback with Logstash
1.2.6. Propagating Span Context
Baggage versus Span Tags
1.3. Adding Sleuth to the Project
1.3.1. Only Sleuth (log correlation)
1.3.2. Sleuth with Zipkin via HTTP
1.3.3. Sleuth with Zipkin over RabbitMQ or Kafka
2. Additional Resources
3. Features
3.1. Introduction to Brave
3.1.1. Tracing
3.1.2. Local Tracing
3.1.3. Customizing Spans
3.1.4. Implicitly Looking up the Current Span
3.1.5. RPC tracing
One-Way tracing
4. Sampling
4.1. Declarative sampling
4.2. Custom sampling
4.3. Sampling in Spring Cloud Sleuth
5. Propagation
5.1. Propagating extra fields
5.1.1. Prefixed fields
5.1.2. Extracting a Propagated Context
5.1.3. Sharing span IDs between Client and Server
5.1.4. Implementing Propagation
6. Current Tracing Component
7. Current Span
7.1. Setting a span in scope manually
8. Instrumentation
9. Span lifecycle
9.1. Creating and finishing spans
9.2. Continuing Spans
9.3. Creating a Span with an explicit Parent
10. Naming spans
10.1. @SpanName Annotation
10.2. toString() method
11. Managing Spans with Annotations
11.1. Rationale
11.2. Creating New Spans
11.3. Continuing Spans
11.4. Advanced Tag Setting
11.4.1. Custom extractor
11.4.2. Resolving Expressions for a Value
11.4.3. Using the toString() method
11.5. TraceFilter
11.6. Custom service name
11.7. Customization of Reported Spans
11.8. Host Locator
12. Sending Spans to Zipkin
13. Zipkin Stream Span Consumer
14. Integrations
14.1. OpenTracing
14.2. Runnable and Callable
14.3. Hystrix
14.3.1. Custom Concurrency Strategy
14.3.2. Manual Command setting
14.4. RxJava
14.5. HTTP integration
14.5.1. HTTP Filter
14.5.2. HandlerInterceptor
14.5.3. Async Servlet support
14.5.4. WebFlux support
14.6. HTTP Client Integration
14.6.1. Synchronous Rest Template
14.6.2. Asynchronous Rest Template
Multiple Asynchronous Rest Templates
14.6.3. WebClient
14.6.4. Traverson
14.7. Feign
14.8. Asynchronous Communication
14.8.1. @Async Annotated methods
14.8.2. @Scheduled Annotated Methods
14.8.3. Executor, ExecutorService, and ScheduledExecutorService
Customization of Executors
14.9. Messaging
14.10. Zuul
15. Running examples
\ No newline at end of file diff --git a/single/spring-cloud-sleuth.html b/single/spring-cloud-sleuth.html index b010dffe9..70adbba29 100644 --- a/single/spring-cloud-sleuth.html +++ b/single/spring-cloud-sleuth.html @@ -1,63 +1,59 @@ - Spring Cloud Sleuth

Spring Cloud Sleuth

Adrian Cole, Spencer Gibb, Marcin Grzejszczak, Dave Syer

Table of Contents

1. Introduction
1.1. Terminology
1.2. Purpose
1.2.1. Distributed tracing with Zipkin
1.2.2. Visualizing errors
1.2.3. Distributed tracing with Brave
1.2.4. Live examples
1.2.5. Log correlation
JSON Logback with Logstash
1.2.6. Propagating Span Context
Baggage vs. Span Tags
1.3. Adding to the project
1.3.1. Only Sleuth (log correlation)
1.3.2. Sleuth with Zipkin via HTTP
1.3.3. Sleuth with Zipkin via RabbitMQ or Kafka
2. Additional resources
3. Features
3.1. Introduction to Brave
3.1.1. Tracing
3.1.2. Tracing
3.1.3. Local Tracing
3.1.4. Customizing spans
3.1.5. Implicitly looking up the current span
3.1.6. RPC tracing
One-Way tracing
4. Sampling
4.1. Declarative sampling
4.2. Custom sampling
4.3. Sampling in Spring Cloud Sleuth
5. Propagation
5.1. Propagating extra fields
5.1.1. Prefixed fields
5.1.2. Extracting a propagated context
5.1.3. Sharing span IDs between client and server
5.1.4. Implementing Propagation
6. Current Tracing Component
7. Current Span
7.1. Setting a span in scope manually
8. Instrumentation
9. Span lifecycle
9.1. Creating and finishing spans
9.2. Continuing spans
9.3. Creating spans with an explicit parent
10. Naming spans
10.1. @SpanName annotation
10.2. toString() method
11. Managing spans with annotations
11.1. Rationale
11.2. Creating new spans
11.3. Continuing spans
11.4. More advanced tag setting
11.4.1. Custom extractor
11.4.2. Resolving expressions for value
11.4.3. Using toString method
12. Customizations
12.1. Spring Integration
12.2. HTTP
12.3. TraceFilter
12.4. Custom service name
12.5. Customization of reported spans
12.6. Host locator
13. Sending spans to Zipkin
14. Zipkin Stream Span Consumer
15. Integrations
15.1. OpenTracing
15.2. Runnable and Callable
15.3. Hystrix
15.3.1. Custom Concurrency Strategy
15.3.2. Manual Command setting
15.4. RxJava
15.5. HTTP integration
15.5.1. HTTP Filter
15.5.2. HandlerInterceptor
15.5.3. Async Servlet support
15.5.4. WebFlux support
15.6. HTTP client integration
15.6.1. Synchronous Rest Template
15.6.2. Asynchronous Rest Template
Multiple Asynchronous Rest Templates
15.6.3. WebClient
15.6.4. Traverson
15.7. Feign
15.8. Asynchronous communication
15.8.1. @Async annotated methods
15.8.2. @Scheduled annotated methods
15.8.3. Executor, ExecutorService and ScheduledExecutorService
Customization of Executors
15.9. Messaging
15.10. Zuul
16. Running examples

2.0.0.BUILD-SNAPSHOT

1. Introduction

Spring Cloud Sleuth implements a distributed tracing solution for Spring Cloud.

1.1 Terminology

Spring Cloud Sleuth borrows Dapper’s terminology.

Span: The basic unit of work. For example, sending an RPC is a new span, as is sending a response to an -RPC. Span’s are identified by a unique 64-bit ID for the span and another 64-bit ID for the trace the span -is a part of. Spans also have other data, such as descriptions, timestamped events, key-value -annotations (tags), the ID of the span that caused them, and process ID’s (normally IP address).

Spans are started and stopped, and they keep track of their timing information. Once you create a -span, you must stop it at some point in the future.

[Tip]Tip

The initial span that starts a trace is called a root span. The value of span id -of that span is equal to trace id.

Trace: A set of spans forming a tree-like structure. For example, if you are running a distributed -big-data store, a trace might be formed by a put request.

Annotation: is used to record existence of an event in time. With -Brave instrumentation we no longer need to set special events -for Zipkin to understand who the client and server are and where -the request started and where it has ended. For learning purposes -however we will mark these events to highlight what kind -of an action took place.

  • cs - Client Sent - The client has made a request. This annotation depicts the start of the span.
  • sr - Server Received - The server side got the request and will start processing it. -If one subtracts the cs timestamp from this timestamp one will receive the network latency.
  • ss - Server Sent - Annotated upon completion of request processing (when the response -got sent back to the client). If one subtracts the sr timestamp from this timestamp one -will receive the time needed by the server side to process the request.
  • cr - Client Received - Signifies the end of the span. The client has successfully received the -response from the server side. If one subtracts the cs timestamp from this timestamp one -will receive the whole time needed by the client to receive the response from the server.

Visualization of what Span and Trace will look in a system together with the Zipkin annotations:

Trace Info propagation

Each color of a note signifies a span (7 spans - from A to G). If you have such information in the note:

Trace Id = X
+   Spring Cloud Sleuth

Spring Cloud Sleuth

Adrian Cole, Spencer Gibb, Marcin Grzejszczak, Dave Syer, Jay Bryant

Table of Contents

1. Introduction
1.1. Terminology
1.2. Purpose
1.2.1. Distributed Tracing with Zipkin
1.2.2. Visualizing errors
1.2.3. Distributed Tracing with Brave
1.2.4. Live examples
1.2.5. Log correlation
JSON Logback with Logstash
1.2.6. Propagating Span Context
Baggage versus Span Tags
1.3. Adding Sleuth to the Project
1.3.1. Only Sleuth (log correlation)
1.3.2. Sleuth with Zipkin via HTTP
1.3.3. Sleuth with Zipkin over RabbitMQ or Kafka
2. Additional Resources
3. Features
3.1. Introduction to Brave
3.1.1. Tracing
3.1.2. Local Tracing
3.1.3. Customizing Spans
3.1.4. Implicitly Looking up the Current Span
3.1.5. RPC tracing
One-Way tracing
4. Sampling
4.1. Declarative sampling
4.2. Custom sampling
4.3. Sampling in Spring Cloud Sleuth
5. Propagation
5.1. Propagating extra fields
5.1.1. Prefixed fields
5.1.2. Extracting a Propagated Context
5.1.3. Sharing span IDs between Client and Server
5.1.4. Implementing Propagation
6. Current Tracing Component
7. Current Span
7.1. Setting a span in scope manually
8. Instrumentation
9. Span lifecycle
9.1. Creating and finishing spans
9.2. Continuing Spans
9.3. Creating a Span with an explicit Parent
10. Naming spans
10.1. @SpanName Annotation
10.2. toString() method
11. Managing Spans with Annotations
11.1. Rationale
11.2. Creating New Spans
11.3. Continuing Spans
11.4. Advanced Tag Setting
11.4.1. Custom extractor
11.4.2. Resolving Expressions for a Value
11.4.3. Using the toString() method
11.5. TraceFilter
11.6. Custom service name
11.7. Customization of Reported Spans
11.8. Host Locator
12. Sending Spans to Zipkin
13. Zipkin Stream Span Consumer
14. Integrations
14.1. OpenTracing
14.2. Runnable and Callable
14.3. Hystrix
14.3.1. Custom Concurrency Strategy
14.3.2. Manual Command setting
14.4. RxJava
14.5. HTTP integration
14.5.1. HTTP Filter
14.5.2. HandlerInterceptor
14.5.3. Async Servlet support
14.5.4. WebFlux support
14.6. HTTP Client Integration
14.6.1. Synchronous Rest Template
14.6.2. Asynchronous Rest Template
Multiple Asynchronous Rest Templates
14.6.3. WebClient
14.6.4. Traverson
14.7. Feign
14.8. Asynchronous Communication
14.8.1. @Async Annotated methods
14.8.2. @Scheduled Annotated Methods
14.8.3. Executor, ExecutorService, and ScheduledExecutorService
Customization of Executors
14.9. Messaging
14.10. Zuul
15. Running examples

2.0.0.BUILD-SNAPSHOT

1. Introduction

Spring Cloud Sleuth implements a distributed tracing solution for Spring Cloud.

1.1 Terminology

Spring Cloud Sleuth borrows Dapper’s terminology.

Span: The basic unit of work. For example, sending an RPC is a new span, as is sending a response to an RPC. +Spans are identified by a unique 64-bit ID for the span and another 64-bit ID for the trace the span is a part of. +Spans also have other data, such as descriptions, timestamped events, key-value annotations (tags), the ID of the span that caused them, and process IDs (normally IP addresses).

Spans can be started and stopped, and they keep track of their timing information. +Once you create a span, you must stop it at some point in the future.

[Tip]Tip

The initial span that starts a trace is called a root span. The value of the ID +of that span is equal to the trace ID.

Trace: A set of spans forming a tree-like structure. +For example, if you run a distributed big-data store, a trace might be formed by a PUT request.

Annotation: Used to record the existence of an event in time. With +Brave instrumentation, we no longer need to set special events +for Zipkin to understand who the client and server are, where +the request started, and where it ended. For learning purposes, +however, we mark these events to highlight what kind +of an action took place.

  • cs: Client Sent. The client has made a request. This annotation indicates the start of the span.
  • sr: Server Received: The server side got the request and started processing it. +Subtracting the cs timestamp from this timestamp reveals the network latency.
  • ss: Server Sent. Annotated upon completion of request processing (when the response got sent back to the client). +Subtracting the sr timestamp from this timestamp reveals the time needed by the server side to process the request.
  • cr> Client Received. Signifies the end of the span. +The client has successfully received the response from the server side. +Subtracting the cs timestamp from this timestamp reveals the whole time needed by the client to receive the response from the server.

The following image shows how Span and Trace look in a system, together with the Zipkin annotations:

Trace Info propagation

Each color of a note signifies a span (there are seven spans - from A to G). +Consider the following note:

Trace Id = X
 Span Id = D
-Client Sent

That means that the current span has Trace-Id set to X, Span-Id set to D. Also, the - Client Sent event took place.

This is how the visualization of the parent / child relationship of spans would look like:

Parent child relationship

1.2 Purpose

In the following sections the example from the image above will be taken into consideration.

1.2.1 Distributed tracing with Zipkin

Altogether there are 7 spans . If you go to traces in Zipkin you will see this number in the second trace:

Traces

However if you pick a particular trace then you will see 4 spans:

Traces Info propagation
[Note]Note

When picking a particular trace you will see merged spans. That means that if there were 2 spans sent to -Zipkin with Server Received and Server Sent / Client Received and Client Sent -annotations then they will presented as a single span.

Why is there a difference between the 7 and 4 spans in this case?

  • 2 spans come from http:/start span. It has the Server Received (SR) and Server Sent (SS) annotations.
  • 2 spans come from the RPC call from service1 to service2 to the http:/foo endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service1 side. Server Received (SR) and Server Sent (SS) events took place -on the service2 side. Physically there are 2 spans but they form 1 logical span related to an RPC call.
  • 2 spans come from the RPC call from service2 to service3 to the http:/bar endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service2 side. Server Received (SR) and Server Sent (SS) events took place -on the service3 side. Physically there are 2 spans but they form 1 logical span related to an RPC call.
  • 2 spans come from the RPC call from service2 to service4 to the http:/baz endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service2 side. Server Received (SR) and Server Sent (SS) events took place -on the service4 side. Physically there are 2 spans but they form 1 logical span related to an RPC call.

So if we count the physical spans we have 1 from http:/start, 2 from service1 calling service2, 2 form service2 -calling service3 and 2 from service2 calling service4. Altogether 7 spans.

Logically we see the information of Total Spans: 4 because we have 1 span related to the incoming request -to service1 and 3 spans related to RPC calls.

1.2.2 Visualizing errors

Zipkin allows you to visualize errors in your trace. When an exception was thrown and wasn’t caught then we’re -setting proper tags on the span which Zipkin can properly colorize. You could see in the list of traces one - trace that was in red color. That’s because there was an exception thrown.

If you click that trace then you’ll see a similar picture

Error Traces

Then if you click on one of the spans you’ll see the following

Error Traces Info propagation

As you can see you can easily see the reason for an error and the whole stacktrace related to it.

1.2.3 Distributed tracing with Brave

Starting with version 2.0.0, Spring Cloud Sleuth uses -Brave as the tracing library. That means -that Sleuth no longer takes care of storing the context but it delegates -that work to Brave.

Due to the fact that Sleuth had different naming / tagging -conventions than Brave, we’ve decided to follow the Brave’s -conventions from now on. However, if you want to use the legacy -Sleuth approaches, it’s enough to set the spring.sleuth.http.legacy.enabled property -to true.

1.2.4 Live examples

Figure 1.1. Click Pivotal Web Services icon to see it live!

Zipkin deployed on Pivotal Web Services

Click here to see it live!

The dependency graph in Zipkin would look like this:

Dependencies

Figure 1.2. Click Pivotal Web Services icon to see it live!

Zipkin deployed on Pivotal Web Services

Click here to see it live!

1.2.5 Log correlation

When grepping the logs of those four applications by trace id equal to e.g. 2485ec27856c56f4 one would get the following:

service1.log:2016-02-26 11:15:47.561  INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application   : Hello from service1. Calling service2
+Client Sent

This note indicats thatthe current span has Trace Id set to X and Span Id set to D. +Also, the Client Sent event took place.

The following image shows how parent-child relationships of spans look:

Parent child relationship

1.2 Purpose

The following sections refer to the example shown in the preceding image.

1.2.1 Distributed Tracing with Zipkin

This example has seven spans. +If you go to traces in Zipkin, you can see this number in the second trace, as shown in the following image:

Traces

However, if you pick a particular trace, you can see four spans, as shown in the following image:

Traces Info propagation
[Note]Note

When you pick a particular trace, you see merged spans. +That means that, if there were two spans sent to Zipkin with Server Received and Server Sent or Client Received and Client Sent annotations, they are presented as a single span.

Why is there a difference between the seven and four spans in this case?

  • Two spans come from the http:/start span. It has the Server Received (sr) and Server Sent (ss) annotations.
  • Two spans come from the RPC call from service1 to service2 to the http:/foo endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service1 side. +Server Received (sr) and Server Sent (ss) events took place on the service2 side. +These two spans form one logical span related to an RPC call.
  • Two spans come from the RPC call from service2 to service3 to the http:/bar endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service2 side. +The Server Received (sr) and Server Sent (ss) events took place on the service3 side. +These two spans form one logical span related to an RPC call.
  • Two spans come from the RPC call from service2 to service4 to the http:/baz endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service2 side. +Server Received (sr) and Server Sent (ss) events took place on the service4 side. +These two spans form one logical span related to an RPC call.

So, if we count the physical spans, we have one from http:/start, two from service1 calling service2, two from service2 +calling service3, and two from service2 calling service4. In sum, we have a total of seven spans.

Logically, we see the information of four total Spans because we have one span related to the incoming request +to service1 and three spans related to RPC calls.

1.2.2 Visualizing errors

Zipkin lets you visualize errors in your trace. +When an exception was thrown and was not caught, we set proper tags on the span, which Zipkin can then properly colorize. +You could see in the list of traces one trace that is red. That appears because an exception was thrown.

If you click that trace, you see a similar picture, as follows:

Error Traces

If you then click on one of the spans, you see the following

Error Traces Info propagation

The span shows the reason for the error and the whole stack trace related to it.

1.2.3 Distributed Tracing with Brave

Starting with version 2.0.0, Spring Cloud Sleuth uses Brave as the tracing library. +Consequently, Sleuth no longer takes care of storing the context but delegates that work to Brave.

Due to the fact that Sleuth had different naming and tagging conventions than Brave, we decided to follow Brave’s conventions from now on. +However, if you want to use the legacy Sleuth approaches, you can set the spring.sleuth.http.legacy.enabled property to true.

1.2.4 Live examples

Figure 1.1. Click the Pivotal Web Services icon to see it live!

Zipkin deployed on Pivotal Web Services

Click here to see it live!

The dependency graph in Zipkin should resemble the following image:

Dependencies

Figure 1.2. Click the Pivotal Web Services icon to see it live!

Zipkin deployed on Pivotal Web Services

Click here to see it live!

1.2.5 Log correlation

When using grep to read the logs of those four applications by scanning for a trace ID equal to (for example) 2485ec27856c56f4, you get output resembling the following:

service1.log:2016-02-26 11:15:47.561  INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application   : Hello from service1. Calling service2
 service2.log:2016-02-26 11:15:47.710  INFO [service2,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.service2.Application   : Hello from service2. Calling service3 and then service4
 service3.log:2016-02-26 11:15:47.895  INFO [service3,2485ec27856c56f4,1210be13194bfe5,true] 68060 --- [nio-8083-exec-1] i.s.c.sleuth.docs.service3.Application   : Hello from service3
 service2.log:2016-02-26 11:15:47.924  INFO [service2,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.service2.Application   : Got response from service3 [Hello from service3]
 service4.log:2016-02-26 11:15:48.134  INFO [service4,2485ec27856c56f4,1b1845262ffba49d,true] 68061 --- [nio-8084-exec-1] i.s.c.sleuth.docs.service4.Application   : Hello from service4
 service2.log:2016-02-26 11:15:48.156  INFO [service2,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.service2.Application   : Got response from service4 [Hello from service4]
-service1.log:2016-02-26 11:15:48.182  INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application   : Got response from service2 [Hello from service2, response from service3 [Hello from service3] and from service4 [Hello from service4]]

If you’re using a log aggregating tool like Kibana, -Splunk etc. you can order the events that took place. An example of -Kibana would look like this:

Log correlation with Kibana

If you want to use Logstash here is the Grok pattern for Logstash:

filter {
+service1.log:2016-02-26 11:15:48.182  INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application   : Got response from service2 [Hello from service2, response from service3 [Hello from service3] and from service4 [Hello from service4]]

If you use a log aggregating tool (such as Kibana, Splunk, and others), you can order the events that took place. +An example from Kibana would resemble the following image:

Log correlation with Kibana

If you want to use Logstash, the following listing shows the Grok pattern for Logstash:

filter {
        # pattern matching logback pattern
        grok {
               match => { "message" => "%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:severity}\s+\[%{DATA:service},%{DATA:trace},%{DATA:span},%{DATA:exportable}\]\s+%{DATA:pid}\s+---\s+\[%{DATA:thread}\]\s+%{DATA:class}\s+:\s+%{GREEDYDATA:rest}" }
        }
-}
[Note]Note

If you want to use Grok together with the logs from Cloud Foundry you have to use this pattern:

filter {
+}
[Note]Note

If you want to use Grok together with the logs from Cloud Foundry, you have to use the following pattern:

filter {
        # pattern matching logback pattern
        grok {
               match => { "message" => "(?m)OUT\s+%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:severity}\s+\[%{DATA:service},%{DATA:trace},%{DATA:span},%{DATA:exportable}\]\s+%{DATA:pid}\s+---\s+\[%{DATA:thread}\]\s+%{DATA:class}\s+:\s+%{GREEDYDATA:rest}" }
        }
-}

JSON Logback with Logstash

Often you do not want to store your logs in a text file but in a JSON file that Logstash can immediately pick. To do that you have to do the following (for readability -we’re passing the dependencies in the groupId:artifactId:version notation.

Dependencies setup

  • Ensure that Logback is on the classpath (ch.qos.logback:logback-core)
  • Add Logstash Logback encode - example for version 4.6 : net.logstash.logback:logstash-logback-encoder:4.6

Logback setup

Below you can find an example of a Logback configuration (file named logback-spring.xml) that:

  • logs information from the application in a JSON format to a build/${spring.application.name}.json file
  • has commented out two additional appenders - console and standard log file
  • has the same logging pattern as the one presented in the previous section
<?xml version="1.0" encoding="UTF-8"?>
+}

JSON Logback with Logstash

Often, you do not want to store your logs in a text file but in a JSON file that Logstash can immediately pick. +To do so, you have to do the following (for readability, we pass the dependencies in the groupId:artifactId:version notation).

Dependencies Setup

  1. Ensure that Logback is on the classpath (ch.qos.logback:logback-core).
  2. Add Logstash Logback encode. For example, to use version 4.6, add net.logstash.logback:logstash-logback-encoder:4.6.

Logback Setup

Consider the following example of a Logback configuration file (named logback-spring.xml).

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
 	<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
 	​
@@ -132,25 +128,23 @@ we’re passing the dependencies in the groupId:artifa
 		<!--<appender-ref ref="logstash"/>-->
 		<!--<appender-ref ref="flatfile"/>-->
 	</root>
-</configuration>
[Note]Note

If you’re using a custom logback-spring.xml then you have to pass the spring.application.name in -bootstrap instead of application property file. Otherwise your custom logback file won’t read the property properly.

1.2.6 Propagating Span Context

The span context is the state that must get propagated to any child Spans across process boundaries. +</configuration>

That Logback configuration file:

  • Logs information from the application in a JSON format to a build/${spring.application.name}.json file.
  • Has commented out two additional appenders: console and standard log file.
  • Has the same logging pattern as the one presented in the previous section.
[Note]Note

If you use a custom logback-spring.xml, you must pass the spring.application.name in the bootstrap rather than the application property file. +Otherwise, your custom logback file does not properly read the property.

1.2.6 Propagating Span Context

The span context is the state that must get propagated to any child spans across process boundaries. Part of the Span Context is the Baggage. The trace and span IDs are a required part of the span context. -Baggage is an optional part.

Baggage is a set of key:value pairs stored in the span context. Baggage travels together with the trace -and is attached to every span. Spring Cloud Sleuth will understand that a header is baggage related if the HTTP - header is prefixed with baggage- and for messaging it starts with baggage_.

[Important]Important

There’s currently no limitation of the count or size of baggage items. However, keep in mind that -too many can decrease system throughput or increase RPC latency. In extreme cases, it could crash the app due -to exceeding transport-level message or header capacity.

Example of setting baggage on a span:

Span initialSpan = this.tracer.nextSpan().name("span").start();
+Baggage is an optional part.

Baggage is a set of key:value pairs stored in the span context. +Baggage travels together with the trace and is attached to every span. +Spring Cloud Sleuth understands that a header is baggage-related if the HTTP header is prefixed with baggage- and, for messaging, it starts with baggage_.

[Important]Important

There is currently no limitation of the count or size of baggage items. +However, keep in mind that too many can decrease system throughput or increase RPC latency. +In extreme cases, too much baggage can crash the application, due to exceeding transport-level message or header capacity.

The following example shows setting baggage on a span:

Span initialSpan = this.tracer.nextSpan().name("span").start();
 try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(initialSpan)) {
 	ExtraFieldPropagation.set("foo", "bar");
 	ExtraFieldPropagation.set("UPPER_CASE", "someValue");
-}

Baggage vs. Span Tags

Baggage travels with the trace (i.e. every child span contains the baggage of its parent). Zipkin has no knowledge of -baggage and will not even receive that information.

Tags are attached to a specific span - they are presented for that particular span only. However you -can search by tag to find the trace, where there exists a span having the searched tag value.

If you want to be able to lookup a span based on baggage, you should add corresponding entry as a tag in the root span.

[Important]Important

Remember that the span needs to be in scope!

initialSpan.tag("foo",
+}

Baggage versus Span Tags

Baggage travels with the trace (every child span contains the baggage of its parent). +Zipkin has no knowledge of baggage and does not receive that information.

Tags are attached to a specific span. In other words, they are presented only for that particular span. +However, you can search by tag to find the trace, assuming a span having the searched tag value exists.

If you want to be able to lookup a span based on baggage, you should add a corresponding entry as a tag in the root span.

[Important]Important

The span must be in scope.

The following listing shows integration tests that use baggage:

initialSpan.tag("foo",
 		ExtraFieldPropagation.get(initialSpan.context(), "foo"));
 initialSpan.tag("UPPER_CASE",
-		ExtraFieldPropagation.get(initialSpan.context(), "UPPER_CASE"));

1.3 Adding to the project

[Important]Important

To ensure that your application name is properly displayed in Zipkin - set the spring.application.name property in bootstrap.yml.

1.3.1 Only Sleuth (log correlation)

If you want to profit only from Spring Cloud Sleuth without the Zipkin integration just add -the spring-cloud-starter-sleuth module to your project.

Maven.  + ExtraFieldPropagation.get(initialSpan.context(), "UPPER_CASE"));

1.3 Adding Sleuth to the Project

This section addresses how to add Sleuth to your project with either Maven or Gradle.

[Important]Important

To ensure that your application name is properly displayed in Zipkin, set the spring.application.name property in bootstrap.yml.

1.3.1 Only Sleuth (log correlation)

If you want to use only Spring Cloud Sleuth without the Zipkin integration, add the spring-cloud-starter-sleuth module to your project.

The following example shows how to add Sleuth with Maven:

Maven. 

<dependencyManagement> 1
       <dependencies>
           <dependency>
@@ -167,8 +161,7 @@ the spring-cloud-starter-sleuth module to your proj
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-sleuth</artifactId>
 </dependency>

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-sleuth

Gradle.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-sleuth.

The following example shows how to add Sleuth with Gradle:

Gradle. 

dependencyManagement { 1
     imports {
         mavenBom "org.springframework.cloud:spring-cloud-dependencies:${releaseTrainVersion}"
@@ -178,8 +171,7 @@ the Spring BOM

2 compile "org.springframework.cloud:spring-cloud-starter-sleuth" }

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-sleuth

1.3.2 Sleuth with Zipkin via HTTP

If you want both Sleuth and Zipkin just add the spring-cloud-starter-zipkin dependency.

Maven.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-sleuth.

1.3.2 Sleuth with Zipkin via HTTP

If you want both Sleuth and Zipkin, add the spring-cloud-starter-zipkin dependency.

The following example shows how to do so for Maven:

Maven. 

<dependencyManagement> 1
       <dependencies>
           <dependency>
@@ -196,8 +188,7 @@ the Spring BOM

<groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-zipkin

Gradle.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-zipkin.

The following example shows how to do so for Gradle:

Gradle. 

dependencyManagement { 1
     imports {
         mavenBom "org.springframework.cloud:spring-cloud-dependencies:${releaseTrainVersion}"
@@ -207,10 +198,9 @@ the Spring BOM

2 compile "org.springframework.cloud:spring-cloud-starter-zipkin" }

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-zipkin

1.3.3 Sleuth with Zipkin via RabbitMQ or Kafka

If you want to use RabbitMQ or Kafka instead of http, add the spring-rabbit or spring-kafka -dependencies. The default destination name is zipkin.

Note: spring-cloud-sleuth-stream is deprecated and incompatible with these destinations

If you want Sleuth over RabbitMQ add the spring-cloud-starter-zipkin and spring-rabbit -dependencies.

Maven.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-zipkin.

1.3.3 Sleuth with Zipkin over RabbitMQ or Kafka

If you want to use RabbitMQ or Kafka instead of HTTP, add the spring-rabbit or spring-kafka dependency. +The default destination name is zipkin.

[Caution]Caution

spring-cloud-sleuth-stream is deprecated and incompatible with these destinations.

If you want Sleuth over RabbitMQ, add the spring-cloud-starter-zipkin and spring-rabbit +dependencies.

The following example shows how to do so for Gradle:

Maven. 

<dependencyManagement> 1
       <dependencies>
           <dependency>
@@ -231,8 +221,7 @@ dependencies.

Maven.  <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> </dependency>

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-zipkin - that way all dependent dependencies will be downloaded

3

To automatically configure rabbit, simply add the spring-rabbit dependency

Gradle.  +

1

We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2

Add the dependency to spring-cloud-starter-zipkin. That way, all nested dependencies get downloaded.

3

To automatically configure RabbitMQ, add the spring-rabbit dependency.

Gradle. 

dependencyManagement { 1
     imports {
         mavenBom "org.springframework.cloud:spring-cloud-dependencies:${releaseTrainVersion}"
@@ -243,34 +232,31 @@ dependencies {
     compile "org.springframework.cloud:spring-cloud-starter-zipkin" 2
     compile "org.springframework.amqp:spring-rabbit" 3
 }

-

1

In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM

2

Add the dependency to spring-cloud-starter-zipkin - that way all dependent dependencies will be downloaded

3

To automatically configure rabbit, simply add the spring-rabbit dependency

2. Additional resources

Marcin Grzejszczak talking about Spring Cloud Sleuth and Zipkin

click here to see the video

3. Features

  • Adds trace and span ids to the Slf4J MDC, so you can extract all the logs from a given trace or span in a log aggregator. Example logs:

    2016-02-02 15:30:57.902  INFO [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ...
    +

    1

    We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

    2

    Add the dependency to spring-cloud-starter-zipkin. That way, all nested dependencies get downloaded.

    3

    To automatically configure RabbitMQ, add the spring-rabbit dependency.

2. Additional Resources

You can watch a video of Marcin Grzejszczak talking about Spring Cloud Sleuth and Zipkin:

click here to see the video

3. Features

  • Adds trace and span IDs to the Slf4J MDC, so you can extract all the logs from a given trace or span in a log aggregator, as shown in the following example logs:

    2016-02-02 15:30:57.902  INFO [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ...
     2016-02-02 15:30:58.372 ERROR [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ...
    -2016-02-02 15:31:01.936  INFO [bar,46ab0d418373cbc9,46ab0d418373cbc9,false] 23030 --- [nio-8081-exec-4] ...

    notice the [appname,traceId,spanId,exportable] entries from the MDC:

    • spanId - the id of a specific operation that took place
    • appname - the name of the application that logged the span
    • traceId - the id of the latency graph that contains the span
    • exportable - whether the log should be exported to Zipkin or not. When would you like the span not to be -exportable? In the case in which you want to wrap some operation in a Span and have it written to the logs -only.
  • Provides an abstraction over common distributed tracing data models: traces, spans (forming a DAG), annotations, -key-value annotations. Loosely based on HTrace, but Zipkin (Dapper) compatible.
  • Sleuth records timing information to aid in latency analysis. Using sleuth, you can pinpoint causes of -latency in your applications. Sleuth is written to not log too much, and to not cause your production application to crash.

    • propagates structural data about your call-graph in-band, and the rest out-of-band.
    • includes opinionated instrumentation of layers such as HTTP
    • includes sampling policy to manage volume
    • can report to a Zipkin system for query and visualization
  • Instruments common ingress and egress points from Spring applications (servlet filter, async endpoints, -rest template, scheduled actions, message channels, zuul filters, feign client).
  • Sleuth includes default logic to join a trace across http or messaging boundaries. For example, http propagation -works via Zipkin-compatible request headers. This propagation logic is defined and customized via -SpanInjector and SpanExtractor implementations.
  • Sleuth gives you the possibility to propagate context (also known as baggage) between processes. That means that if you set on a Span -a baggage element then it will be sent downstream either via HTTP or messaging to other processes.
  • Provides a way to create / continue spans and add tags and logs via annotations.
  • If spring-cloud-sleuth-zipkin is on the classpath then the app will generate and collect Zipkin-compatible traces. -By default it sends them via HTTP to a Zipkin server on localhost (port 9411). -Configure the location of the service using spring.zipkin.baseUrl.

    • If you depend on spring-rabbit or spring-kafka your app will send traces to a broker instead of http.
    • Note: spring-cloud-sleuth-stream is deprecated and should no longer be used.
  • Spring Cloud Sleuth is OpenTracing compatible
[Important]Important

If using Zipkin, configure the percentage of spans exported using spring.sleuth.sampler.percentage -(default 0.1, i.e. 10%). Otherwise you might think that Sleuth is not working cause it’s omitting some spans.

[Note]Note

the SLF4J MDC is always set and logback users will immediately see the trace and span ids in logs per the example - above. Other logging systems have to configure their own formatter to get the same result. The default is - logging.pattern.level set to %5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] - (this is a Spring Boot feature for logback users). - This means that if you’re not using SLF4J this pattern WILL NOT be automatically applied.

3.1 Introduction to Brave

[Important]Important

Starting with version 2.0.0 Spring Cloud Sleuth uses +2016-02-02 15:31:01.936 INFO [bar,46ab0d418373cbc9,46ab0d418373cbc9,false] 23030 --- [nio-8081-exec-4] ...

Notice the [appname,traceId,spanId,exportable] entries from the MDC:

  • spanId: The ID of a specific operation that took place.
  • appname: The name of the application that logged the span.
  • traceId: The ID of the latency graph that contains the span.
  • exportable: Whether the log should be exported to Zipkin. +When would you like the span not to be exportable? +When you want to wrap some operation in a Span and have it written to the logs only.
  • Provides an abstraction over common distributed tracing data models: traces, spans (forming a DAG), annotations, and key-value annotations. +Spring Cloud Slwuth is loosely based on HTrace but is compatible with Zipkin (Dapper).
  • Sleuth records timing information to aid in latency analysis. +By using sleuth, you can pinpoint causes of latency in your applications.
  • Sleuth is written to not log too much and to not cause your production application to crash. +To that end, Sleuth:

    • Propagates structural data about your call graph in-band and the rest out-of-band.
    • Includes opinionated instrumentation of layers such as HTTP.
    • Includes a sampling policy to manage volume.
    • Can report to a Zipkin system for query and visualization.
  • Instruments common ingress and egress points from Spring applications (servlet filter, async endpoints, rest template, scheduled actions, message channels, Zuul filters, and Feign client).
  • Sleuth includes default logic to join a trace across HTTP or messaging boundaries. +For example, HTTP propagation works over Zipkin-compatible request headers. +This propagation logic is defined and customized through SpanInjector and SpanExtractor implementations.
  • Sleuth can propagate context (also known as baggage) between processes. +Consequently, if you set a baggage element on a Span, it is sent downstream to other processes over either HTTP or messaging.
  • Provides a way to create or continue spans and add tags and logs through annotations.
  • If spring-cloud-sleuth-zipkin is on the classpath, the app generates and collects Zipkin-compatible traces. +By default, it sends them over HTTP to a Zipkin server on localhost (port 9411). +You can configure the location of the service by setting spring.zipkin.baseUrl.

    • If you depend on spring-rabbit or spring-kafka, your app sends traces to a broker instead of HTTP. +**
  • [Caution]Caution

    spring-cloud-sleuth-stream is deprecated and should no longer be used.

    [Important]Important

    If you use Zipkin, configure the percentage of spans exported by setting spring.sleuth.sampler.percentage +(default: 0.1, which is 10 percent). Otherwise, you might think that Sleuth is not working be cause it omits some spans.

    [Note]Note

    The SLF4J MDC is always set and logback users immediately see the trace and span IDs in logs per the example +shown earlier. +Other logging systems have to configure their own formatter to get the same result. +The default is as follows: +logging.pattern.level set to %5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] +(this is a Spring Boot feature for logback users). +If you do not use SLF4J, this pattern is NOT automatically applied.

    3.1 Introduction to Brave

    [Important]Important

    Starting with version 2.0.0, Spring Cloud Sleuth uses Brave as the tracing library. -For your convenience we’re embedding part of the Brave’s docs here.

    Brave is a library used to capture and report latency information about -distributed operations to Zipkin. Most users won’t use Brave directly, -rather libraries or frameworks than employ Brave on their behalf.

    This module includes tracer creates and joins spans that model the -latency of potentially distributed work. It also includes libraries to -propagate the trace context over network boundaries, for example, via -http headers.

    3.1.1 Tracing

    Most importantly, you need a brave.Tracer, configured to [report to Zipkin] -(https://github.com/openzipkin/zipkin-reporter-java).

    Here’s an example setup that sends trace data (spans) to Zipkin over -http (as opposed to Kafka).

    class MyClass {
    +For your convenience, we embed part of the Brave’s docs here.

    Brave is a library used to capture and report latency information about distributed operations to Zipkin. +Most users do not use Brave directly. They use libraries or frameworks rather than employ Brave on their behalf.

    This module includes a tracer that creates and joins spans that model the latency of potentially distributed work. +It also includes libraries to propagate the trace context over network boundaries (for example, with HTTP headers).

    3.1.1 Tracing

    Most importantly, you need a brave.Tracer, configured to report to Zipkin.

    The following example setup sends trace data (spans) to Zipkin over HTTP (as opposed to Kafka):

    class MyClass {
     
         private final Tracer tracer;
     
    @@ -283,46 +269,37 @@ http (as opposed to Kafka).

    "encode").start();
             // ...
         }
    -}
    [Important]Important

    If your span contains a name greater than 50 chars, then that name will -be truncated to 50 chars. Your names have to be explicit and concrete. Big names lead to -latency issues and sometimes even thrown exceptions.

    3.1.2 Tracing

    The tracer creates and joins spans that model the latency of potentially -distributed work. It can employ sampling to reduce overhead in process -or to reduce the amount of data sent to Zipkin.

    Spans returned by a tracer report data to Zipkin when finished, or do -nothing if unsampled. After starting a span, you can annotate events of -interest or add tags containing details or lookup keys.

    Spans have a context which includes trace identifiers that place it at -the correct spot in the tree representing the distributed operation.

    3.1.3 Local Tracing

    When tracing local code, just run it inside a span.

    Span span = tracer.newTrace().name("encode").start();
    +}
    [Important]Important

    If your span contains a name longer than 50 chars, then that name is truncated to 50 chars. +Your names have to be explicit and concrete. +Big names lead to latency issues and sometimes even thrown exceptions.

    The tracer creates and joins spans that model the latency of potentially distributed work. +It can employ sampling to reduce overhead during the process, to reduce the amount of data sent to Zipkin, or both.

    Spans returned by a tracer report data to Zipkin when finished or do nothing if unsampled. +After starting a span, you can annotate events of interest or add tags containing details or lookup keys.

    Spans have a context that includes trace identifiers that place the span at the correct spot in the tree representing the distributed operation.

    3.1.2 Local Tracing

    When tracing local code, you can run it inside a span, as shown in the following example:

    Span span = tracer.newTrace().name("encode").start();
     try {
       doSomethingExpensive();
     } finally {
       span.finish();
    -}

    In the above example, the span is the root of the trace. In many cases, -you will be a part of an existing trace. When this is the case, call -newChild instead of newTrace

    Span span = tracer.newChild(root.context()).name("encode").start();
    +}

    In the preceding example, the span is the root of the trace. +In many cases, the span is part of an existing trace. +When this is the case, call newChild instead of newTrace, as shown in the following example:

    Span span = tracer.newChild(root.context()).name("encode").start();
     try {
       doSomethingExpensive();
     } finally {
       span.finish();
    -}

    3.1.4 Customizing spans

    Once you have a span, you can add tags to it, which can be used as lookup -keys or details. For example, you might add a tag with your runtime -version.

    span.tag("clnt/finagle.version", "6.36.0");

    When exposing the ability to customize spans to third parties, prefer -brave.SpanCustomizer as opposed to brave.Span. The former is simpler to -understand and test, and doesn’t tempt users with span lifecycle hooks.

    interface MyTraceCallback {
    +}

    3.1.3 Customizing Spans

    Once you have a span, you can add tags to it. +The tags can be used as lookup keys or details. +For example, you might add a tag with your runtime version, as shown in the following example:

    span.tag("clnt/finagle.version", "6.36.0");

    When exposing the ability to customize spans to third parties, prefer brave.SpanCustomizer as opposed to brave.Span. +The former is simpler to understand and test and does not tempt users with span lifecycle hooks.

    interface MyTraceCallback {
       void request(Request request, SpanCustomizer customizer);
    -}

    Since brave.Span implements brave.SpanCustomizer, it is just as easy for you -to pass to users.

    Ex.

    for (MyTraceCallback callback : userCallbacks) {
    +}

    Since brave.Span implements brave.SpanCustomizer, you can pass it to users, as shown in the following example:

    for (MyTraceCallback callback : userCallbacks) {
       callback.request(request, span);
    -}

    3.1.5 Implicitly looking up the current span

    Sometimes you won’t know if a trace is in progress or not, and you don’t -want users to do null checks. brave.CurrentSpanCustomizer adds to any -span that’s in progress or drops data accordingly.

    Ex.

    // user code can then inject this without a chance of it being null.
    +}

    3.1.4 Implicitly Looking up the Current Span

    Sometimes, you do not know if a trace is in progress or not, and you do not want users to do null checks. +brave.CurrentSpanCustomizer handles this problem by adding data to any span that’s in progress or drops, as shown in the following example:

    Ex.

    // The user code can then inject this without a chance of it being null.
     @Autowire SpanCustomizer span;
     
     void userCode() {
       span.annotate("tx.started");
       ...
    -}

    3.1.6 RPC tracing

    Check for instrumentation written here -and Zipkin’s list -before rolling your own RPC instrumentation!

    RPC tracing is often done automatically by interceptors. Under the scenes, -they add tags and events that relate to their role in an RPC operation.

    Here’s an example of a client span:

    // before you send a request, add metadata that describes the operation
    +}

    3.1.5 RPC tracing

    [Tip]Tip

    Check for instrumentation written here and Zipkin’s list before rolling your own RPC instrumentation.

    RPC tracing is often done automatically by interceptors. Behind the scenes, they add tags and events that relate to their role in an RPC operation.

    The following example shows how to add a client span:

    // before you send a request, add metadata that describes the operation
     span = tracer.newTrace().name("get").type(CLIENT);
     span.tag("clnt/finagle.version", "6.36.0");
     span.tag(TraceKeys.HTTP_PATH, "/api");
    @@ -339,10 +316,10 @@ span.annotate(Constants.WIRE_SEND);
     span.annotate(Constants.WIRE_RECV);
     
     // when the response is complete, finish the span
    -span.finish();

    One-Way tracing

    Sometimes you need to model an asynchronous operation, where there is a -request, but no response. In normal RPC tracing, you use span.finish() -which indicates the response was received. In one-way tracing, you use -span.flush() instead, as you don’t expect a response.

    Here’s how a client might model a one-way operation

    // start a new span representing a client request
    +span.finish();

    One-Way tracing

    Sometimes, you need to model an asynchronous operation where there is a +request but no response. In normal RPC tracing, you use span.finish() +to indicate that the response was received. In one-way tracing, you use +span.flush() instead, as you do not expect a response.

    The following example shows how a client might model a one-way operation:

    // start a new span representing a client request
     oneWaySend = tracer.newSpan(parent).kind(Span.Kind.CLIENT);
     
     // Add the trace context to the request, so it can be propagated in-band
    @@ -353,7 +330,7 @@ tracing.propagation().injector(Request::addHeader)
     request.execute();
     
     // start the client side and flush instead of finish
    -oneWaySend.start().flush();

    And here’s how a server might handle this..

    // pull the context out of the incoming request
    +oneWaySend.start().flush();

    The following example shows how a server might handle a one-way operation:

    // pull the context out of the incoming request
     extractor = tracing.propagation().extractor(Request::getHeader);
     
     // convert that context to a span which you can name and add tags to
    @@ -367,13 +344,10 @@ oneWayReceive.start().flush();
     
     // you should not modify this span anymore as it is complete. However,
     // you can create children to represent follow-up work.
    -next = tracer.newSpan(oneWayReceive.context()).name("step2").start();

    Note The above propagation logic is a simplified version of our [http handlers](https://github.com/openzipkin/sleuth/tree/master/instrumentation/http#http-server).

    There’s a working example of a one-way span [here](src/test/java/sleuth/features/async/OneWaySpanTest.java).

    4. Sampling

    Sampling may be employed to reduce the data collected and reported out -of process. When a span isn’t sampled, it adds no overhead (noop).

    Sampling is an up-front decision, meaning that the decision to report -data is made at the first operation in a trace, and that decision is -propagated downstream.

    By default, there’s a global sampler that applies a single rate to all -traced operations. Tracer.Builder.sampler is how you indicate this, -and it defaults to trace every request.

    4.1 Declarative sampling

    Some need to sample based on the type or annotations of a java method.

    Most users will use a framework interceptor which automates this sort of -policy. Here’s how they might work internally.

    // derives a sample rate from an annotation on a java method
    +next = tracer.newSpan(oneWayReceive.context()).name("step2").start();
    [Note]Note

    The propagation logic shown in the preceding example is a simplified version of our [http handlers](https://github.com/openzipkin/sleuth/tree/master/instrumentation/http#http-server).

    You can find a working example of a one-way span [here](src/test/java/sleuth/features/async/OneWaySpanTest.java).

    4. Sampling

    Sampling may be employed to reduce the data collected and reported out of process. +When a span is not sampled, it adds no overhead (a noop).

    Sampling is an up-front decision, meaning that the decision to report data is made at the first operation in a trace and that decision is propagated downstream.

    By default, a global sampler applies a single rate to all traced operations. +Tracer.Builder.sampler controls this setting, and it defaults to tracing every request.

    4.1 Declarative sampling

    Some applications need to sample based on the type or annotations of a java method.

    Most users use a framework interceptor to automate this sort of policy. +The following example shows how that might work internally:

    // derives a sample rate from an annotation on a java method
     DeclarativeSampler<Traced> sampler = DeclarativeSampler.create(Traced::sampleRate);
     
     @Around("@annotation(traced)")
    @@ -384,10 +358,9 @@ DeclarativeSampler<Traced> sampler = DeclarativeSampler.create(Traced::sam
       } finally {
         span.finish();
       }
    -}

    4.2 Custom sampling

    You may want to apply different policies depending on what the operation -is. For example, you might not want to trace requests to static resources -such as images, or you might want to trace all requests to a new api.

    Most users will use a framework interceptor which automates this sort of -policy. Here’s how they might work internally.

    Span newTrace(Request input) {
    +}

    4.2 Custom sampling

    Depending on what the operation is, you may want to apply different policies. +For example, you might not want to trace requests to static resources such as images, or you might want to trace all requests to a new api.

    Most users use a framework interceptor to automate this sort of policy. +The following example shows how that might work internally:

    Span newTrace(Request input) {
       SamplingFlags flags = SamplingFlags.NONE;
       if (input.url().startsWith("/experimental")) {
         flags = SamplingFlags.SAMPLED;
    @@ -395,25 +368,17 @@ policy. Here’s how they might work internally.

    return tracer.newTrace(flags);
    -}

    Note: the above is the basis for the built-in http sampler

    4.3 Sampling in Spring Cloud Sleuth

    Spring Cloud Sleuth by default sets all spans to non-exportable. -That means that you will see traces in logs, but not in any remote store. -For testing the default is often enough, and it probably is all you need -if you are only using the logs (e.g. with an ELK aggregator). If you are -exporting span data to Zipkin, there is also an Sampler.ALWAYS_SAMPLE -that exports everything and a ProbabilityBasedSampler that samples a -fixed fraction of spans.

    [Note]Note

    The ProbabilityBasedSampler is the default if you are using -spring-cloud-sleuth-zipkin. You can -configure the exports using spring.sleuth.sampler.probability. The passed -value needs to be a double from 0.0 to 1.0.

    A sampler can be installed just by creating a bean definition, e.g:

    @Bean
    +}
    [Note]Note

    The preceding example forms the basis for the built-in http sampler.

    4.3 Sampling in Spring Cloud Sleuth

    By default Spring Cloud Sleuth sets all spans to non-exportable. +That means that traces appear in logs but not in any remote store. +For testing the default is often enough, and it probably is all you need if you use only the logs (for example, with an ELK aggregator). +If you export span data to Zipkin, there is also an Sampler.ALWAYS_SAMPLE setting that exports everything and a ProbabilityBasedSampler setting that samples a fixed fraction of spans.

    [Note]Note

    The ProbabilityBasedSampler is the default if you use spring-cloud-sleuth-zipkin. +You can configure the exports by setting spring.sleuth.sampler.probability. +The passed value needs to be a double from 0.0 to 1.0.

    A sampler can be installed by creating a bean definition, as shown in the following example:

    @Bean
     public Sampler defaultSampler() {
     	return Sampler.ALWAYS_SAMPLE;
    -}
    [Tip]Tip

    You can set the HTTP header X-B3-Flags to 1 or when doing messaging you can -set spanFlags header to 1. Then the current span will be forced to be exportable -regardless of the sampling decision.

    5. Propagation

    Propagation is needed to ensure activity originating from the same root -are collected together in the same trace. The most common propagation -approach is to copy a trace context from a client sending an RPC request -to a server receiving it.

    For example, when an downstream Http call is made, its trace context is -sent along with it, encoded as request headers:

       Client Span                                                Server Span
    +}
    [Tip]Tip

    You can set the HTTP header X-B3-Flags to 1, or, when doing messaging, you can set the spanFlags header to 1. +Doing so forces the current span to be exportable regardless of the sampling decision.

    5. Propagation

    Propagation is needed to ensure activities originating from the same root are collected together in the same trace. +The most common propagation approach is to copy a trace context from a client by sending an RPC request to a server receiving it.

    For example, when a downstream HTTP call is made, its trace context is encoded as request headers and sent along with it, as shown in the following image:

       Client Span                                                Server Span
     ┌──────────────────┐                                       ┌──────────────────┐
     │                  │                                       │                  │
     │   TraceContext   │           Http Request Headers        │   TraceContext   │
    @@ -427,51 +392,43 @@ sent along with it, encoded as request headers:

       Client
     │ │ Sampled      │ │          │ X─B3─Sampled      │        │ │ Sampled      │ │
     │ └──────────────┘ │          └───────────────────┘        │ └──────────────┘ │
     │                  │                                       │                  │
    -└──────────────────┘                                       └──────────────────┘

    The names above are from B3 Propagation, -which is built-in to Brave and has implementations in many languages and -frameworks.

    Most users will use a framework interceptor which automates propagation. -Here’s how they might work internally.

    Here’s what client-side propagation might look like

    // configure a function that injects a trace context into a request
    +└──────────────────┘                                       └──────────────────┘

    The names above are from B3 Propagation, which is built-in to Brave and has implementations in many languages and frameworks.

    Most users use a framework interceptor to automate propagation. +The next two examples show how that might work for a client and a server.

    The following example shows how client-side propagation might work:

    // configure a function that injects a trace context into a request
     injector = tracing.propagation().injector(Request.Builder::addHeader);
     
     // before a request is sent, add the current span's context to it
    -injector.inject(span.context(), request);

    Here’s what server-side propagation might look like

    // configure a function that extracts the trace context from a request
    +injector.inject(span.context(), request);

    The following example shows how server-side propagation might work:

    // configure a function that extracts the trace context from a request
     extracted = tracing.propagation().extractor(Request::getHeader);
     
     // when a server receives a request, it joins or starts a new trace
     span = tracer.nextSpan(extracted, request);

    5.1 Propagating extra fields

    Sometimes you need to propagate extra fields, such as a request ID or an alternate trace context. -For example, if you are in a Cloud Foundry environment, you might want to pass the request ID:

    // when you initialize the builder, define the extra field you want to propagate
    +For example, if you are in a Cloud Foundry environment, you might want to pass the request ID, as shown in the following example:

    // when you initialize the builder, define the extra field you want to propagate
     tracingBuilder.propagationFactory(
       ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-vcap-request-id")
     );
     
     // later, you can tag that request ID or use it in log correlation
    -requestId = ExtraFieldPropagation.get("x-vcap-request-id");

    You may also need to propagate a trace context you aren’t using. For example, you may be in an -Amazon Web Services environment, but not reporting data to X-Ray. To ensure X-Ray can co-exist -correctly, pass-through its tracing header like so.

    tracingBuilder.propagationFactory(
    +requestId = ExtraFieldPropagation.get("x-vcap-request-id");

    You may also need to propagate a trace context that you are not using. +For example, you may be in an Amazon Web Services environment but not be reporting data to X-Ray. +To ensure X-Ray can co-exist correctly, pass-through its tracing header, as shown in the following example:

    tracingBuilder.propagationFactory(
       ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-amzn-trace-id")
    -);

    5.1.1 Prefixed fields

    You can also prefix fields, if they follow a common pattern. For example, the following will -propagate the field "x-vcap-request-id" as-is, but send the fields "country-code" and "user-id" -on the wire as "x-baggage-country-code" and "x-baggage-user-id" respectively.

    Setup your tracing instance with allowed fields:

    tracingBuilder.propagationFactory(
    +);

    5.1.1 Prefixed fields

    If they follow a common pattern, you can also prefix fields. +The following example shows how to propagate x-vcap-request-id the field as-is but send the country-code and user-id fields on the wire as x-baggage-country-code and x-baggage-user-id, respectively:

    tracingBuilder.propagationFactory(
       ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY)
                            .addField("x-vcap-request-id")
                            .addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id"))
                            .build()
    -);

    Later, you can call below to affect the country code of the current trace context

    ExtraFieldPropagation.set("country-code", "FO");
    -String countryCode = ExtraFieldPropagation.get("country-code");

    Or, if you have a reference to a trace context, use it explicitly

    ExtraFieldPropagation.set(span.context(), "country-code", "FO");
    -String countryCode = ExtraFieldPropagation.get(span.context(), "country-code");
    [Important]Important

    In comparison to previous versions of Sleuth, with -Brave it’s required to pass the list of baggage keys. -There are two properties to achieve this. Via the spring.sleuth.baggage-keys you set keys -that will get prefixed with baggage- for http calls and baggage_ for messaging. You can also pass -a list of prefixed keys that will be whitelisted without any prefix via -spring.sleuth.propagation-keys property.

    5.1.2 Extracting a propagated context

    The TraceContext.Extractor<C> reads trace identifiers and sampling status -from an incoming request or message. The carrier is usually a request object -or headers.

    This utility is used in standard instrumentation like [HttpServerHandler](../instrumentation/http/src/main/java/sleuth/http/HttpServerHandler.java), -but can also be used for custom RPC or messaging code.

    TraceContextOrSamplingFlags is usually only used with Tracer.nextSpan(extracted), unless you are -sharing span IDs between a client and a server.

    5.1.3 Sharing span IDs between client and server

    A normal instrumentation pattern is creating a span representing the server -side of an RPC. Extractor.extract might return a complete trace context when -applied to an incoming client request. Tracer.joinSpan attempts to continue -the this trace, using the same span ID if supported, or creating a child span -if not. When span ID is shared, data reported includes a flag saying so.

    Here’s an example of B3 propagation:

                                  ┌───────────────────┐      ┌───────────────────┐
    +);

    Later, you can call the following code to affect the country code of the current trace context:

    ExtraFieldPropagation.set("country-code", "FO");
    +String countryCode = ExtraFieldPropagation.get("country-code");

    Alternatively, if you have a reference to a trace context, you can use it explicitly, as shown in the following example:

    ExtraFieldPropagation.set(span.context(), "country-code", "FO");
    +String countryCode = ExtraFieldPropagation.get(span.context(), "country-code");
    [Important]Important

    A difference from previous versions of Sleuth is that, with Brave, you must pass the list of baggage keys. +There are two properties to achieve this. +With the spring.sleuth.baggage-keys, you set keys that get prefixed with baggage- for HTTP calls and baggage_ for messaging. +You can also use the spring.sleuth.propagation-keys property to pass a list of prefixed keys that are whitelisted without any prefix.

    5.1.2 Extracting a Propagated Context

    The TraceContext.Extractor<C> reads trace identifiers and sampling status from an incoming request or message. +The carrier is usually a request object or headers.

    This utility is used in standard instrumentation (such as [HttpServerHandler](../instrumentation/http/src/main/java/sleuth/http/HttpServerHandler.java)) but can also be used for custom RPC or messaging code.

    TraceContextOrSamplingFlags is usually used only with Tracer.nextSpan(extracted), unless you are +sharing span IDs between a client and a server.

    5.1.3 Sharing span IDs between Client and Server

    A normal instrumentation pattern is to create a span representing the server side of an RPC. +Extractor.extract might return a complete trace context when applied to an incoming client request. +Tracer.joinSpan attempts to continue this trace, using the same span ID if supported or creating a child span +if not. When the span ID is shared, the reported data includes a flag saying so.

    The following image shows an example of B3 propagation:

                                  ┌───────────────────┐      ┌───────────────────┐
      Incoming Headers             │   TraceContext    │      │   TraceContext    │
     ┌───────────────────┐(extract)│ ┌───────────────┐ │(join)│ ┌───────────────┐ │
     │ X─B3-TraceId      │─────────┼─┼> TraceId      │ │──────┼─┼> TraceId      │ │
    @@ -482,9 +439,8 @@ if not. When span ID is shared, data reported includes a flag saying so.

    H └───────────────────┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ Shared: true │ │ │ └───────────────┘ │ │ └───────────────┘ │ - └───────────────────┘ └───────────────────┘

    Some propagation systems only forward the parent span ID, detected when -Propagation.Factory.supportsJoin() == false. In this case, a new span ID is -always provisioned and the incoming context determines the parent ID.

    Here’s an example of AWS propagation:

                                  ┌───────────────────┐      ┌───────────────────┐
    +                              └───────────────────┘      └───────────────────┘

    Some propagation systems forward only the parent span ID, detected when Propagation.Factory.supportsJoin() == false. +In this case, a new span ID is always provisioned, and the incoming context determines the parent ID.

    The following image shows an example of AWS propagation:

                                  ┌───────────────────┐      ┌───────────────────┐
      x-amzn-trace-id              │   TraceContext    │      │   TraceContext    │
     ┌───────────────────┐(extract)│ ┌───────────────┐ │(join)│ ┌───────────────┐ │
     │ Root              │─────────┼─┼> TraceId      │ │──────┼─┼> TraceId      │ │
    @@ -493,56 +449,35 @@ always provisioned and the incoming context determines the parent ID.

    Here └───────────────────┘ │ └───────────────┘ │ │ │ │ │ └───────────────────┘ │ │ SpanId: New │ │ │ └───────────────┘ │ - └───────────────────┘

    Note: Some span reporters do not support sharing span IDs. For example, if you -set Tracing.Builder.spanReporter(amazonXrayOrGoogleStackdrive), disable join -via Tracing.Builder.supportsJoin(false). This will force a new child span on -Tracer.joinSpan().

    5.1.4 Implementing Propagation

    TraceContext.Extractor<C> is implemented by a Propagation.Factory plugin. Internally, this code -will create the union type TraceContextOrSamplingFlags with one of the following: + └───────────────────┘

    Note: Some span reporters do not support sharing span IDs. +For example, if you set Tracing.Builder.spanReporter(amazonXrayOrGoogleStackdrive), you should disable join by setting Tracing.Builder.supportsJoin(false). +Doing so forces a new child span on Tracer.joinSpan().

    5.1.4 Implementing Propagation

    TraceContext.Extractor<C> is implemented by a Propagation.Factory plugin. +Internally, this code creates the union type, TraceContextOrSamplingFlags, with one of the following: * TraceContext if trace and span IDs were present. -* TraceIdContext if a trace ID was present, but not span IDs. -* SamplingFlags if no identifiers were present

    Some Propagation implementations carry extra data from point of extraction (ex reading incoming -headers) to injection (ex writing outgoing headers). For example, it might carry a request ID. When -implementations have extra data, here’s how they handle it. -* If a TraceContext was extracted, add the extra data as TraceContext.extra() -* Otherwise, add it as TraceContextOrSamplingFlags.extra(), which Tracer.nextSpan handles.

    6. Current Tracing Component

    Brave supports a "current tracing component" concept which should only -be used when you have no other means to get a reference. This was made -for JDBC connections, as they often initialize prior to the tracing -component.

    The most recent tracing component instantiated is available via -Tracing.current(). You there’s also a shortcut to get only the tracer -via Tracing.currentTracer(). If you use either of these methods, do -noot cache the result. Instead, look them up each time you need them.

    7. Current Span

    Brave supports a "current span" concept which represents the in-flight -operation. Tracer.currentSpan() can be used to add custom tags to a -span and Tracer.nextSpan() can be used to create a child of whatever -is in-flight.

    7.1 Setting a span in scope manually

    When writing new instrumentation, it is important to place a span you -created in scope as the current span. Not only does this allow users to -access it with Tracer.currentSpan(), but it also allows customizations -like SLF4J MDC to see the current trace IDs.

    Tracer.withSpanInScope(Span) facilitates this and is most conveniently -employed via the try-with-resources idiom. Whenever external code might -be invoked (such as proceeding an interceptor or otherwise), place the -span in scope like this.

    try (SpanInScope ws = tracer.withSpanInScope(span)) {
    +* TraceIdContext if a trace ID was present but span IDs were not present.
    +* SamplingFlags if no identifiers were present.

    Some Propagation implementations carry extra data from the point of extraction (for example, reading incoming headers) to injection (for example, writing outgoing headers). +For example, it might carry a request ID. +When implementations have extra data, they handle it as follows: +* If a TraceContext were extracted, add the extra data as TraceContext.extra(). +* Otherwise, add it as TraceContextOrSamplingFlags.extra(), which Tracer.nextSpan handles.

    6. Current Tracing Component

    Brave supports a "current tracing component" concept, which should only be used when you have no other way to get a reference. +This was made for JDBC connections, as they often initialize prior to the tracing component.

    The most recent tracing component instantiated is available through Tracing.current(). +You can also use Tracing.currentTracer() to get only the tracer. +If you use either of these methods, do not cache the result. +Instead, look them up each time you need them.

    7. Current Span

    Brave supports a "current span" concept which represents the in-flight operation. +You can use Tracer.currentSpan() to add custom tags to a span and Tracer.nextSpan() to create a child of whatever is in-flight.

    7.1 Setting a span in scope manually

    When writing new instrumentation, it is important to place a span you created in scope as the current span. +Not only does doing so let users access it with Tracer.currentSpan(), but it also allows customizations such as SLF4J MDC to see the current trace IDs.

    Tracer.withSpanInScope(Span) facilitates this and is most conveniently employed by using the try-with-resources idiom. +Whenever external code might be invoked (such as proceeding an interceptor or otherwise), place the span in scope, as shown in the following example:

    try (SpanInScope ws = tracer.withSpanInScope(span)) {
       return inboundRequest.invoke();
     } finally { // note the scope is independent of the span
       span.finish();
    -}

    In edge cases, you may need to clear the current span temporarily. For -example, launching a task that should not be associated with the current -request. To do this, simply pass null to withSpanInScope.

    try (SpanInScope cleared = tracer.withSpanInScope(null)) {
    +}

    In edge cases, you may need to clear the current span temporarily (for example, launching a task that should not be associated with the current request). To do tso, pass null to withSpanInScope, as shown in the following example:

    try (SpanInScope cleared = tracer.withSpanInScope(null)) {
       startBackgroundThread();
    -}

    8. Instrumentation

    Spring Cloud Sleuth instruments all your Spring application -automatically, so you shouldn’t have to do anything to activate -it. The instrumentation is added using a variety of technologies -according to the stack that is available, e.g. for a servlet web -application we use a Filter, and for Spring Integration we use -ChannelInterceptors.

    You can customize the keys used in span tags. To limit the volume of -span data, by default an HTTP request will be tagged only with a -handful of metadata like the status code, host and URL. You can add -request headers by configuring spring.sleuth.keys.http.headers (a -list of header names).

    [Note]Note

    Remember that tags are only collected and exported if there is a -Sampler that allows it (by default there is not, so there is no -danger of accidentally collecting too much data without configuring -something).

    9. Span lifecycle

    You can do the following operations on the Span by means of brave.Tracer:

    • start - when you start a span its name is assigned and start timestamp is recorded.
    • close - the span gets finished (the end time of the span is recorded) and if -the span is sampled then it will be eligible for collection to e.g. Zipkin.
    • continue - a new instance of span will be created whereas it will be a copy of the -one that it continues.
    • detach - the span doesn’t get stopped or closed. It only gets removed from the current thread.
    • create with explicit parent - you can create a new span and set an explicit parent to it
    [Tip]Tip

    Spring Cloud Sleuth creates the instance of Tracer for you. In order to use it, -all you need is to just autowire it.

    9.1 Creating and finishing spans

    You can manually create spans by using the Tracer.

    // Start a span. If there was a span present in this thread it will become
    +}

    8. Instrumentation

    Spring Cloud Sleuth automatically instruments all your Spring applications, so you should not have to do anything to activate it. +The instrumentation is added by using a variety of technologies according to the stack that is available. For example, for a servlet web application, we use a Filter, and, for Spring Integration, we use ChannelInterceptors.

    You can customize the keys used in span tags. +To limit the volume of span data, an HTTP request is, by default, tagged only with a handful of metadata, such as the status code, the host, and the URL. +You can add request headers by configuring spring.sleuth.keys.http.headers (a list of header names).

    [Note]Note

    Tags are collected and exported only if there is a Sampler that allows it. By default, there is no such Sampler, to ensure that there is no danger of accidentally collecting too much data without configuring something).

    9. Span lifecycle

    You can do the following operations on the Span by means of brave.Tracer:

    • start: When you start a span, its name is assigned and the start timestamp is recorded.
    • close: The span gets finished (the end time of the span is recorded) and, if the span is sampled, it is eligible for collection (for example, to Zipkin).
    • continue: A new instance of span is created. +It is a copy of the one that it continues.
    • detach: The span does not get stopped or closed. +It only gets removed from the current thread.
    • create with explicit parent: You can create a new span and set an explicit parent for it.
    [Tip]Tip

    Spring Cloud Sleuth creates an instance of Tracer for you. In order to use it, you can autowire it.

    9.1 Creating and finishing spans

    You can manually create spans by using the Tracer, as shown in the following example:

    // Start a span. If there was a span present in this thread it will become
     // the `newSpan`'s parent.
     Span newSpan = this.tracer.nextSpan().name("calculateTax");
     try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(newSpan.start())) {
    @@ -556,12 +491,11 @@ Span newSpan = 
     	// Once done remember to finish the span. This will allow collecting
     	// the span to send it to Zipkin
     	newSpan.finish();
    -}

    In this example we could see how to create a new instance of span. Assuming that there already -was a span present in this thread then it would become the parent of that span.

    [Important]Important

    Always clean after you create a span! Don’t forget to finish a span if you want to send it to Zipkin.

    [Important]Important

    If your span contains a name greater than 50 chars, then that name will -be truncated to 50 chars. Your names have to be explicit and concrete. Big names lead to -latency issues and sometimes even thrown exceptions.

    9.2 Continuing spans

    Sometimes you don’t want to create a new span but you want to continue one. Example of such a -situation might be (of course it all depends on the use-case):

    • AOP - If there was already a span created before an aspect was reached then you might not want to create a new span.
    • Hystrix - executing a Hystrix command is most likely a logical part of the current processing. It’s in fact -only a technical implementation detail that you wouldn’t necessarily want to reflect in tracing as a separate being.

    To continue a span you can use brave.Tracer.

    // let's assume that we're in a thread Y and we've received
    +}

    In the preceding example, we could see how to create a new instance of the span. +If there is already a span in this thread, it becomes the parent of the new span.

    [Important]Important

    Always clean after you create a span. Also, always finish any span that you want to send to Zipkin.

    [Important]Important

    If your span contains a name greater than 50 chars, that name is truncated to 50 chars. +Your names have to be explicit and concrete. Big names lead to latency issues and sometimes even exceptions.

    9.2 Continuing Spans

    Sometimes, you do not want to create a new span but you want to continue one. An example of such a +situation might be as follows:

    • AOP: If there was already a span created before an aspect was reached, you might not want to create a new span.
    • Hystrix: Executing a Hystrix command is most likely a logical part of the current processing. +It is in fact merely a technical implementation detail that you would not necessarily want to reflect in tracing as a separate being.

    To continue a span, you can use brave.Tracer, as shown in the following example:

    // let's assume that we're in a thread Y and we've received
     // the `initialSpan` from thread X
     Span continuedSpan = this.tracer.joinSpan(newSpan.context());
     try {
    @@ -575,11 +509,10 @@ Span continuedSpan = // Once done remember to flush the span. That means that
     	// it will get reported but the span itself is not yet finished
     	continuedSpan.flush();
    -}

    9.3 Creating spans with an explicit parent

    There is a possibility that you want to start a new span and provide an explicit parent of that span. -Let’s assume that the parent of a span is in one thread and you want to start a new span in another thread. -In Brave, whenever you call nextSpan(), it’s creating one in reference -to the span being currently in scope. It’s enough to just put -the span in scope and then call nextSpan(), as presented in the example below:

    // let's assume that we're in a thread Y and we've received
    +}

    9.3 Creating a Span with an explicit Parent

    You might want to start a new span and provide an explicit parent of that span. +Assume that the parent of a span is in one thread and you want to start a new span in another thread. +In Brave, whenever you call nextSpan(), it creates a span in reference to the span that is currently in scope. +You can put the span in scope and then call nextSpan(), as shown in the following example:

    // let's assume that we're in a thread Y and we've received
     // the `initialSpan` from thread X. `initialSpan` will be the parent
     // of the `newSpan`
     Span newSpan = null;
    @@ -598,22 +531,21 @@ Span newSpan = null;
     	if (newSpan != null) {
     		newSpan.finish();
     	}
    -}
    [Important]Important

    After having created such a span remember to finish it, otherwise it will not get -reported to e.g. Zipkin

    10. Naming spans

    Picking a span name is not a trivial task. Span name should depict an operation name. The name should -be low cardinality (e.g. not include identifiers).

    Since there is a lot of instrumentation going on some of the span names will be -artificial like:

    • controller-method-name when received by a Controller with a method name conrollerMethodName
    • async for asynchronous operations done via wrapped Callable and Runnable.
    • @Scheduled annotated methods will return the simple name of the class.

    Fortunately, for the asynchronous processing you can provide explicit naming.

    10.1 @SpanName annotation

    You can name the span explicitly via the @SpanName annotation.

    @SpanName("calculateTax")
    +}
    [Important]Important

    After creating such a span, you must finish it. Otherwise it is not reported (for example, to Zipkin).

    10. Naming spans

    Picking a span name is not a trivial task. A span name should depict an operation name. +The name should be low cardinality, so it should not include identifiers.

    Since there is a lot of instrumentation going on, some span names are artificial:

    • controller-method-name when received by a Controller with a method name of conrollerMethodName
    • async for asynchronous operations done with wrapped Callable and Runnable interfaces.
    • Methods annotated with @Scheduled return the simple name of the class.

    Fortunately, for asynchronous processing, you can provide explicit naming.

    10.1 @SpanName Annotation

    You can name the span explicitly by using the @SpanName annotation, as shown in the followwng example:

    @SpanName("calculateTax")
     class TaxCountingRunnable implements Runnable {
     
     	@Override public void run() {
     		// perform logic
     	}
    -}

    In this case, when processed in the following manner:

    Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser,
    +}

    In this case, when processed in the following manner, the span is named calculateTax:

    Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser,
     		new TaxCountingRunnable());
     Future<?> future = executorService.submit(runnable);
     // ... some additional logic ...
    -future.get();

    The span will be named calculateTax.

    10.2 toString() method

    It’s pretty rare to create separate classes for Runnable or Callable. Typically one creates an anonymous -instance of those classes. You can’t annotate such classes thus to override that, if there is no @SpanName annotation present, -we’re checking if the class has a custom implementation of the toString() method.

    So executing such code:

    Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser, new Runnable() {
    +future.get();

    10.2 toString() method

    It is pretty rare to create separate classes for Runnable or Callable. +Typically, one creates an anonymous instance of those classes. +You cannot annotate such classes. +To overcome that limitation, if there is no @SpanName annotation present, we check whether the class has a custom implementation of the toString() method.

    Running such code leads to creating a span named calculateTax, as shown in the following example:

    Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser, new Runnable() {
     	@Override public void run() {
     		// perform logic
     	}
    @@ -624,56 +556,47 @@ we’re checking if the class has a custom implementation of the // ... some additional logic ...
    -future.get();

    will lead in creating a span named calculateTax.

    11. Managing spans with annotations

    11.1 Rationale

    The main arguments for this features are

    • api-agnostic means to collaborate with a span

      • use of annotations allows users to add to a span with no library dependency on a span api. -This allows Sleuth to change its core api less impact to user code.
    • reduced surface area for basic span operations.

      • without this feature one has to use the span api, which has lifecycle commands that -could be used incorrectly. By only exposing scope, tag and log functionality, users can -collaborate without accidentally breaking span lifecycle.
    • collaboration with runtime generated code

      • with libraries such as Spring Data / Feign the implementations of interfaces are generated -at runtime thus span wrapping of objects was tedious. Now you can provide annotations - over interfaces and arguments of those interfaces

    11.2 Creating new spans

    If you really don’t want to take care of creating local spans manually you can profit from the -@NewSpan annotation. Also we give you the @SpanTag annotation to add tags in an automated -fashion.

    Let’s look at some examples of usage.

    @NewSpan
    -void testMethod();

    Annotating the method without any parameter will lead to a creation of a new span whose name -will be equal to annotated method name.

    @NewSpan("customNameOnTestMethod4")
    -void testMethod4();

    If you provide the value in the annotation (either directly or via the name parameter) then -the created span will have the name as the provided value.

    // method declaration
    +future.get();

    11. Managing Spans with Annotations

    You can manage spans with a variety of annotations.

    11.1 Rationale

    There are a number of good reasons to manage spans with annotations, including:

    • API-agnostic means to collaborate with a span. Use of annotations lets users add to a span with no library dependency on a span api. +Doing so lets Sleuth change its core API to create less impact to user code.
    • Reduced surface area for basic span operations. Without this feature, you must use the span api, which has lifecycle commands that could be used incorrectly. +By only exposing scope, tag, and log functionality, you can collaborate without accidentally breaking span lifecycle.
    • Collaboration with runtime generated code. With libraries such as Spring Data and Feign, the implementations of interfaces are generated at runtime. +Consequently, span wrapping of objects was tedious. +Now you can provide annotations over interfaces and the arguments of those interfaces.

    11.2 Creating New Spans

    If you do not want to create local spans manually, you can use the @NewSpan annotation. +Also, we provide the @SpanTag annotation to add tags in an automated fashion.

    Now we can consider some examples of usage.

    @NewSpan
    +void testMethod();

    Annotating the method without any parameter leads to creating a new span whose name equals the annotated method name.

    @NewSpan("customNameOnTestMethod4")
    +void testMethod4();

    If you provide the value in the annotation (either directly or by setting the name parameter), the created span has the provided value as the name.

    // method declaration
     @NewSpan(name = "customNameOnTestMethod5")
     void testMethod5(@SpanTag("testTag") String param);
     
     // and method execution
    -this.testBean.testMethod5("test");

    You can combine both the name and a tag. Let’s focus on the latter. In this case whatever the value of -the annotated method’s parameter runtime value will be - that will be the value of the tag. In our sample -the tag key will be testTag and the tag value will be test.

    @NewSpan(name = "customNameOnTestMethod3")
    +this.testBean.testMethod5("test");

    You can combine both the name and a tag. Let’s focus on the latter. +In this case, the value of the annotated method’s parameter runtime value becomes the value of the tag. +In our sample, the tag key is testTag, and the tag value is test.

    @NewSpan(name = "customNameOnTestMethod3")
     @Override
     public void testMethod3() {
    -}

    You can place the @NewSpan annotation on both the class and an interface. If you override the -interface’s method and provide a different value of the @NewSpan annotation then the most -concrete one wins (in this case customNameOnTestMethod3 will be set).

    11.3 Continuing spans

    If you want to just add tags and annotations to an existing span it’s enough -to use the @ContinueSpan annotation as presented below. Note that in contrast -with the @NewSpan annotation you can also add logs via the log parameter:

    // method declaration
    +}

    You can place the @NewSpan annotation on both the class and an interface. +If you override the interface’s method and provide a different value for the @NewSpan annotation, the most +concrete one wins (in this case customNameOnTestMethod3 is set).

    11.3 Continuing Spans

    If you want to add tags and annotations to an existing span, you can use the @ContinueSpan annotation, as shown in the following example:

    // method declaration
     @ContinueSpan(log = "testMethod11")
     void testMethod11(@SpanTag("testTag11") String param);
     
     // method execution
     this.testBean.testMethod11("test");
    -this.testBean.testMethod13();

    That way the span will get continued and:

    • logs with name testMethod11.before and testMethod11.after will be created
    • if an exception will be thrown a log testMethod11.afterFailure will also be created
    • tag with key testTag11 and value test will be created

    11.4 More advanced tag setting

    There are 3 different ways to add tags to a span. All of them are controlled by the SpanTag annotation. -Precedence is:

    • try with the bean of TagValueResolver type and provided name
    • if one hasn’t provided the bean name, try to evaluate an expression. We’re searching for a TagValueExpressionResolver bean. -The default implementation uses SPEL expression resolution.
    • if one hasn’t provided any expression to evaluate just return a toString() value of the parameter

    11.4.1 Custom extractor

    The value of the tag for following method will be computed by an implementation of TagValueResolver interface. -Its class name has to be passed as the value of the resolver attribute.

    Having such an annotated method:

    @NewSpan
    +this.testBean.testMethod13();

    (Note that, in contrast with the @NewSpan annotation ,you can also add logs with the log parameter.)

    That way, the span gets continued and:

    • Log entries named testMethod11.before and testMethod11.after are created.
    • If an exception is thrown, a log entry named testMethod11.afterFailure is also created.
    • A tag with a key of testTag11 and a value of test is created.

    11.4 Advanced Tag Setting

    There are 3 different ways to add tags to a span. All of them are controlled by the SpanTag annotation. +The precedence is as follows:

    1. Try with a bean of TagValueResolver type and a provided name.
    2. If the bean name has not been provided, try to evaluate an expression. +We search for a TagValueExpressionResolver bean. +The default implementation uses SPEL expression resolution.
    3. If we do not find any expression to evaluate, return the toString() value of the parameter.

    11.4.1 Custom extractor

    The value of the tag for the following method is computed by an implementation of TagValueResolver interface. +Its class name has to be passed as the value of the resolver attribute.

    Consider the following annotated method:

    @NewSpan
     public void getAnnotationForTagValueResolver(@SpanTag(key = "test", resolver = TagValueResolver.class) String test) {
    -}

    and such a TagValueResolver bean implementation

    @Bean(name = "myCustomTagValueResolver")
    +}

    Now further consider the following TagValueResolver bean implementation:

    @Bean(name = "myCustomTagValueResolver")
     public TagValueResolver tagValueResolver() {
     	return parameter -> "Value from myCustomTagValueResolver";
    -}

    Will lead to setting of a tag value equal to Value from myCustomTagValueResolver.

    11.4.2 Resolving expressions for value

    Having such an annotated method:

    @NewSpan
    +}

    The two preceding examples lead to setting a tag value equal to Value from myCustomTagValueResolver.

    11.4.2 Resolving Expressions for a Value

    Consider the following annotated method:

    @NewSpan
     public void getAnnotationForTagValueExpression(@SpanTag(key = "test", expression = "length() + ' characters'") String test) {
    -}

    and no custom implementation of a TagValueExpressionResolver will lead to evaluation of the SPEL expression and a tag with value 4 characters will be set on the span. -If you want to use some other expression resolution mechanism you can create your own implementation -of the bean.

    11.4.3 Using toString method

    Having such an annotated method:

    @NewSpan
    +}

    No custom implementation of a TagValueExpressionResolver leads to evaluation of the SPEL expression, and a tag with a value of 4 characters is set on the span. +If you want to use some other expression resolution mechanism, you can create your own implementation of the bean.

    11.4.3 Using the toString() method

    Consider the following annotated method:

    @NewSpan
     public void getAnnotationForArgumentToString(@SpanTag("test") Long param) {
    -}

    if executed with a value of 15 will lead to setting of a tag with a String value of "15".

    12. Customizations

    12.1 Spring Integration

    12.2 HTTP

    12.3 TraceFilter

    You can also modify the behaviour of the TraceFilter - the component that is responsible -for processing the input HTTP request and adding tags basing on the HTTP response. You can customize -the tags, or modify the response headers by registering your own instance of the TraceFilter bean.

    In the following example we will register the TraceFilter bean and we will add the -ZIPKIN-TRACE-ID response header containing the current Span’s trace id. Also we will -add to the Span a tag with key custom and a value tag.

    @Component
    +}

    Running the preceding method with a value of 15 leads to setting a tag with a String value of "15".

    11.5 TraceFilter

    You can also modify the behavior of the TraceFilter, which is the component that is responsible for processing the input HTTP request and adding tags basing on the HTTP response. +You can customize the tags or modify the response headers by registering your own instance of the TraceFilter bean.

    In the following example, we register the TraceFilter bean, add the ZIPKIN-TRACE-ID response header containing the current Span’s trace id, and add a tag with key custom and a value tag to the span.

    @Component
     @Order(TraceFilter.ORDER + 1)
     class MyFilter extends GenericFilterBean {
     
    @@ -695,37 +618,31 @@ add to the Span a tag with key custom and a value <
     		currentSpan.tag("custom", "tag");
     		chain.doFilter(request, response);
     	}
    -}

    12.4 Custom service name

    By default Sleuth assumes that when you send a span to Zipkin, you want the span’s service name - to be equal to spring.application.name value. That’s not always the case though. There - are situations in which you want to explicitly provide a different service name for all spans coming - from your application. To achieve that it’s enough to just pass the following property - to your application to override that value (example for foo service name):

    spring.zipkin.service.name: foo

    12.5 Customization of reported spans

    Before reporting spans to e.g. Zipkin you can be interested in modifying that span in some way. - You can achieve that by using the SpanAdjuster interface.

    In Sleuth we’re generating spans with a fixed name. Some users want to modify the name depending on values -of tags. Implementation of the SpanAdjuster interface can be used to alter that name. Example:

    Example. If you register two beans of SpanAdjuster type:

    @Bean SpanAdjuster adjusterOne() {
    +}

    11.6 Custom service name

    By default, Sleuth assumes that, when you send a span to Zipkin, you want the span’s service name to be equal to the value of the spring.application.name property. +That is not always the case, though. +There are situations in which you want to explicitly provide a different service name for all spans coming from your application. +To achieve that, you can pass the following property to your application to override that value (the example is for a service named myService):

    spring.zipkin.service.name: myService

    11.7 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.

    In Sleuth, we generat 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.

    The following example shows how to register two beans that implement SpanAdjuster:

    @Bean SpanAdjuster adjusterOne() {
     	return span -> span.toBuilder().name("foo").build();
     }
     
     @Bean SpanAdjuster adjusterTwo() {
     	return span -> span.toBuilder().name(span.name() + " bar").build();
    -}

    This will lead in changing the name of the reported span to foo bar, just before it gets reported (e.g. to Zipkin).

    12.6 Host locator

    [Important]Important

    This section is about defining host from service discovery. It’s NOT -about finding Zipkin in service discovery.

    In order to define the host that is corresponding to a particular span we need to resolve the host name -and port. The default approach is to take it from server properties. If those for some reason are not set -then we’re trying to retrieve the host name from the network interfaces.

    If you have the discovery client enabled and prefer to retrieve the host address from the registered -instance in a service registry then you have to set the property (it’s applicable for both HTTP and -Stream based span reporting).

    spring.zipkin.locator.discovery.enabled: true

    13. Sending spans to Zipkin

    By default if you add spring-cloud-starter-zipkin as a dependency to your project, -when the span is closed, it will be sent to Zipkin over HTTP. The communication -is asynchronous. You can configure the URL by setting the spring.zipkin.baseUrl -property as follows:

    spring.zipkin.baseUrl: http://192.168.99.100:9411/

    If you want to find Zipkin via service discovery it’s enough to pass the -Zipkin’s service id inside the URL (example for zipkinserver service id)

    spring.zipkin.baseUrl: http://zipkinserver/

    If you have web, rabbit or kafka together on the classpath, you might need -to pick the means by which you would like to send spans to zipkin. To do that -just set either web, rabbit or kafka to the spring.zipkin.sender.type property. -Example for web:

    spring.zipkin.sender.type: web

    14. Zipkin Stream Span Consumer

    [Important]Important

    The suggested approach is to use the Zipkin’s -native support for message based span sending. Starting from -Edgware Zipkin Stream server is deprecated and in Finchley -it got removed.

    Please refer to the Dalston Documentaion -on how to create a Stream Zipkin server.

    15. Integrations

    15.1 OpenTracing

    Spring Cloud Sleuth is OpenTracing compatible. If you have -OpenTracing on the classpath we will automatically register the OpenTracing -Tracer bean. If you wish to disable this just set spring.sleuth.opentracing.enabled to false

    15.2 Runnable and Callable

    If you’re wrapping your logic in Runnable or Callable it’s enough to wrap those classes in their Sleuth representative.

    Example for Runnable:

    Runnable runnable = new Runnable() {
    +}

    The preceding example results in changing the name of the reported span to foo bar, just before it gets reported (for example, to Zipkin).

    11.8 Host Locator

    [Important]Important

    This section is about defining host from service discovery. +It is NOT about finding Zipkin through service discovery.

    To define the host that corresponds to a particular span, we need to resolve the host name and port. +The default approach is to take these values from server properties. +If those are not set, we try to retrieve the host name from the network interfaces.

    If you have the discovery client enabled and prefer to retrieve the host address from the registered instance in a service registry, you have to set the spring.zipkin.locator.discovery.enabled property (it is applicable for both HTTP-based and Stream-based span reporting), as follows:

    spring.zipkin.locator.discovery.enabled: true

    12. Sending Spans to Zipkin

    By default, if you add spring-cloud-starter-zipkin as a dependency to your project, when the span is closed, it is sent to Zipkin over HTTP. +The communication is asynchronous. +You can configure the URL by setting the spring.zipkin.baseUrl property, as follows:

    spring.zipkin.baseUrl: http://192.168.99.100:9411/

    If you want to find Zipkin through service discovery, you can pass the Zipkin’s service ID inside the URL, as shown in the following example for zipkinserver service ID:

    spring.zipkin.baseUrl: http://zipkinserver/

    If you have web, rabbit, or kafka together on the classpath, you might need to pick the means by which you would like to send spans to zipkin. +To do so, set web, rabbit, or kafka to the spring.zipkin.sender.type property. +The following example shows setting the sender type for web:

    spring.zipkin.sender.type: web

    13. Zipkin Stream Span Consumer

    [Important]Important

    We recommend using Zipkin’s native support for message-based span sending. +Starting from the Edgware release, the Zipkin Stream server is deprecated. +In the Finchley release, it got removed.

    See the Dalston Documentaion +for how to create a Stream Zipkin server.

    14. Integrations

    14.1 OpenTracing

    Spring Cloud Sleuth is compatible with OpenTracing. +If you have OpenTracing on the classpath, we automatically register the OpenTracing Tracer bean. +If you wish to disable this, set spring.sleuth.opentracing.enabled to false

    14.2 Runnable and Callable

    If you wrap your logic in Runnable or Callable, you can wrap those classes in their Sleuth representative, as shown in the following example for Runnable:

    Runnable runnable = new Runnable() {
     	@Override
     	public void run() {
     		// do some work
    @@ -741,7 +658,7 @@ Runnable traceRunnable = "calculateTax");
     // Wrapping `Runnable` with `Tracing`. That way the current span will be available
     // in the thread of `Runnable`
    -Runnable traceRunnableFromTracer = tracing.currentTraceContext().wrap(runnable);

    Example for Callable:

    Callable<String> callable = new Callable<String>() {
    +Runnable traceRunnableFromTracer = tracing.currentTraceContext().wrap(runnable);

    The following example shows how to do so for Callable:

    Callable<String> callable = new Callable<String>() {
     	@Override
     	public String call() throws Exception {
     		return someLogic();
    @@ -757,48 +674,47 @@ Callable<String> traceCallable = "calculateTax");
     // Wrapping `Callable` with `Tracing`. That way the current span will be available
     // in the thread of `Callable`
    -Callable<String> traceCallableFromTracer = tracing.currentTraceContext().wrap(callable);

    That way you will ensure that a new Span is created and closed for each execution.

    15.3 Hystrix

    15.3.1 Custom Concurrency Strategy

    We’re registering a custom HystrixConcurrencyStrategy -that wraps all Callable instances into their Sleuth representative - -the TraceCallable. The strategy either starts or continues a span depending on the fact whether tracing was already going -on before the Hystrix command was called. To disable the custom Hystrix Concurrency Strategy set the spring.sleuth.hystrix.strategy.enabled to false.

    15.3.2 Manual Command setting

    Assuming that you have the following HystrixCommand:

    HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {
    +Callable<String> traceCallableFromTracer = tracing.currentTraceContext().wrap(callable);

    That way, you ensure that a new span is created and closed for each execution.

    14.3 Hystrix

    14.3.1 Custom Concurrency Strategy

    We register a custom HystrixConcurrencyStrategy called TraceCallable that wraps all Callable instances in their Sleuth representative. +The strategy either starts or continues a span, depending on whether tracing was already going on before the Hystrix command was called. +To disable the custom Hystrix Concurrency Strategy, set the spring.sleuth.hystrix.strategy.enabled to false.

    14.3.2 Manual Command setting

    Assume that you have the following HystrixCommand:

    HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {
     	@Override
     	protected String run() throws Exception {
     		return someLogic();
     	}
    -};

    In order to pass the tracing information you have to wrap the same logic in the Sleuth version of the HystrixCommand which is the -TraceCommand:

    TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, traceKeys, setter) {
    +};

    To pass the tracing information, you have to wrap the same logic in the Sleuth version of the HystrixCommand, which is called +TraceCommand, as shown in the following example:

    TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, traceKeys, setter) {
     	@Override
     	public String doRun() throws Exception {
     		return someLogic();
     	}
    -};

    15.4 RxJava

    We’re registering a custom RxJavaSchedulersHook -that wraps all Action0 instances into their Sleuth representative - -the TraceAction. The hook either starts or continues a span depending on the fact whether tracing was already going -on before the Action was scheduled. To disable the custom RxJavaSchedulersHook set the spring.sleuth.rxjava.schedulers.hook.enabled to false.

    You can define a list of regular expressions for thread names, for which you don’t want a Span to be created. Just provide a comma separated list -of regular expressions in the spring.sleuth.rxjava.schedulers.ignoredthreads property.

    15.5 HTTP integration

    Features from this section can be disabled by providing the spring.sleuth.web.enabled property with value equal to false.

    15.5.1 HTTP Filter

    Via the TraceFilter all sampled incoming requests result in creation of a Span. That Span’s name is http: + the path to which - the request was sent. E.g. if the request was sent to /foo/bar then the name will be http:/foo/bar. You can configure which URIs you would - like to skip via the spring.sleuth.web.skipPattern property. If you have ManagementServerProperties on classpath then - its value of contextPath gets appended to the provided skip pattern. If you want to reuse the - Sleuth’s default skip patterns and just append your own, pass those patterns via - the spring.sleuth.web.additionalSkipPattern.

    15.5.2 HandlerInterceptor

    Since we want the span names to be precise we’re using a TraceHandlerInterceptor that either wraps an - existing HandlerInterceptor or is added directly to the list of existing HandlerInterceptors. The - TraceHandlerInterceptor adds a special request attribute to the given HttpServletRequest. If the - the TraceFilter doesn’t see this attribute set it will create a "fallback" span which is an additional - span created on the server side so that the trace is presented properly in the UI. Seeing that most likely - signifies that there is a missing instrumentation. In that case please file an issue in Spring Cloud Sleuth.

    15.5.3 Async Servlet support

    If your controller returns a Callable or a WebAsyncTask Spring Cloud Sleuth will continue the existing span instead of creating a new one.

    15.5.4 WebFlux support

    Via the TraceWebFilter all sampled incoming requests result in creation of a Span. That Span’s name is http: + the path to which - the request was sent. E.g. if the request was sent to /foo/bar then the name will be http:/foo/bar. You can configure which URIs you would - like to skip via the spring.sleuth.web.skipPattern property. If you have ManagementServerProperties on classpath then - its value of contextPath gets appended to the provided skip pattern. If you want to reuse the - Sleuth’s default skip patterns and just append your own, pass those patterns via - the spring.sleuth.web.additionalSkipPattern.

    15.6 HTTP client integration

    15.6.1 Synchronous Rest Template

    We’re injecting a RestTemplate interceptor that ensures that all the tracing information is passed to the requests. Each time a -call is made a new Span is created. It gets closed upon receiving the response. In order to block the synchronous RestTemplate features -just set spring.sleuth.web.client.enabled to false.

    [Important]Important

    You have to register RestTemplate as a bean so that the interceptors will get injected. -If you create a RestTemplate instance with a new keyword then the instrumentation WILL NOT work.

    15.6.2 Asynchronous Rest Template

    [Important]Important

    Starting with Sleuth 2.0.0 we no longer register -a bean of AsyncRestTemplate type. It’s up to you to create such -a bean. Then we will instrument it.

    To block the AsyncRestTemplate features set spring.sleuth.web.async.client.enabled to false. -To disable creation of the default TraceAsyncClientHttpRequestFactoryWrapper set spring.sleuth.web.async.client.factory.enabled -to false. If you don’t want to create AsyncRestClient at all set spring.sleuth.web.async.client.template.enabled to false.

    Multiple Asynchronous Rest Templates

    Sometimes you need to use multiple implementations of Asynchronous Rest Template. In the following snippet you -can see an example of how to set up such a custom AsyncRestTemplate.

    @Configuration
    +};

    14.4 RxJava

    We registering a custom RxJavaSchedulersHook that wraps all Action0 instances in their Sleuth representative, which is called TraceAction. +The hook either starts or continues a span, depending on whether tracing was already going on before the Action was scheduled. +To disable the custom RxJavaSchedulersHook, set the spring.sleuth.rxjava.schedulers.hook.enabled to false.

    You can define a list of regular expressions for thread names for which you do not want spans to be created. +To do so, provide a comma-separated list of regular expressions in the spring.sleuth.rxjava.schedulers.ignoredthreads property.

    14.5 HTTP integration

    Features from this section can be disabled by setting the spring.sleuth.web.enabled property with value equal to false.

    14.5.1 HTTP Filter

    Through the TraceFilter, all sampled incoming requests result in creation of a Span. +That Span’s name is http: + the path to which the request was sent. +For example, if the request was sent to /this/this then the name will be http:/this/that. +You can configure which URIs you would like to skip by setting the spring.sleuth.web.skipPattern property. +If you have ManagementServerProperties on classpath, its value of contextPath gets appended to the provided skip pattern. +If you want to reuse the Sleuth’s default skip patterns and just append your own, pass those patterns by using the spring.sleuth.web.additionalSkipPattern.

    14.5.2 HandlerInterceptor

    Since we want the span names to be precise, we use a TraceHandlerInterceptor that either wraps an existing HandlerInterceptor or is added directly to the list of existing HandlerInterceptors. +The TraceHandlerInterceptor adds a special request attribute to the given HttpServletRequest. +If the the TraceFilter does not see this attribute, it creates a "fallback" span, which is an additional span created on the server side so that the trace is presented properly in the UI. +If that happens, there is probably missing instrumentation. +In that case, please file an issue in Spring Cloud Sleuth.

    14.5.3 Async Servlet support

    If your controller returns a Callable or a WebAsyncTask, Spring Cloud Sleuth continues the existing span instead of creating a new one.

    14.5.4 WebFlux support

    Through TraceWebFilter, all sampled incoming requests result in creation of a Span. +That Span’s name is http: + the path to which the request was sent. +For example, if the request was sent to /this/that, the name is http:/this/that. +You can configure which URIs you would like to skip by using the spring.sleuth.web.skipPattern property. +If you have ManagementServerProperties on the classpath, its value of contextPath gets appended to the provided skip pattern. +If you want to reuse Sleuth’s default skip patterns and append your own, pass those patterns by using the spring.sleuth.web.additionalSkipPattern.

    14.6 HTTP Client Integration

    14.6.1 Synchronous Rest Template

    We inject a RestTemplate interceptor to ensure that all the tracing information is passed to the requests. +Each time a call is made, a new Span is created. +It gets closed upon receiving the response. +To block the synchronous RestTemplate features, set spring.sleuth.web.client.enabled to false.

    [Important]Important

    You have to register RestTemplate as a bean so that the interceptors get injected. +If you create a RestTemplate instance with a new keyword, the instrumentation does NOT work.

    14.6.2 Asynchronous Rest Template

    [Important]Important

    Starting with Sleuth 2.0.0, we no longer register a bean of AsyncRestTemplate type. +It is up to you to create such a bean. +Then we instrument it.

    To block the AsyncRestTemplate features, set spring.sleuth.web.async.client.enabled to false. +To disable creation of the default TraceAsyncClientHttpRequestFactoryWrapper, set spring.sleuth.web.async.client.factory.enabled +to false. +If you do not want to create AsyncRestClient at all, set spring.sleuth.web.async.client.template.enabled to false.

    Multiple Asynchronous Rest Templates

    Sometimes you need to use multiple implementations of the Asynchronous Rest Template. +In the following snippet, you can see an example of how to set up such a custom AsyncRestTemplate:

    @Configuration
     @EnableAutoConfiguration
     static class Config {
     
    @@ -818,34 +734,30 @@ can see an example of how to set up such a custom AsyncRes
     		//CUSTOMIZE HERE
     		return factory;
     	}
    -}

    15.6.3 WebClient

    We inject a ExchangeFilterFunction implementation that creates a span and via on success and on -error callbacks takes care of closing client side spans.

    [Important]Important

    You have to register WebClient as a bean so that the tracing instrumention gets applied. -If you create a WebClient instance with a new keyword then the instrumentation WILL NOT work.

    15.6.4 Traverson

    If you’re using the Traverson library -it’s enough for you to inject a RestTemplate as a bean into your Traverson object. Since RestTemplate -is already intercepted, you will get full support of tracing in your client. Below you can find a pseudo code -of how to do that:

    @Autowired RestTemplate restTemplate;
    +}

    14.6.3 WebClient

    We inject a ExchangeFilterFunction implementation that creates a span and, through on-success and on-error callbacks, takes care of closing client-side spans.

    [Important]Important

    You have to register WebClient as a bean so that the tracing instrumentation gets applied. +If you create a WebClient instance with a new keyword, the instrumentation does NOT work.

    14.6.4 Traverson

    If you use the Traverson library, you can inject a RestTemplate as a bean into your Traverson object. +Since RestTemplate is already intercepted, you get full support for tracing in your client. The following pseudo code +shows how to do that:

    @Autowired RestTemplate restTemplate;
     
     Traverson traverson = new Traverson(URI.create("http://some/address"),
         MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8).setRestOperations(restTemplate);
    -// use Traverson

    15.7 Feign

    By default Spring Cloud Sleuth provides integration with feign via the TraceFeignClientAutoConfiguration. You can disable it entirely -by setting spring.sleuth.feign.enabled to false. If you do so then no Feign related instrumentation will take place.

    Part of Feign instrumentation is done via a FeignBeanPostProcessor. You can disable it by providing the spring.sleuth.feign.processor.enabled equal to false. -If you set it like this then Spring Cloud Sleuth will not instrument any of your custom Feign components. All the default instrumentation -however will be still there.

    15.8 Asynchronous communication

    15.8.1 @Async annotated methods

    In Spring Cloud Sleuth we’re instrumenting async related components so that the tracing information is passed between threads. -You can disable this behaviour by setting the value of spring.sleuth.async.enabled to false.

    If you annotate your method with @Async then we’ll automatically create a new Span with the following characteristics:

    • if the method is annotated with @SpanName then the value of the annotation will be the Span’s name
    • if the method is not annotated with @SpanName the Span name will be the annotated method name
    • the Span will be tagged with that method’s class name and the method name too

    15.8.2 @Scheduled annotated methods

    In Spring Cloud Sleuth we’re instrumenting scheduled method execution so that the tracing information is passed between threads. You can disable this behaviour -by setting the value of spring.sleuth.scheduled.enabled to false.

    If you annotate your method with @Scheduled then we’ll automatically create a new Span with the following characteristics:

    • the Span name will be the annotated method name
    • the Span will be tagged with that method’s class name and the method name too

    If you want to skip Span creation for some @Scheduled annotated classes you can set the -spring.sleuth.scheduled.skipPattern with a regular expression that will match the fully qualified name of the -@Scheduled annotated class.

    [Tip]Tip

    If you are using spring-cloud-sleuth-stream and spring-cloud-netflix-hystrix-stream together, Span will be created for -each Hystrix metrics and sent to Zipkin. This may be annoying. You can prevent this by setting -spring.sleuth.scheduled.skipPattern=org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask

    15.8.3 Executor, ExecutorService and ScheduledExecutorService

    We’re providing LazyTraceExecutor, TraceableExecutorService and TraceableScheduledExecutorService. Those implementations -are creating Spans each time a new task is submitted, invoked or scheduled.

    Here you can see an example of how to pass tracing information with TraceableExecutorService when working with CompletableFuture:

    CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
    +// use Traverson

    14.7 Feign

    By default, Spring Cloud Sleuth provides integration with Feign through TraceFeignClientAutoConfiguration. +You can disable it entirely by setting spring.sleuth.feign.enabled to false. +If you do so, no Feign-related instrumentation take place.

    Part of Feign instrumentation is done through a FeignBeanPostProcessor. +You can disable it by setting spring.sleuth.feign.processor.enabled to false. +If you set it to false, Spring Cloud Sleuth does not instrument any of your custom Feign components. +However, all the default instrumentation is still there.

    14.8 Asynchronous Communication

    14.8.1 @Async Annotated methods

    In Spring Cloud Sleuth, we instrument async-related components so that the tracing information is passed between threads. +You can disable this behavior by setting the value of spring.sleuth.async.enabled to false.

    If you annotate your method with @Async, we automatically create a new Span with the following characteristics:

    • If the method is annotated with @SpanName, the value of the annotation is the Span’s name.
    • If the method is not annotated with @SpanName, the Span name is the annotated method name.
    • The span is tagged with the method’s class name and method name.

    14.8.2 @Scheduled Annotated Methods

    In Spring Cloud Sleuth, we instrument scheduled method execution so that the tracing information is passed between threads. +You can disable this behavior by setting the value of spring.sleuth.scheduled.enabled to false.

    If you annotate your method with @Scheduled, we automatically create a new span with the following characteristics:

    • The span name is the annotated method name.
    • The span is tagged with the method’s class name and method name.

    If you want to skip span creation for some @Scheduled annotated classes, you can set the spring.sleuth.scheduled.skipPattern with a regular expression that matches the fully qualified name of the @Scheduled annotated class.

    [Tip]Tip

    If you use spring-cloud-sleuth-stream and spring-cloud-netflix-hystrix-stream together, a span is created for each Hystrix metrics and sent to Zipkin. +This behavior may be annoying. +You can prevent it by setting spring.sleuth.scheduled.skipPattern=org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask.

    14.8.3 Executor, ExecutorService, and ScheduledExecutorService

    We provide LazyTraceExecutor, TraceableExecutorService, and TraceableScheduledExecutorService. Those implementations create spans each time a new task is submitted, invoked, or scheduled.

    The following example shows how to pass tracing information with TraceableExecutorService when working with CompletableFuture:

    CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
     	// perform some logic
     	return 1_000_000L;
     }, new TraceableExecutorService(beanFactory, executorService,
     		// 'calculateTax' explicitly names the span - this param is optional
    -		"calculateTax"));
    [Important]Important

    Sleuth doesn’t work with parallelStream() out of the box. If you want -to have the tracing information propagated through the stream you have to use the -approach with supplyAsync(...) as presented above.

    Customization of Executors

    Sometimes you need to set up a custom instance of the AsyncExecutor. In the following snippet you -can see an example of how to set up such a custom Executor.

    @Configuration
    +		"calculateTax"));
    [Important]Important

    Sleuth does not work with parallelStream() out of the box. +If you want to have the tracing information propagated through the stream, you have to use the approach with supplyAsync(...), as shown earlier.

    Customization of Executors

    Sometimes, you need to set up a custom instance of the AsyncExecutor. +The following example shows how to set up such a custom Executor:

    @Configuration
     @EnableAutoConfiguration
     @EnableAsync
     static class CustomExecutorConfig extends AsyncConfigurerSupport {
    @@ -863,9 +775,10 @@ can see an example of how to set up such a custom Executor
     		executor.initialize();
     		return new LazyTraceExecutor(this.beanFactory, executor);
     	}
    -}

    15.9 Messaging

    Spring Cloud Sleuth integrates with Spring Integration. It creates spans for publish and -subscribe events. To disable Spring Integration instrumentation, set spring.sleuth.integration.enabled to false.

    You can provide the spring.sleuth.integration.patterns pattern to explicitly -provide the names of channels that you want to include for tracing. By default all channels -are included.

    [Important]Important

    When using the Executor to build a Spring Integration IntegrationFlow remember to use the untraced version of the Executor. -Decorating Spring Integration Executor Channel with TraceableExecutorService will cause the spans to be improperly closed.

    15.10 Zuul

    We’re instrumenting the Zuul Ribbon integration by enriching the Ribbon requests with tracing information. -To disable Zuul support set the spring.sleuth.zuul.enabled property to false.

    16. Running examples

    You can find the running examples deployed in the Pivotal Web Services. Check them out in the following links:

    \ No newline at end of file +}

    14.9 Messaging

    Spring Cloud Sleuth integrates with Spring Integration. +It creates spans for publish and subscribe events. +To disable Spring Integration instrumentation, set spring.sleuth.integration.enabled to false.

    You can provide the spring.sleuth.integration.patterns pattern to explicitly provide the names of channels that you want to include for tracing. +By default, all channels are included.

    [Important]Important

    When using the Executor to build a Spring Integration IntegrationFlow, you must use the untraced version of the Executor. +Decorating the Spring Integration Executor Channel with TraceableExecutorService causes the spans to be improperly closed.

    14.10 Zuul

    We instrument the Zuul Ribbon integration by enriching the Ribbon requests with tracing information. +To disable Zuul support, set the spring.sleuth.zuul.enabled property to false.

    15. Running examples

    You can see the running examples deployed in the Pivotal Web Services. +Check them out at the following links:

    \ No newline at end of file diff --git a/spring-cloud-sleuth.xml b/spring-cloud-sleuth.xml index a5e2702ec..69381257f 100644 --- a/spring-cloud-sleuth.xml +++ b/spring-cloud-sleuth.xml @@ -7,7 +7,7 @@ 2018-02-28 -Adrian Cole, Spencer Gibb, Marcin Grzejszczak, Dave Syer +Adrian Cole, Spencer Gibb, Marcin Grzejszczak, Dave Syer, Jay Bryant A @@ -22,44 +22,42 @@
    Terminology Spring Cloud Sleuth borrows Dapper’s terminology. -Span: The basic unit of work. For example, sending an RPC is a new span, as is sending a response to an -RPC. Span’s are identified by a unique 64-bit ID for the span and another 64-bit ID for the trace the span -is a part of. Spans also have other data, such as descriptions, timestamped events, key-value -annotations (tags), the ID of the span that caused them, and process ID’s (normally IP address). -Spans are started and stopped, and they keep track of their timing information. Once you create a -span, you must stop it at some point in the future. +Span: The basic unit of work. For example, sending an RPC is a new span, as is sending a response to an RPC. +Spans are identified by a unique 64-bit ID for the span and another 64-bit ID for the trace the span is a part of. +Spans also have other data, such as descriptions, timestamped events, key-value annotations (tags), the ID of the span that caused them, and process IDs (normally IP addresses). +Spans can be started and stopped, and they keep track of their timing information. +Once you create a span, you must stop it at some point in the future. -The initial span that starts a trace is called a root span. The value of span id -of that span is equal to trace id. +The initial span that starts a trace is called a root span. The value of the ID +of that span is equal to the trace ID. -Trace: A set of spans forming a tree-like structure. For example, if you are running a distributed -big-data store, a trace might be formed by a put request. -Annotation: is used to record existence of an event in time. With -Brave instrumentation we no longer need to set special events -for Zipkin to understand who the client and server are and where -the request started and where it has ended. For learning purposes -however we will mark these events to highlight what kind +Trace: A set of spans forming a tree-like structure. +For example, if you run a distributed big-data store, a trace might be formed by a PUT request. +Annotation: Used to record the existence of an event in time. With +Brave instrumentation, we no longer need to set special events +for Zipkin to understand who the client and server are, where +the request started, and where it ended. For learning purposes, +however, we mark these events to highlight what kind of an action took place. -cs - Client Sent - The client has made a request. This annotation depicts the start of the span. +cs: Client Sent. The client has made a request. This annotation indicates the start of the span. -sr - Server Received - The server side got the request and will start processing it. -If one subtracts the cs timestamp from this timestamp one will receive the network latency. +sr: Server Received: The server side got the request and started processing it. +Subtracting the cs timestamp from this timestamp reveals the network latency. -ss - Server Sent - Annotated upon completion of request processing (when the response -got sent back to the client). If one subtracts the sr timestamp from this timestamp one -will receive the time needed by the server side to process the request. +ss: Server Sent. Annotated upon completion of request processing (when the response got sent back to the client). +Subtracting the sr timestamp from this timestamp reveals the time needed by the server side to process the request. -cr - Client Received - Signifies the end of the span. The client has successfully received the -response from the server side. If one subtracts the cs timestamp from this timestamp one -will receive the whole time needed by the client to receive the response from the server. +cr> Client Received. Signifies the end of the span. +The client has successfully received the response from the server side. +Subtracting the cs timestamp from this timestamp reveals the whole time needed by the client to receive the response from the server. -Visualization of what Span and Trace will look in a system together with the Zipkin annotations: +The following image shows how Span and Trace look in a system, together with the Zipkin annotations: @@ -68,13 +66,14 @@ will receive the whole time needed by the client to receive the response from th Trace Info propagation -Each color of a note signifies a span (7 spans - from A to G). If you have such information in the note: +Each color of a note signifies a span (there are seven spans - from A to G). +Consider the following note: Trace Id = X Span Id = D Client Sent -That means that the current span has Trace-Id set to X, Span-Id set to D. Also, the - Client Sent event took place. -This is how the visualization of the parent / child relationship of spans would look like: +This note indicats thatthe current span has Trace Id set to X and Span Id set to D. +Also, the Client Sent event took place. +The following image shows how parent-child relationships of spans look: @@ -86,10 +85,11 @@ Client Sent
    Purpose -In the following sections the example from the image above will be taken into consideration. +The following sections refer to the example shown in the preceding image.
    -Distributed tracing with Zipkin -Altogether there are 7 spans . If you go to traces in Zipkin you will see this number in the second trace: +Distributed Tracing with Zipkin +This example has seven spans. +If you go to traces in Zipkin, you can see this number in the second trace, as shown in the following image: @@ -98,7 +98,7 @@ Client Sent Traces -However if you pick a particular trace then you will see 4 spans: +However, if you pick a particular trace, you can see four spans, as shown in the following image: @@ -108,42 +108,44 @@ Client Sent -When picking a particular trace you will see merged spans. That means that if there were 2 spans sent to -Zipkin with Server Received and Server Sent / Client Received and Client Sent -annotations then they will presented as a single span. +When you pick a particular trace, you see merged spans. +That means that, if there were two spans sent to Zipkin with Server Received and Server Sent or Client Received and Client Sent annotations, they are presented as a single span. -Why is there a difference between the 7 and 4 spans in this case? +Why is there a difference between the seven and four spans in this case? -2 spans come from http:/start span. It has the Server Received (SR) and Server Sent (SS) annotations. +Two spans come from the http:/start span. It has the Server Received (sr) and Server Sent (ss) annotations. -2 spans come from the RPC call from service1 to service2 to the http:/foo endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service1 side. Server Received (SR) and Server Sent (SS) events took place -on the service2 side. Physically there are 2 spans but they form 1 logical span related to an RPC call. +Two spans come from the RPC call from service1 to service2 to the http:/foo endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service1 side. +Server Received (sr) and Server Sent (ss) events took place on the service2 side. +These two spans form one logical span related to an RPC call. -2 spans come from the RPC call from service2 to service3 to the http:/bar endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service2 side. Server Received (SR) and Server Sent (SS) events took place -on the service3 side. Physically there are 2 spans but they form 1 logical span related to an RPC call. +Two spans come from the RPC call from service2 to service3 to the http:/bar endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service2 side. +The Server Received (sr) and Server Sent (ss) events took place on the service3 side. +These two spans form one logical span related to an RPC call. -2 spans come from the RPC call from service2 to service4 to the http:/baz endpoint. The Client Sent (CS) -and Client Received (CR) events took place on service2 side. Server Received (SR) and Server Sent (SS) events took place -on the service4 side. Physically there are 2 spans but they form 1 logical span related to an RPC call. +Two spans come from the RPC call from service2 to service4 to the http:/baz endpoint. +The Client Sent (cs) and Client Received (cr) events took place on the service2 side. +Server Received (sr) and Server Sent (ss) events took place on the service4 side. +These two spans form one logical span related to an RPC call. -So if we count the physical spans we have 1 from http:/start, 2 from service1 calling service2, 2 form service2 -calling service3 and 2 from service2 calling service4. Altogether 7 spans. -Logically we see the information of Total Spans: 4 because we have 1 span related to the incoming request -to service1 and 3 spans related to RPC calls. +So, if we count the physical spans, we have one from http:/start, two from service1 calling service2, two from service2 +calling service3, and two from service2 calling service4. In sum, we have a total of seven spans. +Logically, we see the information of four total Spans because we have one span related to the incoming request +to service1 and three spans related to RPC calls.
    Visualizing errors -Zipkin allows you to visualize errors in your trace. When an exception was thrown and wasn’t caught then we’re -setting proper tags on the span which Zipkin can properly colorize. You could see in the list of traces one - trace that was in red color. That’s because there was an exception thrown. -If you click that trace then you’ll see a similar picture +Zipkin lets you visualize errors in your trace. +When an exception was thrown and was not caught, we set proper tags on the span, which Zipkin can then properly colorize. +You could see in the list of traces one trace that is red. That appears because an exception was thrown. +If you click that trace, you see a similar picture, as follows: @@ -152,7 +154,7 @@ setting proper tags on the span which Zipkin can properly colorize. You could se Error Traces -Then if you click on one of the spans you’ll see the following +If you then click on one of the spans, you see the following @@ -161,24 +163,19 @@ setting proper tags on the span which Zipkin can properly colorize. You could se Error Traces Info propagation -As you can see you can easily see the reason for an error and the whole stacktrace related to it. +The span shows the reason for the error and the whole stack trace related to it.
    -Distributed tracing with Brave -Starting with version 2.0.0, Spring Cloud Sleuth uses -Brave as the tracing library. That means -that Sleuth no longer takes care of storing the context but it delegates -that work to Brave. -Due to the fact that Sleuth had different naming / tagging -conventions than Brave, we’ve decided to follow the Brave’s -conventions from now on. However, if you want to use the legacy -Sleuth approaches, it’s enough to set the spring.sleuth.http.legacy.enabled property -to true. +Distributed Tracing with Brave +Starting with version 2.0.0, Spring Cloud Sleuth uses Brave as the tracing library. +Consequently, Sleuth no longer takes care of storing the context but delegates that work to Brave. +Due to the fact that Sleuth had different naming and tagging conventions than Brave, we decided to follow Brave’s conventions from now on. +However, if you want to use the legacy Sleuth approaches, you can set the spring.sleuth.http.legacy.enabled property to true.
    Live examples
    -Click Pivotal Web Services icon to see it live! +Click the Pivotal Web Services icon to see it live! @@ -187,7 +184,7 @@ to true.
    Click here to see it live! -The dependency graph in Zipkin would look like this: +The dependency graph in Zipkin should resemble the following image: @@ -197,7 +194,7 @@ to true.
    -Click Pivotal Web Services icon to see it live! +Click the Pivotal Web Services icon to see it live! @@ -209,7 +206,7 @@ to true.
    Log correlation -When grepping the logs of those four applications by trace id equal to e.g. 2485ec27856c56f4 one would get the following: +When using grep to read the logs of those four applications by scanning for a trace ID equal to (for example) 2485ec27856c56f4, you get output resembling the following: service1.log:2016-02-26 11:15:47.561 INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application : Hello from service1. Calling service2 service2.log:2016-02-26 11:15:47.710 INFO [service2,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.service2.Application : Hello from service2. Calling service3 and then service4 service3.log:2016-02-26 11:15:47.895 INFO [service3,2485ec27856c56f4,1210be13194bfe5,true] 68060 --- [nio-8083-exec-1] i.s.c.sleuth.docs.service3.Application : Hello from service3 @@ -217,9 +214,8 @@ service2.log:2016-02-26 11:15:47.924 INFO [service2,2485ec27856c56f4,9aa10ee6fb service4.log:2016-02-26 11:15:48.134 INFO [service4,2485ec27856c56f4,1b1845262ffba49d,true] 68061 --- [nio-8084-exec-1] i.s.c.sleuth.docs.service4.Application : Hello from service4 service2.log:2016-02-26 11:15:48.156 INFO [service2,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.service2.Application : Got response from service4 [Hello from service4] service1.log:2016-02-26 11:15:48.182 INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.service1.Application : Got response from service2 [Hello from service2, response from service3 [Hello from service3] and from service4 [Hello from service4]] -If you’re using a log aggregating tool like Kibana, -Splunk etc. you can order the events that took place. An example of -Kibana would look like this: +If you use a log aggregating tool (such as Kibana, Splunk, and others), you can order the events that took place. +An example from Kibana would resemble the following image: @@ -228,7 +224,7 @@ Kibana would look like this: Log correlation with Kibana -If you want to use Logstash here is the Grok pattern for Logstash: +If you want to use Logstash, the following listing shows the Grok pattern for Logstash: filter { # pattern matching logback pattern grok { @@ -236,7 +232,7 @@ Kibana would look like this: } } -If you want to use Grok together with the logs from Cloud Foundry you have to use this pattern: +If you want to use Grok together with the logs from Cloud Foundry, you have to use the following pattern: filter { # pattern matching logback pattern @@ -246,30 +242,19 @@ Kibana would look like this: }
    JSON Logback with Logstash -Often you do not want to store your logs in a text file but in a JSON file that Logstash can immediately pick. To do that you have to do the following (for readability -we’re passing the dependencies in the groupId:artifactId:version notation. -Dependencies setup - +Often, you do not want to store your logs in a text file but in a JSON file that Logstash can immediately pick. +To do so, you have to do the following (for readability, we pass the dependencies in the groupId:artifactId:version notation). +Dependencies Setup + -Ensure that Logback is on the classpath (ch.qos.logback:logback-core) +Ensure that Logback is on the classpath (ch.qos.logback:logback-core). -Add Logstash Logback encode - example for version 4.6 : net.logstash.logback:logstash-logback-encoder:4.6 +Add Logstash Logback encode. For example, to use version 4.6, add net.logstash.logback:logstash-logback-encoder:4.6. - -Logback setup -Below you can find an example of a Logback configuration (file named logback-spring.xml) that: - - -logs information from the application in a JSON format to a build/${spring.application.name}.json file - - -has commented out two additional appenders - console and standard log file - - -has the same logging pattern as the one presented in the previous section - - + +Logback Setup +Consider the following example of a Logback configuration file (named logback-spring.xml). <?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> @@ -346,41 +331,54 @@ we’re passing the dependencies in the groupId:artifactId:version< <!--<appender-ref ref="flatfile"/>--> </root> </configuration> +That Logback configuration file: + + +Logs information from the application in a JSON format to a build/${spring.application.name}.json file. + + +Has commented out two additional appenders: console and standard log file. + + +Has the same logging pattern as the one presented in the previous section. + + -If you’re using a custom logback-spring.xml then you have to pass the spring.application.name in -bootstrap instead of application property file. Otherwise your custom logback file won’t read the property properly. +If you use a custom logback-spring.xml, you must pass the spring.application.name in the bootstrap rather than the application property file. +Otherwise, your custom logback file does not properly read the property.
    Propagating Span Context -The span context is the state that must get propagated to any child Spans across process boundaries. +The span context is the state that must get propagated to any child spans across process boundaries. Part of the Span Context is the Baggage. The trace and span IDs are a required part of the span context. Baggage is an optional part. -Baggage is a set of key:value pairs stored in the span context. Baggage travels together with the trace -and is attached to every span. Spring Cloud Sleuth will understand that a header is baggage related if the HTTP - header is prefixed with baggage- and for messaging it starts with baggage_. +Baggage is a set of key:value pairs stored in the span context. +Baggage travels together with the trace and is attached to every span. +Spring Cloud Sleuth understands that a header is baggage-related if the HTTP header is prefixed with baggage- and, for messaging, it starts with baggage_. -There’s currently no limitation of the count or size of baggage items. However, keep in mind that -too many can decrease system throughput or increase RPC latency. In extreme cases, it could crash the app due -to exceeding transport-level message or header capacity. +There is currently no limitation of the count or size of baggage items. +However, keep in mind that too many can decrease system throughput or increase RPC latency. +In extreme cases, too much baggage can crash the application, due to exceeding transport-level message or header capacity. -Example of setting baggage on a span: +The following example shows setting baggage on a span: Span initialSpan = this.tracer.nextSpan().name("span").start(); try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(initialSpan)) { ExtraFieldPropagation.set("foo", "bar"); ExtraFieldPropagation.set("UPPER_CASE", "someValue"); } -
    -Baggage vs. Span Tags -Baggage travels with the trace (i.e. every child span contains the baggage of its parent). Zipkin has no knowledge of -baggage and will not even receive that information. -Tags are attached to a specific span - they are presented for that particular span only. However you -can search by tag to find the trace, where there exists a span having the searched tag value. -If you want to be able to lookup a span based on baggage, you should add corresponding entry as a tag in the root span. +
    +Baggage versus Span Tags +Baggage travels with the trace (every child span contains the baggage of its parent). +Zipkin has no knowledge of baggage and does not receive that information. +Tags are attached to a specific span. In other words, they are presented only for that particular span. +However, you can search by tag to find the trace, assuming a span having the searched tag value exists. +If you want to be able to lookup a span based on baggage, you should add a corresponding entry as a tag in the root span. -Remember that the span needs to be in scope! +The span must be in scope. +The following listing shows integration tests that use baggage: initialSpan.tag("foo", ExtraFieldPropagation.get(initialSpan.context(), "foo")); initialSpan.tag("UPPER_CASE", @@ -388,16 +386,16 @@ initialSpan.tag("UPPER_CASE",
    -
    -Adding to the project +
    +Adding Sleuth to the Project +This section addresses how to add Sleuth to your project with either Maven or Gradle. -To ensure that your application name is properly displayed in Zipkin - set the spring.application.name property in bootstrap.yml. +To ensure that your application name is properly displayed in Zipkin, set the spring.application.name property in bootstrap.yml.
    Only Sleuth (log correlation) -If you want to profit only from Spring Cloud Sleuth without the Zipkin integration just add -the spring-cloud-starter-sleuth module to your project. +If you want to use only Spring Cloud Sleuth without the Zipkin integration, add the spring-cloud-starter-sleuth module to your project. +The following example shows how to add Sleuth with Maven: Maven @@ -421,13 +419,13 @@ the spring-cloud-starter-sleuth module to your project. -In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM +We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself. -Add the dependency to spring-cloud-starter-sleuth +Add the dependency to spring-cloud-starter-sleuth. +The following example shows how to add Sleuth with Gradle: Gradle @@ -444,17 +442,17 @@ dependencies { -In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM +We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself. -Add the dependency to spring-cloud-starter-sleuth +Add the dependency to spring-cloud-starter-sleuth.
    Sleuth with Zipkin via HTTP -If you want both Sleuth and Zipkin just add the spring-cloud-starter-zipkin dependency. +If you want both Sleuth and Zipkin, add the spring-cloud-starter-zipkin dependency. +The following example shows how to do so for Maven: Maven @@ -478,13 +476,13 @@ the Spring BOM -In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM +We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself. -Add the dependency to spring-cloud-starter-zipkin +Add the dependency to spring-cloud-starter-zipkin. +The following example shows how to do so for Gradle: Gradle @@ -501,21 +499,23 @@ dependencies { -In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM +We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself. -Add the dependency to spring-cloud-starter-zipkin +Add the dependency to spring-cloud-starter-zipkin.
    -
    -Sleuth with Zipkin via RabbitMQ or Kafka -If you want to use RabbitMQ or Kafka instead of http, add the spring-rabbit or spring-kafka -dependencies. The default destination name is zipkin. -Note: spring-cloud-sleuth-stream is deprecated and incompatible with these destinations -If you want Sleuth over RabbitMQ add the spring-cloud-starter-zipkin and spring-rabbit +
    +Sleuth with Zipkin over RabbitMQ or Kafka +If you want to use RabbitMQ or Kafka instead of HTTP, add the spring-rabbit or spring-kafka dependency. +The default destination name is zipkin. + +spring-cloud-sleuth-stream is deprecated and incompatible with these destinations. + +If you want Sleuth over RabbitMQ, add the spring-cloud-starter-zipkin and spring-rabbit dependencies. +The following example shows how to do so for Gradle: Maven @@ -543,14 +543,13 @@ dependencies. -In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM +We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself. -Add the dependency to spring-cloud-starter-zipkin - that way all dependent dependencies will be downloaded +Add the dependency to spring-cloud-starter-zipkin. That way, all nested dependencies get downloaded. -To automatically configure rabbit, simply add the spring-rabbit dependency +To automatically configure RabbitMQ, add the spring-rabbit dependency. @@ -570,22 +569,21 @@ dependencies { -In order not to pick versions by yourself it’s much better if you add the dependency management via -the Spring BOM +We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself. -Add the dependency to spring-cloud-starter-zipkin - that way all dependent dependencies will be downloaded +Add the dependency to spring-cloud-starter-zipkin. That way, all nested dependencies get downloaded. -To automatically configure rabbit, simply add the spring-rabbit dependency +To automatically configure RabbitMQ, add the spring-rabbit dependency.
    -Additional resources -Marcin Grzejszczak talking about Spring Cloud Sleuth and Zipkin +Additional Resources +You can watch a video of Marcin Grzejszczak talking about Spring Cloud Sleuth and Zipkin: click here to see the video @@ -593,114 +591,117 @@ the Spring BOM Features -Adds trace and span ids to the Slf4J MDC, so you can extract all the logs from a given trace or span in a log aggregator. Example logs: +Adds trace and span IDs to the Slf4J MDC, so you can extract all the logs from a given trace or span in a log aggregator, as shown in the following example logs: 2016-02-02 15:30:57.902 INFO [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ... 2016-02-02 15:30:58.372 ERROR [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ... 2016-02-02 15:31:01.936 INFO [bar,46ab0d418373cbc9,46ab0d418373cbc9,false] 23030 --- [nio-8081-exec-4] ... -notice the [appname,traceId,spanId,exportable] entries from the MDC: +Notice the [appname,traceId,spanId,exportable] entries from the MDC: -spanId - the id of a specific operation that took place +spanId: The ID of a specific operation that took place. -appname - the name of the application that logged the span +appname: The name of the application that logged the span. -traceId - the id of the latency graph that contains the span +traceId: The ID of the latency graph that contains the span. -exportable - whether the log should be exported to Zipkin or not. When would you like the span not to be -exportable? In the case in which you want to wrap some operation in a Span and have it written to the logs -only. +exportable: Whether the log should be exported to Zipkin. +When would you like the span not to be exportable? +When you want to wrap some operation in a Span and have it written to the logs only. -Provides an abstraction over common distributed tracing data models: traces, spans (forming a DAG), annotations, -key-value annotations. Loosely based on HTrace, but Zipkin (Dapper) compatible. +Provides an abstraction over common distributed tracing data models: traces, spans (forming a DAG), annotations, and key-value annotations. +Spring Cloud Slwuth is loosely based on HTrace but is compatible with Zipkin (Dapper). -Sleuth records timing information to aid in latency analysis. Using sleuth, you can pinpoint causes of -latency in your applications. Sleuth is written to not log too much, and to not cause your production application to crash. +Sleuth records timing information to aid in latency analysis. +By using sleuth, you can pinpoint causes of latency in your applications. + + +Sleuth is written to not log too much and to not cause your production application to crash. +To that end, Sleuth: -propagates structural data about your call-graph in-band, and the rest out-of-band. +Propagates structural data about your call graph in-band and the rest out-of-band. -includes opinionated instrumentation of layers such as HTTP +Includes opinionated instrumentation of layers such as HTTP. -includes sampling policy to manage volume +Includes a sampling policy to manage volume. -can report to a Zipkin system for query and visualization +Can report to a Zipkin system for query and visualization. -Instruments common ingress and egress points from Spring applications (servlet filter, async endpoints, -rest template, scheduled actions, message channels, zuul filters, feign client). +Instruments common ingress and egress points from Spring applications (servlet filter, async endpoints, rest template, scheduled actions, message channels, Zuul filters, and Feign client). -Sleuth includes default logic to join a trace across http or messaging boundaries. For example, http propagation -works via Zipkin-compatible request headers. This propagation logic is defined and customized via -SpanInjector and SpanExtractor implementations. +Sleuth includes default logic to join a trace across HTTP or messaging boundaries. +For example, HTTP propagation works over Zipkin-compatible request headers. +This propagation logic is defined and customized through SpanInjector and SpanExtractor implementations. -Sleuth gives you the possibility to propagate context (also known as baggage) between processes. That means that if you set on a Span -a baggage element then it will be sent downstream either via HTTP or messaging to other processes. +Sleuth can propagate context (also known as baggage) between processes. +Consequently, if you set a baggage element on a Span, it is sent downstream to other processes over either HTTP or messaging. -Provides a way to create / continue spans and add tags and logs via annotations. +Provides a way to create or continue spans and add tags and logs through annotations. -If spring-cloud-sleuth-zipkin is on the classpath then the app will generate and collect Zipkin-compatible traces. -By default it sends them via HTTP to a Zipkin server on localhost (port 9411). -Configure the location of the service using spring.zipkin.baseUrl. +If spring-cloud-sleuth-zipkin is on the classpath, the app generates and collects Zipkin-compatible traces. +By default, it sends them over HTTP to a Zipkin server on localhost (port 9411). +You can configure the location of the service by setting spring.zipkin.baseUrl. -If you depend on spring-rabbit or spring-kafka your app will send traces to a broker instead of http. - - -Note: spring-cloud-sleuth-stream is deprecated and should no longer be used. +If you depend on spring-rabbit or spring-kafka, your app sends traces to a broker instead of HTTP. +** + + +spring-cloud-sleuth-stream is deprecated and should no longer be used. + + -Spring Cloud Sleuth is OpenTracing compatible +Spring Cloud Sleuth is OpenTracing compatible. -If using Zipkin, configure the percentage of spans exported using spring.sleuth.sampler.percentage -(default 0.1, i.e. 10%). Otherwise you might think that Sleuth is not working cause it’s omitting some spans. +If you use Zipkin, configure the percentage of spans exported by setting spring.sleuth.sampler.percentage +(default: 0.1, which is 10 percent). Otherwise, you might think that Sleuth is not working be cause it omits some spans. -the SLF4J MDC is always set and logback users will immediately see the trace and span ids in logs per the example - above. Other logging systems have to configure their own formatter to get the same result. The default is - logging.pattern.level set to %5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] - (this is a Spring Boot feature for logback users). - This means that if you’re not using SLF4J this pattern WILL NOT be automatically applied. +The SLF4J MDC is always set and logback users immediately see the trace and span IDs in logs per the example +shown earlier. +Other logging systems have to configure their own formatter to get the same result. +The default is as follows: +logging.pattern.level set to %5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] +(this is a Spring Boot feature for logback users). +If you do not use SLF4J, this pattern is NOT automatically applied.
    Introduction to Brave -Starting with version 2.0.0 Spring Cloud Sleuth uses +Starting with version 2.0.0, Spring Cloud Sleuth uses Brave as the tracing library. -For your convenience we’re embedding part of the Brave’s docs here. +For your convenience, we embed part of the Brave’s docs here. -Brave is a library used to capture and report latency information about -distributed operations to Zipkin. Most users won’t use Brave directly, -rather libraries or frameworks than employ Brave on their behalf. -This module includes tracer creates and joins spans that model the -latency of potentially distributed work. It also includes libraries to -propagate the trace context over network boundaries, for example, via -http headers. +Brave is a library used to capture and report latency information about distributed operations to Zipkin. +Most users do not use Brave directly. They use libraries or frameworks rather than employ Brave on their behalf. +This module includes a tracer that creates and joins spans that model the latency of potentially distributed work. +It also includes libraries to propagate the trace context over network boundaries (for example, with HTTP headers).
    Tracing -Most importantly, you need a brave.Tracer, configured to [report to Zipkin] -(https://github.com/openzipkin/zipkin-reporter-java). -Here’s an example setup that sends trace data (spans) to Zipkin over -http (as opposed to Kafka). +Most importantly, you need a brave.Tracer, configured to report to Zipkin. +The following example setup sends trace data (spans) to Zipkin over HTTP (as opposed to Kafka): class MyClass { private final Tracer tracer; @@ -716,34 +717,28 @@ http (as opposed to Kafka). } } -If your span contains a name greater than 50 chars, then that name will -be truncated to 50 chars. Your names have to be explicit and concrete. Big names lead to -latency issues and sometimes even thrown exceptions. +If your span contains a name longer than 50 chars, then that name is truncated to 50 chars. +Your names have to be explicit and concrete. +Big names lead to latency issues and sometimes even thrown exceptions. -
    -
    -Tracing -The tracer creates and joins spans that model the latency of potentially -distributed work. It can employ sampling to reduce overhead in process -or to reduce the amount of data sent to Zipkin. -Spans returned by a tracer report data to Zipkin when finished, or do -nothing if unsampled. After starting a span, you can annotate events of -interest or add tags containing details or lookup keys. -Spans have a context which includes trace identifiers that place it at -the correct spot in the tree representing the distributed operation. +The tracer creates and joins spans that model the latency of potentially distributed work. +It can employ sampling to reduce overhead during the process, to reduce the amount of data sent to Zipkin, or both. +Spans returned by a tracer report data to Zipkin when finished or do nothing if unsampled. +After starting a span, you can annotate events of interest or add tags containing details or lookup keys. +Spans have a context that includes trace identifiers that place the span at the correct spot in the tree representing the distributed operation.
    Local Tracing -When tracing local code, just run it inside a span. +When tracing local code, you can run it inside a span, as shown in the following example: Span span = tracer.newTrace().name("encode").start(); try { doSomethingExpensive(); } finally { span.finish(); } -In the above example, the span is the root of the trace. In many cases, -you will be a part of an existing trace. When this is the case, call -newChild instead of newTrace +In the preceding example, the span is the root of the trace. +In many cases, the span is part of an existing trace. +When this is the case, call newChild instead of newTrace, as shown in the following example: Span span = tracer.newChild(root.context()).name("encode").start(); try { doSomethingExpensive(); @@ -752,31 +747,27 @@ try { }
    -Customizing spans -Once you have a span, you can add tags to it, which can be used as lookup -keys or details. For example, you might add a tag with your runtime -version. +Customizing Spans +Once you have a span, you can add tags to it. +The tags can be used as lookup keys or details. +For example, you might add a tag with your runtime version, as shown in the following example: span.tag("clnt/finagle.version", "6.36.0"); -When exposing the ability to customize spans to third parties, prefer -brave.SpanCustomizer as opposed to brave.Span. The former is simpler to -understand and test, and doesn’t tempt users with span lifecycle hooks. +When exposing the ability to customize spans to third parties, prefer brave.SpanCustomizer as opposed to brave.Span. +The former is simpler to understand and test and does not tempt users with span lifecycle hooks. interface MyTraceCallback { void request(Request request, SpanCustomizer customizer); } -Since brave.Span implements brave.SpanCustomizer, it is just as easy for you -to pass to users. -Ex. +Since brave.Span implements brave.SpanCustomizer, you can pass it to users, as shown in the following example: for (MyTraceCallback callback : userCallbacks) { callback.request(request, span); }
    -Implicitly looking up the current span -Sometimes you won’t know if a trace is in progress or not, and you don’t -want users to do null checks. brave.CurrentSpanCustomizer adds to any -span that’s in progress or drops data accordingly. +Implicitly Looking up the Current Span +Sometimes, you do not know if a trace is in progress or not, and you do not want users to do null checks. +brave.CurrentSpanCustomizer handles this problem by adding data to any span that’s in progress or drops, as shown in the following example: Ex. -// user code can then inject this without a chance of it being null. +// The user code can then inject this without a chance of it being null. @Autowire SpanCustomizer span; void userCode() { @@ -786,12 +777,11 @@ void userCode() {
    RPC tracing -Check for instrumentation written here -and Zipkin’s list -before rolling your own RPC instrumentation! -RPC tracing is often done automatically by interceptors. Under the scenes, -they add tags and events that relate to their role in an RPC operation. -Here’s an example of a client span: + +Check for instrumentation written here and Zipkin’s list before rolling your own RPC instrumentation. + +RPC tracing is often done automatically by interceptors. Behind the scenes, they add tags and events that relate to their role in an RPC operation. +The following example shows how to add a client span: // before you send a request, add metadata that describes the operation span = tracer.newTrace().name("get").type(CLIENT); span.tag("clnt/finagle.version", "6.36.0"); @@ -812,11 +802,11 @@ span.annotate(Constants.WIRE_RECV); span.finish();
    One-Way tracing -Sometimes you need to model an asynchronous operation, where there is a -request, but no response. In normal RPC tracing, you use span.finish() -which indicates the response was received. In one-way tracing, you use -span.flush() instead, as you don’t expect a response. -Here’s how a client might model a one-way operation +Sometimes, you need to model an asynchronous operation where there is a +request but no response. In normal RPC tracing, you use span.finish() +to indicate that the response was received. In one-way tracing, you use +span.flush() instead, as you do not expect a response. +The following example shows how a client might model a one-way operation: // start a new span representing a client request oneWaySend = tracer.newSpan(parent).kind(Span.Kind.CLIENT); @@ -829,7 +819,7 @@ request.execute(); // start the client side and flush instead of finish oneWaySend.start().flush(); -And here’s how a server might handle this.. +The following example shows how a server might handle a one-way operation: // pull the context out of the incoming request extractor = tracing.propagation().extractor(Request::getHeader); @@ -845,27 +835,26 @@ oneWayReceive.start().flush(); // you should not modify this span anymore as it is complete. However, // you can create children to represent follow-up work. next = tracer.newSpan(oneWayReceive.context()).name("step2").start(); -Note The above propagation logic is a simplified version of our [http handlers](https://github.com/openzipkin/sleuth/tree/master/instrumentation/http#http-server). -There’s a working example of a one-way span [here](src/test/java/sleuth/features/async/OneWaySpanTest.java). + +The propagation logic shown in the preceding example is a simplified version of our [http handlers](https://github.com/openzipkin/sleuth/tree/master/instrumentation/http#http-server). + +You can find a working example of a one-way span [here](src/test/java/sleuth/features/async/OneWaySpanTest.java).
    Sampling -Sampling may be employed to reduce the data collected and reported out -of process. When a span isn’t sampled, it adds no overhead (noop). -Sampling is an up-front decision, meaning that the decision to report -data is made at the first operation in a trace, and that decision is -propagated downstream. -By default, there’s a global sampler that applies a single rate to all -traced operations. Tracer.Builder.sampler is how you indicate this, -and it defaults to trace every request. +Sampling may be employed to reduce the data collected and reported out of process. +When a span is not sampled, it adds no overhead (a noop). +Sampling is an up-front decision, meaning that the decision to report data is made at the first operation in a trace and that decision is propagated downstream. +By default, a global sampler applies a single rate to all traced operations. +Tracer.Builder.sampler controls this setting, and it defaults to tracing every request.
    Declarative sampling -Some need to sample based on the type or annotations of a java method. -Most users will use a framework interceptor which automates this sort of -policy. Here’s how they might work internally. +Some applications need to sample based on the type or annotations of a java method. +Most users use a framework interceptor to automate this sort of policy. +The following example shows how that might work internally: // derives a sample rate from an annotation on a java method DeclarativeSampler<Traced> sampler = DeclarativeSampler.create(Traced::sampleRate); @@ -881,11 +870,10 @@ public Object traceThing(ProceedingJoinPoint pjp, Traced traced) throws Throwabl
    Custom sampling -You may want to apply different policies depending on what the operation -is. For example, you might not want to trace requests to static resources -such as images, or you might want to trace all requests to a new api. -Most users will use a framework interceptor which automates this sort of -policy. Here’s how they might work internally. +Depending on what the operation is, you may want to apply different policies. +For example, you might not want to trace requests to static resources such as images, or you might want to trace all requests to a new api. +Most users use a framework interceptor to automate this sort of policy. +The following example shows how that might work internally: Span newTrace(Request input) { SamplingFlags flags = SamplingFlags.NONE; if (input.url().startsWith("/experimental")) { @@ -895,43 +883,37 @@ policy. Here’s how they might work internally. } return tracer.newTrace(flags); } -Note: the above is the basis for the built-in http sampler + +The preceding example forms the basis for the built-in http sampler. +
    Sampling in Spring Cloud Sleuth -Spring Cloud Sleuth by default sets all spans to non-exportable. -That means that you will see traces in logs, but not in any remote store. -For testing the default is often enough, and it probably is all you need -if you are only using the logs (e.g. with an ELK aggregator). If you are -exporting span data to Zipkin, there is also an Sampler.ALWAYS_SAMPLE -that exports everything and a ProbabilityBasedSampler that samples a -fixed fraction of spans. +By default Spring Cloud Sleuth sets all spans to non-exportable. +That means that traces appear in logs but not in any remote store. +For testing the default is often enough, and it probably is all you need if you use only the logs (for example, with an ELK aggregator). +If you export span data to Zipkin, there is also an Sampler.ALWAYS_SAMPLE setting that exports everything and a ProbabilityBasedSampler setting that samples a fixed fraction of spans. -The ProbabilityBasedSampler is the default if you are using -spring-cloud-sleuth-zipkin. You can -configure the exports using spring.sleuth.sampler.probability. The passed -value needs to be a double from 0.0 to 1.0. +The ProbabilityBasedSampler is the default if you use spring-cloud-sleuth-zipkin. +You can configure the exports by setting spring.sleuth.sampler.probability. +The passed value needs to be a double from 0.0 to 1.0. -A sampler can be installed just by creating a bean definition, e.g: +A sampler can be installed by creating a bean definition, as shown in the following example: @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } -You can set the HTTP header X-B3-Flags to 1 or when doing messaging you can -set spanFlags header to 1. Then the current span will be forced to be exportable -regardless of the sampling decision. +You can set the HTTP header X-B3-Flags to 1, or, when doing messaging, you can set the spanFlags header to 1. +Doing so forces the current span to be exportable regardless of the sampling decision.
    Propagation -Propagation is needed to ensure activity originating from the same root -are collected together in the same trace. The most common propagation -approach is to copy a trace context from a client sending an RPC request -to a server receiving it. -For example, when an downstream Http call is made, its trace context is -sent along with it, encoded as request headers: +Propagation is needed to ensure activities originating from the same root are collected together in the same trace. +The most common propagation approach is to copy a trace context from a client by sending an RPC request to a server receiving it. +For example, when a downstream HTTP call is made, its trace context is encoded as request headers and sent along with it, as shown in the following image: Client Span Server Span ┌──────────────────┐ ┌──────────────────┐ │ │ │ │ @@ -947,18 +929,16 @@ sent along with it, encoded as request headers: │ └──────────────┘ │ └───────────────────┘ │ └──────────────┘ │ │ │ │ │ └──────────────────┘ └──────────────────┘ -The names above are from B3 Propagation, -which is built-in to Brave and has implementations in many languages and -frameworks. -Most users will use a framework interceptor which automates propagation. -Here’s how they might work internally. -Here’s what client-side propagation might look like +The names above are from B3 Propagation, which is built-in to Brave and has implementations in many languages and frameworks. +Most users use a framework interceptor to automate propagation. +The next two examples show how that might work for a client and a server. +The following example shows how client-side propagation might work: // configure a function that injects a trace context into a request injector = tracing.propagation().injector(Request.Builder::addHeader); // before a request is sent, add the current span's context to it injector.inject(span.context(), request); -Here’s what server-side propagation might look like +The following example shows how server-side propagation might work: // configure a function that extracts the trace context from a request extracted = tracing.propagation().extractor(Request::getHeader); @@ -967,7 +947,7 @@ span = tracer.nextSpan(extracted, request);
    Propagating extra fields Sometimes you need to propagate extra fields, such as a request ID or an alternate trace context. -For example, if you are in a Cloud Foundry environment, you might want to pass the request ID: +For example, if you are in a Cloud Foundry environment, you might want to pass the request ID, as shown in the following example: // when you initialize the builder, define the extra field you want to propagate tracingBuilder.propagationFactory( ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-vcap-request-id") @@ -975,57 +955,50 @@ tracingBuilder.propagationFactory( // later, you can tag that request ID or use it in log correlation requestId = ExtraFieldPropagation.get("x-vcap-request-id"); -You may also need to propagate a trace context you aren’t using. For example, you may be in an -Amazon Web Services environment, but not reporting data to X-Ray. To ensure X-Ray can co-exist -correctly, pass-through its tracing header like so. +You may also need to propagate a trace context that you are not using. +For example, you may be in an Amazon Web Services environment but not be reporting data to X-Ray. +To ensure X-Ray can co-exist correctly, pass-through its tracing header, as shown in the following example: tracingBuilder.propagationFactory( ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-amzn-trace-id") );
    Prefixed fields -You can also prefix fields, if they follow a common pattern. For example, the following will -propagate the field "x-vcap-request-id" as-is, but send the fields "country-code" and "user-id" -on the wire as "x-baggage-country-code" and "x-baggage-user-id" respectively. -Setup your tracing instance with allowed fields: +If they follow a common pattern, you can also prefix fields. +The following example shows how to propagate x-vcap-request-id the field as-is but send the country-code and user-id fields on the wire as x-baggage-country-code and x-baggage-user-id, respectively: tracingBuilder.propagationFactory( ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY) .addField("x-vcap-request-id") .addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id")) .build() ); -Later, you can call below to affect the country code of the current trace context +Later, you can call the following code to affect the country code of the current trace context: ExtraFieldPropagation.set("country-code", "FO"); String countryCode = ExtraFieldPropagation.get("country-code"); -Or, if you have a reference to a trace context, use it explicitly +Alternatively, if you have a reference to a trace context, you can use it explicitly, as shown in the following example: ExtraFieldPropagation.set(span.context(), "country-code", "FO"); String countryCode = ExtraFieldPropagation.get(span.context(), "country-code"); -In comparison to previous versions of Sleuth, with -Brave it’s required to pass the list of baggage keys. -There are two properties to achieve this. Via the spring.sleuth.baggage-keys you set keys -that will get prefixed with baggage- for http calls and baggage_ for messaging. You can also pass -a list of prefixed keys that will be whitelisted without any prefix via -spring.sleuth.propagation-keys property. +A difference from previous versions of Sleuth is that, with Brave, you must pass the list of baggage keys. +There are two properties to achieve this. +With the spring.sleuth.baggage-keys, you set keys that get prefixed with baggage- for HTTP calls and baggage_ for messaging. +You can also use the spring.sleuth.propagation-keys property to pass a list of prefixed keys that are whitelisted without any prefix.
    -Extracting a propagated context -The TraceContext.Extractor<C> reads trace identifiers and sampling status -from an incoming request or message. The carrier is usually a request object -or headers. -This utility is used in standard instrumentation like [HttpServerHandler](../instrumentation/http/src/main/java/sleuth/http/HttpServerHandler.java), -but can also be used for custom RPC or messaging code. -TraceContextOrSamplingFlags is usually only used with Tracer.nextSpan(extracted), unless you are +Extracting a Propagated Context +The TraceContext.Extractor<C> reads trace identifiers and sampling status from an incoming request or message. +The carrier is usually a request object or headers. +This utility is used in standard instrumentation (such as [HttpServerHandler](../instrumentation/http/src/main/java/sleuth/http/HttpServerHandler.java)) but can also be used for custom RPC or messaging code. +TraceContextOrSamplingFlags is usually used only with Tracer.nextSpan(extracted), unless you are sharing span IDs between a client and a server.
    -Sharing span IDs between client and server -A normal instrumentation pattern is creating a span representing the server -side of an RPC. Extractor.extract might return a complete trace context when -applied to an incoming client request. Tracer.joinSpan attempts to continue -the this trace, using the same span ID if supported, or creating a child span -if not. When span ID is shared, data reported includes a flag saying so. -Here’s an example of B3 propagation: +Sharing span IDs between Client and Server +A normal instrumentation pattern is to create a span representing the server side of an RPC. +Extractor.extract might return a complete trace context when applied to an incoming client request. +Tracer.joinSpan attempts to continue this trace, using the same span ID if supported or creating a child span +if not. When the span ID is shared, the reported data includes a flag saying so. +The following image shows an example of B3 propagation: ┌───────────────────┐ ┌───────────────────┐ Incoming Headers │ TraceContext │ │ TraceContext │ ┌───────────────────┐(extract)│ ┌───────────────┐ │(join)│ ┌───────────────┐ │ @@ -1038,10 +1011,9 @@ if not. When span ID is shared, data reported includes a flag saying so. -Some propagation systems only forward the parent span ID, detected when -Propagation.Factory.supportsJoin() == false. In this case, a new span ID is -always provisioned and the incoming context determines the parent ID. -Here’s an example of AWS propagation: +Some propagation systems forward only the parent span ID, detected when Propagation.Factory.supportsJoin() == false. +In this case, a new span ID is always provisioned, and the incoming context determines the parent ID. +The following image shows an example of AWS propagation: ┌───────────────────┐ ┌───────────────────┐ x-amzn-trace-id │ TraceContext │ │ TraceContext │ ┌───────────────────┐(extract)│ ┌───────────────┐ │(join)│ ┌───────────────┐ │ @@ -1052,61 +1024,50 @@ always provisioned and the incoming context determines the parent ID. └───────────────────┘ │ │ SpanId: New │ │ │ └───────────────┘ │ └───────────────────┘ -Note: Some span reporters do not support sharing span IDs. For example, if you -set Tracing.Builder.spanReporter(amazonXrayOrGoogleStackdrive), disable join -via Tracing.Builder.supportsJoin(false). This will force a new child span on -Tracer.joinSpan(). +Note: Some span reporters do not support sharing span IDs. +For example, if you set Tracing.Builder.spanReporter(amazonXrayOrGoogleStackdrive), you should disable join by setting Tracing.Builder.supportsJoin(false). +Doing so forces a new child span on Tracer.joinSpan().
    Implementing Propagation -TraceContext.Extractor<C> is implemented by a Propagation.Factory plugin. Internally, this code -will create the union type TraceContextOrSamplingFlags with one of the following: +TraceContext.Extractor<C> is implemented by a Propagation.Factory plugin. +Internally, this code creates the union type, TraceContextOrSamplingFlags, with one of the following: * TraceContext if trace and span IDs were present. -* TraceIdContext if a trace ID was present, but not span IDs. -* SamplingFlags if no identifiers were present -Some Propagation implementations carry extra data from point of extraction (ex reading incoming -headers) to injection (ex writing outgoing headers). For example, it might carry a request ID. When -implementations have extra data, here’s how they handle it. -* If a TraceContext was extracted, add the extra data as TraceContext.extra() +* TraceIdContext if a trace ID was present but span IDs were not present. +* SamplingFlags if no identifiers were present. +Some Propagation implementations carry extra data from the point of extraction (for example, reading incoming headers) to injection (for example, writing outgoing headers). +For example, it might carry a request ID. +When implementations have extra data, they handle it as follows: +* If a TraceContext were extracted, add the extra data as TraceContext.extra(). * Otherwise, add it as TraceContextOrSamplingFlags.extra(), which Tracer.nextSpan handles.
    Current Tracing Component -Brave supports a "current tracing component" concept which should only -be used when you have no other means to get a reference. This was made -for JDBC connections, as they often initialize prior to the tracing -component. -The most recent tracing component instantiated is available via -Tracing.current(). You there’s also a shortcut to get only the tracer -via Tracing.currentTracer(). If you use either of these methods, do -noot cache the result. Instead, look them up each time you need them. +Brave supports a "current tracing component" concept, which should only be used when you have no other way to get a reference. +This was made for JDBC connections, as they often initialize prior to the tracing component. +The most recent tracing component instantiated is available through Tracing.current(). +You can also use Tracing.currentTracer() to get only the tracer. +If you use either of these methods, do not cache the result. +Instead, look them up each time you need them. Current Span -Brave supports a "current span" concept which represents the in-flight -operation. Tracer.currentSpan() can be used to add custom tags to a -span and Tracer.nextSpan() can be used to create a child of whatever -is in-flight. +Brave supports a "current span" concept which represents the in-flight operation. +You can use Tracer.currentSpan() to add custom tags to a span and Tracer.nextSpan() to create a child of whatever is in-flight.
    Setting a span in scope manually -When writing new instrumentation, it is important to place a span you -created in scope as the current span. Not only does this allow users to -access it with Tracer.currentSpan(), but it also allows customizations -like SLF4J MDC to see the current trace IDs. -Tracer.withSpanInScope(Span) facilitates this and is most conveniently -employed via the try-with-resources idiom. Whenever external code might -be invoked (such as proceeding an interceptor or otherwise), place the -span in scope like this. +When writing new instrumentation, it is important to place a span you created in scope as the current span. +Not only does doing so let users access it with Tracer.currentSpan(), but it also allows customizations such as SLF4J MDC to see the current trace IDs. +Tracer.withSpanInScope(Span) facilitates this and is most conveniently employed by using the try-with-resources idiom. +Whenever external code might be invoked (such as proceeding an interceptor or otherwise), place the span in scope, as shown in the following example: try (SpanInScope ws = tracer.withSpanInScope(span)) { return inboundRequest.invoke(); } finally { // note the scope is independent of the span span.finish(); } -In edge cases, you may need to clear the current span temporarily. For -example, launching a task that should not be associated with the current -request. To do this, simply pass null to withSpanInScope. +In edge cases, you may need to clear the current span temporarily (for example, launching a task that should not be associated with the current request). To do tso, pass null to withSpanInScope, as shown in the following example: try (SpanInScope cleared = tracer.withSpanInScope(null)) { startBackgroundThread(); } @@ -1114,53 +1075,43 @@ request. To do this, simply pass null to withSpanInScope. Instrumentation -Spring Cloud Sleuth instruments all your Spring application -automatically, so you shouldn’t have to do anything to activate -it. The instrumentation is added using a variety of technologies -according to the stack that is available, e.g. for a servlet web -application we use a Filter, and for Spring Integration we use -ChannelInterceptors. -You can customize the keys used in span tags. To limit the volume of -span data, by default an HTTP request will be tagged only with a -handful of metadata like the status code, host and URL. You can add -request headers by configuring spring.sleuth.keys.http.headers (a -list of header names). +Spring Cloud Sleuth automatically instruments all your Spring applications, so you should not have to do anything to activate it. +The instrumentation is added by using a variety of technologies according to the stack that is available. For example, for a servlet web application, we use a Filter, and, for Spring Integration, we use ChannelInterceptors. +You can customize the keys used in span tags. +To limit the volume of span data, an HTTP request is, by default, tagged only with a handful of metadata, such as the status code, the host, and the URL. +You can add request headers by configuring spring.sleuth.keys.http.headers (a list of header names). -Remember that tags are only collected and exported if there is a -Sampler that allows it (by default there is not, so there is no -danger of accidentally collecting too much data without configuring -something). +Tags are collected and exported only if there is a Sampler that allows it. By default, there is no such Sampler, to ensure that there is no danger of accidentally collecting too much data without configuring something). Span lifecycle -You can do the following operations on the Span by means of brave.Tracer: +You can do the following operations on the Span by means of brave.Tracer: -start - when you start a span its name is assigned and start timestamp is recorded. +start: When you start a span, its name is assigned and the start timestamp is recorded. -close - the span gets finished (the end time of the span is recorded) and if -the span is sampled then it will be eligible for collection to e.g. Zipkin. +close: The span gets finished (the end time of the span is recorded) and, if the span is sampled, it is eligible for collection (for example, to Zipkin). -continue - a new instance of span will be created whereas it will be a copy of the -one that it continues. +continue: A new instance of span is created. +It is a copy of the one that it continues. -detach - the span doesn’t get stopped or closed. It only gets removed from the current thread. +detach: The span does not get stopped or closed. +It only gets removed from the current thread. -create with explicit parent - you can create a new span and set an explicit parent to it +create with explicit parent: You can create a new span and set an explicit parent for it. -Spring Cloud Sleuth creates the instance of Tracer for you. In order to use it, -all you need is to just autowire it. +Spring Cloud Sleuth creates an instance of Tracer for you. In order to use it, you can autowire it.
    Creating and finishing spans -You can manually create spans by using the Tracer. +You can manually create spans by using the Tracer, as shown in the following example: // Start a span. If there was a span present in this thread it will become // the `newSpan`'s parent. Span newSpan = this.tracer.nextSpan().name("calculateTax"); @@ -1176,31 +1127,30 @@ try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(newSpan.start())) { // the span to send it to Zipkin newSpan.finish(); } -In this example we could see how to create a new instance of span. Assuming that there already -was a span present in this thread then it would become the parent of that span. +In the preceding example, we could see how to create a new instance of the span. +If there is already a span in this thread, it becomes the parent of the new span. -Always clean after you create a span! Don’t forget to finish a span if you want to send it to Zipkin. +Always clean after you create a span. Also, always finish any span that you want to send to Zipkin. -If your span contains a name greater than 50 chars, then that name will -be truncated to 50 chars. Your names have to be explicit and concrete. Big names lead to -latency issues and sometimes even thrown exceptions. +If your span contains a name greater than 50 chars, that name is truncated to 50 chars. +Your names have to be explicit and concrete. Big names lead to latency issues and sometimes even exceptions.
    -Continuing spans -Sometimes you don’t want to create a new span but you want to continue one. Example of such a -situation might be (of course it all depends on the use-case): +Continuing Spans +Sometimes, you do not want to create a new span but you want to continue one. An example of such a +situation might be as follows: -AOP - If there was already a span created before an aspect was reached then you might not want to create a new span. +AOP: If there was already a span created before an aspect was reached, you might not want to create a new span. -Hystrix - executing a Hystrix command is most likely a logical part of the current processing. It’s in fact -only a technical implementation detail that you wouldn’t necessarily want to reflect in tracing as a separate being. +Hystrix: Executing a Hystrix command is most likely a logical part of the current processing. +It is in fact merely a technical implementation detail that you would not necessarily want to reflect in tracing as a separate being. -To continue a span you can use brave.Tracer. +To continue a span, you can use brave.Tracer, as shown in the following example: // let's assume that we're in a thread Y and we've received // the `initialSpan` from thread X Span continuedSpan = this.tracer.joinSpan(newSpan.context()); @@ -1218,12 +1168,11 @@ try { }
    -Creating spans with an explicit parent -There is a possibility that you want to start a new span and provide an explicit parent of that span. -Let’s assume that the parent of a span is in one thread and you want to start a new span in another thread. -In Brave, whenever you call nextSpan(), it’s creating one in reference -to the span being currently in scope. It’s enough to just put -the span in scope and then call nextSpan(), as presented in the example below: +Creating a Span with an explicit Parent +You might want to start a new span and provide an explicit parent of that span. +Assume that the parent of a span is in one thread and you want to start a new span in another thread. +In Brave, whenever you call nextSpan(), it creates a span in reference to the span that is currently in scope. +You can put the span in scope and then call nextSpan(), as shown in the following example: // let's assume that we're in a thread Y and we've received // the `initialSpan` from thread X. `initialSpan` will be the parent // of the `newSpan` @@ -1245,32 +1194,30 @@ try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(initialSpan)) { } } -After having created such a span remember to finish it, otherwise it will not get -reported to e.g. Zipkin +After creating such a span, you must finish it. Otherwise it is not reported (for example, to Zipkin).
    Naming spans -Picking a span name is not a trivial task. Span name should depict an operation name. The name should -be low cardinality (e.g. not include identifiers). -Since there is a lot of instrumentation going on some of the span names will be -artificial like: +Picking a span name is not a trivial task. A span name should depict an operation name. +The name should be low cardinality, so it should not include identifiers. +Since there is a lot of instrumentation going on, some span names are artificial: -controller-method-name when received by a Controller with a method name conrollerMethodName +controller-method-name when received by a Controller with a method name of conrollerMethodName -async for asynchronous operations done via wrapped Callable and Runnable. +async for asynchronous operations done with wrapped Callable and Runnable interfaces. -@Scheduled annotated methods will return the simple name of the class. +Methods annotated with @Scheduled return the simple name of the class. -Fortunately, for the asynchronous processing you can provide explicit naming. -
    -@SpanName annotation -You can name the span explicitly via the @SpanName annotation. +Fortunately, for asynchronous processing, you can provide explicit naming. +
    +<literal>@SpanName</literal> Annotation +You can name the span explicitly by using the @SpanName annotation, as shown in the followwng example: @SpanName("calculateTax") class TaxCountingRunnable implements Runnable { @@ -1278,20 +1225,20 @@ class TaxCountingRunnable implements Runnable { // perform logic } } -In this case, when processed in the following manner: +In this case, when processed in the following manner, the span is named calculateTax: Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser, new TaxCountingRunnable()); Future<?> future = executorService.submit(runnable); // ... some additional logic ... future.get(); -The span will be named calculateTax.
    -
    -toString() method -It’s pretty rare to create separate classes for Runnable or Callable. Typically one creates an anonymous -instance of those classes. You can’t annotate such classes thus to override that, if there is no @SpanName annotation present, -we’re checking if the class has a custom implementation of the toString() method. -So executing such code: +
    +<literal>toString()</literal> method +It is pretty rare to create separate classes for Runnable or Callable. +Typically, one creates an anonymous instance of those classes. +You cannot annotate such classes. +To overcome that limitation, if there is no @SpanName annotation present, we check whether the class has a custom implementation of the toString() method. +Running such code leads to creating a span named calculateTax, as shown in the following example: Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser, new Runnable() { @Override public void run() { // perform logic @@ -1304,82 +1251,61 @@ we’re checking if the class has a custom implementation of the to Future<?> future = executorService.submit(runnable); // ... some additional logic ... future.get(); -will lead in creating a span named calculateTax.
    -Managing spans with annotations +Managing Spans with Annotations +You can manage spans with a variety of annotations.
    Rationale -The main arguments for this features are +There are a number of good reasons to manage spans with annotations, including: -api-agnostic means to collaborate with a span - - -use of annotations allows users to add to a span with no library dependency on a span api. -This allows Sleuth to change its core api less impact to user code. - - +API-agnostic means to collaborate with a span. Use of annotations lets users add to a span with no library dependency on a span api. +Doing so lets Sleuth change its core API to create less impact to user code. -reduced surface area for basic span operations. - - -without this feature one has to use the span api, which has lifecycle commands that -could be used incorrectly. By only exposing scope, tag and log functionality, users can -collaborate without accidentally breaking span lifecycle. - - +Reduced surface area for basic span operations. Without this feature, you must use the span api, which has lifecycle commands that could be used incorrectly. +By only exposing scope, tag, and log functionality, you can collaborate without accidentally breaking span lifecycle. -collaboration with runtime generated code - - -with libraries such as Spring Data / Feign the implementations of interfaces are generated -at runtime thus span wrapping of objects was tedious. Now you can provide annotations - over interfaces and arguments of those interfaces - - +Collaboration with runtime generated code. With libraries such as Spring Data and Feign, the implementations of interfaces are generated at runtime. +Consequently, span wrapping of objects was tedious. +Now you can provide annotations over interfaces and the arguments of those interfaces.
    -Creating new spans -If you really don’t want to take care of creating local spans manually you can profit from the -@NewSpan annotation. Also we give you the @SpanTag annotation to add tags in an automated -fashion. -Let’s look at some examples of usage. +Creating New Spans +If you do not want to create local spans manually, you can use the @NewSpan annotation. +Also, we provide the @SpanTag annotation to add tags in an automated fashion. +Now we can consider some examples of usage. @NewSpan void testMethod(); -Annotating the method without any parameter will lead to a creation of a new span whose name -will be equal to annotated method name. +Annotating the method without any parameter leads to creating a new span whose name equals the annotated method name. @NewSpan("customNameOnTestMethod4") void testMethod4(); -If you provide the value in the annotation (either directly or via the name parameter) then -the created span will have the name as the provided value. +If you provide the value in the annotation (either directly or by setting the name parameter), the created span has the provided value as the name. // method declaration @NewSpan(name = "customNameOnTestMethod5") void testMethod5(@SpanTag("testTag") String param); // and method execution this.testBean.testMethod5("test"); -You can combine both the name and a tag. Let’s focus on the latter. In this case whatever the value of -the annotated method’s parameter runtime value will be - that will be the value of the tag. In our sample -the tag key will be testTag and the tag value will be test. +You can combine both the name and a tag. Let’s focus on the latter. +In this case, the value of the annotated method’s parameter runtime value becomes the value of the tag. +In our sample, the tag key is testTag, and the tag value is test. @NewSpan(name = "customNameOnTestMethod3") @Override public void testMethod3() { } -You can place the @NewSpan annotation on both the class and an interface. If you override the -interface’s method and provide a different value of the @NewSpan annotation then the most -concrete one wins (in this case customNameOnTestMethod3 will be set). +You can place the @NewSpan annotation on both the class and an interface. +If you override the interface’s method and provide a different value for the @NewSpan annotation, the most +concrete one wins (in this case customNameOnTestMethod3 is set).
    -Continuing spans -If you want to just add tags and annotations to an existing span it’s enough -to use the @ContinueSpan annotation as presented below. Note that in contrast -with the @NewSpan annotation you can also add logs via the log parameter: +Continuing Spans +If you want to add tags and annotations to an existing span, you can use the @ContinueSpan annotation, as shown in the following example: // method declaration @ContinueSpan(log = "testMethod11") void testMethod11(@SpanTag("testTag11") String param); @@ -1387,88 +1313,75 @@ void testMethod11(@SpanTag("testTag11") String param); // method execution this.testBean.testMethod11("test"); this.testBean.testMethod13(); -That way the span will get continued and: +(Note that, in contrast with the @NewSpan annotation ,you can also add logs with the log parameter.) +That way, the span gets continued and: -logs with name testMethod11.before and testMethod11.after will be created +Log entries named testMethod11.before and testMethod11.after are created. -if an exception will be thrown a log testMethod11.afterFailure will also be created +If an exception is thrown, a log entry named testMethod11.afterFailure is also created. -tag with key testTag11 and value test will be created +A tag with a key of testTag11 and a value of test is created.
    -
    -More advanced tag setting +
    +Advanced Tag Setting There are 3 different ways to add tags to a span. All of them are controlled by the SpanTag annotation. -Precedence is: - +The precedence is as follows: + -try with the bean of TagValueResolver type and provided name +Try with a bean of TagValueResolver type and a provided name. -if one hasn’t provided the bean name, try to evaluate an expression. We’re searching for a TagValueExpressionResolver bean. +If the bean name has not been provided, try to evaluate an expression. +We search for a TagValueExpressionResolver bean. The default implementation uses SPEL expression resolution. -if one hasn’t provided any expression to evaluate just return a toString() value of the parameter +If we do not find any expression to evaluate, return the toString() value of the parameter. - +
    Custom extractor -The value of the tag for following method will be computed by an implementation of TagValueResolver interface. +The value of the tag for the following method is computed by an implementation of TagValueResolver interface. Its class name has to be passed as the value of the resolver attribute. -Having such an annotated method: +Consider the following annotated method: @NewSpan public void getAnnotationForTagValueResolver(@SpanTag(key = "test", resolver = TagValueResolver.class) String test) { } -and such a TagValueResolver bean implementation +Now further consider the following TagValueResolver bean implementation: @Bean(name = "myCustomTagValueResolver") public TagValueResolver tagValueResolver() { return parameter -> "Value from myCustomTagValueResolver"; } -Will lead to setting of a tag value equal to Value from myCustomTagValueResolver. +The two preceding examples lead to setting a tag value equal to Value from myCustomTagValueResolver.
    -
    -Resolving expressions for value -Having such an annotated method: +
    +Resolving Expressions for a Value +Consider the following annotated method: @NewSpan public void getAnnotationForTagValueExpression(@SpanTag(key = "test", expression = "length() + ' characters'") String test) { } -and no custom implementation of a TagValueExpressionResolver will lead to evaluation of the SPEL expression and a tag with value 4 characters will be set on the span. -If you want to use some other expression resolution mechanism you can create your own implementation -of the bean. +No custom implementation of a TagValueExpressionResolver leads to evaluation of the SPEL expression, and a tag with a value of 4 characters is set on the span. +If you want to use some other expression resolution mechanism, you can create your own implementation of the bean.
    -
    -Using toString method -Having such an annotated method: +
    +Using the <literal>toString()</literal> method +Consider the following annotated method: @NewSpan public void getAnnotationForArgumentToString(@SpanTag("test") Long param) { } -if executed with a value of 15 will lead to setting of a tag with a String value of "15". +Running the preceding method with a value of 15 leads to setting a tag with a String value of "15".
    - - -Customizations -
    -Spring Integration - -
    -
    -HTTP - -
    -
    -TraceFilter -You can also modify the behaviour of the TraceFilter - the component that is responsible -for processing the input HTTP request and adding tags basing on the HTTP response. You can customize -the tags, or modify the response headers by registering your own instance of the TraceFilter bean. -In the following example we will register the TraceFilter bean and we will add the -ZIPKIN-TRACE-ID response header containing the current Span’s trace id. Also we will -add to the Span a tag with key custom and a value tag. +
    +<literal>TraceFilter</literal> +You can also modify the behavior of the TraceFilter, which is the component that is responsible for processing the input HTTP request and adding tags basing on the HTTP response. +You can customize the tags or modify the response headers by registering your own instance of the TraceFilter bean. +In the following example, we register the TraceFilter bean, add the ZIPKIN-TRACE-ID response header containing the current Span’s trace id, and add a tag with key custom and a value tag to the span. @Component @Order(TraceFilter.ORDER + 1) class MyFilter extends GenericFilterBean { @@ -1495,20 +1408,20 @@ class MyFilter extends GenericFilterBean {
    Custom service name -By default Sleuth assumes that when you send a span to Zipkin, you want the span’s service name - to be equal to spring.application.name value. That’s not always the case though. There - are situations in which you want to explicitly provide a different service name for all spans coming - from your application. To achieve that it’s enough to just pass the following property - to your application to override that value (example for foo service name): -spring.zipkin.service.name: foo +By default, Sleuth assumes that, when you send a span to Zipkin, you want the span’s service name to be equal to the value of the spring.application.name property. +That is not always the case, though. +There are situations in which you want to explicitly provide a different service name for all spans coming from your application. +To achieve that, you can pass the following property to your application to override that value (the example is for a service named myService): +spring.zipkin.service.name: myService
    -Customization of reported spans -Before reporting spans to e.g. Zipkin you can be interested in modifying that span in some way. - You can achieve that by using the SpanAdjuster interface. -In Sleuth we’re generating spans with a fixed name. Some users want to modify the name depending on values -of tags. Implementation of the SpanAdjuster interface can be used to alter that name. Example: -Example. If you register two beans of SpanAdjuster type: +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. +In Sleuth, we generat 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. +The following example shows how to register two beans that implement SpanAdjuster: @Bean SpanAdjuster adjusterOne() { return span -> span.toBuilder().name("foo").build(); } @@ -1516,62 +1429,55 @@ of tags. Implementation of the SpanAdjuster interface can be @Bean SpanAdjuster adjusterTwo() { return span -> span.toBuilder().name(span.name() + " bar").build(); } -This will lead in changing the name of the reported span to foo bar, just before it gets reported (e.g. to Zipkin). +The preceding example results in changing the name of the reported span to foo bar, just before it gets reported (for example, to Zipkin).
    -Host locator +Host Locator -This section is about defining host from service discovery. It’s NOT -about finding Zipkin in service discovery. +This section is about defining host from service discovery. +It is NOT about finding Zipkin through service discovery. -In order to define the host that is corresponding to a particular span we need to resolve the host name -and port. The default approach is to take it from server properties. If those for some reason are not set -then we’re trying to retrieve the host name from the network interfaces. -If you have the discovery client enabled and prefer to retrieve the host address from the registered -instance in a service registry then you have to set the property (it’s applicable for both HTTP and -Stream based span reporting). +To define the host that corresponds to a particular span, we need to resolve the host name and port. +The default approach is to take these values from server properties. +If those are not set, we try to retrieve the host name from the network interfaces. +If you have the discovery client enabled and prefer to retrieve the host address from the registered instance in a service registry, you have to set the spring.zipkin.locator.discovery.enabled property (it is applicable for both HTTP-based and Stream-based span reporting), as follows: spring.zipkin.locator.discovery.enabled: true
    -Sending spans to Zipkin -By default if you add spring-cloud-starter-zipkin as a dependency to your project, -when the span is closed, it will be sent to Zipkin over HTTP. The communication -is asynchronous. You can configure the URL by setting the spring.zipkin.baseUrl -property as follows: +Sending Spans to Zipkin +By default, if you add spring-cloud-starter-zipkin as a dependency to your project, when the span is closed, it is sent to Zipkin over HTTP. +The communication is asynchronous. +You can configure the URL by setting the spring.zipkin.baseUrl property, as follows: spring.zipkin.baseUrl: http://192.168.99.100:9411/ -If you want to find Zipkin via service discovery it’s enough to pass the -Zipkin’s service id inside the URL (example for zipkinserver service id) +If you want to find Zipkin through service discovery, you can pass the Zipkin’s service ID inside the URL, as shown in the following example for zipkinserver service ID: spring.zipkin.baseUrl: http://zipkinserver/ -If you have web, rabbit or kafka together on the classpath, you might need -to pick the means by which you would like to send spans to zipkin. To do that -just set either web, rabbit or kafka to the spring.zipkin.sender.type property. -Example for web: +If you have web, rabbit, or kafka together on the classpath, you might need to pick the means by which you would like to send spans to zipkin. +To do so, set web, rabbit, or kafka to the spring.zipkin.sender.type property. +The following example shows setting the sender type for web: spring.zipkin.sender.type: web Zipkin Stream Span Consumer -The suggested approach is to use the Zipkin’s -native support for message based span sending. Starting from -Edgware Zipkin Stream server is deprecated and in Finchley -it got removed. +We recommend using Zipkin’s native support for message-based span sending. +Starting from the Edgware release, the Zipkin Stream server is deprecated. +In the Finchley release, it got removed. -Please refer to the Dalston Documentaion -on how to create a Stream Zipkin server. +See the Dalston Documentaion +for how to create a Stream Zipkin server. Integrations
    OpenTracing -Spring Cloud Sleuth is OpenTracing compatible. If you have -OpenTracing on the classpath we will automatically register the OpenTracing -Tracer bean. If you wish to disable this just set spring.sleuth.opentracing.enabled to false +Spring Cloud Sleuth is compatible with OpenTracing. +If you have OpenTracing on the classpath, we automatically register the OpenTracing Tracer bean. +If you wish to disable this, set spring.sleuth.opentracing.enabled to false
    Runnable and Callable -If you’re wrapping your logic in Runnable or Callable it’s enough to wrap those classes in their Sleuth representative. -Example for Runnable: +If you wrap your logic in Runnable or Callable, you can wrap those classes in their Sleuth representative, as shown in the following example for Runnable: Runnable runnable = new Runnable() { @Override public void run() { @@ -1589,7 +1495,7 @@ Runnable traceRunnable = new TraceRunnable(tracer, spanNamer, errorParser, // Wrapping `Runnable` with `Tracing`. That way the current span will be available // in the thread of `Runnable` Runnable traceRunnableFromTracer = tracing.currentTraceContext().wrap(runnable); -Example for Callable: +The following example shows how to do so for Callable: Callable<String> callable = new Callable<String>() { @Override public String call() throws Exception { @@ -1607,28 +1513,27 @@ Callable<String> traceCallable = new TraceCallable<>(tracer, spanNam // Wrapping `Callable` with `Tracing`. That way the current span will be available // in the thread of `Callable` Callable<String> traceCallableFromTracer = tracing.currentTraceContext().wrap(callable); -That way you will ensure that a new Span is created and closed for each execution. +That way, you ensure that a new span is created and closed for each execution.
    Hystrix
    Custom Concurrency Strategy -We’re registering a custom HystrixConcurrencyStrategy -that wraps all Callable instances into their Sleuth representative - -the TraceCallable. The strategy either starts or continues a span depending on the fact whether tracing was already going -on before the Hystrix command was called. To disable the custom Hystrix Concurrency Strategy set the spring.sleuth.hystrix.strategy.enabled to false. +We register a custom HystrixConcurrencyStrategy called TraceCallable that wraps all Callable instances in their Sleuth representative. +The strategy either starts or continues a span, depending on whether tracing was already going on before the Hystrix command was called. +To disable the custom Hystrix Concurrency Strategy, set the spring.sleuth.hystrix.strategy.enabled to false.
    Manual Command setting -Assuming that you have the following HystrixCommand: +Assume that you have the following HystrixCommand: HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) { @Override protected String run() throws Exception { return someLogic(); } }; -In order to pass the tracing information you have to wrap the same logic in the Sleuth version of the HystrixCommand which is the -TraceCommand: +To pass the tracing information, you have to wrap the same logic in the Sleuth version of the HystrixCommand, which is called +TraceCommand, as shown in the following example: TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, traceKeys, setter) { @Override public String doRun() throws Exception { @@ -1639,74 +1544,74 @@ on before the Hystrix command was called. To disable the custom Hystrix Concurre
    RxJava -We’re registering a custom RxJavaSchedulersHook -that wraps all Action0 instances into their Sleuth representative - -the TraceAction. The hook either starts or continues a span depending on the fact whether tracing was already going -on before the Action was scheduled. To disable the custom RxJavaSchedulersHook set the spring.sleuth.rxjava.schedulers.hook.enabled to false. -You can define a list of regular expressions for thread names, for which you don’t want a Span to be created. Just provide a comma separated list -of regular expressions in the spring.sleuth.rxjava.schedulers.ignoredthreads property. +We registering a custom RxJavaSchedulersHook that wraps all Action0 instances in their Sleuth representative, which is called TraceAction. +The hook either starts or continues a span, depending on whether tracing was already going on before the Action was scheduled. +To disable the custom RxJavaSchedulersHook, set the spring.sleuth.rxjava.schedulers.hook.enabled to false. +You can define a list of regular expressions for thread names for which you do not want spans to be created. +To do so, provide a comma-separated list of regular expressions in the spring.sleuth.rxjava.schedulers.ignoredthreads property.
    HTTP integration -Features from this section can be disabled by providing the spring.sleuth.web.enabled property with value equal to false. +Features from this section can be disabled by setting the spring.sleuth.web.enabled property with value equal to false.
    HTTP Filter -Via the TraceFilter all sampled incoming requests result in creation of a Span. That Span’s name is http: + the path to which - the request was sent. E.g. if the request was sent to /foo/bar then the name will be http:/foo/bar. You can configure which URIs you would - like to skip via the spring.sleuth.web.skipPattern property. If you have ManagementServerProperties on classpath then - its value of contextPath gets appended to the provided skip pattern. If you want to reuse the - Sleuth’s default skip patterns and just append your own, pass those patterns via - the spring.sleuth.web.additionalSkipPattern. +Through the TraceFilter, all sampled incoming requests result in creation of a Span. +That Span’s name is http: + the path to which the request was sent. +For example, if the request was sent to /this/this then the name will be http:/this/that. +You can configure which URIs you would like to skip by setting the spring.sleuth.web.skipPattern property. +If you have ManagementServerProperties on classpath, its value of contextPath gets appended to the provided skip pattern. +If you want to reuse the Sleuth’s default skip patterns and just append your own, pass those patterns by using the spring.sleuth.web.additionalSkipPattern.
    HandlerInterceptor -Since we want the span names to be precise we’re using a TraceHandlerInterceptor that either wraps an - existing HandlerInterceptor or is added directly to the list of existing HandlerInterceptors. The - TraceHandlerInterceptor adds a special request attribute to the given HttpServletRequest. If the - the TraceFilter doesn’t see this attribute set it will create a "fallback" span which is an additional - span created on the server side so that the trace is presented properly in the UI. Seeing that most likely - signifies that there is a missing instrumentation. In that case please file an issue in Spring Cloud Sleuth. +Since we want the span names to be precise, we use a TraceHandlerInterceptor that either wraps an existing HandlerInterceptor or is added directly to the list of existing HandlerInterceptors. +The TraceHandlerInterceptor adds a special request attribute to the given HttpServletRequest. +If the the TraceFilter does not see this attribute, it creates a "fallback" span, which is an additional span created on the server side so that the trace is presented properly in the UI. +If that happens, there is probably missing instrumentation. +In that case, please file an issue in Spring Cloud Sleuth.
    Async Servlet support -If your controller returns a Callable or a WebAsyncTask Spring Cloud Sleuth will continue the existing span instead of creating a new one. +If your controller returns a Callable or a WebAsyncTask, Spring Cloud Sleuth continues the existing span instead of creating a new one.
    WebFlux support -Via the TraceWebFilter all sampled incoming requests result in creation of a Span. That Span’s name is http: + the path to which - the request was sent. E.g. if the request was sent to /foo/bar then the name will be http:/foo/bar. You can configure which URIs you would - like to skip via the spring.sleuth.web.skipPattern property. If you have ManagementServerProperties on classpath then - its value of contextPath gets appended to the provided skip pattern. If you want to reuse the - Sleuth’s default skip patterns and just append your own, pass those patterns via - the spring.sleuth.web.additionalSkipPattern. +Through TraceWebFilter, all sampled incoming requests result in creation of a Span. +That Span’s name is http: + the path to which the request was sent. +For example, if the request was sent to /this/that, the name is http:/this/that. +You can configure which URIs you would like to skip by using the spring.sleuth.web.skipPattern property. +If you have ManagementServerProperties on the classpath, its value of contextPath gets appended to the provided skip pattern. +If you want to reuse Sleuth’s default skip patterns and append your own, pass those patterns by using the spring.sleuth.web.additionalSkipPattern.
    -HTTP client integration +HTTP Client Integration
    Synchronous Rest Template -We’re injecting a RestTemplate interceptor that ensures that all the tracing information is passed to the requests. Each time a -call is made a new Span is created. It gets closed upon receiving the response. In order to block the synchronous RestTemplate features -just set spring.sleuth.web.client.enabled to false. +We inject a RestTemplate interceptor to ensure that all the tracing information is passed to the requests. +Each time a call is made, a new Span is created. +It gets closed upon receiving the response. +To block the synchronous RestTemplate features, set spring.sleuth.web.client.enabled to false. -You have to register RestTemplate as a bean so that the interceptors will get injected. -If you create a RestTemplate instance with a new keyword then the instrumentation WILL NOT work. +You have to register RestTemplate as a bean so that the interceptors get injected. +If you create a RestTemplate instance with a new keyword, the instrumentation does NOT work.
    Asynchronous Rest Template -Starting with Sleuth 2.0.0 we no longer register -a bean of AsyncRestTemplate type. It’s up to you to create such -a bean. Then we will instrument it. +Starting with Sleuth 2.0.0, we no longer register a bean of AsyncRestTemplate type. +It is up to you to create such a bean. +Then we instrument it. -To block the AsyncRestTemplate features set spring.sleuth.web.async.client.enabled to false. -To disable creation of the default TraceAsyncClientHttpRequestFactoryWrapper set spring.sleuth.web.async.client.factory.enabled -to false. If you don’t want to create AsyncRestClient at all set spring.sleuth.web.async.client.template.enabled to false. +To block the AsyncRestTemplate features, set spring.sleuth.web.async.client.enabled to false. +To disable creation of the default TraceAsyncClientHttpRequestFactoryWrapper, set spring.sleuth.web.async.client.factory.enabled +to false. +If you do not want to create AsyncRestClient at all, set spring.sleuth.web.async.client.template.enabled to false.
    Multiple Asynchronous Rest Templates -Sometimes you need to use multiple implementations of Asynchronous Rest Template. In the following snippet you -can see an example of how to set up such a custom AsyncRestTemplate. +Sometimes you need to use multiple implementations of the Asynchronous Rest Template. +In the following snippet, you can see an example of how to set up such a custom AsyncRestTemplate: @Configuration @EnableAutoConfiguration static class Config { @@ -1730,21 +1635,19 @@ static class Config { }
    -
    -WebClient -We inject a ExchangeFilterFunction implementation that creates a span and via on success and on -error callbacks takes care of closing client side spans. +
    +<literal>WebClient</literal> +We inject a ExchangeFilterFunction implementation that creates a span and, through on-success and on-error callbacks, takes care of closing client-side spans. -You have to register WebClient as a bean so that the tracing instrumention gets applied. -If you create a WebClient instance with a new keyword then the instrumentation WILL NOT work. +You have to register WebClient as a bean so that the tracing instrumentation gets applied. +If you create a WebClient instance with a new keyword, the instrumentation does NOT work.
    Traverson -If you’re using the Traverson library -it’s enough for you to inject a RestTemplate as a bean into your Traverson object. Since RestTemplate -is already intercepted, you will get full support of tracing in your client. Below you can find a pseudo code -of how to do that: +If you use the Traverson library, you can inject a RestTemplate as a bean into your Traverson object. +Since RestTemplate is already intercepted, you get full support for tracing in your client. The following pseudo code +shows how to do that: @Autowired RestTemplate restTemplate; Traverson traverson = new Traverson(URI.create("http://some/address"), @@ -1754,58 +1657,57 @@ Traverson traverson = new Traverson(URI.create("http://some/address"),
    Feign -By default Spring Cloud Sleuth provides integration with feign via the TraceFeignClientAutoConfiguration. You can disable it entirely -by setting spring.sleuth.feign.enabled to false. If you do so then no Feign related instrumentation will take place. -Part of Feign instrumentation is done via a FeignBeanPostProcessor. You can disable it by providing the spring.sleuth.feign.processor.enabled equal to false. -If you set it like this then Spring Cloud Sleuth will not instrument any of your custom Feign components. All the default instrumentation -however will be still there. +By default, Spring Cloud Sleuth provides integration with Feign through TraceFeignClientAutoConfiguration. +You can disable it entirely by setting spring.sleuth.feign.enabled to false. +If you do so, no Feign-related instrumentation take place. +Part of Feign instrumentation is done through a FeignBeanPostProcessor. +You can disable it by setting spring.sleuth.feign.processor.enabled to false. +If you set it to false, Spring Cloud Sleuth does not instrument any of your custom Feign components. +However, all the default instrumentation is still there.
    -Asynchronous communication -
    -@Async annotated methods -In Spring Cloud Sleuth we’re instrumenting async related components so that the tracing information is passed between threads. -You can disable this behaviour by setting the value of spring.sleuth.async.enabled to false. -If you annotate your method with @Async then we’ll automatically create a new Span with the following characteristics: +Asynchronous Communication +
    +<literal>@Async</literal> Annotated methods +In Spring Cloud Sleuth, we instrument async-related components so that the tracing information is passed between threads. +You can disable this behavior by setting the value of spring.sleuth.async.enabled to false. +If you annotate your method with @Async, we automatically create a new Span with the following characteristics: -if the method is annotated with @SpanName then the value of the annotation will be the Span’s name +If the method is annotated with @SpanName, the value of the annotation is the Span’s name. -if the method is not annotated with @SpanName the Span name will be the annotated method name +If the method is not annotated with @SpanName, the Span name is the annotated method name. -the Span will be tagged with that method’s class name and the method name too +The span is tagged with the method’s class name and method name.
    -
    -@Scheduled annotated methods -In Spring Cloud Sleuth we’re instrumenting scheduled method execution so that the tracing information is passed between threads. You can disable this behaviour -by setting the value of spring.sleuth.scheduled.enabled to false. -If you annotate your method with @Scheduled then we’ll automatically create a new Span with the following characteristics: +
    +<literal>@Scheduled</literal> Annotated Methods +In Spring Cloud Sleuth, we instrument scheduled method execution so that the tracing information is passed between threads. +You can disable this behavior by setting the value of spring.sleuth.scheduled.enabled to false. +If you annotate your method with @Scheduled, we automatically create a new span with the following characteristics: -the Span name will be the annotated method name +The span name is the annotated method name. -the Span will be tagged with that method’s class name and the method name too +The span is tagged with the method’s class name and method name. -If you want to skip Span creation for some @Scheduled annotated classes you can set the -spring.sleuth.scheduled.skipPattern with a regular expression that will match the fully qualified name of the -@Scheduled annotated class. +If you want to skip span creation for some @Scheduled annotated classes, you can set the spring.sleuth.scheduled.skipPattern with a regular expression that matches the fully qualified name of the @Scheduled annotated class. -If you are using spring-cloud-sleuth-stream and spring-cloud-netflix-hystrix-stream together, Span will be created for -each Hystrix metrics and sent to Zipkin. This may be annoying. You can prevent this by setting -spring.sleuth.scheduled.skipPattern=org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask +If you use spring-cloud-sleuth-stream and spring-cloud-netflix-hystrix-stream together, a span is created for each Hystrix metrics and sent to Zipkin. +This behavior may be annoying. +You can prevent it by setting spring.sleuth.scheduled.skipPattern=org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask.
    -Executor, ExecutorService and ScheduledExecutorService -We’re providing LazyTraceExecutor, TraceableExecutorService and TraceableScheduledExecutorService. Those implementations -are creating Spans each time a new task is submitted, invoked or scheduled. -Here you can see an example of how to pass tracing information with TraceableExecutorService when working with CompletableFuture: +Executor, ExecutorService, and ScheduledExecutorService +We provide LazyTraceExecutor, TraceableExecutorService, and TraceableScheduledExecutorService. Those implementations create spans each time a new task is submitted, invoked, or scheduled. +The following example shows how to pass tracing information with TraceableExecutorService when working with CompletableFuture: CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> { // perform some logic return 1_000_000L; @@ -1813,14 +1715,13 @@ are creating Spans each time a new task is submitted, invoked or scheduled. -Sleuth doesn’t work with parallelStream() out of the box. If you want -to have the tracing information propagated through the stream you have to use the -approach with supplyAsync(...) as presented above. +Sleuth does not work with parallelStream() out of the box. +If you want to have the tracing information propagated through the stream, you have to use the approach with supplyAsync(...), as shown earlier.
    Customization of Executors -Sometimes you need to set up a custom instance of the AsyncExecutor. In the following snippet you -can see an example of how to set up such a custom Executor. +Sometimes, you need to set up a custom instance of the AsyncExecutor. +The following example shows how to set up such a custom Executor: @Configuration @EnableAutoConfiguration @EnableAsync @@ -1845,25 +1746,26 @@ static class CustomExecutorConfig extends AsyncConfigurerSupport {
    Messaging -Spring Cloud Sleuth integrates with Spring Integration. It creates spans for publish and -subscribe events. To disable Spring Integration instrumentation, set spring.sleuth.integration.enabled to false. -You can provide the spring.sleuth.integration.patterns pattern to explicitly -provide the names of channels that you want to include for tracing. By default all channels -are included. +Spring Cloud Sleuth integrates with Spring Integration. +It creates spans for publish and subscribe events. +To disable Spring Integration instrumentation, set spring.sleuth.integration.enabled to false. +You can provide the spring.sleuth.integration.patterns pattern to explicitly provide the names of channels that you want to include for tracing. +By default, all channels are included. -When using the Executor to build a Spring Integration IntegrationFlow remember to use the untraced version of the Executor. -Decorating Spring Integration Executor Channel with TraceableExecutorService will cause the spans to be improperly closed. +When using the Executor to build a Spring Integration IntegrationFlow, you must use the untraced version of the Executor. +Decorating the Spring Integration Executor Channel with TraceableExecutorService causes the spans to be improperly closed.
    Zuul -We’re instrumenting the Zuul Ribbon integration by enriching the Ribbon requests with tracing information. -To disable Zuul support set the spring.sleuth.zuul.enabled property to false. +We instrument the Zuul Ribbon integration by enriching the Ribbon requests with tracing information. +To disable Zuul support, set the spring.sleuth.zuul.enabled property to false.
    Running examples -You can find the running examples deployed in the Pivotal Web Services. Check them out in the following links: +You can see the running examples deployed in the Pivotal Web Services. +Check them out at the following links: Zipkin for apps presented in the samples to the top