|
| 1 | +package software.amazon.opentelemetry.javaagent.providers; |
| 2 | + |
| 3 | +import io.opentelemetry.api.trace.SpanContext; |
| 4 | +import io.opentelemetry.context.Context; |
| 5 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 6 | +import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; |
| 7 | +import io.opentelemetry.sdk.trace.ReadWriteSpan; |
| 8 | +import io.opentelemetry.sdk.trace.ReadableSpan; |
| 9 | +import io.opentelemetry.sdk.trace.SpanProcessor; |
| 10 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 11 | +import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; |
| 12 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.mockito.ArgumentCaptor; |
| 15 | + |
| 16 | +import java.util.Collection; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +public class AwsUnsampledOnlySpanProcessorTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testDefaultSpanProcessor() { |
| 25 | + AwsUnsampledOnlySpanProcessorBuilder builder = AwsUnsampledOnlySpanProcessor.builder(); |
| 26 | + AwsUnsampledOnlySpanProcessor unsampledSP = builder.build(); |
| 27 | + |
| 28 | + assertThat(builder.getSpanExporter()).isInstanceOf(OtlpUdpSpanExporter.class); |
| 29 | + SpanProcessor delegate = unsampledSP.getDelegate(); |
| 30 | + assertThat(delegate).isInstanceOf(BatchSpanProcessor.class); |
| 31 | + BatchSpanProcessor delegateBsp = (BatchSpanProcessor) delegate; |
| 32 | + String delegateBspString = delegateBsp.toString(); |
| 33 | + assertThat(delegateBspString).contains("spanExporter=software.amazon.opentelemetry.javaagent.providers.OtlpUdpSpanExporter"); |
| 34 | + assertThat(delegateBspString).contains("exportUnsampledSpans=true"); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testSpanProcessorWithExporter() { |
| 39 | + AwsUnsampledOnlySpanProcessorBuilder builder = AwsUnsampledOnlySpanProcessor |
| 40 | + .builder() |
| 41 | + .setSpanExporter(InMemorySpanExporter.create()); |
| 42 | + AwsUnsampledOnlySpanProcessor unsampledSP = builder.build(); |
| 43 | + |
| 44 | + assertThat(builder.getSpanExporter()).isInstanceOf(InMemorySpanExporter.class); |
| 45 | + SpanProcessor delegate = unsampledSP.getDelegate(); |
| 46 | + assertThat(delegate).isInstanceOf(BatchSpanProcessor.class); |
| 47 | + BatchSpanProcessor delegateBsp = (BatchSpanProcessor) delegate; |
| 48 | + String delegateBspString = delegateBsp.toString(); |
| 49 | + assertThat(delegateBspString).contains("spanExporter=io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter"); |
| 50 | + assertThat(delegateBspString).contains("exportUnsampledSpans=true"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testStartAddsAttributeToSampledSpan() { |
| 55 | + SpanContext mockSpanContext = mock(SpanContext.class); |
| 56 | + when(mockSpanContext.isSampled()).thenReturn(true); |
| 57 | + Context parentContextMock = mock(Context.class); |
| 58 | + ReadWriteSpan spanMock = mock(ReadWriteSpan.class); |
| 59 | + when(spanMock.getSpanContext()).thenReturn(mockSpanContext); |
| 60 | + |
| 61 | + AwsUnsampledOnlySpanProcessor processor = AwsUnsampledOnlySpanProcessor.builder().build(); |
| 62 | + processor.onStart(parentContextMock, spanMock); |
| 63 | + |
| 64 | + //verify setAttribute was called with the correct arguments |
| 65 | + verify(spanMock, times(1)).setAttribute(AwsAttributeKeys.AWS_TRACE_FLAG_SAMPLED, true); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testStartAddsAttributeToUnsampledSpan() { |
| 70 | + SpanContext mockSpanContext = mock(SpanContext.class); |
| 71 | + when(mockSpanContext.isSampled()).thenReturn(false); |
| 72 | + Context parentContextMock = mock(Context.class); |
| 73 | + ReadWriteSpan spanMock = mock(ReadWriteSpan.class); |
| 74 | + when(spanMock.getSpanContext()).thenReturn(mockSpanContext); |
| 75 | + |
| 76 | + AwsUnsampledOnlySpanProcessor processor = AwsUnsampledOnlySpanProcessor.builder().build(); |
| 77 | + processor.onStart(parentContextMock, spanMock); |
| 78 | + |
| 79 | + //verify setAttribute was called with the correct arguments |
| 80 | + verify(spanMock, times(1)).setAttribute(AwsAttributeKeys.AWS_TRACE_FLAG_SAMPLED, false); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testExportsOnlyUnsampledSpans() { |
| 85 | + SpanExporter mockExporter = mock(SpanExporter.class); |
| 86 | + when(mockExporter.export(anyCollection())).thenReturn(CompletableResultCode.ofSuccess()); |
| 87 | + |
| 88 | + BatchSpanProcessor delegate = BatchSpanProcessor.builder(mockExporter) |
| 89 | + .setExportUnsampledSpans(true) |
| 90 | + .setMaxExportBatchSize(1) |
| 91 | + .setMaxQueueSize(1) |
| 92 | + .build(); |
| 93 | + |
| 94 | + AwsUnsampledOnlySpanProcessor processor = new AwsUnsampledOnlySpanProcessor(delegate); |
| 95 | + |
| 96 | + // unsampled span |
| 97 | + SpanContext mockSpanContextUnsampled = mock(SpanContext.class); |
| 98 | + when(mockSpanContextUnsampled.isSampled()).thenReturn(false); |
| 99 | + ReadableSpan mockSpanUnsampled = mock(ReadableSpan.class); |
| 100 | + when(mockSpanUnsampled.getSpanContext()).thenReturn(mockSpanContextUnsampled); |
| 101 | + |
| 102 | + // sampled span |
| 103 | + SpanContext mockSpanContextSampled = mock(SpanContext.class); |
| 104 | + when(mockSpanContextSampled.isSampled()).thenReturn(true); |
| 105 | + ReadableSpan mockSpanSampled = mock(ReadableSpan.class); |
| 106 | + when(mockSpanSampled.getSpanContext()).thenReturn(mockSpanContextSampled); |
| 107 | + |
| 108 | + // flush the unsampled span and verify export was called once |
| 109 | + processor.onEnd(mockSpanUnsampled); |
| 110 | + processor.forceFlush(); |
| 111 | + verify(mockExporter, times(1)).export(anyCollection()); |
| 112 | + |
| 113 | + // flush the sampled span and verify export was not called again |
| 114 | + processor.onEnd(mockSpanSampled); |
| 115 | + processor.forceFlush(); |
| 116 | + verify(mockExporter, times(1)).export(anyCollection()); |
| 117 | + } |
| 118 | +} |
0 commit comments