diff --git a/multi/multi__features.html b/multi/multi__features.html index 878b49fb5..30821373f 100644 --- a/multi/multi__features.html +++ b/multi/multi__features.html @@ -10,9 +10,9 @@ latency in your applications. Sleuth is written to not log too much, and to not 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.
  • Provides simple metrics of accepted / dropped spans.
  • If spring-cloud-sleuth-zipkin then the app will generate and collect Zipkin-compatible traces. +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 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.

  • [Important]Important

    If using Zipkin, configure the percentage of spans exported using spring.sleuth.sampler.percentage +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:-}] diff --git a/multi/multi__integrations.html b/multi/multi__integrations.html index 14f982f14..b95972eed 100644 --- a/multi/multi__integrations.html +++ b/multi/multi__integrations.html @@ -1,6 +1,8 @@ - 15. Integrations

    15. Integrations

    15.1 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() {
    +   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() {
     	@Override
     	public void run() {
     		// do some work
    @@ -32,10 +34,10 @@ 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.2 Hystrix

    15.2.1 Custom Concurrency Strategy

    We’re registering a custom HystrixConcurrencyStrategy +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.2.2 Manual Command setting

    Assuming that you have the following HystrixCommand:

    HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {
    +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) {
     	@Override
     	protected String run() throws Exception {
     		return someLogic();
    @@ -46,25 +48,25 @@ on before the Hystrix command was called. To disable the custom Hystrix Concurre
     	public String doRun() throws Exception {
     		return someLogic();
     	}
    -};

    15.3 RxJava

    We’re registering a custom RxJavaSchedulersHook +};

    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.4 HTTP integration

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

    15.4.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 +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.

    15.4.2 HandlerInterceptor

    Since we want the span names to be precise we’re using a TraceHandlerInterceptor that either wraps an + its value of contextPath gets appended to the provided skip pattern.

    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.4.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.4.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 + 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.

    15.5 HTTP client integration

    15.5.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 + its value of contextPath gets appended to the provided skip pattern.

    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.5.2 Asynchronous Rest Template

    [Important]Important

    Starting with Sleuth 2.0.0 we no longer register +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 @@ -89,23 +91,23 @@ can see an example of how to set up such a custom AsyncRes //CUSTOMIZE HERE return factory; } -}

    15.5.3 WebClient

    We inject a ExchangeFilterFunction implementation that creates a span and via on success and on +}

    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.5.4 Traverson

    If you’re using the Traverson library +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;
     
     Traverson traverson = new Traverson(URI.create("http://some/address"),
         MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8).setRestOperations(restTemplate);
    -// use Traverson

    15.6 Feign

    By default Spring Cloud Sleuth provides integration with feign via the TraceFeignClientAutoConfiguration. You can disable it entirely +// 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.7 Asynchronous communication

    15.7.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.7.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 +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.7.3 Executor, ExecutorService and ScheduledExecutorService

    We’re providing LazyTraceExecutor, TraceableExecutorService and TraceableScheduledExecutorService. Those implementations +@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(() -> {
     	// perform some logic
     	return 1_000_000L;
    @@ -132,9 +134,9 @@ can see an example of how to set up such a custom Executor
     		executor.initialize();
     		return new LazyTraceExecutor(this.beanFactory, executor);
     	}
    -}

    15.8 Messaging

    Spring Cloud Sleuth integrates with Spring Integration. It creates spans for publish and +}

    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.9 Zuul

    We’re registering Zuul filters to propagate the tracing information (the request header is enriched with tracing data). +Decorating Spring Integration Executor Channel with TraceableExecutorService will cause the spans to be improperly closed.

    15.10 Zuul

    We’re registering Zuul filters to propagate the tracing information (the request header is enriched with tracing data). To disable Zuul support set the spring.sleuth.zuul.enabled property to false.

    \ No newline at end of file diff --git a/multi/multi_spring-cloud-sleuth.html b/multi/multi_spring-cloud-sleuth.html index f5a19ea28..b6f14c4ee 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. Runnable and Callable
    15.2. Hystrix
    15.2.1. Custom Concurrency Strategy
    15.2.2. Manual Command setting
    15.3. RxJava
    15.4. HTTP integration
    15.4.1. HTTP Filter
    15.4.2. HandlerInterceptor
    15.4.3. Async Servlet support
    15.4.4. WebFlux support
    15.5. HTTP client integration
    15.5.1. Synchronous Rest Template
    15.5.2. Asynchronous Rest Template
    Multiple Asynchronous Rest Templates
    15.5.3. WebClient
    15.5.4. Traverson
    15.6. Feign
    15.7. Asynchronous communication
    15.7.1. @Async annotated methods
    15.7.2. @Scheduled annotated methods
    15.7.3. Executor, ExecutorService and ScheduledExecutorService
    Customization of Executors
    15.8. Messaging
    15.9. Zuul
    16. Running examples
    \ No newline at end of file + 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 diff --git a/single/spring-cloud-sleuth.html b/single/spring-cloud-sleuth.html index a78ecc7c5..221454978 100644 --- a/single/spring-cloud-sleuth.html +++ b/single/spring-cloud-sleuth.html @@ -1,6 +1,6 @@ - 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. Runnable and Callable
    15.2. Hystrix
    15.2.1. Custom Concurrency Strategy
    15.2.2. Manual Command setting
    15.3. RxJava
    15.4. HTTP integration
    15.4.1. HTTP Filter
    15.4.2. HandlerInterceptor
    15.4.3. Async Servlet support
    15.4.4. WebFlux support
    15.5. HTTP client integration
    15.5.1. Synchronous Rest Template
    15.5.2. Asynchronous Rest Template
    Multiple Asynchronous Rest Templates
    15.5.3. WebClient
    15.5.4. Traverson
    15.6. Feign
    15.7. Asynchronous communication
    15.7.1. @Async annotated methods
    15.7.2. @Scheduled annotated methods
    15.7.3. Executor, ExecutorService and ScheduledExecutorService
    Customization of Executors
    15.8. Messaging
    15.9. 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 + 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 @@ -248,9 +248,9 @@ latency in your applications. Sleuth is written to not log too much, and to not 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.
  • Provides simple metrics of accepted / dropped spans.
  • If spring-cloud-sleuth-zipkin then the app will generate and collect Zipkin-compatible traces. +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 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.
  • [Important]Important

    If using Zipkin, configure the percentage of spans exported using spring.sleuth.sampler.percentage +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:-}] @@ -714,7 +714,9 @@ Zipkin’s service id inside the URL (example for zipk 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 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() {
    +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() {
     	@Override
     	public void run() {
     		// do some work
    @@ -746,10 +748,10 @@ 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.2 Hystrix

    15.2.1 Custom Concurrency Strategy

    We’re registering a custom HystrixConcurrencyStrategy +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.2.2 Manual Command setting

    Assuming that you have the following HystrixCommand:

    HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {
    +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) {
     	@Override
     	protected String run() throws Exception {
     		return someLogic();
    @@ -760,25 +762,25 @@ on before the Hystrix command was called. To disable the custom Hystrix Concurre
     	public String doRun() throws Exception {
     		return someLogic();
     	}
    -};

    15.3 RxJava

    We’re registering a custom RxJavaSchedulersHook +};

    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.4 HTTP integration

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

    15.4.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 +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.

    15.4.2 HandlerInterceptor

    Since we want the span names to be precise we’re using a TraceHandlerInterceptor that either wraps an + its value of contextPath gets appended to the provided skip pattern.

    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.4.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.4.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 + 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.

    15.5 HTTP client integration

    15.5.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 + its value of contextPath gets appended to the provided skip pattern.

    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.5.2 Asynchronous Rest Template

    [Important]Important

    Starting with Sleuth 2.0.0 we no longer register +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 @@ -803,23 +805,23 @@ can see an example of how to set up such a custom AsyncRes //CUSTOMIZE HERE return factory; } -}

    15.5.3 WebClient

    We inject a ExchangeFilterFunction implementation that creates a span and via on success and on +}

    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.5.4 Traverson

    If you’re using the Traverson library +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;
     
     Traverson traverson = new Traverson(URI.create("http://some/address"),
         MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8).setRestOperations(restTemplate);
    -// use Traverson

    15.6 Feign

    By default Spring Cloud Sleuth provides integration with feign via the TraceFeignClientAutoConfiguration. You can disable it entirely +// 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.7 Asynchronous communication

    15.7.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.7.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 +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.7.3 Executor, ExecutorService and ScheduledExecutorService

    We’re providing LazyTraceExecutor, TraceableExecutorService and TraceableScheduledExecutorService. Those implementations +@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(() -> {
     	// perform some logic
     	return 1_000_000L;
    @@ -846,9 +848,9 @@ can see an example of how to set up such a custom Executor
     		executor.initialize();
     		return new LazyTraceExecutor(this.beanFactory, executor);
     	}
    -}

    15.8 Messaging

    Spring Cloud Sleuth integrates with Spring Integration. It creates spans for publish and +}

    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.9 Zuul

    We’re registering Zuul filters to propagate the tracing information (the request header is enriched with tracing data). +Decorating Spring Integration Executor Channel with TraceableExecutorService will cause the spans to be improperly closed.

    15.10 Zuul

    We’re registering Zuul filters to propagate the tracing information (the request header is enriched with tracing data). 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 diff --git a/spring-cloud-sleuth.xml b/spring-cloud-sleuth.xml index 7e4f37898..838c76e33 100644 --- a/spring-cloud-sleuth.xml +++ b/spring-cloud-sleuth.xml @@ -648,9 +648,6 @@ a baggage element then it will be sent downstream either via HTTP or messaging t Provides a way to create / continue spans and add tags and logs via annotations. -Provides simple metrics of accepted / dropped spans. - - If spring-cloud-sleuth-zipkin 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. @@ -663,6 +660,9 @@ Configure the location of the service using spring.zipkin.baseUrl + +Spring Cloud Sleuth is OpenTracing compatible + If using Zipkin, configure the percentage of spans exported using spring.sleuth.sampler.percentage @@ -1551,6 +1551,12 @@ on 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 +
    Runnable and Callable If you’re wrapping your logic in Runnable or Callable it’s enough to wrap those classes in their Sleuth representative.