Search in sources :

Example 1 with TraceRunnable

use of org.springframework.cloud.sleuth.instrument.async.TraceRunnable in project spring-cloud-sleuth by spring-cloud.

the class SpringCloudSleuthDocTests method should_wrap_runnable_in_its_sleuth_representative.

@Test
public void should_wrap_runnable_in_its_sleuth_representative() {
    SpanNamer spanNamer = new DefaultSpanNamer();
    ErrorParser errorParser = new ExceptionMessageErrorParser();
    // tag::trace_runnable[]
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
        // do some work
        }

        @Override
        public String toString() {
            return "spanNameFromToStringMethod";
        }
    };
    // Manual `TraceRunnable` creation with explicit "calculateTax" Span name
    Runnable traceRunnable = new TraceRunnable(tracer, spanNamer, errorParser, runnable, "calculateTax");
    // Wrapping `Runnable` with `Tracing`. That way the current span will be available
    // in the thread of `Runnable`
    Runnable traceRunnableFromTracer = tracing.currentTraceContext().wrap(runnable);
    // end::trace_runnable[]
    then(traceRunnable).isExactlyInstanceOf(TraceRunnable.class);
}
Also used : TraceRunnable(org.springframework.cloud.sleuth.instrument.async.TraceRunnable) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) SpanNamer(org.springframework.cloud.sleuth.SpanNamer) DefaultSpanNamer(org.springframework.cloud.sleuth.DefaultSpanNamer) TraceRunnable(org.springframework.cloud.sleuth.instrument.async.TraceRunnable) DefaultSpanNamer(org.springframework.cloud.sleuth.DefaultSpanNamer) ErrorParser(org.springframework.cloud.sleuth.ErrorParser) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) Test(org.junit.Test)

Example 2 with TraceRunnable

use of org.springframework.cloud.sleuth.instrument.async.TraceRunnable in project spring-cloud-sleuth by spring-cloud.

the class SpringCloudSleuthDocTests method should_set_runnable_name_to_to_string_value.

@Test
public void should_set_runnable_name_to_to_string_value() throws ExecutionException, InterruptedException {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    SpanNamer spanNamer = new DefaultSpanNamer();
    ErrorParser errorParser = new ExceptionMessageErrorParser();
    // tag::span_name_to_string_runnable_execution[]
    Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser, new Runnable() {

        @Override
        public void run() {
        // perform logic
        }

        @Override
        public String toString() {
            return "calculateTax";
        }
    });
    Future<?> future = executorService.submit(runnable);
    // ... some additional logic ...
    future.get();
    // end::span_name_to_string_runnable_execution[]
    List<zipkin2.Span> spans = this.reporter.getSpans();
    then(spans).hasSize(1);
    then(spans.get(0).name()).isEqualTo("calculatetax");
    executorService.shutdown();
}
Also used : TraceRunnable(org.springframework.cloud.sleuth.instrument.async.TraceRunnable) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) SpanNamer(org.springframework.cloud.sleuth.SpanNamer) DefaultSpanNamer(org.springframework.cloud.sleuth.DefaultSpanNamer) TraceRunnable(org.springframework.cloud.sleuth.instrument.async.TraceRunnable) ExecutorService(java.util.concurrent.ExecutorService) DefaultSpanNamer(org.springframework.cloud.sleuth.DefaultSpanNamer) ErrorParser(org.springframework.cloud.sleuth.ErrorParser) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) Span(brave.Span) Test(org.junit.Test)

Example 3 with TraceRunnable

use of org.springframework.cloud.sleuth.instrument.async.TraceRunnable in project spring-cloud-sleuth by spring-cloud.

the class SpringCloudSleuthDocTests method should_set_runnable_name_to_annotated_value.

// end::span_name_annotation[]
@Test
public void should_set_runnable_name_to_annotated_value() throws ExecutionException, InterruptedException {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    SpanNamer spanNamer = new DefaultSpanNamer();
    ErrorParser errorParser = new ExceptionMessageErrorParser();
    // tag::span_name_annotated_runnable_execution[]
    Runnable runnable = new TraceRunnable(tracer, spanNamer, errorParser, new TaxCountingRunnable());
    Future<?> future = executorService.submit(runnable);
    // ... some additional logic ...
    future.get();
    // end::span_name_annotated_runnable_execution[]
    List<zipkin2.Span> spans = this.reporter.getSpans();
    then(spans).hasSize(1);
    then(spans.get(0).name()).isEqualTo("calculatetax");
}
Also used : TraceRunnable(org.springframework.cloud.sleuth.instrument.async.TraceRunnable) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) SpanNamer(org.springframework.cloud.sleuth.SpanNamer) DefaultSpanNamer(org.springframework.cloud.sleuth.DefaultSpanNamer) TraceRunnable(org.springframework.cloud.sleuth.instrument.async.TraceRunnable) ExecutorService(java.util.concurrent.ExecutorService) DefaultSpanNamer(org.springframework.cloud.sleuth.DefaultSpanNamer) ErrorParser(org.springframework.cloud.sleuth.ErrorParser) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) Span(brave.Span) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 DefaultSpanNamer (org.springframework.cloud.sleuth.DefaultSpanNamer)3 ErrorParser (org.springframework.cloud.sleuth.ErrorParser)3 ExceptionMessageErrorParser (org.springframework.cloud.sleuth.ExceptionMessageErrorParser)3 SpanNamer (org.springframework.cloud.sleuth.SpanNamer)3 TraceRunnable (org.springframework.cloud.sleuth.instrument.async.TraceRunnable)3 Span (brave.Span)2 ExecutorService (java.util.concurrent.ExecutorService)2