Search in sources :

Example 1 with BDDAssertions.then

use of org.assertj.core.api.BDDAssertions.then in project spring-cloud-sleuth by spring-cloud.

the class SpringCloudSleuthDocTests method should_start_a_span_with_explicit_parent.

@Test
public void should_start_a_span_with_explicit_parent() throws Exception {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    String commissionValue = "10";
    Span initialSpan = this.tracer.nextSpan().name("calculateTax").start();
    executorService.submit(() -> {
        // tag::manual_span_joining[]
        // 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;
        try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(initialSpan)) {
            newSpan = this.tracer.nextSpan().name("calculateCommission");
            // ...
            // You can tag a span
            newSpan.tag("commissionValue", commissionValue);
            // ...
            // You can log an event on a span
            newSpan.annotate("commissionCalculated");
        } finally {
            // newSpan will not be present on the parent
            if (newSpan != null) {
                newSpan.finish();
            }
        }
    // end::manual_span_joining[]
    }).get();
    List<zipkin2.Span> spans = this.reporter.getSpans();
    Optional<zipkin2.Span> calculateTax = spans.stream().filter(span -> span.name().equals("calculatecommission")).findFirst();
    BDDAssertions.then(calculateTax).isPresent();
    BDDAssertions.then(calculateTax.get().tags()).containsEntry("commissionValue", "10");
    BDDAssertions.then(calculateTax.get().annotations()).hasSize(1);
    executorService.shutdown();
}
Also used : BDDAssertions(org.assertj.core.api.BDDAssertions) Span(brave.Span) Callable(java.util.concurrent.Callable) ErrorParser(org.springframework.cloud.sleuth.ErrorParser) CurrentTraceContext(brave.propagation.CurrentTraceContext) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) SpanNamer(org.springframework.cloud.sleuth.SpanNamer) Future(java.util.concurrent.Future) Sampler(brave.sampler.Sampler) SpanName(org.springframework.cloud.sleuth.SpanName) ArrayListSpanReporter(org.springframework.cloud.sleuth.util.ArrayListSpanReporter) TraceCallable(org.springframework.cloud.sleuth.instrument.async.TraceCallable) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Tracing(brave.Tracing) Tracer(brave.Tracer) Test(org.junit.Test) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) DefaultSpanNamer(org.springframework.cloud.sleuth.DefaultSpanNamer) Optional(java.util.Optional) TraceRunnable(org.springframework.cloud.sleuth.instrument.async.TraceRunnable) Bean(org.springframework.context.annotation.Bean) ExecutorService(java.util.concurrent.ExecutorService) Span(brave.Span) Test(org.junit.Test)

Aggregations

Span (brave.Span)1 Tracer (brave.Tracer)1 Tracing (brave.Tracing)1 CurrentTraceContext (brave.propagation.CurrentTraceContext)1 Sampler (brave.sampler.Sampler)1 List (java.util.List)1 Optional (java.util.Optional)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 BDDAssertions (org.assertj.core.api.BDDAssertions)1 BDDAssertions.then (org.assertj.core.api.BDDAssertions.then)1 Before (org.junit.Before)1 Test (org.junit.Test)1 DefaultSpanNamer (org.springframework.cloud.sleuth.DefaultSpanNamer)1 ErrorParser (org.springframework.cloud.sleuth.ErrorParser)1 ExceptionMessageErrorParser (org.springframework.cloud.sleuth.ExceptionMessageErrorParser)1 SpanName (org.springframework.cloud.sleuth.SpanName)1