use of io.opentracing.Scope in project opentracing-java by opentracing.
the class ActiveSpanReplacementTest method test.
@Test
public void test() throws Exception {
// Start an isolated task and query for its result in another task/thread
try (Scope scope = tracer.buildSpan("initial").startActive(false)) {
// Explicitly pass a Span to be finished once a late calculation is done.
submitAnotherTask(scope.span());
}
await().atMost(15, TimeUnit.SECONDS).until(finishedSpansSize(tracer), equalTo(3));
List<MockSpan> spans = tracer.finishedSpans();
assertEquals(3, spans.size());
// Isolated task
assertEquals("initial", spans.get(0).operationName());
assertEquals("subtask", spans.get(1).operationName());
assertEquals("task", spans.get(2).operationName());
// task/subtask are part of the same trace, and subtask is a child of task
assertEquals(spans.get(1).context().traceId(), spans.get(2).context().traceId());
assertEquals(spans.get(2).context().spanId(), spans.get(1).parentId());
// initial task is not related in any way to those two tasks
assertNotEquals(spans.get(0).context().traceId(), spans.get(1).context().traceId());
assertEquals(0, spans.get(0).parentId());
assertNull(tracer.scopeManager().active());
}
use of io.opentracing.Scope 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.Scope in project opentracing-java by opentracing.
the class Client method send.
public void send() throws InterruptedException {
Message message = new Message();
try (Scope scope = tracer.buildSpan("send").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).withTag(Tags.COMPONENT.getKey(), "example-client").startActive(true)) {
tracer.inject(scope.span().context(), Builtin.TEXT_MAP, new TextMapInjectAdapter(message));
queue.put(message);
}
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class ActorPropagationTest method testActorAsk.
@Test
public void testActorAsk() throws ExecutionException, InterruptedException {
try (Actor actor = new Actor(tracer, phaser)) {
phaser.register();
Future<String> future1;
Future<String> future2;
try (Scope parent = tracer.buildSpan("actorAsk").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_PRODUCER).withTag(Tags.COMPONENT.getKey(), "example-actor").startActive(true)) {
future1 = actor.ask("my message 1");
future2 = actor.ask("my message 2");
}
// child tracer started
phaser.arriveAndAwaitAdvance();
assertThat(tracer.finishedSpans().size()).isEqualTo(1);
// continue...
phaser.arriveAndAwaitAdvance();
// child tracer finished
phaser.arriveAndAwaitAdvance();
assertThat(tracer.finishedSpans().size()).isEqualTo(3);
assertThat(getByTag(tracer.finishedSpans(), Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER)).hasSize(2);
// continue...
phaser.arriveAndDeregister();
List<MockSpan> finished = tracer.finishedSpans();
// This really should be a non-blocking callback...
String message1 = future1.get();
// This really should be a non-blocking callback...
String message2 = future2.get();
assertThat(message1).isEqualTo("received my message 1");
assertThat(message2).isEqualTo("received my message 2");
assertThat(finished.size()).isEqualTo(3);
assertThat(finished.get(0).context().traceId()).isEqualTo(finished.get(1).context().traceId());
assertThat(getByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER)).hasSize(2);
assertThat(getOneByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_PRODUCER)).isNotNull();
assertThat(tracer.scopeManager().active()).isNull();
}
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class ActorPropagationTest method testActorTell.
@Test
public void testActorTell() {
try (Actor actor = new Actor(tracer, phaser)) {
phaser.register();
try (Scope parent = tracer.buildSpan("actorTell").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_PRODUCER).withTag(Tags.COMPONENT.getKey(), "example-actor").startActive(true)) {
actor.tell("my message 1");
actor.tell("my message 2");
}
// child tracer started
phaser.arriveAndAwaitAdvance();
// Parent should be reported
assertThat(tracer.finishedSpans().size()).isEqualTo(1);
// continue...
phaser.arriveAndAwaitAdvance();
// child tracer finished
phaser.arriveAndAwaitAdvance();
assertThat(tracer.finishedSpans().size()).isEqualTo(3);
assertThat(getByTag(tracer.finishedSpans(), Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER)).hasSize(2);
// continue...
phaser.arriveAndDeregister();
List<MockSpan> finished = tracer.finishedSpans();
assertThat(finished.size()).isEqualTo(3);
assertThat(finished.get(0).context().traceId()).isEqualTo(finished.get(1).context().traceId());
assertThat(getByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER)).hasSize(2);
assertThat(getOneByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_PRODUCER)).isNotNull();
assertThat(tracer.scopeManager().active()).isNull();
}
}
Aggregations