@@ -59,7 +59,12 @@ class SimpleSpanProcessorTest {
59
59
SpanId .getInvalid (),
60
60
TraceFlags .getSampled (),
61
61
TraceState .getDefault ());
62
- private static final SpanContext NOT_SAMPLED_SPAN_CONTEXT = SpanContext .getInvalid ();
62
+ private static final SpanContext NOT_SAMPLED_SPAN_CONTEXT =
63
+ SpanContext .create (
64
+ TraceId .getInvalid (),
65
+ SpanId .getInvalid (),
66
+ TraceFlags .getDefault (),
67
+ TraceState .getDefault ());
63
68
64
69
private SpanProcessor simpleSampledSpansProcessor ;
65
70
@@ -100,29 +105,29 @@ void onEndSync_NotSampledSpan() {
100
105
}
101
106
102
107
@ Test
103
- void onEndSync_OnlySampled_NotSampledSpan () {
108
+ void onEndSync_ExportUnsampledSpans_NotSampledSpan () {
109
+ SpanData spanData = TestUtils .makeBasicSpan ();
104
110
when (readableSpan .getSpanContext ()).thenReturn (NOT_SAMPLED_SPAN_CONTEXT );
105
- when (readableSpan .toSpanData ())
106
- .thenReturn (TestUtils .makeBasicSpan ())
107
- .thenThrow (new RuntimeException ());
108
- SpanProcessor simpleSpanProcessor = SimpleSpanProcessor .create (spanExporter );
111
+ when (readableSpan .toSpanData ()).thenReturn (spanData );
112
+ SpanProcessor simpleSpanProcessor =
113
+ SimpleSpanProcessor .builder (spanExporter ).setExportUnsampledSpans (true ).build ();
109
114
simpleSpanProcessor .onEnd (readableSpan );
110
- verifyNoInteractions (spanExporter );
115
+ verify (spanExporter ). export ( Collections . singletonList ( spanData ) );
111
116
}
112
117
113
118
@ Test
114
- void onEndSync_OnlySampled_SampledSpan () {
119
+ void onEndSync_ExportUnsampledSpans_SampledSpan () {
120
+ SpanData spanData = TestUtils .makeBasicSpan ();
115
121
when (readableSpan .getSpanContext ()).thenReturn (SAMPLED_SPAN_CONTEXT );
116
- when (readableSpan .toSpanData ())
117
- .thenReturn (TestUtils .makeBasicSpan ())
118
- .thenThrow (new RuntimeException ());
119
- SpanProcessor simpleSpanProcessor = SimpleSpanProcessor .create (spanExporter );
122
+ when (readableSpan .toSpanData ()).thenReturn (spanData );
123
+ SpanProcessor simpleSpanProcessor =
124
+ SimpleSpanProcessor .builder (spanExporter ).setExportUnsampledSpans (true ).build ();
120
125
simpleSpanProcessor .onEnd (readableSpan );
121
- verify (spanExporter ).export (Collections .singletonList (TestUtils . makeBasicSpan () ));
126
+ verify (spanExporter ).export (Collections .singletonList (spanData ));
122
127
}
123
128
124
129
@ Test
125
- void tracerSdk_NotSampled_Span () {
130
+ void tracerSdk_SampledSpan () {
126
131
WaitingSpanExporter waitingSpanExporter =
127
132
new WaitingSpanExporter (1 , CompletableResultCode .ofSuccess ());
128
133
@@ -159,25 +164,43 @@ void tracerSdk_NotSampled_Span() {
159
164
}
160
165
161
166
@ Test
162
- void tracerSdk_NotSampled_RecordingEventsSpan () {
163
- // TODO(bdrutu): Fix this when Sampler return RECORD_ONLY option.
164
- /*
165
- tracer.addSpanProcessor(
166
- BatchSpanProcessor.builder(waitingSpanExporter)
167
- .setScheduleDelayMillis(MAX_SCHEDULE_DELAY_MILLIS)
168
- .reportOnlySampled(false)
169
- .build());
170
-
171
- io.opentelemetry.trace.Span span =
172
- tracer
173
- .spanBuilder("FOO")
174
- .setSampler(Samplers.neverSample())
175
- .startSpanWithSampler();
176
- span.end();
177
-
178
- List<SpanData> exported = waitingSpanExporter.waitForExport(1);
179
- assertThat(exported).containsExactly(((ReadableSpan) span).toSpanData());
180
- */
167
+ void tracerSdk_ExportUnsampledSpans_NotSampledSpan () {
168
+ WaitingSpanExporter waitingSpanExporter =
169
+ new WaitingSpanExporter (1 , CompletableResultCode .ofSuccess ());
170
+
171
+ SdkTracerProvider sdkTracerProvider =
172
+ SdkTracerProvider .builder ()
173
+ .addSpanProcessor (
174
+ SimpleSpanProcessor .builder (waitingSpanExporter )
175
+ .setExportUnsampledSpans (true )
176
+ .build ())
177
+ .setSampler (mockSampler )
178
+ .build ();
179
+
180
+ when (mockSampler .shouldSample (any (), any (), any (), any (), any (), anyList ()))
181
+ .thenReturn (SamplingResult .drop ());
182
+
183
+ try {
184
+ Tracer tracer = sdkTracerProvider .get (getClass ().getName ());
185
+ tracer .spanBuilder (SPAN_NAME ).startSpan ();
186
+ tracer .spanBuilder (SPAN_NAME ).startSpan ();
187
+
188
+ when (mockSampler .shouldSample (any (), any (), any (), any (), any (), anyList ()))
189
+ .thenReturn (SamplingResult .recordOnly ());
190
+ Span span = tracer .spanBuilder (SPAN_NAME ).startSpan ();
191
+ span .end ();
192
+
193
+ // Spans are recorded and exported in the same order as they are ended, we test that a non
194
+ // sampled span is not exported by creating and ending a sampled span after a non sampled span
195
+ // and checking that the first exported span is the sampled span (the non sampled did not get
196
+ // exported).
197
+ List <SpanData > exported = waitingSpanExporter .waitForExport ();
198
+ // Need to check this because otherwise the variable span1 is unused, other option is to not
199
+ // have a span1 variable.
200
+ assertThat (exported ).containsExactly (((ReadableSpan ) span ).toSpanData ());
201
+ } finally {
202
+ sdkTracerProvider .shutdown ();
203
+ }
181
204
}
182
205
183
206
@ Test
0 commit comments