Skip to content

Commit 4b851ad

Browse files
committed
docs(tracing): add Javadoc and explanatory comments for T3 operation spans
1 parent 412f398 commit 4b851ad

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/OpenTelemetryTracingTracer.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class OpenTelemetryTracingTracer implements ApiTracer {
7171

7272
@Override
7373
public void injectTraceContext(Map<String, String> carrier) {
74+
// Prefer the active attempt span (T4) so outgoing RPC wire context reflects the specific
75+
// attempt;
76+
// fall back to the overall operation span (T3) if no attempt is currently in-flight.
7477
Span currentAttempt = attemptSpan;
7578
Span spanToInject = currentAttempt != null ? currentAttempt : operationSpan;
7679
if (spanToInject != null) {
@@ -90,6 +93,8 @@ public void injectTraceContext(Map<String, String> carrier) {
9093
@Override
9194
@SuppressWarnings("MustBeClosedChecker")
9295
public Scope inScope() {
96+
// Attach the active attempt span to the current execution thread context;
97+
// fall back to the overall operation span when between attempts.
9398
Span currentAttempt = attemptSpan;
9499
Span currentSpan = currentAttempt != null ? currentAttempt : operationSpan;
95100
if (currentSpan == null) {
@@ -180,6 +185,12 @@ private static String resolveOperationSpanName(String attemptSpanName) {
180185
return "operation";
181186
}
182187

188+
/**
189+
* Resolves the canonical attempt-level span name based on transport and context.
190+
*
191+
* @param apiTracerContext the tracer context containing transport and method metadata
192+
* @return the attempt span name
193+
*/
183194
private static String resolveAttemptSpanName(ApiTracerContext apiTracerContext) {
184195
if (apiTracerContext.transport() == ApiTracerContext.Transport.GRPC) {
185196
// gRPC Uses the full method name as span name.
@@ -195,6 +206,7 @@ private static String resolveAttemptSpanName(ApiTracerContext apiTracerContext)
195206
}
196207
}
197208

209+
/** Copies attempt-level attributes from the tracer context into the local attribute cache. */
198210
private void buildAttributes() {
199211
this.attemptAttributes.putAll(this.apiTracerContext.getAttemptAttributes());
200212
}

‎sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/OpenTelemetryTracingTracerIntegrationTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ void tearDown() {
9191

9292
@Test
9393
void testAttemptSpan_linkedToParentContextFromCallingThread() {
94+
// Verifies that when an application trace parent exists, the operation span (T3)
95+
// links to the application parent, and the attempt span (T4) links to the operation span.
9496
Span parentSpan = tracer.spanBuilder("application-parent-operation").startSpan();
9597
ApiTracer apiTracer;
9698
try (Scope scope = parentSpan.makeCurrent()) {
@@ -132,6 +134,8 @@ void testAttemptSpan_linkedToParentContextFromCallingThread() {
132134

133135
@Test
134136
void testAttemptSpan_withoutParentContext_hasNoParent() {
137+
// Verifies that without an external parent context, the operation span acts as root
138+
// and the attempt span is a child of the operation span.
135139
ApiTracer apiTracer = tracingFactory.newTracer(BaseApiTracer.getInstance(), TRACER_CONTEXT);
136140

137141
apiTracer.attemptStarted(new Object(), 0);
@@ -209,6 +213,8 @@ void testSequentialAttempts_closesPreviousAttemptSpanAndLinksAllToParent() {
209213

210214
@Test
211215
void testOperationFailed_endsActiveAttemptSpanWithErrorAttributes() {
216+
// Verifies that when the operation fails permanently while an attempt is in-flight,
217+
// both the attempt span and the operation span end with the error status and message.
212218
Span parentSpan = tracer.spanBuilder("application-parent-operation").startSpan();
213219
ApiTracer apiTracer;
214220
try (Scope scope = parentSpan.makeCurrent()) {
@@ -245,6 +251,8 @@ void testOperationFailed_endsActiveAttemptSpanWithErrorAttributes() {
245251

246252
@Test
247253
void testAttemptStarted_afterOperationCompleted_doesNotEmitNewSpan() {
254+
// Verifies that after operation completion, subsequent attemptStarted calls
255+
// do not create orphan attempt spans.
248256
ApiTracer apiTracer = tracingFactory.newTracer(BaseApiTracer.getInstance(), TRACER_CONTEXT);
249257

250258
apiTracer.operationSucceeded();

‎sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/OpenTelemetryTracingTracerTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ void testOperationCancelled_endsActiveAttemptSpanWithCancellation() {
747747

748748
@Test
749749
void testInScope_withAttemptSpan() {
750+
// Verifies that inScope() activates the current attempt span if an attempt is currently active.
750751
io.opentelemetry.context.Scope mockScope = mock(io.opentelemetry.context.Scope.class);
751752
when(span.makeCurrent()).thenReturn(mockScope);
752753

@@ -759,6 +760,8 @@ void testInScope_withAttemptSpan() {
759760

760761
@Test
761762
void testInScope_withOperationSpanFallback() {
763+
// Verifies that inScope() falls back to activating the operation span when no attempt span is
764+
// active.
762765
io.opentelemetry.context.Scope mockScope = mock(io.opentelemetry.context.Scope.class);
763766
when(operationSpan.makeCurrent()).thenReturn(mockScope);
764767

@@ -770,6 +773,8 @@ void testInScope_withOperationSpanFallback() {
770773

771774
@Test
772775
void testInjectTraceContext_withOperationSpanFallback() {
776+
// Verifies that injectTraceContext() injects the operation span context into the carrier
777+
// when between attempts so that context propagation doesn't drop trace state.
773778
io.opentelemetry.api.trace.SpanContext mockSpanContext =
774779
io.opentelemetry.api.trace.SpanContext.create(
775780
"00000000000000000000000000000003",
@@ -792,6 +797,8 @@ void testInjectTraceContext_withOperationSpanFallback() {
792797

793798
@Test
794799
void testAttemptStarted_whenPreviousAttemptActive_closesOldSpan() {
800+
// Verifies that starting a new retry attempt cleanly closes any lingering previous attempt
801+
// span.
795802
Span span1 = mock(Span.class);
796803
Span span2 = mock(Span.class);
797804

@@ -811,6 +818,7 @@ void testAttemptStarted_whenPreviousAttemptActive_closesOldSpan() {
811818

812819
@Test
813820
void testAttemptStarted_afterOperationCompleted_doesNotStartNewSpan() {
821+
// Verifies that after operation completion, late callbacks cannot spawn new attempt spans.
814822
openTelemetryTracingTracer.operationSucceeded();
815823

816824
// Attempting to start a new attempt after operation completion should be a no-op

0 commit comments

Comments
 (0)