use of io.opentracing.Scope in project opentracing-java by opentracing.
the class HandlerTest method parent_not_picked_up.
/**
* active parent is not picked up by child
*/
@Test
public void parent_not_picked_up() throws Exception {
try (Scope parent = tracer.buildSpan("parent").startActive(true)) {
String response = client.send("no_parent").get(15, TimeUnit.SECONDS);
assertEquals("no_parent:response", response);
}
List<MockSpan> finished = tracer.finishedSpans();
assertEquals(2, finished.size());
MockSpan child = getOneByOperationName(finished, RequestHandler.OPERATION_NAME);
assertNotNull(child);
MockSpan parent = getOneByOperationName(finished, "parent");
assertNotNull(parent);
// Here check that there is no parent-child relation although it should be because child is
// created when parent is active
assertNotEquals(parent.context().spanId(), child.parentId());
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class NestedCallbacksTest method test.
@Test
public void test() throws Exception {
try (Scope scope = tracer.buildSpan("one").startActive(false)) {
submitCallbacks(scope.span());
}
await().atMost(15, TimeUnit.SECONDS).until(finishedSpansSize(tracer), equalTo(1));
List<MockSpan> spans = tracer.finishedSpans();
assertEquals(1, spans.size());
assertEquals("one", spans.get(0).operationName());
Map<String, Object> tags = spans.get(0).tags();
assertEquals(3, tags.size());
for (int i = 1; i <= 3; i++) {
assertEquals(Integer.toString(i), tags.get("key" + i));
}
assertNull(tracer.scopeManager().active());
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class PromisePropagationTest method testPromiseCallback.
@Test
public void testPromiseCallback() {
// register test thread
phaser.register();
final AtomicReference<String> successResult1 = new AtomicReference<>();
final AtomicReference<String> successResult2 = new AtomicReference<>();
final AtomicReference<Throwable> errorResult = new AtomicReference<>();
try (PromiseContext context = new PromiseContext(phaser, 3)) {
try (Scope parent = tracer.buildSpan("promises").withTag(Tags.COMPONENT.getKey(), "example-promises").startActive(true)) {
Promise<String> successPromise = new Promise<>(context, tracer);
successPromise.onSuccess(new Promise.SuccessCallback<String>() {
@Override
public void accept(String s) {
tracer.scopeManager().active().span().log("Promised 1 " + s);
successResult1.set(s);
// result set
phaser.arriveAndAwaitAdvance();
}
});
successPromise.onSuccess(new Promise.SuccessCallback<String>() {
@Override
public void accept(String s) {
tracer.scopeManager().active().span().log("Promised 2 " + s);
successResult2.set(s);
// result set
phaser.arriveAndAwaitAdvance();
}
});
Promise errorPromise = new Promise(context, tracer);
errorPromise.onError(new Promise.ErrorCallback() {
@Override
public void accept(Throwable t) {
errorResult.set(t);
// result set
phaser.arriveAndAwaitAdvance();
}
});
assertThat(tracer.finishedSpans().size()).isEqualTo(0);
successPromise.success("success!");
errorPromise.error(new Exception("some error."));
}
// wait for results to be set
phaser.arriveAndAwaitAdvance();
assertThat(successResult1.get()).isEqualTo("success!");
assertThat(successResult2.get()).isEqualTo("success!");
assertThat(errorResult.get()).hasMessage("some error.");
// wait for traces to be reported
phaser.arriveAndAwaitAdvance();
List<MockSpan> finished = tracer.finishedSpans();
assertThat(finished.size()).isEqualTo(4);
assertThat(getOneByTag(finished, Tags.COMPONENT, "example-promises")).isNotNull();
assertThat(getOneByTag(finished, Tags.COMPONENT, "example-promises").parentId()).isEqualTo(0);
long parentId = getOneByTag(finished, Tags.COMPONENT, "example-promises").context().spanId();
assertThat(getByTag(finished, Tags.COMPONENT, "success")).hasSize(2);
for (MockSpan span : getByTag(finished, Tags.COMPONENT, "success")) {
assertThat(span.parentId()).isEqualTo(parentId);
}
assertThat(getOneByTag(finished, Tags.COMPONENT, "error")).isNotNull();
assertThat(getOneByTag(finished, Tags.COMPONENT, "error").parentId()).isEqualTo(parentId);
}
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class Actor method tell.
public void tell(final String message) {
final Span parent = tracer.scopeManager().active().span();
phaser.register();
executor.submit(new Runnable() {
@Override
public void run() {
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();
child.span().log("received " + message);
// assert size
phaser.arriveAndAwaitAdvance();
}
// child tracer finished
phaser.arriveAndAwaitAdvance();
// assert size
phaser.arriveAndAwaitAdvance();
}
});
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class MockTracerTest method testActiveSpan.
@Test
public void testActiveSpan() {
MockTracer mockTracer = new MockTracer();
Assert.assertNull(mockTracer.activeSpan());
Scope scope = null;
try {
scope = mockTracer.buildSpan("foo").startActive(true);
Assert.assertEquals(mockTracer.scopeManager().active().span(), mockTracer.activeSpan());
} finally {
scope.close();
}
Assert.assertNull(mockTracer.activeSpan());
}
Aggregations