|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
1 | 16 | package software.amazon.opentelemetry.javaagent.providers; |
2 | 17 |
|
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
3 | 21 | import io.opentelemetry.api.trace.SpanContext; |
4 | 22 | import io.opentelemetry.context.Context; |
5 | 23 | import io.opentelemetry.sdk.common.CompletableResultCode; |
6 | 24 | import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; |
7 | 25 | import io.opentelemetry.sdk.trace.ReadWriteSpan; |
8 | 26 | import io.opentelemetry.sdk.trace.ReadableSpan; |
9 | 27 | import io.opentelemetry.sdk.trace.SpanProcessor; |
10 | | -import io.opentelemetry.sdk.trace.data.SpanData; |
11 | 28 | import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; |
12 | 29 | import io.opentelemetry.sdk.trace.export.SpanExporter; |
13 | 30 | 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 | 31 |
|
21 | 32 | public class AwsUnsampledOnlySpanProcessorTest { |
22 | 33 |
|
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 | | - } |
| 34 | + @Test |
| 35 | + public void testDefaultSpanProcessor() { |
| 36 | + AwsUnsampledOnlySpanProcessorBuilder builder = AwsUnsampledOnlySpanProcessor.builder(); |
| 37 | + AwsUnsampledOnlySpanProcessor unsampledSP = builder.build(); |
| 38 | + |
| 39 | + assertThat(builder.getSpanExporter()).isInstanceOf(OtlpUdpSpanExporter.class); |
| 40 | + SpanProcessor delegate = unsampledSP.getDelegate(); |
| 41 | + assertThat(delegate).isInstanceOf(BatchSpanProcessor.class); |
| 42 | + BatchSpanProcessor delegateBsp = (BatchSpanProcessor) delegate; |
| 43 | + String delegateBspString = delegateBsp.toString(); |
| 44 | + assertThat(delegateBspString) |
| 45 | + .contains( |
| 46 | + "spanExporter=software.amazon.opentelemetry.javaagent.providers.OtlpUdpSpanExporter"); |
| 47 | + assertThat(delegateBspString).contains("exportUnsampledSpans=true"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testSpanProcessorWithExporter() { |
| 52 | + AwsUnsampledOnlySpanProcessorBuilder builder = |
| 53 | + AwsUnsampledOnlySpanProcessor.builder().setSpanExporter(InMemorySpanExporter.create()); |
| 54 | + AwsUnsampledOnlySpanProcessor unsampledSP = builder.build(); |
| 55 | + |
| 56 | + assertThat(builder.getSpanExporter()).isInstanceOf(InMemorySpanExporter.class); |
| 57 | + SpanProcessor delegate = unsampledSP.getDelegate(); |
| 58 | + assertThat(delegate).isInstanceOf(BatchSpanProcessor.class); |
| 59 | + BatchSpanProcessor delegateBsp = (BatchSpanProcessor) delegate; |
| 60 | + String delegateBspString = delegateBsp.toString(); |
| 61 | + assertThat(delegateBspString) |
| 62 | + .contains("spanExporter=io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter"); |
| 63 | + assertThat(delegateBspString).contains("exportUnsampledSpans=true"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testStartAddsAttributeToSampledSpan() { |
| 68 | + SpanContext mockSpanContext = mock(SpanContext.class); |
| 69 | + when(mockSpanContext.isSampled()).thenReturn(true); |
| 70 | + Context parentContextMock = mock(Context.class); |
| 71 | + ReadWriteSpan spanMock = mock(ReadWriteSpan.class); |
| 72 | + when(spanMock.getSpanContext()).thenReturn(mockSpanContext); |
| 73 | + |
| 74 | + AwsUnsampledOnlySpanProcessor processor = AwsUnsampledOnlySpanProcessor.builder().build(); |
| 75 | + processor.onStart(parentContextMock, spanMock); |
| 76 | + |
| 77 | + // verify setAttribute was called with the correct arguments |
| 78 | + verify(spanMock, times(1)).setAttribute(AwsAttributeKeys.AWS_TRACE_FLAG_SAMPLED, true); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testStartAddsAttributeToUnsampledSpan() { |
| 83 | + SpanContext mockSpanContext = mock(SpanContext.class); |
| 84 | + when(mockSpanContext.isSampled()).thenReturn(false); |
| 85 | + Context parentContextMock = mock(Context.class); |
| 86 | + ReadWriteSpan spanMock = mock(ReadWriteSpan.class); |
| 87 | + when(spanMock.getSpanContext()).thenReturn(mockSpanContext); |
| 88 | + |
| 89 | + AwsUnsampledOnlySpanProcessor processor = AwsUnsampledOnlySpanProcessor.builder().build(); |
| 90 | + processor.onStart(parentContextMock, spanMock); |
| 91 | + |
| 92 | + // verify setAttribute was called with the correct arguments |
| 93 | + verify(spanMock, times(1)).setAttribute(AwsAttributeKeys.AWS_TRACE_FLAG_SAMPLED, false); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testExportsOnlyUnsampledSpans() { |
| 98 | + SpanExporter mockExporter = mock(SpanExporter.class); |
| 99 | + when(mockExporter.export(anyCollection())).thenReturn(CompletableResultCode.ofSuccess()); |
| 100 | + |
| 101 | + AwsUnsampledOnlySpanProcessor processor = |
| 102 | + AwsUnsampledOnlySpanProcessor.builder().setSpanExporter(mockExporter).build(); |
| 103 | + |
| 104 | + BatchSpanProcessor delegate = |
| 105 | + BatchSpanProcessor.builder(mockExporter).setExportUnsampledSpans(true).build(); |
| 106 | + |
| 107 | + // unsampled span |
| 108 | + SpanContext mockSpanContextUnsampled = mock(SpanContext.class); |
| 109 | + when(mockSpanContextUnsampled.isSampled()).thenReturn(false); |
| 110 | + ReadableSpan mockSpanUnsampled = mock(ReadableSpan.class); |
| 111 | + when(mockSpanUnsampled.getSpanContext()).thenReturn(mockSpanContextUnsampled); |
| 112 | + |
| 113 | + // sampled span |
| 114 | + SpanContext mockSpanContextSampled = mock(SpanContext.class); |
| 115 | + when(mockSpanContextSampled.isSampled()).thenReturn(true); |
| 116 | + ReadableSpan mockSpanSampled = mock(ReadableSpan.class); |
| 117 | + when(mockSpanSampled.getSpanContext()).thenReturn(mockSpanContextSampled); |
| 118 | + |
| 119 | + // flush the unsampled span and verify export was called once |
| 120 | + processor.onEnd(mockSpanUnsampled); |
| 121 | + processor.forceFlush(); |
| 122 | + verify(mockExporter, times(1)).export(anyCollection()); |
| 123 | + |
| 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()); |
| 128 | + } |
118 | 129 | } |
0 commit comments