-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
How to check type of Runnable inside the RxJavaPlugins onScheduleHandler #5733
Comments
Unfortunately you can't; it requires exposing the type or at least some API to unwrap the internal wrappers. I don't have much time this week to work the details out though. |
Sketch for a solution:
Names are only suggestions. |
Awesome :) I will try to write PR based on your tips. Thank you very much for your answer. :) |
@lukaszguz I was reviewing your PR and caught myself couple times on the question why would anyone need to unwrap runnable and what useful actions can you actually do with it if original runnable is created by RxJava itself (unless you use Schedulers in a decoupled way from reactive types). While I'm trying to find answer to this question myself, answer from you would be great :) I've looked through the tracing code you've posted and not sure I understand the purpose of But JFYI information check output of following simple case: @Test
public void t() {
final AtomicInteger scheduleHandlerCallCounter = new AtomicInteger();
final AtomicInteger isTraceActionDecoratedByRxWorkerCounter = new AtomicInteger();
final AtomicInteger isTraceActionNotDecoratedByRxWorkerCounter = new AtomicInteger();
RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {
@Override
public Runnable apply(Runnable action) throws Exception {
scheduleHandlerCallCounter.incrementAndGet();
if (isTraceActionDecoratedByRxWorker(action)) {
isTraceActionDecoratedByRxWorkerCounter.incrementAndGet();
return action;
}
isTraceActionNotDecoratedByRxWorkerCounter.incrementAndGet();
return new TraceAction(action);
}
});
Observable
.fromCallable(() -> "hello")
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.test()
.awaitTerminalEvent();
System.out.println("scheduleHandlerCallCounter == " + scheduleHandlerCallCounter.get());
System.out.println("isTraceActionDecoratedByRxWorkerCounter == " + isTraceActionDecoratedByRxWorkerCounter.get());
System.out.println("isTraceActionNotDecoratedByRxWorkerCounter == " + isTraceActionNotDecoratedByRxWorkerCounter.get());
} Rest of code is copied from your example. Output:
So as you can see By "original" I mean // Sorry for holding this, had to deal with some personal stuff |
Thanks for answer :) I really appreciate your help and advices :)
Results with checking Runnable type using isTraceActionDecoratedByRxWorker:
Each Observables, Flowables etc. has correct closed or detached span in Spring Sleuth.
I can't use any counter or flag which tell me when I have to wrap runnable in TraceAction because I don't have any place when I can check it. :/ The problem is solved when I have information about original type of runnable. |
Closing via #5734. |
Hi
RxJava2 version: [2.1.6]
I have problem with RxJavaPlugins.onScheduleHandler.
At the moment of changing thread pool Runnable is wrapped into DisposeTask so I can't check type of action inside scheduleHandler because DisposeTask is package scope.
Do you have any idea how to solve this problem without reflection? 😀
Here is my sample code: https://github.com/lukaszguz/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/rxjava2/SleuthRxJava2SchedulersHandler.java
The text was updated successfully, but these errors were encountered: