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

Filter out scalar Mono/Flux instances #8571

Merged
Merged
Show file tree
Hide file tree
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 @@ -155,7 +155,8 @@ public void resetOnEachOperator() {

private static <T> Function<? super Publisher<T>, ? extends Publisher<T>> tracingLift(
ReactorAsyncOperationEndStrategy asyncOperationEndStrategy) {
return Operators.lift(new Lifter<>(asyncOperationEndStrategy));
return Operators.lift(
ContextPropagationOperator::shouldInstrument, new Lifter<>(asyncOperationEndStrategy));
}

/** Forces Mono to run in traceContext scope. */
Expand Down Expand Up @@ -220,7 +221,12 @@ public reactor.util.context.Context apply(reactor.util.context.Context context)
}
}

public static class Lifter<T>
private static boolean shouldInstrument(Scannable publisher) {
// skip if Flux/Mono #just, #empty, #error
return !(publisher instanceof Fuseable.ScalarCallable);
}

private static class Lifter<T>
implements BiFunction<Scannable, CoreSubscriber<? super T>, CoreSubscriber<? super T>> {

/** Holds reference to strategy to prevent it from being collected. */
Expand All @@ -233,10 +239,6 @@ public Lifter(ReactorAsyncOperationEndStrategy asyncOperationEndStrategy) {

@Override
public CoreSubscriber<? super T> apply(Scannable publisher, CoreSubscriber<? super T> sub) {
// if Flux/Mono #just, #empty, #error
if (publisher instanceof Fuseable.ScalarCallable) {
return sub;
}
return new TracingSubscriber<>(sub, sub.currentContext());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;
import reactor.core.CoreSubscriber;
import reactor.core.Disposable;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

class HooksTest {

Expand Down Expand Up @@ -43,4 +47,28 @@ public void subscribe(CoreSubscriber<? super Integer> actual) {
subscriber.set(actual);
}
}

@Test
void testInvalidBlockUsage() throws InterruptedException {
ContextPropagationOperator operator = ContextPropagationOperator.create();
operator.registerOnEachOperator();

Callable<String> callable =
() -> {
Mono.just("test1").block();
return "call1";
};

Disposable disposable =
Mono.defer(
() ->
Mono.fromCallable(callable).publishOn(Schedulers.elastic()).flatMap(Mono::just))
.subscribeOn(Schedulers.single())
.subscribe();

TimeUnit.MILLISECONDS.sleep(100);

disposable.dispose();
operator.resetOnEachOperator();
}
}