Skip to content
This repository was archived by the owner on Sep 14, 2022. It is now read-only.
This repository was archived by the owner on Sep 14, 2022. It is now read-only.

StartSpan & EndSpan from within different Threads #85

@sebastian-garn

Description

@sebastian-garn

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions