Skip to content

Commit 262d771

Browse files
author
Mateusz Rzeszutek
authored
Filter out scalar Mono/Flux instances (#8571)
1 parent ba4eea2 commit 262d771

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

instrumentation/reactor/reactor-3.1/library/src/main/java/io/opentelemetry/instrumentation/reactor/v3_1/ContextPropagationOperator.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ public void resetOnEachOperator() {
155155

156156
private static <T> Function<? super Publisher<T>, ? extends Publisher<T>> tracingLift(
157157
ReactorAsyncOperationEndStrategy asyncOperationEndStrategy) {
158-
return Operators.lift(new Lifter<>(asyncOperationEndStrategy));
158+
return Operators.lift(
159+
ContextPropagationOperator::shouldInstrument, new Lifter<>(asyncOperationEndStrategy));
159160
}
160161

161162
/** Forces Mono to run in traceContext scope. */
@@ -220,7 +221,12 @@ public reactor.util.context.Context apply(reactor.util.context.Context context)
220221
}
221222
}
222223

223-
public static class Lifter<T>
224+
private static boolean shouldInstrument(Scannable publisher) {
225+
// skip if Flux/Mono #just, #empty, #error
226+
return !(publisher instanceof Fuseable.ScalarCallable);
227+
}
228+
229+
private static class Lifter<T>
224230
implements BiFunction<Scannable, CoreSubscriber<? super T>, CoreSubscriber<? super T>> {
225231

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

234240
@Override
235241
public CoreSubscriber<? super T> apply(Scannable publisher, CoreSubscriber<? super T> sub) {
236-
// if Flux/Mono #just, #empty, #error
237-
if (publisher instanceof Fuseable.ScalarCallable) {
238-
return sub;
239-
}
240242
return new TracingSubscriber<>(sub, sub.currentContext());
241243
}
242244
}

instrumentation/reactor/reactor-3.1/library/src/test/java/io/opentelemetry/instrumentation/reactor/v3_1/HooksTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77

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

10+
import java.util.concurrent.Callable;
11+
import java.util.concurrent.TimeUnit;
1012
import java.util.concurrent.atomic.AtomicReference;
1113
import org.junit.jupiter.api.Test;
1214
import reactor.core.CoreSubscriber;
15+
import reactor.core.Disposable;
1316
import reactor.core.publisher.Mono;
17+
import reactor.core.scheduler.Schedulers;
1418

1519
class HooksTest {
1620

@@ -43,4 +47,28 @@ public void subscribe(CoreSubscriber<? super Integer> actual) {
4347
subscriber.set(actual);
4448
}
4549
}
50+
51+
@Test
52+
void testInvalidBlockUsage() throws InterruptedException {
53+
ContextPropagationOperator operator = ContextPropagationOperator.create();
54+
operator.registerOnEachOperator();
55+
56+
Callable<String> callable =
57+
() -> {
58+
Mono.just("test1").block();
59+
return "call1";
60+
};
61+
62+
Disposable disposable =
63+
Mono.defer(
64+
() ->
65+
Mono.fromCallable(callable).publishOn(Schedulers.elastic()).flatMap(Mono::just))
66+
.subscribeOn(Schedulers.single())
67+
.subscribe();
68+
69+
TimeUnit.MILLISECONDS.sleep(100);
70+
71+
disposable.dispose();
72+
operator.resetOnEachOperator();
73+
}
4674
}

0 commit comments

Comments
 (0)