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

Make built-in spanProcessor extendable

Open doki23 opened this issue 1 year ago • 9 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. The implementations of SpanProcessor are final class, we cannot extend it.

Describe the solution you'd like A clear and concise description of what you want to happen. Make them extendable.

doki23 avatar Jul 10 '24 09:07 doki23

Can you explain what your use-case is, and why you need to extend, rather than just create a wrapper that delegates some functionality to the underlying SpanProcessor?

jkwatson avatar Jul 11 '24 03:07 jkwatson

I want BatchSpanProcessor to skip executing forceFlush when shutdown. It's impossible to create a wrapper that delegates the shutdown because it's a private method of the inner class Worker.

doki23 avatar Jul 11 '24 05:07 doki23

Seems easy enough to have your delegate keep track of the shutdown state, and just use that to make forceFlush a no-op after shutdown has been called. Am I missing something?

jkwatson avatar Jul 11 '24 14:07 jkwatson

Seems easy enough to have your delegate keep track of the shutdown state, and just use that to make forceFlush a no-op after shutdown has been called. Am I missing something?

Sorry, I can't get it. Would you please show me a code example?

doki23 avatar Jul 18 '24 09:07 doki23

Seems easy enough to have your delegate keep track of the shutdown state, and just use that to make forceFlush a no-op after shutdown has been called. Am I missing something?

Sorry, I can't get it. Would you please show me a code example?

public class DelegatingSpanProcessor implements SpanProcessor {
  private final SpanProcessor delegate;
  private final AtomicBoolean isShutdown = new AtomicBoolean();

  public DelegatingSpanProcessor(SpanProcessor delegate) {this.delegate = delegate;}

  @Override
  public CompletableResultCode shutdown() {
    CompletableResultCode shutdown = delegate.shutdown();
    isShutdown.set(true);
    return shutdown;
  }

  @Override
  public CompletableResultCode forceFlush() {
    if (isShutdown.get()) {
      return CompletableResultCode.ofSuccess();
    }
    return delegate.forceFlush();
  }

  @Override
  public void onStart(Context parentContext, ReadWriteSpan span) {
    delegate.onStart(parentContext, span);
  }

  @Override
  public boolean isStartRequired() {
    return delegate.isStartRequired();
  }

  @Override
  public void onEnd(ReadableSpan span) {
    delegate.onEnd(span);
  }

  @Override
  public boolean isEndRequired() {
    return delegate.isEndRequired();
  }

  @Override
  public void close() {
    delegate.close();
  }
}

jkwatson avatar Jul 18 '24 23:07 jkwatson

delegate.shutdown() calls worker.shutdown(), and then the worker will flush itself. The forceFlush of BatchSpanProcessor itself doesn't make sense. Let's see the implementation of the forceFlush of BatchSpanProcessor:

@Override
public CompletableResultCode shutdown() {
  if (isShutdown.getAndSet(true)) {
    return CompletableResultCode.ofSuccess();
  }
  return worker.shutdown(); // <=== I want to skip this flush
}

doki23 avatar Jul 19 '24 03:07 doki23

Why do you want to skip the controlled shutdown of the worker?

jkwatson avatar Jul 20 '24 01:07 jkwatson

I don't want to flush spans when shutdown.

doki23 avatar Jul 23 '24 05:07 doki23

Making the BatchSpanProcessor extendable wouldn't solve your issue, then, since we will still need to shut down the worker thread, even if it doesn't flush its spans before doing so.

I recommend making a copy of the BatchSpanProcessor code and modifying it to your liking. If you need this as a first-class feature (skipping the flushing of the spans on shutdown), I recommend creating an issue in the specification for it, as this is required behavior from the BatchSpanProcessor. See here for the specific requirement.

jkwatson avatar Jul 28 '24 00:07 jkwatson

I think a sufficient workaround has been proposed here, and I think we collectively feel pretty strongly about preventing users from inheriting from core otel classes. Please feel free to reopen if this needs to be discussed further.

breedx-splk avatar Oct 03 '24 20:10 breedx-splk