Java Code Examples for io.opentracing.mock.MockTracer#finishedSpans()

The following examples show how to use io.opentracing.mock.MockTracer#finishedSpans() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LettuceTest.java    From java-specialagent with Apache License 2.0 10 votes vote down vote up
@Test
public void testPubSub(final MockTracer tracer) {
  final StatefulRedisPubSubConnection<String,String> connection = client.connectPubSub();
  connection.addListener(new RedisPubSubAdapter<>());

  final RedisPubSubCommands<String,String> commands = connection.sync();
  commands.subscribe("channel");

  final RedisCommands<String,String> commands2 = client.connect().sync();
  commands2.publish("channel", "msg");

  await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(4));

  client.shutdown();

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(4, spans.size());
}
 
Example 2
Source File: HttpURLConnectionTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void failedRequest(final MockTracer tracer) throws Exception {
  final URL url = new URL("http://localhost:12345");
  final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("GET");
  try {
    final int responseCode = connection.getResponseCode();
    assertEquals(200, responseCode);
  }
  catch (final ConnectException ignore) {
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(1, spans.size());
  assertEquals(HttpURLConnectionAgentIntercept.COMPONENT_NAME, spans.get(0).tags().get(Tags.COMPONENT.getKey()));
}
 
Example 3
Source File: ThriftTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void test(final MockTracer tracer) throws Exception {
  startNewThreadPoolServer();

  final TTransport transport = new TSocket("localhost", port);
  transport.open();

  final TProtocol protocol = new TBinaryProtocol(transport);

  final CustomService.Client client = new CustomService.Client(protocol);
  assertEquals("Say Good bye World", client.say("Good bye", "World"));

  await().atMost(5, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(2));

  final List<MockSpan> mockSpans = tracer.finishedSpans();
  assertEquals(2, mockSpans.size());

  assertTrue(mockSpans.get(0).parentId() != 0 || mockSpans.get(1).parentId() != 0);
  assertNull(tracer.activeSpan());
}
 
Example 4
Source File: TomcatServletTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelloRequest(final MockTracer tracer) throws IOException {
  MockFilter.count = 0;
  MockServlet.count = 0;

  final OkHttpClient client = new OkHttpClient();
  final Request request = new Request.Builder().url("http://localhost:" + serverPort + "/hello").addHeader("key", "val").build();
  final Response response = client.newCall(request).execute();

  assertEquals("MockServlet response", HttpServletResponse.SC_ACCEPTED, response.code());
  assertEquals("MockServlet count", 1, MockServlet.count);
  assertEquals("MockFilter count", 1, MockFilter.count);

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals("MockTracer spans: " + spans, 1, spans.size());
  assertEquals("val", spans.get(0).tags().get("http.header.key"));
}
 
Example 5
Source File: TestUtils.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
static void verifyWithSpanDecorators(MockTracer mockTracer) {
  await().until(() -> mockTracer.finishedSpans().size() == 1);
  List<MockSpan> mockSpans = mockTracer.finishedSpans();
  assertEquals(1, mockSpans.size());
  Map<String, Object> tags = mockSpans.get(0).tags();
  assertEquals(Tags.SPAN_KIND_CLIENT, tags.get(Tags.SPAN_KIND.getKey()));
  assertEquals(MyFeignSpanDecorator.TAG_VALUE, tags.get(MyFeignSpanDecorator.TAG_NAME));
  assertEquals(AnotherFeignSpanDecorator.TAG_VALUE, tags.get(AnotherFeignSpanDecorator.TAG_NAME));
}
 
Example 6
Source File: Dubbo27AgentTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalSpans(final MockTracer tracer) throws Exception {
    client.get().sayHello("jorge");
    List<MockSpan> mockSpans = tracer.finishedSpans();
    Assert.assertEquals(2, mockSpans.size());
    Assert.assertEquals("GreeterService/sayHello", mockSpans.get(0).operationName());
    Assert.assertEquals("GreeterService/sayHello", mockSpans.get(1).operationName());
    Assert.assertEquals("server", mockSpans.get(0).tags().get(Tags.SPAN_KIND.getKey()));
    Assert.assertEquals("client", mockSpans.get(1).tags().get(Tags.SPAN_KIND.getKey()));
}
 
Example 7
Source File: RxJava3Test.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void consumerTest4(final MockTracer tracer) {
  final Observable<Integer> observable = createSequentialObservable(tracer, false);
  final List<Integer> result = new ArrayList<>();
  final Consumer<Integer> onNext = consumer(result);
  final List<String> subscribeList = new ArrayList<>();
  final String subscribed = "subscribed";
  final Consumer<Object> onSubscribe = new Consumer<Object>() {
    @Override
    public void accept(final Object t) {
      subscribeList.add(subscribed);
    }
  };

  final List<String> completeList = new ArrayList<>();
  final List<String> errorList = new ArrayList<>();
  observable.doOnSubscribe(onSubscribe).subscribe(onNext, onError(errorList), onComplete(completeList, tracer));

  assertEquals(5, result.size());

  assertEquals(1, completeList.size());
  assertTrue(completeList.contains(COMPLETED));

  assertEquals(1, subscribeList.size());
  assertTrue(subscribeList.contains(subscribed));

  assertTrue(errorList.isEmpty());

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(1, spans.size());

  assertNull(tracer.scopeManager().active());
}
 
Example 8
Source File: MetricsTest.java    From java-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsChildOf() {
    MetricsReporter reporter = Mockito.mock(MetricsReporter.class);
    MockTracer tracer = new MockTracer();
    Tracer metricsTracer = Metrics.decorate(tracer, reporter);

    Scope parentSpan = metricsTracer.buildSpan("parent")
            .withTag("spanName","parent")
            .startActive(true);
    parentSpan.span().setTag("additionalTag", "parent");

    Scope childSpan = metricsTracer.buildSpan("child")
            .asChildOf(parentSpan.span())
            .withTag("spanName","child")
            .startActive(true);
    childSpan.span().setTag("additionalTag", "child");

    childSpan.close();
    parentSpan.close();

    List<MockSpan> spans = tracer.finishedSpans();
    assertEquals(2, spans.size());
    MockSpan span1 = spans.get(0);
    MockSpan span2 = spans.get(1);

    MockSpan parent, child;
    if (span1.operationName().equals("parent")) {
        parent = span1;
        child = span2;
    } else {
        parent = span2;
        child = span1;
    }

    assertEquals("Child's parent id should be spanId of parent", child.parentId(), parent.context().spanId());
    assertEquals(0, parent.parentId());
}
 
Example 9
Source File: PulsarClientTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void test(final MockTracer tracer, final boolean async) throws Exception {
  if (!isJdkSupported) {
    logger.warning("jdk" + System.getProperty("java.version") + " is not supported by Pulsar");
    return;
  }

  try (
    final PulsarClient client = PulsarClient.builder().serviceUrl(pulsarService.getBrokerServiceUrl()).build();
    final Consumer<byte[]> consumer = client.newConsumer().topic("my-topic").subscriptionName("my-subscription").subscribe();
    final Producer<byte[]> producer = client.newProducer().topic("my-topic").create();
  ) {
    if (async)
      producer.sendAsync("My message".getBytes()).get(15, TimeUnit.SECONDS);
    else
      producer.send("My message".getBytes());

    final Message<byte[]> message;
    if (async)
      message = consumer.receiveAsync().get(15, TimeUnit.SECONDS);
    else
      message = consumer.receive();

    System.out.println("Message received: " + new String(message.getData()));
    consumer.acknowledge(message);
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(2, spans.size());
  assertNull(tracer.activeSpan());
  for (final MockSpan span : spans)
    assertEquals(PulsarClientAgentIntercept.COMPONENT_NAME, span.tags().get(Tags.COMPONENT.getKey()));

  assertEquals(spans.get(0).context().traceId(), spans.get(1).context().traceId());
}
 
Example 10
Source File: JedisTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void withError(final MockTracer tracer) {
  try {
    jedis.eval("bla-bla-bla");
    fail();
  }
  catch (final Exception ignore) {
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(1, spans.size());
  assertEquals(Boolean.TRUE, spans.get(0).tags().get(Tags.ERROR.getKey()));
  assertFalse(spans.get(0).logEntries().isEmpty());
  checkSpans(spans);
}
 
Example 11
Source File: HttpURLConnectionTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void succeedRequest(final MockTracer tracer) throws Exception {
  final URL url = new URL("http://www.google.com");
  final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("GET");
  final int responseCode = connection.getResponseCode();
  assertEquals(200, responseCode);

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(1, spans.size());
  assertEquals(HttpURLConnectionAgentIntercept.COMPONENT_NAME, spans.get(0).tags().get(Tags.COMPONENT.getKey()));
}
 
Example 12
Source File: JedisTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void test(final MockTracer tracer) {
  assertEquals("OK", jedis.set("key", "value"));
  assertEquals("value", jedis.get("key"));

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(2, spans.size());
  checkSpans(spans);
  for (final MockSpan span : spans) {
    assertNull(span.tags().get(Tags.ERROR.getKey()));
  }
}
 
Example 13
Source File: NettyTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void test(final MockTracer tracer) throws InterruptedException {
  final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
  final EventLoopGroup workerGroup = new NioEventLoopGroup();
  try {
    final ServerBootstrap bootstrap = new ServerBootstrap()
      .option(ChannelOption.SO_BACKLOG, 1024)
      .group(bossGroup, workerGroup)
      .channel(NioServerSocketChannel.class)
      .handler(new LoggingHandler(LogLevel.INFO))
      .childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(final SocketChannel socketChannel) {
          final ChannelPipeline pipeline = socketChannel.pipeline();
          pipeline.addLast(new HttpServerCodec());
          pipeline.addLast(new HttpServerHandler());
        }
      });

    bootstrap.bind(8086).sync().channel();
    client();
  }
  finally {
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 14
Source File: Dubbo26AgentTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalSpans(final MockTracer tracer) {
  client.get().sayHello("jorge");
  final List<MockSpan> mockSpans = tracer.finishedSpans();
  Assert.assertEquals(2, mockSpans.size());
  Assert.assertEquals("GreeterService/sayHello", mockSpans.get(0).operationName());
  Assert.assertEquals("GreeterService/sayHello", mockSpans.get(1).operationName());
  Assert.assertEquals("server", mockSpans.get(0).tags().get(Tags.SPAN_KIND.getKey()));
  Assert.assertEquals("client", mockSpans.get(1).tags().get(Tags.SPAN_KIND.getKey()));
}
 
Example 15
Source File: SpringKafkaMessagingTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static MockSpan getSpanByOperation(final String operationName, final MockTracer tracer) {
  for (final MockSpan span : tracer.finishedSpans())
    if (operationName.equals(span.operationName()))
      return span;

  throw new RuntimeException(String.format("Span for operation '%s' doesn't exist", operationName));
}
 
Example 16
Source File: HazelcastTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void test(final HazelcastInstance instance, final MockTracer tracer) {
  final IMap<String,String> map = instance.getMap("map");
  map.put("key", "value");
  assertEquals("value", map.get("key"));
  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 17
Source File: LettuceTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectAsync(final MockTracer tracer) throws Exception {
  final ConnectionFuture<StatefulRedisConnection<String,String>> connectionFuture = client.connectAsync(StringCodec.UTF8, RedisURI.create(address));
  try (final StatefulRedisConnection<String,String> connection = connectionFuture.get(10, TimeUnit.SECONDS)) {
    final RedisCommands<String,String> commands = connection.sync();
    assertEquals("OK", commands.set("key", "value"));
    assertEquals("value", commands.get("key"));
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(3, spans.size());
}
 
Example 18
Source File: GoogleHttpClientTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void failedRequest(final MockTracer tracer) throws IOException {
  final HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();
  final HttpRequest request = requestFactory.buildGetRequest(new GenericUrl("http://localhost:12345"));
  try {
    final int statusCode = request.execute().getStatusCode();
    assertEquals(200, statusCode);
  }
  catch (final ConnectException ignore) {
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(1, spans.size());
  assertEquals(GoogleHttpClientAgentIntercept.COMPONENT_NAME, spans.get(0).tags().get(Tags.COMPONENT.getKey()));
}
 
Example 19
Source File: LettuceTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void testSync(final MockTracer tracer) {
  try (final StatefulRedisConnection<String,String> connection = client.connect()) {
    final RedisCommands<String,String> commands = connection.sync();
    assertEquals("OK", commands.set("key", "value"));
    assertEquals("value", commands.get("key"));
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(3, spans.size());
}
 
Example 20
Source File: FeignTest.java    From java-spring-cloud with Apache License 2.0 4 votes vote down vote up
static void verify(MockTracer mockTracer) {
  await().until(() -> mockTracer.finishedSpans().size() == 1);
  List<MockSpan> mockSpans = mockTracer.finishedSpans();
  assertEquals(1, mockSpans.size());
  assertEquals(Tags.SPAN_KIND_CLIENT, mockSpans.get(0).tags().get(Tags.SPAN_KIND.getKey()));
}