Search in sources :

Example 46 with MockSpan

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

the class AbstractBaseITests method testAsync.

@Test
public void testAsync() throws Exception {
    {
        getRestTemplate().getForEntity("/async", String.class);
        Awaitility.await().until(reportedSpansSize(), IsEqual.equalTo(1));
    }
    List<MockSpan> mockSpans = TracingBeansConfiguration.mockTracer.finishedSpans();
    Assert.assertEquals(1, mockSpans.size());
    assertOnErrors(mockSpans);
    MockSpan span = mockSpans.get(0);
    Assert.assertEquals("async", span.operationName());
    assertLogEvents(span.logEntries(), Arrays.asList("preHandle", "afterConcurrentHandlingStarted", "preHandle", "afterCompletion"));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 47 with MockSpan

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

the class AbstractBaseITests method testAsyncDeferred.

@Test
public void testAsyncDeferred() throws Exception {
    {
        getRestTemplate().getForEntity("/asyncDeferred", String.class);
        Awaitility.await().until(reportedSpansSize(), IsEqual.equalTo(1));
    }
    List<MockSpan> mockSpans = TracingBeansConfiguration.mockTracer.finishedSpans();
    Assert.assertEquals(1, mockSpans.size());
    assertOnErrors(mockSpans);
    MockSpan span = mockSpans.get(0);
    Assert.assertEquals("test", span.operationName());
    Assert.assertEquals(5, span.tags().size());
    Assert.assertEquals(Tags.SPAN_KIND_SERVER, span.tags().get(Tags.SPAN_KIND.getKey()));
    Assert.assertEquals("GET", span.tags().get(Tags.HTTP_METHOD.getKey()));
    Assert.assertEquals(getUrl("/asyncDeferred"), span.tags().get(Tags.HTTP_URL.getKey()));
    Assert.assertEquals(202, span.tags().get(Tags.HTTP_STATUS.getKey()));
    Assert.assertNotNull(span.tags().get(Tags.COMPONENT.getKey()));
    assertLogEvents(span.logEntries(), Arrays.asList("preHandle", "afterConcurrentHandlingStarted", "preHandle", "afterCompletion"));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 48 with MockSpan

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

the class AbstractBaseITests method testNoURLMapping.

@Test
public void testNoURLMapping() {
    {
        getRestTemplate().getForEntity("/nouUrlMapping", String.class);
        Awaitility.await().until(reportedSpansSize(), IsEqual.equalTo(2));
    }
    List<MockSpan> mockSpans = TracingBeansConfiguration.mockTracer.finishedSpans();
    Assert.assertEquals(2, mockSpans.size());
    assertOnErrors(mockSpans);
    MockSpan span = mockSpans.get(0);
    Assert.assertEquals("GET", span.operationName());
    Assert.assertEquals(404, span.tags().get(Tags.HTTP_STATUS.getKey()));
    assertLogEvents(span.logEntries(), Collections.<String>emptyList());
    span = mockSpans.get(1);
    Assert.assertEquals(0, span.tags().size());
    Assert.assertEquals(mockSpans.get(0).context().spanId(), span.parentId());
    Assert.assertEquals(0, span.tags().size());
    assertLogEvents(span.logEntries(), Arrays.asList("preHandle", "afterCompletion"));
    Assert.assertEquals("BasicErrorController", span.logEntries().get(0).fields().get("handler.class_simple_name"));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 49 with MockSpan

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

the class AbstractBaseITests method testLocalSpan.

@Test
public void testLocalSpan() {
    {
        getRestTemplate().getForEntity("/localSpan", String.class);
        Awaitility.await().until(reportedSpansSize(), IsEqual.equalTo(2));
    }
    List<MockSpan> mockSpans = TracingBeansConfiguration.mockTracer.finishedSpans();
    Assert.assertEquals(2, mockSpans.size());
    assertOnErrors(mockSpans);
    MockSpan childSpan = mockSpans.get(0);
    MockSpan parentSpan = mockSpans.get(1);
    Assert.assertEquals("localSpan", parentSpan.operationName());
    Assert.assertEquals(childSpan.context().traceId(), parentSpan.context().traceId());
    Assert.assertEquals(childSpan.parentId(), parentSpan.context().spanId());
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 50 with MockSpan

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

the class TracingAsyncRestTemplateTest method testMultipleRequests.

@Test
public void testMultipleRequests() throws InterruptedException, ExecutionException {
    final String url = "http://localhost:8080/foo/";
    int numberOfCalls = 1000;
    mockServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(new Contains("/foo"))).andRespond(MockRestResponseCreators.withSuccess());
    ExecutorService executorService = Executors.newFixedThreadPool(100);
    List<Future<?>> futures = new ArrayList<>(numberOfCalls);
    for (int i = 0; i < numberOfCalls; i++) {
        final String requestUrl = url + i;
        final Scope parentSpan = mockTracer.buildSpan("foo").startActive(false);
        parentSpan.span().setTag("request-url", requestUrl);
        final Span cont = parentSpan.span();
        futures.add(executorService.submit(new Runnable() {

            @Override
            public void run() {
                try (Scope span = mockTracer.scopeManager().activate(cont, true)) {
                    client.getForEntity(requestUrl, String.class);
                }
            }
        }));
        parentSpan.close();
    }
    // wait to finish all calls
    for (Future<?> future : futures) {
        future.get();
    }
    executorService.awaitTermination(1, TimeUnit.SECONDS);
    executorService.shutdown();
    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    Assert.assertEquals(numberOfCalls * 2, mockSpans.size());
    final List<MockSpan> parentSpans = new ArrayList<>();
    final Map<Long, MockSpan> childSpans = new HashMap<>();
    for (MockSpan mockSpan : mockSpans) {
        if (mockSpan.tags().containsKey("request-url")) {
            parentSpans.add(mockSpan);
        } else {
            childSpans.put(mockSpan.parentId(), mockSpan);
        }
    }
    Assert.assertEquals(numberOfCalls, parentSpans.size());
    Assert.assertEquals(numberOfCalls, childSpans.size());
    for (MockSpan parentSpan : parentSpans) {
        MockSpan childSpan = childSpans.get(parentSpan.context().spanId());
        Assert.assertEquals(parentSpan.tags().get("request-url"), childSpan.tags().get(Tags.HTTP_URL.getKey()));
        Assert.assertEquals(parentSpan.context().traceId(), childSpan.context().traceId());
        Assert.assertEquals(parentSpan.context().spanId(), childSpan.parentId());
        Assert.assertEquals(0, childSpan.generatedErrors().size());
        Assert.assertEquals(0, parentSpan.generatedErrors().size());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MockSpan(io.opentracing.mock.MockSpan) Span(io.opentracing.Span) Scope(io.opentracing.Scope) Contains(org.mockito.internal.matchers.Contains) ExecutorService(java.util.concurrent.ExecutorService) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) Future(java.util.concurrent.Future) 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