SpanServiceElasticsearch implements SpanService { @Override public List<Span> getChildren(String tenantId, String id) { List<Span> spans = new ArrayList<>(); final String index = client.getIndex(tenantId); try { RefreshRequestBuilder refreshRequestBuilder = client.getClient() .admin() .indices() .prepareRefresh(index); client.getClient() .admin() .indices() .refresh(refreshRequestBuilder.request()) .actionGet(); QueryBuilder query = QueryBuilders.termQuery("parentId", id); SearchRequestBuilder request = client.getClient() .prepareSearch(index) .setTypes(SPAN_TYPE) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(query); SearchResponse response = request.execute() .actionGet(); for (SearchHit searchHitFields : response.getHits()) { try { Span span = deserialize(searchHitFields.getSourceAsString(), Span.class); if (!span.serverSpan()) { spans.add(span); } } catch (IOException ex) { log.errorFailedToParse(ex); } } } catch (IndexMissingException ex) { log.errorf("No index[%s] found, so unable to retrieve spans", index); } log.tracef("Get children with parentId[%s] is: %s", id, spans); return spans; }  @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 testGetChildren() throws StoreException, InterruptedException { Span parentSpan = new Span(); parentSpan.setId("parent"); parentSpan.setName("foo parent"); storeAndWait(null, Collections.singletonList(parentSpan)); Annotation annotation = new Annotation(); annotation.setValue("foo bar"); annotation.setTimestamp(123456789); 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("id1"); span.setTraceId("traceId"); span.setParentId("parent"); span.setName("foo"); span.setDebug(true); span.setTimestamp(1234456L); span.setDuration(55468L); storeAndWait(null, Collections.singletonList(span)); span = new Span(); span.setId("id2"); span.setParentId("parent"); storeAndWait(null, Collections.singletonList(span)); List<Span> children = spanService.getChildren(null, "parent"); Assert.assertEquals(2, children.size()); }
@Test public void testGetChildrenFromEmptyDB() throws StoreException, InterruptedException { List<Span> spans = spanService.getChildren(null, "id"); Assert.assertTrue(spans.isEmpty()); }
@Test public void testGetChildrenTenantIndexExists() throws StoreException, InterruptedException { storeAndWait(null, Collections.singletonList(new Span())); List<Span> spans = spanService.getChildren(null, "id"); Assert.assertTrue(spans.isEmpty()); }