SpanServiceElasticsearch implements SpanService { @Override public Span getSpan(String tenantId, String id) { GetResponse response = null; try { response = client.getClient() .prepareGet(client.getIndex(tenantId), SPAN_TYPE, id) .setRouting(id) .execute() .actionGet(); } catch (IndexMissingException ex) { log.errorf("Missing span index %s", tenantId); return null; } Span span = null; if (!response.isSourceEmpty()) { try { span = deserialize(response.getSourceAsString(), Span.class); if (span.serverSpan() && span.url() == null) { Span clientSpan = getSpan(tenantId, SpanUniqueIdGenerator.getClientId(span.getId())); if (clientSpan != null && clientSpan.url() != null) { BinaryAnnotation httpURLAnnotation = new BinaryAnnotation(); httpURLAnnotation.setKey(Constants.ZIPKIN_BIN_ANNOTATION_HTTP_URL); httpURLAnnotation.setValue(clientSpan.url().toString()); List<BinaryAnnotation> binaryAnnotationsWithURL = new ArrayList<>(span.getBinaryAnnotations()); binaryAnnotationsWithURL.add(httpURLAnnotation); span = new Span(span, binaryAnnotationsWithURL, span.getAnnotations()); } } } catch (IOException ex) { log.errorFailedToParse(ex); } } log.tracef("Get span with id[%s] is: %s", id, span); return span; }  @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 testStoreOneAndGetOne() throws StoreException, InterruptedException { Annotation annotation = new Annotation(); annotation.setValue("foo bar"); annotation.setTimestamp(123456789L); annotation.setEndpoint(createEndpoint("123.123.123.1233", (short)123, "foo service")); BinaryAnnotation binaryAnnotation = new BinaryAnnotation(); binaryAnnotation.setEndpoint(createEndpoint("123.123.123.1233", (short)123, "foo service")); binaryAnnotation.setValue("foo"); binaryAnnotation.setKey("foo key"); binaryAnnotation.setType(AnnotationType.I64); Span span = new Span(Arrays.asList(binaryAnnotation), Arrays.asList(annotation)); span.setId("id"); span.setTraceId("traceId"); span.setParentId("parentId"); span.setName("foo"); span.setDebug(true); span.setTimestamp(1234456L); span.setDuration(55468L); storeAndWait(null, Collections.singletonList(span)); Span spanFromDb = spanService.getSpan(null, span.getId()); Assert.assertEquals(span, spanFromDb); }
@Test public void testStoreWithIdSupplier() throws StoreException, InterruptedException { final String clientIdsuffix = "-client"; Span clientSpan = new Span(null, clientAnnotations()); clientSpan.setId("foo"); clientSpan.setDuration(111L); storeAndWait(null, Collections.singletonList(clientSpan), x -> x.getId() + clientIdsuffix); Span span = new Span(); span.setId("foo"); span.setDuration(444L); storeAndWait(null, Collections.singletonList(span), x -> x.getId()); Span spanFromDb = spanService.getSpan(null, "foo"); Assert.assertEquals(span, spanFromDb); spanFromDb = spanService.getSpan(null, "foo" + clientIdsuffix); Assert.assertEquals(clientSpan, spanFromDb); }
@Test public void testGetSpanFromEmptyDB() throws StoreException, InterruptedException { Span spanFromDB = spanService.getSpan(null, "id"); Assert.assertNull(spanFromDB); }