Skip to content

Commit a261a4e

Browse files
committed
chore(gax): add explanatory comments for tracer ordering and span scope lifecycle
1 parent 1251fdd commit a261a4e

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

‎sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ static ApiTracerFactory getApiTracerFactory(
298298
ApiTracerFactory apiTracerFactory = settings.getTracerFactory();
299299

300300
if (LoggingUtils.isLoggingEnabled()) {
301+
// When an attempt ends, CompositeTracer calls tracers in reverse order.
302+
// Placing LoggingTracerFactory second ensures logs are written before OpenTelemetry
303+
// closes the active trace span.
301304
apiTracerFactory =
302305
new CompositeTracerFactory(
303306
ImmutableList.of(apiTracerFactory, new LoggingTracerFactory()));

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public void attemptStarted(Object request, int attemptNumber) {
148148
spanBuilder.setAllAttributes(ObservabilityUtils.toOtelAttributes(currentAttemptAttributes));
149149

150150
this.attemptSpan = spanBuilder.startSpan();
151+
// Make the span active on the current thread so logs can capture the trace ID.
151152
this.scope = attemptSpan.makeCurrent();
152153
}
153154

@@ -241,6 +242,7 @@ private void endAttempt() {
241242
return;
242243
}
243244

245+
// Remove the span from the current thread before closing the span.
244246
try {
245247
if (scope != null) {
246248
scope.close();

0 commit comments

Comments
 (0)