cloud-trace-java
cloud-trace-java copied to clipboard
StartSpan & EndSpan from within different Threads
Hi,
my plan is to integrate tracing into our existing backend. All the request handling is currently accomplished by a chain of CompletionStages. My intention now was to simply hook in the tracing by wrapping the existing chains like shown below (exemplary!):
CompletableFuture.runAsync(() -> {
// tracer.startSpan("abc");
}, tracerPool)
.thenCompose(x -> {
return completionStage; // <-- stuff to measure
})
.thenApplyAsync(result -> {
// tracer.endSpan(traceContextChild);
return result;
}, tracerPool)
What might happen over here is that startSpan and endSpan get executed in different threads. But since the information about the last context is stored within a thread local variable, stopping the span will fail with the following error:
Context was not attached when detaching
java.lang.Throwable
at io.grpc.Context.detach(Context.java:353)
at com.google.cloud.trace.GrpcSpanContextHandler$GrpcSpanContextHandle.detach(GrpcSpanContextHandler.java:64)
at com.google.cloud.trace.SpanContextHandlerTracer.endSpan(SpanContextHandlerTracer.java:92)
Do I miss something?