SpanServiceElasticsearch implements SpanService { @Override public Trace getTrace(String tenantId, String id) { Span span = getSpan(tenantId, id); if (span == null || span.serverSpan()) { String clientId = SpanUniqueIdGenerator.getClientId(id); if (getSpan(tenantId, clientId) != null) { id = clientId; } } Trace trace = getTraceFragment(tenantId, id); if (trace != null) { processConnectedFragment(tenantId, trace); } 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 testGetEndToEndTrace() 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.getTrace(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.assertEquals(1, descendantProducerNode.getNodes().size()); InteractionNode descendantConsumerNode = ((InteractionNode) descendantProducerNode.getNodes().get(0)); Assert.assertThat(descendantConsumerNode, instanceOf(Consumer.class)); Assert.assertEquals(1, descendantConsumerNode.getCorrelationIds().size()); Assert.assertEquals("descendant", descendantConsumerNode.getCorrelationIds().get(0).getValue()); Assert.assertEquals(0, descendantConsumerNode.getNodes().size()); 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.assertEquals(1, descendantProducerNode2.getNodes().size()); InteractionNode descendant2ConsumerNode = ((InteractionNode) descendantProducerNode2.getNodes().get(0)); Assert.assertThat(descendant2ConsumerNode, instanceOf(Consumer.class)); Assert.assertEquals(1, descendant2ConsumerNode.getCorrelationIds().size()); Assert.assertEquals("descendant2", descendant2ConsumerNode.getCorrelationIds().get(0).getValue()); Assert.assertEquals(0, descendant2ConsumerNode.getNodes().size()); }
@Test public void testGetEndToEndTraceClientRoot() throws StoreException, InterruptedException { Span rootClientSpan = new Span(null, clientAnnotations()); rootClientSpan.setId("root"); rootClientSpan.setTraceId("root"); 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(rootClientSpan, rootServerSpan, clientDescendant, clientDescendant2, serverDescendantOfDescendant, serverDescendantOfDescendant2), SpanUniqueIdGenerator::toUnique); Trace trace = spanService.getTrace(null, "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(1, rootProducerNode.getNodes().size()); Assert.assertEquals(new HashSet<>(Arrays.asList("root")), extractCorrelationIds(rootProducerNode.getNodes())); InteractionNode rootConsumerNode = ((InteractionNode) rootProducerNode.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.assertEquals(1, descendantProducerNode.getNodes().size()); InteractionNode descendantConsumerNode = ((InteractionNode) descendantProducerNode.getNodes().get(0)); Assert.assertThat(descendantConsumerNode, instanceOf(Consumer.class)); Assert.assertEquals(1, descendantConsumerNode.getCorrelationIds().size()); Assert.assertEquals("descendant", descendantConsumerNode.getCorrelationIds().get(0).getValue()); Assert.assertEquals(0, descendantConsumerNode.getNodes().size()); 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.assertEquals(1, descendantProducerNode2.getNodes().size()); InteractionNode descendant2ConsumerNode = ((InteractionNode) descendantProducerNode2.getNodes().get(0)); Assert.assertThat(descendant2ConsumerNode, instanceOf(Consumer.class)); Assert.assertEquals(1, descendant2ConsumerNode.getCorrelationIds().size()); Assert.assertEquals("descendant2", descendant2ConsumerNode.getCorrelationIds().get(0).getValue()); Assert.assertEquals(0, descendant2ConsumerNode.getNodes().size()); }
@Test public void testGetEndToEndTraceClientOnly() throws StoreException, InterruptedException { Span rootClientSpan = new Span(null, clientAnnotations()); rootClientSpan.setId("root"); rootClientSpan.setTraceId("root"); storeAndWait(null, Collections.singletonList(rootClientSpan), SpanUniqueIdGenerator::toUnique); Trace trace = spanService.getTrace(null, "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()); }