Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce reactor stack trace depth #9923

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Scope;
import java.util.function.Supplier;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
Expand Down Expand Up @@ -56,49 +57,53 @@ public TracingSubscriber(

@Override
public void onSubscribe(Subscription subscription) {
withActiveSpan(() -> subscriber.onSubscribe(subscription));
try (Scope ignore = openScope()) {
subscriber.onSubscribe(subscription);
}
}

@Override
public void onNext(T o) {
withActiveSpan(() -> subscriber.onNext(o));
try (Scope ignore = openScope()) {
subscriber.onNext(o);
}
}

@Override
public void onError(Throwable throwable) {
Supplier<Scope> scopeSupplier;
if (!hasContextToPropagate
&& (fluxRetrySubscriberClass == subscriber.getClass()
|| fluxRetryWhenSubscriberClass == subscriber.getClass())) {
// clear context for retry to avoid having retried operations run with currently active
// context as parent context
withActiveSpan(io.opentelemetry.context.Context.root(), () -> subscriber.onError(throwable));
scopeSupplier = () -> openScope(io.opentelemetry.context.Context.root());
} else {
withActiveSpan(() -> subscriber.onError(throwable));
scopeSupplier = () -> openScope();
}
try (Scope ignore = scopeSupplier.get()) {
subscriber.onError(throwable);
}
}

@Override
public void onComplete() {
withActiveSpan(subscriber::onComplete);
try (Scope ignore = openScope()) {
subscriber.onComplete();
}
}

@Override
public Context currentContext() {
return context;
}

private void withActiveSpan(Runnable runnable) {
withActiveSpan(hasContextToPropagate ? traceContext : null, runnable);
private Scope openScope() {
return openScope(hasContextToPropagate ? traceContext : null);
}

private static void withActiveSpan(io.opentelemetry.context.Context context, Runnable runnable) {
if (context != null) {
try (Scope ignored = context.makeCurrent()) {
runnable.run();
}
} else {
runnable.run();
}
private static Scope openScope(io.opentelemetry.context.Context context) {
return context != null ? context.makeCurrent() : null;
}

private static Class<?> getFluxRetrySubscriberClass() {
Expand Down
Loading