SpanServiceElasticsearch implements SpanService { @Override public Trace getTraceFragment(String tenantId, String id) { Span span = getSpan(tenantId, id); if (span == null) { return null; } InteractionNode interactionNode = spanToNode(span); interactionNode.setNodes(recursiveTraceFragment(tenantId, span)); Trace trace = spanToTrace(span); trace.getNodes().add(interactionNode); return trace; }  @Override Span getSpan(String tenantId, String id); @Override List<Span> getChildren(String tenantId, String id); @Override void storeSpan(String tenantId, List<Span> spans); @Override void storeSpan(String tenantId, List<Span> spans, Function<Span, String> spanIdSupplier); @Override void clear(String tenantId); @Override Trace getTraceFragment(String tenantId, String id); @Override Trace getTrace(String tenantId, String id); static final String SPAN_TYPE; }
@Test public void testGetTraceFragmentRootServerSpan() throws StoreException, InterruptedException { Span rootServerSpan = new Span(null, serverAnnotations()); rootServerSpan.setId("root"); rootServerSpan.setTraceId("root"); Span clientDescendant = new Span(null, clientAnnotations()); clientDescendant.setId("descendant"); clientDescendant.setParentId("root"); clientDescendant.setTraceId("root"); Span clientDescendant2 = new Span(null, clientAnnotations()); clientDescendant2.setId("descendant2"); clientDescendant2.setParentId("root"); clientDescendant2.setTraceId("root"); Span serverDescendantOfDescendant2 = new Span(null, serverAnnotations()); serverDescendantOfDescendant2.setId("descendant2"); serverDescendantOfDescendant2.setParentId("root"); serverDescendantOfDescendant2.setTraceId("root"); Span serverDescendantOfDescendant = new Span(null, serverAnnotations()); serverDescendantOfDescendant.setId("descendant"); serverDescendantOfDescendant.setParentId("root"); serverDescendantOfDescendant.setTraceId("root"); storeAndWait(null, Arrays.asList(rootServerSpan, clientDescendant, clientDescendant2, serverDescendantOfDescendant, serverDescendantOfDescendant2), SpanUniqueIdGenerator::toUnique); Trace trace = spanService.getTraceFragment(null, "root"); Assert.assertEquals("root", trace.getFragmentId()); Assert.assertEquals(1, trace.getNodes().size()); InteractionNode rootConsumerNode = ((InteractionNode) trace.getNodes().get(0)); Assert.assertThat(rootConsumerNode, instanceOf(Consumer.class)); Assert.assertEquals(1, rootConsumerNode.getCorrelationIds().size()); Assert.assertEquals("root", rootConsumerNode.getCorrelationIds().get(0).getValue()); Assert.assertEquals(2, rootConsumerNode.getNodes().size()); Assert.assertEquals(new HashSet<>(Arrays.asList("descendant", "descendant2")), extractCorrelationIds(rootConsumerNode.getNodes())); InteractionNode descendantProducerNode = ((InteractionNode) rootConsumerNode.getNodes().get(0)); Assert.assertThat(descendantProducerNode, instanceOf(Producer.class)); Assert.assertEquals(1, descendantProducerNode.getCorrelationIds().size()); Assert.assertEquals("descendant", descendantProducerNode.getCorrelationIds().get(0).getValue()); Assert.assertTrue(descendantProducerNode.getNodes().isEmpty()); InteractionNode descendantProducerNode2 = ((InteractionNode) rootConsumerNode.getNodes().get(1)); Assert.assertThat(descendantProducerNode2, instanceOf(Producer.class)); Assert.assertEquals(1, descendantProducerNode2.getCorrelationIds().size()); Assert.assertEquals("descendant2", descendantProducerNode2.getCorrelationIds().get(0).getValue()); Assert.assertTrue(descendantProducerNode2.getNodes().isEmpty()); }
@Test public void testGetTraceFragmentClientRoot() throws StoreException, InterruptedException { Span rootClientSpan = new Span(null, clientAnnotations()); rootClientSpan.setId("root"); rootClientSpan.setTraceId("root"); rootClientSpan.setName("rootClientSpan"); Span serverSpan = new Span(null, serverAnnotations()); serverSpan.setId("root"); serverSpan.setTraceId("root"); Span clientSpanDescendantRoot = new Span(null, clientAnnotations()); clientSpanDescendantRoot.setId("root2"); clientSpanDescendantRoot.setParentId("root"); clientSpanDescendantRoot.setTraceId("root"); Span serverSpanOfClientDescendantRoot = new Span(null, serverAnnotations()); serverSpanOfClientDescendantRoot.setId("root2"); serverSpanOfClientDescendantRoot.setParentId("root"); serverSpanOfClientDescendantRoot.setTraceId("root"); storeAndWait(null, Arrays.asList( rootClientSpan, serverSpan, clientSpanDescendantRoot, serverSpanOfClientDescendantRoot), SpanUniqueIdGenerator::toUnique); Trace trace = spanService.getTraceFragment(null, SpanUniqueIdGenerator.getClientId("root")); Assert.assertEquals("root", trace.getFragmentId()); Assert.assertEquals(1, trace.getNodes().size()); InteractionNode rootProducerNode = (InteractionNode) trace.getNodes().get(0); Assert.assertThat(rootProducerNode, instanceOf(Producer.class)); Assert.assertEquals(1, rootProducerNode.getCorrelationIds().size()); Assert.assertEquals("root", rootProducerNode.getCorrelationIds().get(0).getValue()); Assert.assertEquals(0, rootProducerNode.getNodes().size()); }
@Test public void testGetTraceFragmentComponent() throws StoreException, InterruptedException { Span rootServerSpan = new Span(null, serverAnnotations()); rootServerSpan.setId("root"); rootServerSpan.setTraceId("root"); Span componentSpan = new Span(); componentSpan.setId("component"); componentSpan.setParentId("root"); componentSpan.setTraceId("root"); componentSpan.setName(Constants.COMPONENT_EJB); Span clientComponentSpan = new Span(null, clientAnnotations()); clientComponentSpan.setId("clientComponent"); clientComponentSpan.setParentId("component"); clientComponentSpan.setTraceId("root"); Span clientComponentSpan2 = new Span(null, clientAnnotations()); clientComponentSpan2.setId("clientComponent2"); clientComponentSpan2.setParentId("component"); clientComponentSpan2.setTraceId("root"); Span descendant = new Span(null, clientAnnotations()); descendant.setId("descendant"); descendant.setParentId("root"); descendant.setTraceId("root"); Span descendantServer = new Span(null, serverAnnotations()); descendantServer.setId("descendant"); descendantServer.setParentId("root"); descendantServer.setTraceId("root"); storeAndWait(null, Arrays.asList( rootServerSpan, componentSpan, clientComponentSpan, clientComponentSpan2, descendant, descendantServer), SpanUniqueIdGenerator::toUnique); Trace trace = spanService.getTraceFragment(null, "root"); Assert.assertEquals("root", trace.getFragmentId()); Assert.assertEquals(1, trace.getNodes().size()); InteractionNode rootConsumerNode = (InteractionNode) trace.getNodes().get(0); Assert.assertThat(rootConsumerNode, instanceOf(Consumer.class)); Assert.assertEquals(1, rootConsumerNode.getCorrelationIds().size()); Assert.assertEquals("root", rootConsumerNode.getCorrelationIds().get(0).getValue()); Assert.assertEquals(2, rootConsumerNode.getNodes().size()); Assert.assertEquals(new HashSet<>(Arrays.asList("descendant")), extractCorrelationIds(rootConsumerNode.getNodes())); InteractionNode componentNode = (InteractionNode) rootConsumerNode.getNodes().get(0); Assert.assertThat(componentNode, instanceOf(Component.class)); Assert.assertEquals(0, componentNode.getCorrelationIds().size()); Assert.assertEquals(2, componentNode.getNodes().size()); Assert.assertEquals(new HashSet<>(Arrays.asList("clientComponent", "clientComponent2")), extractCorrelationIds(componentNode.getNodes())); InteractionNode producerNode = (InteractionNode) rootConsumerNode.getNodes().get(1); Assert.assertThat(producerNode, instanceOf(Producer.class)); Assert.assertEquals(1, producerNode.getCorrelationIds().size()); Assert.assertEquals("descendant", producerNode.getCorrelationIds().get(0).getValue()); Assert.assertEquals(0, producerNode.getNodes().size()); }