SourceInfoUtil { public static List<SourceInfo> getSourceInfo(String tenantId, List<Trace> items) throws RetryAttemptException { List<SourceInfo> sourceInfoList = new ArrayList<SourceInfo>(); int curpos=0; for (int i = 0; i < items.size(); i++) { Trace trace = items.get(i); StringBuffer nodeId = new StringBuffer(trace.getFragmentId()); for (int j = 0; j < trace.getNodes().size(); j++) { Node node = trace.getNodes().get(j); int len = nodeId.length(); initialiseSourceInfo(sourceInfoList, tenantId, trace, nodeId, j, node); nodeId.delete(len, nodeId.length()); } EndpointRef ep = EndpointUtil.getSourceEndpoint(trace); for (int j=curpos; j < sourceInfoList.size(); j++) { SourceInfo si = sourceInfoList.get(j); si.setEndpoint(ep); } curpos = sourceInfoList.size(); } return sourceInfoList; } private  SourceInfoUtil(); static List<SourceInfo> getSourceInfo(String tenantId, List<Trace> items); static SourceInfo getSourceInfo(String tenantId, Span serverSpan, SpanCache spanCache);  }
@Test public void testInitialiseIds() throws RetryAttemptException { Trace trace = new Trace(); trace.setTraceId("trace1"); trace.setFragmentId(trace.getTraceId()); Consumer consumer1 = new Consumer(); consumer1.setUri("uri1"); consumer1.setOperation("op1"); consumer1.addCausedByCorrelationId("cb1"); consumer1.addInteractionCorrelationId("in1"); trace.getNodes().add(consumer1); Component component1 = new Component(); consumer1.getNodes().add(component1); Producer producer1 = new Producer(); producer1.addControlFlowCorrelationId("cf1"); component1.getNodes().add(producer1); Producer producer2 = new Producer(); producer2.addInteractionCorrelationId("in2"); component1.getNodes().add(producer2); List<SourceInfo> sourceInfoList = SourceInfoUtil.getSourceInfo(null, Collections.singletonList(trace)); assertNotNull(sourceInfoList); assertEquals(6, sourceInfoList.size()); SourceInfo si1 = sourceInfoList.get(0); SourceInfo si2 = sourceInfoList.get(1); SourceInfo si3 = sourceInfoList.get(2); SourceInfo si4 = sourceInfoList.get(3); SourceInfo si5 = sourceInfoList.get(4); SourceInfo si6 = sourceInfoList.get(5); assertEquals("trace1:0", si1.getId()); assertEquals("trace1:0:0", si2.getId()); assertEquals("trace1:0:0:0", si3.getId()); assertEquals("cf1", si4.getId()); assertEquals("trace1:0:0:1", si5.getId()); assertEquals("in2", si6.getId()); }
@Test public void testInitialiseFragmentOrigins() throws RetryAttemptException { Trace trace1 = new Trace(); trace1.setTraceId("1"); trace1.setFragmentId(trace1.getTraceId()); Producer producer1 = new Producer(); producer1.setUri("uri1"); producer1.setOperation("op1"); producer1.addInteractionCorrelationId("in1"); trace1.getNodes().add(producer1); Trace trace2 = new Trace(); trace2.setTraceId(trace1.getTraceId()); trace2.setFragmentId("2"); Consumer consumer1 = new Consumer(); consumer1.setUri("uri1"); consumer1.setOperation("op1"); consumer1.addInteractionCorrelationId("in1"); trace2.getNodes().add(consumer1); Component component2 = new Component(); consumer1.getNodes().add(component2); List<SourceInfo> sourceInfoList = SourceInfoUtil.getSourceInfo(null, Arrays.asList(trace1, trace2)); assertNotNull(sourceInfoList); assertEquals(4, sourceInfoList.size()); SourceInfo si1 = sourceInfoList.get(0); SourceInfo si2 = sourceInfoList.get(1); SourceInfo si3 = sourceInfoList.get(2); SourceInfo si4 = sourceInfoList.get(3); assertEquals(EndpointUtil.encodeClientURI("uri1"), si1.getEndpoint().getUri()); assertEquals("op1", si1.getEndpoint().getOperation()); assertEquals(EndpointUtil.encodeClientURI("uri1"), si2.getEndpoint().getUri()); assertEquals("op1", si2.getEndpoint().getOperation()); assertEquals("uri1", si3.getEndpoint().getUri()); assertEquals("op1", si3.getEndpoint().getOperation()); assertEquals("uri1", si4.getEndpoint().getUri()); assertEquals("op1", si4.getEndpoint().getOperation()); }
@Test public void testGetSourceInfoNoClient() throws CacheException { SpanCache spanCache = new TestSpanCache(); Span serverSpan = new Span(null, null); serverSpan.setId("1"); serverSpan.setParentId("1"); spanCache.store(null, Arrays.asList(serverSpan), SpanUniqueIdGenerator::toUnique); assertNull(SourceInfoUtil.getSourceInfo(null, serverSpan, spanCache)); }
@Test public void testGetSourceInfoJustClient() throws CacheException { SpanCache spanCache = new TestSpanCache(); Span serverSpan = new Span(null, serverAnnotations()); serverSpan.setId("1"); serverSpan.setParentId("1"); BinaryAnnotation ba = new BinaryAnnotation(); ba.setKey(Constants.ZIPKIN_BIN_ANNOTATION_HTTP_URL); ba.setValue("http: Span clientSpan = new Span(Arrays.asList(ba), clientAnnotations()); clientSpan.setId("1"); clientSpan.setParentId("1"); spanCache.store(null, Arrays.asList(serverSpan, clientSpan), SpanUniqueIdGenerator::toUnique); SourceInfo si = SourceInfoUtil.getSourceInfo(null, serverSpan, spanCache); assertNotNull(si); assertEquals(EndpointUtil.encodeClientURI("/myuri"), si.getEndpoint().getUri()); }
@Test public void testGetSourceInfoPartialClient() throws CacheException { SpanCache spanCache = new TestSpanCache(); Span serverSpan = new Span(null, serverAnnotations()); serverSpan.setId("2"); serverSpan.setParentId("1"); BinaryAnnotation ba = new BinaryAnnotation(); ba.setKey(Constants.ZIPKIN_BIN_ANNOTATION_HTTP_URL); ba.setValue("http: Span clientSpan = new Span(Arrays.asList(ba), clientAnnotations()); clientSpan.setId("2"); clientSpan.setParentId("1"); Span parentSpan = new Span(); parentSpan.setName("Parent"); parentSpan.setId("1"); parentSpan.setParentId("1"); spanCache.store(null, Arrays.asList(serverSpan, clientSpan, parentSpan), SpanUniqueIdGenerator::toUnique); SourceInfo si = SourceInfoUtil.getSourceInfo(null, serverSpan, spanCache); assertNotNull(si); assertEquals(new EndpointRef(null, parentSpan.getName(), true), si.getEndpoint()); }
@Test public void testGetSourceInfoCompleteClient() throws CacheException { SpanCache spanCache = new TestSpanCache(); Span serverSpan = new Span(null, serverAnnotations()); serverSpan.setId("3"); serverSpan.setParentId("2"); BinaryAnnotation ba = new BinaryAnnotation(); ba.setKey(Constants.ZIPKIN_BIN_ANNOTATION_HTTP_URL); ba.setValue("http: Span clientSpan = new Span(Arrays.asList(ba), clientAnnotations()); clientSpan.setId("3"); clientSpan.setParentId("2"); Span midSpan = new Span(); midSpan.setId("2"); midSpan.setParentId("1"); BinaryAnnotation ba2 = new BinaryAnnotation(); ba2.setKey(Constants.ZIPKIN_BIN_ANNOTATION_HTTP_URL); ba2.setValue("http: Span topServerSpan = new Span(Arrays.asList(ba2), serverAnnotations()); topServerSpan.setId("1"); topServerSpan.setParentId("1"); spanCache.store(null, Arrays.asList(serverSpan, clientSpan, midSpan, topServerSpan), SpanUniqueIdGenerator::toUnique); SourceInfo si = SourceInfoUtil.getSourceInfo(null, serverSpan, spanCache); assertNotNull(si); assertEquals("/originaluri", si.getEndpoint().getUri()); }