2727import io .opentelemetry .sdk .trace .SpanProcessor ;
2828import io .opentelemetry .sdk .trace .export .BatchSpanProcessor ;
2929import io .opentelemetry .sdk .trace .export .SpanExporter ;
30+ import java .util .ArrayList ;
31+ import java .util .Collection ;
3032import org .junit .jupiter .api .Test ;
3133
3234public class AwsUnsampledOnlySpanProcessorTest {
@@ -98,11 +100,8 @@ public void testExportsOnlyUnsampledSpans() {
98100 SpanExporter mockExporter = mock (SpanExporter .class );
99101 when (mockExporter .export (anyCollection ())).thenReturn (CompletableResultCode .ofSuccess ());
100102
101- AwsUnsampledOnlySpanProcessor processor =
102- AwsUnsampledOnlySpanProcessor .builder ().setSpanExporter (mockExporter ).build ();
103-
104- BatchSpanProcessor delegate =
105- BatchSpanProcessor .builder (mockExporter ).setExportUnsampledSpans (true ).build ();
103+ TestDelegateProcessor delegate = new TestDelegateProcessor ();
104+ AwsUnsampledOnlySpanProcessor processor = new AwsUnsampledOnlySpanProcessor (delegate );
106105
107106 // unsampled span
108107 SpanContext mockSpanContextUnsampled = mock (SpanContext .class );
@@ -116,14 +115,37 @@ public void testExportsOnlyUnsampledSpans() {
116115 ReadableSpan mockSpanSampled = mock (ReadableSpan .class );
117116 when (mockSpanSampled .getSpanContext ()).thenReturn (mockSpanContextSampled );
118117
119- // flush the unsampled span and verify export was called once
118+ processor . onEnd ( mockSpanSampled );
120119 processor .onEnd (mockSpanUnsampled );
121- processor .forceFlush ();
122- verify (mockExporter , times (1 )).export (anyCollection ());
123120
124- // flush the sampled span and verify export was not called again
125- processor .onEnd (mockSpanSampled );
126- processor .forceFlush ();
127- verify (mockExporter , times (1 )).export (anyCollection ());
121+ // validate that only the unsampled span was delegated
122+ assertThat (delegate .getEndedSpans ()).containsExactly (mockSpanUnsampled );
123+ }
124+
125+ private static class TestDelegateProcessor implements SpanProcessor {
126+ // keep a queue of Readable spans added when onEnd is called
127+ Collection <ReadableSpan > endedSpans = new ArrayList <>();
128+
129+ @ Override
130+ public void onStart (Context parentContext , ReadWriteSpan span ) {}
131+
132+ @ Override
133+ public boolean isStartRequired () {
134+ return false ;
135+ }
136+
137+ @ Override
138+ public void onEnd (ReadableSpan span ) {
139+ endedSpans .add (span );
140+ }
141+
142+ @ Override
143+ public boolean isEndRequired () {
144+ return false ;
145+ }
146+
147+ public Collection <ReadableSpan > getEndedSpans () {
148+ return endedSpans ;
149+ }
128150 }
129151}
0 commit comments