Search in sources :

Example 11 with Span

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);
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Test(org.junit.Test)

Example 12 with Span

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();
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Test(org.junit.Test)

Example 13 with Span

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);
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Test(org.junit.Test)

Example 14 with Span

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;
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span)

Example 15 with Span

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());
}
Also used : MockSpan(io.opentracing.mock.MockSpan) Span(io.opentracing.Span) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Aggregations

Span (io.opentracing.Span)368 Future (io.vertx.core.Future)182 HttpURLConnection (java.net.HttpURLConnection)174 Tracer (io.opentracing.Tracer)123 JsonObject (io.vertx.core.json.JsonObject)119 ClientErrorException (org.eclipse.hono.client.ClientErrorException)117 Map (java.util.Map)115 SpanContext (io.opentracing.SpanContext)114 Objects (java.util.Objects)109 List (java.util.List)104 TracingHelper (org.eclipse.hono.tracing.TracingHelper)104 Optional (java.util.Optional)102 Promise (io.vertx.core.Promise)98 Test (org.junit.jupiter.api.Test)96 MessageHelper (org.eclipse.hono.util.MessageHelper)89 Vertx (io.vertx.core.Vertx)84 Constants (org.eclipse.hono.util.Constants)82 ServerErrorException (org.eclipse.hono.client.ServerErrorException)80 Truth.assertThat (com.google.common.truth.Truth.assertThat)79 Mockito.mock (org.mockito.Mockito.mock)79