Skip to content

Commit

Permalink
Only call SpanProcessor onStart / onEnd if required (#6112)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Jan 3, 2024
1 parent bc0df3b commit 63fe708
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -448,7 +450,9 @@ private void endInternal(long endEpochNanos) {
this.endEpochNanos = endEpochNanos;
hasEnded = true;
}
spanProcessor.onEnd(this);
if (spanProcessor.isEndRequired()) {
spanProcessor.onEnd(this);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<AttributeKey, Object> attributes) {
SpanLimits spanLimits = SpanLimits.getDefault();
AttributesMap attributesMap =
Expand Down

0 comments on commit 63fe708

Please sign in to comment.