Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only call SpanProcessor onStart / onEnd if required #6112

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading