TracingCallback { static Callback create(@Nullable Callback delegate, Span span, CurrentTraceContext current) { if (delegate == null) return new FinishSpan(span); return new DelegateAndFinishSpan(delegate, span, current); }    }
@Test public void onCompletion_shouldKeepContext_whenNotSampled() { Span span = tracing.tracer().nextSpan(TraceContextOrSamplingFlags.NOT_SAMPLED); Callback delegate = (metadata, exception) -> assertThat(tracing.tracer().currentSpan()).isEqualTo(span); Callback tracingCallback = TracingCallback.create(delegate, span, tracing.currentTraceContext()); tracingCallback.onCompletion(null, null); }
@Test public void on_completion_should_finish_span() { Span span = tracing.tracer().nextSpan().start(); Callback tracingCallback = TracingCallback.create(null, span, currentTraceContext); tracingCallback.onCompletion(createRecordMetadata(), null); assertThat(spans.get(0).finishTimestamp()).isNotZero(); }
@Test public void on_completion_should_tag_if_exception() { Span span = tracing.tracer().nextSpan().start(); Callback tracingCallback = TracingCallback.create(null, span, currentTraceContext); tracingCallback.onCompletion(null, error); assertThat(spans.get(0).finishTimestamp()).isNotZero(); assertThat(spans.get(0).error()).isEqualTo(error); }
@Test public void on_completion_should_forward_then_finish_span() { Span span = tracing.tracer().nextSpan().start(); Callback delegate = mock(Callback.class); Callback tracingCallback = TracingCallback.create(delegate, span, currentTraceContext); RecordMetadata md = createRecordMetadata(); tracingCallback.onCompletion(md, null); verify(delegate).onCompletion(md, null); assertThat(spans.get(0).finishTimestamp()).isNotZero(); }
@Test public void on_completion_should_have_span_in_scope() { Span span = tracing.tracer().nextSpan().start(); Callback delegate = (metadata, exception) -> assertThat(currentTraceContext.get()).isSameAs(span.context()); TracingCallback.create(delegate, span, currentTraceContext) .onCompletion(createRecordMetadata(), null); assertThat(spans.get(0).finishTimestamp()).isNotZero(); }
@Test public void on_completion_should_forward_then_tag_if_exception() { Span span = tracing.tracer().nextSpan().start(); Callback delegate = mock(Callback.class); Callback tracingCallback = TracingCallback.create(delegate, span, currentTraceContext); RecordMetadata md = createRecordMetadata(); tracingCallback.onCompletion(md, error); verify(delegate).onCompletion(md, error); assertThat(spans.get(0).finishTimestamp()).isNotZero(); assertThat(spans.get(0).error()).isEqualTo(error); }