diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java index c6e72632547..70608e35a32 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java @@ -194,7 +194,9 @@ static SdkSpan startSpan( startEpochNanos); // Call onStart here instead of calling in the constructor to make sure the span is completely // initialized. - spanProcessor.onStart(parentContext, span); + if (spanProcessor.isStartRequired()) { + spanProcessor.onStart(parentContext, span); + } return span; } @@ -448,7 +450,9 @@ private void endInternal(long endEpochNanos) { this.endEpochNanos = endEpochNanos; hasEnded = true; } - spanProcessor.onEnd(this); + if (spanProcessor.isEndRequired()) { + spanProcessor.onEnd(this); + } } @Override diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java index a87c58d7c3c..d117a7b2e82 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java @@ -16,6 +16,10 @@ import static java.util.stream.Collectors.joining; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; @@ -60,9 +64,12 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; @SuppressWarnings({"rawtypes", "unchecked"}) @ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) class SdkSpanTest { private static final String SPAN_NAME = "MySpanName"; private static final String SPAN_NEW_NAME = "NewName"; @@ -97,6 +104,8 @@ void setUp() { } expectedAttributes = builder.build(); testClock = TestClock.create(Instant.ofEpochSecond(0, START_EPOCH_NANOS)); + when(spanProcessor.isStartRequired()).thenReturn(true); + when(spanProcessor.isEndRequired()).thenReturn(true); } @Test @@ -1040,6 +1049,39 @@ void badArgsIgnored() { assertThat(data.getName()).isEqualTo(SPAN_NAME); } + @Test + void onStartOnEndNotRequired() { + when(spanProcessor.isStartRequired()).thenReturn(false); + when(spanProcessor.isEndRequired()).thenReturn(false); + + SpanLimits spanLimits = SpanLimits.getDefault(); + SdkSpan span = + SdkSpan.startSpan( + spanContext, + SPAN_NAME, + instrumentationScopeInfo, + SpanKind.INTERNAL, + parentSpanId != null + ? Span.wrap( + SpanContext.create( + traceId, parentSpanId, TraceFlags.getDefault(), TraceState.getDefault())) + : Span.getInvalid(), + Context.root(), + spanLimits, + spanProcessor, + testClock, + resource, + AttributesMap.create( + spanLimits.getMaxNumberOfAttributes(), spanLimits.getMaxAttributeValueLength()), + Collections.emptyList(), + 1, + 0); + verify(spanProcessor, never()).onStart(any(), any()); + + span.end(); + verify(spanProcessor, never()).onEnd(any()); + } + private SdkSpan createTestSpanWithAttributes(Map attributes) { SpanLimits spanLimits = SpanLimits.getDefault(); AttributesMap attributesMap =