Search in sources :

Example 31 with MockSpan

use of io.opentracing.mock.MockSpan in project camel by apache.

the class SqlSpanDecoratorTest method testPre.

@Test
public void testPre() {
    Endpoint endpoint = Mockito.mock(Endpoint.class);
    Exchange exchange = Mockito.mock(Exchange.class);
    Message message = Mockito.mock(Message.class);
    Mockito.when(endpoint.getEndpointUri()).thenReturn("test");
    Mockito.when(exchange.getIn()).thenReturn(message);
    Mockito.when(message.getHeader(SqlSpanDecorator.CAMEL_SQL_QUERY)).thenReturn(SQL_STATEMENT);
    SpanDecorator decorator = new SqlSpanDecorator();
    MockTracer tracer = new MockTracer();
    MockSpan span = (MockSpan) tracer.buildSpan("TestSpan").start();
    decorator.pre(span, exchange, endpoint);
    assertEquals("sql", span.tags().get(Tags.DB_TYPE.getKey()));
    assertEquals(SQL_STATEMENT, span.tags().get(Tags.DB_STATEMENT.getKey()));
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Message(org.apache.camel.Message) SpanDecorator(org.apache.camel.opentracing.SpanDecorator) MockTracer(io.opentracing.mock.MockTracer) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 32 with MockSpan

use of io.opentracing.mock.MockSpan in project opentracing-java by opentracing.

the class HandlerTest method bad_solution_to_set_parent.

/**
 * Solution is bad because parent is per client (we don't have better choice).
 * Therefore all client requests will have the same parent.
 * But if client is long living and injected/reused in different places then initial parent will not be correct.
 */
@Test
public void bad_solution_to_set_parent() throws Exception {
    Client client;
    try (Scope parent = tracer.buildSpan("parent").startActive(true)) {
        client = new Client(new RequestHandler(tracer, parent.span().context()));
        String response = client.send("correct_parent").get(15, TimeUnit.SECONDS);
        assertEquals("correct_parent:response", response);
    }
    // Send second request, now there is no active parent, but it will be set, ups
    String response = client.send("wrong_parent").get(15, TimeUnit.SECONDS);
    assertEquals("wrong_parent:response", response);
    List<MockSpan> finished = tracer.finishedSpans();
    assertEquals(3, finished.size());
    sortByStartMicros(finished);
    MockSpan parent = getOneByOperationName(finished, "parent");
    assertNotNull(parent);
    // now there is parent/child relation between first and second span:
    assertEquals(parent.context().spanId(), finished.get(1).parentId());
    // third span should not have parent, but it has, damn it
    assertEquals(parent.context().spanId(), finished.get(2).parentId());
}
Also used : Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 33 with MockSpan

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

Example 34 with MockSpan

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

Example 35 with MockSpan

use of io.opentracing.mock.MockSpan 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);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Aggregations

MockSpan (io.opentracing.mock.MockSpan)51 Test (org.junit.Test)50 MockTracer (io.opentracing.mock.MockTracer)17 Matchers.anyString (org.mockito.Matchers.anyString)14 SpanDecorator (org.apache.camel.opentracing.SpanDecorator)13 Endpoint (org.apache.camel.Endpoint)11 Scope (io.opentracing.Scope)10 Exchange (org.apache.camel.Exchange)10 Message (org.apache.camel.Message)8 Span (io.opentracing.Span)2 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)1 Response (com.weibo.api.motan.rpc.Response)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Client (javax.ws.rs.client.Client)1 WebTarget (javax.ws.rs.client.WebTarget)1