NodeDetailsDeriver extends AbstractProcessor<Span, NodeDetails> { @Override public NodeDetails processOneToOne(String tenantId, Span item) throws RetryAttemptException { NodeDetails nd = createTypedNodeDetails(item); URL url = item.url(); if (url != null) { nd.setUri(url.getPath()); } else if (item.serverSpan()) { Span clientSpan = spanCache.get(null, SpanUniqueIdGenerator.getClientId(item.getId())); if (clientSpan == null) { log.debugf("Server span does not contain URL, waiting for client span, span id=%s", item.getId()); throw new RetryAttemptException("URL is null, span id = " + item.getId()); } url = clientSpan.url(); if (url == null) { log.debugf("Unable to determine URL for server span id=%s", item.getId()); return null; } nd.setUri(url.getPath()); } if (item.getTimestamp() != null) { nd.setTimestamp(item.getTimestamp()); } if (item.getDuration() != null) { nd.setElapsed(item.getDuration()); nd.setActual(item.getDuration()); } nd.getProperties().addAll(item.binaryAnnotationMapping().getProperties()); nd.setHostAddress(item.ipv4()); if (item.service() != null) { nd.getProperties().add(new Property(Constants.PROP_SERVICE_NAME, item.service())); } nd.getProperties().add(new Property(Constants.PROP_FAULT, SpanDeriverUtil.deriveFault(item))); nd.setOperation(SpanDeriverUtil.deriveOperation(item)); nd.setInitial(item.topLevelSpan() || item.serverSpan()); log.debugf("NodeDetailsDeriver ret=%s", nd); return nd; } NodeDetailsDeriver(SpanCache spanCache); @Override NodeDetails processOneToOne(String tenantId, Span item);  }
@Test public void testServerSpanWithURL() throws RetryAttemptException { SpanCache spanCacheMock = Mockito.mock(SpanCache.class); NodeDetailsDeriver nodeDetailsDeriver = new NodeDetailsDeriver(spanCacheMock); Annotation sr = new Annotation(); sr.setValue("sr"); Annotation ss = new Annotation(); ss.setValue("ss"); BinaryAnnotation httpAddress = new BinaryAnnotation(); httpAddress.setKey(Constants.ZIPKIN_BIN_ANNOTATION_HTTP_URL); httpAddress.setValue("http: Span serverSpan = new Span(Arrays.asList(httpAddress), Arrays.asList(sr, ss)); serverSpan.setId("2"); serverSpan.setTraceId("1"); NodeDetails result = nodeDetailsDeriver.processOneToOne(null, serverSpan); assertEquals("/hello", result.getUri()); }
@Test(expected = RetryAttemptException.class) public void testServerSpanNoURLClientSpanUnavailable() throws RetryAttemptException { SpanCache spanCacheMock = Mockito.mock(SpanCache.class); NodeDetailsDeriver nodeDetailsDeriver = new NodeDetailsDeriver(spanCacheMock); Annotation sr = new Annotation(); sr.setValue("sr"); Annotation ss = new Annotation(); ss.setValue("ss"); Span serverSpan = new Span(Collections.emptyList(), Arrays.asList(sr, ss)); serverSpan.setId("2"); serverSpan.setTraceId("1"); Mockito.when(spanCacheMock.get(null, SpanUniqueIdGenerator.getClientId(serverSpan.getId()))) .thenReturn(null); nodeDetailsDeriver.processOneToOne(null, serverSpan); }
@Test public void testServerSpanNoURLClientSpanNoURL() throws RetryAttemptException { SpanCache spanCacheMock = Mockito.mock(SpanCache.class); NodeDetailsDeriver nodeDetailsDeriver = new NodeDetailsDeriver(spanCacheMock); Annotation cs = new Annotation(); cs.setValue("cs"); Annotation cr = new Annotation(); cr.setValue("cr"); Span clientSpan = new Span(Collections.emptyList(), Arrays.asList(cs, cr)); clientSpan.setId("2"); clientSpan.setTraceId("1"); Annotation sr = new Annotation(); sr.setValue("sr"); Annotation ss = new Annotation(); ss.setValue("ss"); Span serverSpan = new Span(Collections.emptyList(), Arrays.asList(sr, ss)); serverSpan.setId("2"); serverSpan.setTraceId("1"); Mockito.when(spanCacheMock.get(null, SpanUniqueIdGenerator.getClientId(serverSpan.getId()))) .thenReturn(clientSpan); assertNull(nodeDetailsDeriver.processOneToOne(null, serverSpan)); }
@Test public void testServerSpanNoURLClientSpanWithURL() throws RetryAttemptException { SpanCache spanCacheMock = Mockito.mock(SpanCache.class); NodeDetailsDeriver nodeDetailsDeriver = new NodeDetailsDeriver(spanCacheMock); Annotation cs = new Annotation(); cs.setValue("cs"); Annotation cr = new Annotation(); cr.setValue("cr"); BinaryAnnotation httpAddress = new BinaryAnnotation(); httpAddress.setKey(Constants.ZIPKIN_BIN_ANNOTATION_HTTP_URL); httpAddress.setValue("http: Span clientSpan = new Span(Collections.singletonList(httpAddress), Arrays.asList(cs, cr)); clientSpan.setId("2"); clientSpan.setTraceId("1"); Annotation sr = new Annotation(); sr.setValue("sr"); Annotation ss = new Annotation(); ss.setValue("ss"); Span serverSpan = new Span(Collections.emptyList(), Arrays.asList(sr, ss)); serverSpan.setId("2"); serverSpan.setTraceId("1"); Mockito.when(spanCacheMock.get(null, SpanUniqueIdGenerator.getClientId(serverSpan.getId()))) .thenReturn(clientSpan); NodeDetails result = nodeDetailsDeriver.processOneToOne(null, serverSpan); assertNotNull(result); assertEquals("/hello", result.getUri()); }