Skip to content

Commit 23bc11f

Browse files
committed
fix issue related to traces being not returned from getTrace api call
need to wait some time for traces to be visible.
1 parent 943045f commit 23bc11f

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

google-cloud-spanner-executor/src/main/java/com/google/cloud/executor/spanner/CloudClientExecutor.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@
176176
import java.util.Objects;
177177
import java.util.concurrent.ExecutionException;
178178
import java.util.concurrent.Executor;
179+
import java.util.concurrent.ExecutorService;
179180
import java.util.concurrent.Executors;
181+
import java.util.concurrent.Future;
180182
import java.util.concurrent.TimeUnit;
181183
import java.util.logging.Level;
182184
import java.util.logging.Logger;
@@ -768,7 +770,7 @@ public synchronized void closeBatchTxn() throws SpannerException {
768770
new ThreadFactoryBuilder().setNameFormat("action-pool-%d").build());
769771

770772
// Thread pool to verify end to end traces.
771-
private static final Executor endToEndTracesThreadPool =
773+
private static final ExecutorService endToEndTracesThreadPool =
772774
Executors.newCachedThreadPool(
773775
new ThreadFactoryBuilder().setNameFormat("end-to-end-traces-pool-%d").build());
774776

@@ -891,23 +893,27 @@ private synchronized TraceServiceClient getTraceServiceClient() throws IOExcepti
891893
return traceServiceClient;
892894
}
893895

894-
/* Handles verification of end to end traces */
895-
public Status startVerificationOfEndToEndTrace(
896-
String traceId, ExecutionFlowContext executionContext) {
897-
endToEndTracesThreadPool.execute(
896+
public Future<Boolean> getEndToEndTraceVerificationTask(String traceId) {
897+
return endToEndTracesThreadPool.submit(
898898
() -> {
899-
boolean isValidTrace = isExportedEndToEndTraceValid(traceId);
900-
if (!isValidTrace) {
901-
LOGGER.log(Level.WARNING, String.format("traceId:%s failed to be verified.", traceId));
902-
executionContext.onError(
903-
Status.INTERNAL
904-
.withDescription(
905-
String.format(
906-
"failed to verify end to end trace for trace_id: %s", traceId))
907-
.getCause());
899+
try {
900+
// Wait for 10 seconds before verifying to ensure traces are exported.
901+
long sleepDuration = TimeUnit.SECONDS.toMillis(10);
902+
LOGGER.log(
903+
Level.INFO,
904+
String.format(
905+
"Sleeping for %d milliseconds before verifying end to end trace",
906+
sleepDuration));
907+
Thread.sleep(sleepDuration);
908+
} catch (InterruptedException e) {
909+
Thread.currentThread().interrupt(); // Handle interruption
910+
LOGGER.log(
911+
Level.INFO,
912+
String.format("Thread interrupted."));
913+
return false; // Return false if interrupted
908914
}
915+
return isExportedEndToEndTraceValid(traceId);
909916
});
910-
return Status.OK;
911917
}
912918

913919
private static final String READ_WRITE_TRANSACTION = "CloudSpanner.ReadWriteTransaction";
@@ -924,7 +930,8 @@ public boolean isExportedEndToEndTraceValid(String traceId) {
924930
Trace trace = getTraceServiceClient().getTrace(getTraceRequest);
925931
boolean readWriteOrReadOnlyTxnPresent = false, spannerServerSideSpanPresent = false;
926932
for (TraceSpan span : trace.getSpansList()) {
927-
if (span.getName() == READ_ONLY_TRANSACTION || span.getName() == READ_WRITE_TRANSACTION) {
933+
if (span.getName().contains(READ_ONLY_TRANSACTION)
934+
|| span.getName().contains(READ_WRITE_TRANSACTION)) {
928935
readWriteOrReadOnlyTxnPresent = true;
929936
}
930937
if (span.getName().startsWith("Spanner.")) {
@@ -934,8 +941,8 @@ public boolean isExportedEndToEndTraceValid(String traceId) {
934941
if (readWriteOrReadOnlyTxnPresent && !spannerServerSideSpanPresent) {
935942
return false;
936943
}
937-
} catch (IOException e) {
938-
LOGGER.log(Level.WARNING, "failed to verify end to end traces.", e);
944+
} catch (Exception e) {
945+
LOGGER.log(Level.WARNING, "Failed to verify end to end trace.", e);
939946
return false;
940947
}
941948
return true;

google-cloud-spanner-executor/src/main/java/com/google/cloud/executor/spanner/CloudExecutorImpl.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.opentelemetry.api.trace.Span;
3030
import io.opentelemetry.api.trace.Tracer;
3131
import io.opentelemetry.context.Scope;
32+
import java.util.concurrent.Future;
3233
import java.util.concurrent.atomic.AtomicBoolean;
3334
import java.util.logging.Level;
3435
import java.util.logging.Logger;
@@ -136,19 +137,44 @@ public void onError(Throwable t) {
136137

137138
@Override
138139
public void onCompleted() {
140+
// Close the scope and end the span.
141+
scope.close();
142+
span.end();
139143
if (isSampled
140144
&& getCloudTraceCheckCount() < MAX_CLOUD_TRACE_CHECK_LIMIT
141145
&& requestHasReadOrQueryAction.get()) {
142-
LOGGER.log(
143-
Level.WARNING, String.format("traceId:%s will be verified for e2e tracing", traceId));
144-
incrementCloudTraceCheckCount();
145-
clientExecutor.startVerificationOfEndToEndTrace(traceId, executionContext);
146+
Future<Boolean> traceVerificationTask =
147+
clientExecutor.getEndToEndTraceVerificationTask(traceId);
148+
try {
149+
LOGGER.log(
150+
Level.INFO,
151+
String.format("Starting end to end trace verification for trace_id:%s", traceId));
152+
Boolean isValidTrace = traceVerificationTask.get();
153+
incrementCloudTraceCheckCount();
154+
if (!isValidTrace) {
155+
executionContext.onError(
156+
Status.INTERNAL
157+
.withDescription(
158+
String.format(
159+
"failed to verify end to end trace for trace_id: %s", traceId))
160+
.getCause());
161+
executionContext.cleanup();
162+
return;
163+
}
164+
} catch (Exception e) {
165+
LOGGER.log(
166+
Level.WARNING,
167+
String.format(
168+
"Failed to verify end to end trace with exception: %s\n", e.getMessage()),
169+
e);
170+
executionContext.onError(e);
171+
executionContext.cleanup();
172+
return;
173+
}
146174
}
147175
LOGGER.log(Level.INFO, "Client called Done, half closed");
148176
executionContext.cleanup();
149177
responseObserver.onCompleted();
150-
scope.close();
151-
span.end();
152178
}
153179
};
154180
}

0 commit comments

Comments
 (0)