From 2806747c6eedb15bad5136d39cf1c9493fcb2bd8 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 19 Apr 2016 10:01:17 +0200 Subject: [PATCH] Added docs with an example of CompletableFuture usage --- .../main/asciidoc/spring-cloud-sleuth.adoc | 8 ++++++++ .../async/TraceableExecutorServiceTests.java | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc index e7b1da43b..2b9a1ca9c 100644 --- a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc +++ b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc @@ -487,6 +487,14 @@ If you want to skip Span creation for some `@Scheduled` annotated classes you ca 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`: + +[source,java] +---- + +include::../../../../spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceableExecutorServiceTests.java[tags=completablefuture,indent=0] +---- + === Messaging Spring Cloud Sleuth integrates with http://projects.spring.io/spring-integration/[Spring Integration]. It creates spans for publish and diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceableExecutorServiceTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceableExecutorServiceTests.java index c74d4f770..5c3c7047e 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceableExecutorServiceTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceableExecutorServiceTests.java @@ -15,6 +15,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.cloud.sleuth.DefaultSpanNamer; import org.springframework.cloud.sleuth.NoOpSpanReporter; import org.springframework.cloud.sleuth.Span; import org.springframework.cloud.sleuth.SpanNamer; @@ -66,6 +67,25 @@ public class TraceableExecutorServiceTests { then(this.spanVerifyingRunnable.spanIds.stream().distinct().collect(toList())).hasSize(TOTAL_THREADS); } + @Test + public void should_propagate_trace_info_when_compleable_future_is_used() throws Exception { + Tracer tracer = this.tracer; + TraceKeys traceKeys = new TraceKeys(); + SpanNamer spanNamer = new DefaultSpanNamer(); + + // tag::completablefuture[] + CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> { + // perform some logic + return 1_000_000L; + }, new TraceableExecutorService(Executors.newCachedThreadPool(), + // 'calculateTax' explicitly names the span - this param is optional + tracer, traceKeys, spanNamer, "calculateTax")); + // end::completablefuture[] + + then(completableFuture.get()).isEqualTo(1_000_000L); + then(this.tracer.getCurrentSpan()).isNull(); + } + private CompletableFuture[] runnablesExecutedViaTraceManagerableExecutorService() { List> futures = new ArrayList<>(); for (int i = 0; i < TOTAL_THREADS; i++) {