sdk-java icon indicating copy to clipboard operation
sdk-java copied to clipboard

Tracing context does not propagate into .thenCompose

Open ghaskins opened this issue 1 year ago • 5 comments

Expected Behavior

Temporal workflows and activities should generate related spans when the Temporal client is properly configured with OpenTracing interceptors.

Actual Behavior

Some spans end up severed from the parent. This seems to be isolated to spans that are generated with a promise chain using .thenCompose. Other spans seem to work as expected.

Steps to Reproduce the Problem

  1. Enable trace interceptors
  2. Write a workflow that has at least two activities that are composed serially via .executeAsync -> .thenCompose
  3. Observe that the workflow and first activity end up in one span, and the second activity ends up orphaned within its own span.

Specifications

  • Version: Temporal Java SDK 1.22.3

ghaskins avatar Dec 31 '23 21:12 ghaskins

Looking at how thenCompose is implemented

  @Override
  public <U> Promise<U> thenCompose(Functions.Func1<? super V, ? extends Promise<U>> fn) {
    return then(
        (result) -> {
          if (failure != null) {
            result.completeExceptionally(failure);
            return;
          }
          try {
            Promise<U> r = fn.apply(value);
            result.completeFrom(r);
          } catch (RuntimeException e) {
            result.completeExceptionally(e);
          }
        });
  }
...

  private <U> Promise<U> then(Functions.Proc1<CompletablePromise<U>> proc) {
    CompletablePromise<U> resultPromise = new CompletablePromiseImpl<>();
    if (completed) {
      proc.apply(resultPromise);
      unregisterWithRunner();
    } else {
      handlers.add(() -> proc.apply(resultPromise));
    }
    return resultPromise;
  }

Different code paths can be taken if the future is already complete or not by the time it is called. If the future is already complete then fn is run in the calling thread so the trace context should be setup, but if the future is not ready it will be run in a callback thread without the context set.

Quinn-With-Two-Ns avatar Jan 08 '24 22:01 Quinn-With-Two-Ns

Just curious if there is any movement on this?

ghaskins avatar Mar 26 '24 19:03 ghaskins

No update at this time

Quinn-With-Two-Ns avatar Mar 26 '24 19:03 Quinn-With-Two-Ns

Would you consider a PR if I submitted one?

ghaskins avatar Apr 15 '24 13:04 ghaskins

Yes, PRs are always welcome!

Quinn-With-Two-Ns avatar Apr 15 '24 15:04 Quinn-With-Two-Ns