APMSpan extends AbstractSpan implements PropagableState { public static Reference findPrimaryReference(List<Reference> references) { List<Reference> followsFrom = references.stream() .filter(ref -> References.FOLLOWS_FROM.equals(ref.getReferenceType()) && ref.getReferredTo() instanceof APMSpan) .collect(Collectors.toList()); List<Reference> childOfSpan = references.stream() .filter(ref -> References.CHILD_OF.equals(ref.getReferenceType()) && ref.getReferredTo() instanceof APMSpan) .collect(Collectors.toList()); List<Reference> extractedTraceState = references.stream() .filter(ref -> ref.getReferredTo() instanceof APMSpanBuilder) .collect(Collectors.toList()); if (!extractedTraceState.isEmpty()) { if (extractedTraceState.size() == 1) { return extractedTraceState.get(0); } return null; } if (!childOfSpan.isEmpty()) { if (childOfSpan.size() == 1) { return childOfSpan.get(0); } return null; } if (followsFrom.size() == 1) { return followsFrom.get(0); } return null; } APMSpan(APMSpanBuilder builder, TraceRecorder recorder, ContextSampler sampler); static Reference findPrimaryReference(List<Reference> references); @Override void finish(); @Override void finish(long finishMicros); @Override Map<String, Object> state();  }
@Test public void testFindPrimaryReferenceNoRefs() { assertNull(APMSpan.findPrimaryReference(Collections.emptyList())); }
@Test public void testFindPrimaryReferenceSingleChildOfSpan() { Tracer tracer = new APMTracer(); Span span = tracer.buildSpan("test").start(); Reference ref = new Reference(References.CHILD_OF, span.context()); assertEquals(ref, APMSpan.findPrimaryReference(Arrays.asList(ref))); }
@Test public void testFindPrimaryReferenceMultipleChildOfSpan() { Tracer tracer = new APMTracer(); Span span1 = tracer.buildSpan("test1").start(); Reference ref1 = new Reference(References.CHILD_OF, span1.context()); Span span2 = tracer.buildSpan("test2").start(); Reference ref2 = new Reference(References.CHILD_OF, span2.context()); assertNull(APMSpan.findPrimaryReference(Arrays.asList(ref1, ref2))); }
@Test public void testFindPrimaryReferenceSingleChildOfSpanContext() { Tracer tracer = new APMTracer(); SpanContext spanCtx = extractSpanContext(tracer, TEST_APM_ID1); Reference ref = new Reference(References.CHILD_OF, spanCtx); assertEquals(ref, APMSpan.findPrimaryReference(Arrays.asList(ref))); }
@Test public void testFindPrimaryReferenceMultipleChildOfSpanContext() { Tracer tracer = new APMTracer(); SpanContext spanCtx1 = extractSpanContext(tracer, TEST_APM_ID1); SpanContext spanCtx2 = extractSpanContext(tracer, TEST_APM_ID2); Reference ref1 = new Reference(References.CHILD_OF, spanCtx1); Reference ref2 = new Reference(References.CHILD_OF, spanCtx2); assertNull(APMSpan.findPrimaryReference(Arrays.asList(ref1, ref2))); }
@Test public void testFindPrimaryReferenceSingleFollowsFrom() { Tracer tracer = new APMTracer(); Span span = tracer.buildSpan("test").start(); Reference ref = new Reference(References.FOLLOWS_FROM, span.context()); assertEquals(ref, APMSpan.findPrimaryReference(Arrays.asList(ref))); }
@Test public void testFindPrimaryReferenceMultipleFollowsFrom() { Tracer tracer = new APMTracer(); Span span1 = tracer.buildSpan("test1").start(); Reference ref1 = new Reference(References.FOLLOWS_FROM, span1.context()); Span span2 = tracer.buildSpan("test2").start(); Reference ref2 = new Reference(References.FOLLOWS_FROM, span2.context()); assertNull(APMSpan.findPrimaryReference(Arrays.asList(ref1, ref2))); }
@Test public void testFindPrimaryReferenceSingleChildOfSpanContextWithOtherRefs() { Tracer tracer = new APMTracer(); SpanContext spanCtx1 = extractSpanContext(tracer, TEST_APM_ID1); Reference ref1 = new Reference(References.CHILD_OF, spanCtx1); Span span2 = tracer.buildSpan("test2").start(); Reference ref2 = new Reference(References.FOLLOWS_FROM, span2.context()); Span span3 = tracer.buildSpan("test3").start(); Reference ref3 = new Reference(References.CHILD_OF, span3.context()); Span span4 = tracer.buildSpan("test4").start(); Reference ref4 = new Reference(References.CHILD_OF, span4.context()); assertEquals(ref1, APMSpan.findPrimaryReference(Arrays.asList(ref1, ref2, ref3, ref4))); }