use of io.opentracing.Span in project opentracing-java by opentracing.
the class AutoFinishScopeTest method implicitSpanStack.
@Test
public void implicitSpanStack() throws Exception {
Span backgroundSpan = mock(Span.class);
Span foregroundSpan = mock(Span.class);
// Quasi try-with-resources (this is 1.6).
Scope backgroundActive = manager.activate(backgroundSpan, true);
try {
assertNotNull(backgroundActive);
// Activate a new Scope on top of the background one.
Scope foregroundActive = manager.activate(foregroundSpan, true);
try {
Scope shouldBeForeground = manager.active();
assertEquals(foregroundActive, shouldBeForeground);
} finally {
foregroundActive.close();
}
// And now the backgroundActive should be reinstated.
Scope shouldBeBackground = manager.active();
assertEquals(backgroundActive, shouldBeBackground);
} finally {
backgroundActive.close();
}
// The background and foreground Spans should be finished.
verify(backgroundSpan, times(1)).finish();
verify(foregroundSpan, times(1)).finish();
// And now nothing is active.
Scope missingSpan = manager.active();
assertNull(missingSpan);
}
use of io.opentracing.Span in project opentracing-java by opentracing.
the class AutoFinishScopeTest method testDeactivateWhenDifferentSpanIsActive.
@Test
public void testDeactivateWhenDifferentSpanIsActive() {
Span span = mock(Span.class);
Scope active = manager.activate(span, true);
manager.activate(mock(Span.class), true);
active.close();
verify(span, times(0)).finish();
}
use of io.opentracing.Span in project opentracing-java by opentracing.
the class ThreadLocalScopeManagerTest method defaultActivate.
@Test
public void defaultActivate() throws Exception {
Span span = mock(Span.class);
// We can't use 1.7 features like try-with-resources in this repo without meddling with pom details for tests.
Scope scope = source.activate(span, false);
try {
assertNotNull(scope);
Scope otherScope = source.active();
assertEquals(otherScope, scope);
} finally {
scope.close();
}
// Make sure the Span is not finished.
verify(span, times(0)).finish();
// And now it's gone:
Scope missingScope = source.active();
assertNull(missingScope);
}
use of io.opentracing.Span in project opentracing-java by opentracing.
the class Actor method ask.
public Future<String> ask(final String message) {
final Span parent = tracer.scopeManager().active().span();
phaser.register();
Future<String> future = executor.submit(new Callable<String>() {
@Override
public String call() throws Exception {
try (Scope child = tracer.buildSpan("received").addReference(References.FOLLOWS_FROM, parent.context()).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CONSUMER).startActive(true)) {
// child tracer started
phaser.arriveAndAwaitAdvance();
// assert size
phaser.arriveAndAwaitAdvance();
return "received " + message;
} finally {
// child tracer finished
phaser.arriveAndAwaitAdvance();
// assert size
phaser.arriveAndAwaitAdvance();
}
}
});
return future;
}
use of io.opentracing.Span in project opentracing-java by opentracing.
the class LateSpanFinishTest method test.
@Test
public void test() throws Exception {
// Create a Span manually and use it as parent of a pair of subtasks
Span parentSpan = tracer.buildSpan("parent").startManual();
submitTasks(parentSpan);
// Wait for the threadpool to be done first, instead of polling/waiting
executor.shutdown();
executor.awaitTermination(15, TimeUnit.SECONDS);
// Late-finish the parent Span now
parentSpan.finish();
List<MockSpan> spans = tracer.finishedSpans();
assertEquals(3, spans.size());
assertEquals("task1", spans.get(0).operationName());
assertEquals("task2", spans.get(1).operationName());
assertEquals("parent", spans.get(2).operationName());
assertSameTrace(spans);
assertNull(tracer.scopeManager().active());
}
Aggregations