Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,29 @@ class OpenTelemetryTracingTracer implements ApiTracer {
private final Context parentContext;
// Trace context containing the operationSpan, serving as the parent for attempt spans.
private final Context operationContext;
private @Nullable Span operationSpan;
private @Nullable Span attemptSpan;
private volatile @Nullable Span operationSpan;
private volatile @Nullable Span attemptSpan;

/**
* Activates the current span on the calling thread's OpenTelemetry {@link Context}.
*
* @return a {@link Scope} that restores the previous context when closed
*/
@Override
public Scope inScope() {
Span currentSpan = attemptSpan;
if (currentSpan == null) {
currentSpan = operationSpan;
}
if (currentSpan == null) {
return () -> {};
}
// Suppressing MustBeClosedChecker because the scope is closed indirectly via the returned
// Scope.
@SuppressWarnings("MustBeClosedChecker")
io.opentelemetry.context.Scope otelScope = currentSpan.makeCurrent();
return otelScope::close;
}
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved

@Override
public void injectTraceContext(Map<String, String> carrier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.api.trace.Tracer;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -783,4 +789,86 @@ void testOperationSpanName_replacesSlashWithDotInFullMethodName() {
verify(tracer)
.spanBuilder("google.cloud.secretmanager.v1.SecretManagerService.AccessSecretVersion");
}

@Test
void testInScope_withAttemptSpan_activatesAttemptSpanAndFallsBackToOperationSpan() {
SpanContext opContext =
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000010",
TraceFlags.getSampled(),
TraceState.getDefault());
SpanContext attemptContext =
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000020",
TraceFlags.getSampled(),
TraceState.getDefault());
Span realOpSpan = Span.wrap(opContext);
Span realAttemptSpan = Span.wrap(attemptContext);
when(operationSpanBuilder.startSpan()).thenReturn(realOpSpan);
when(spanBuilder.startSpan()).thenReturn(realAttemptSpan);

openTelemetryTracingTracer =
new OpenTelemetryTracingTracer(tracer, ApiTracerContext.empty(), ATTEMPT_SPAN_NAME);
openTelemetryTracingTracer.attemptStarted(new Object(), 0);

assertThat(Span.current().getSpanContext().isValid()).isFalse();

// While attempt is active, inScope() activates attemptSpan
try (ApiTracer.Scope ignored = openTelemetryTracingTracer.inScope()) {
assertThat(Span.current()).isEqualTo(realAttemptSpan);
assertThat(Span.current().getSpanContext().getSpanId()).isEqualTo("0000000000000020");
}
assertThat(Span.current().getSpanContext().isValid()).isFalse();

// After attempt ends, inScope() falls back to operationSpan
openTelemetryTracingTracer.attemptSucceeded();
try (ApiTracer.Scope ignored = openTelemetryTracingTracer.inScope()) {
assertThat(Span.current()).isEqualTo(realOpSpan);
assertThat(Span.current().getSpanContext().getSpanId()).isEqualTo("0000000000000010");
}
assertThat(Span.current().getSpanContext().isValid()).isFalse();

// After operation ends, inScope() is a no-op
openTelemetryTracingTracer.operationSucceeded();
try (ApiTracer.Scope ignored = openTelemetryTracingTracer.inScope()) {
assertThat(Span.current().getSpanContext().isValid()).isFalse();
}
}

@Test
void testInScope_multithreadedExecution_noLeakAcrossThreads() throws Exception {
SpanContext attemptContext =
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000002",
TraceFlags.getSampled(),
TraceState.getDefault());
Span realAttemptSpan = Span.wrap(attemptContext);
when(spanBuilder.startSpan()).thenReturn(realAttemptSpan);

openTelemetryTracingTracer =
new OpenTelemetryTracingTracer(tracer, ApiTracerContext.empty(), ATTEMPT_SPAN_NAME);

openTelemetryTracingTracer.attemptStarted(new Object(), 0);
assertThat(Span.current().getSpanContext().isValid()).isFalse();

ExecutorService executor = Executors.newSingleThreadExecutor();
try {
executor
.submit(
() -> {
try (ApiTracer.Scope ignored = openTelemetryTracingTracer.inScope()) {
assertThat(Span.current()).isEqualTo(realAttemptSpan);
}
assertThat(Span.current().getSpanContext().isValid()).isFalse();
})
.get(5, TimeUnit.SECONDS);
} finally {
executor.shutdownNow();
}

assertThat(Span.current().getSpanContext().isValid()).isFalse();
}
}
Loading