-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchedulingTracingAssembly.java
More file actions
190 lines (161 loc) · 6.56 KB
/
SchedulingTracingAssembly.java
File metadata and controls
190 lines (161 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package io.opentelemetry.demo.android.tracing;
import org.reactivestreams.Subscriber;
import java.util.concurrent.atomic.AtomicBoolean;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.reactivex.Completable;
import io.reactivex.CompletableObserver;
import io.reactivex.CompletableSource;
import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.MaybeObserver;
import io.reactivex.MaybeSource;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.SingleSource;
import io.reactivex.functions.Function;
import io.reactivex.plugins.RxJavaPlugins;
/**
* @author Ponomarev Leonid (leonid.phoenix@gmail.com)
* @since 16.03.2023
*/
@SuppressWarnings("rawtypes")
public final class SchedulingTracingAssembly {
static final AtomicBoolean lock = new AtomicBoolean();
private static Function<? super Single, ? extends Single> onSingleAssemblySource;
private static Function<? super Completable, ? extends Completable> onCompletableAssemblySource;
private static Function<? super Maybe, ? extends Maybe> onMaybeAssemblySource;
private static Function<? super Flowable, ? extends Flowable> onFlowableAssemblySource;
private static Function<? super Observable, ? extends Observable> onObservableAssemblySource;
private SchedulingTracingAssembly() {throw new IllegalStateException("singleton");}
@SuppressWarnings({"unchecked", "rawtypes"})
public static void enable() {
if (lock.compareAndSet(false, true)) {
onCompletableAssemblySource = RxJavaPlugins.getOnCompletableAssembly();
RxJavaPlugins.setOnCompletableAssembly(
compose(
onCompletableAssemblySource,
completable -> new CompletableOnAssemblyScheduled(Context.current(), completable)
)
);
onMaybeAssemblySource = RxJavaPlugins.getOnMaybeAssembly();
RxJavaPlugins.setOnMaybeAssembly(
compose(onMaybeAssemblySource,
maybe -> new MaybeOnAssemblyScheduled(Context.current(), maybe)
)
);
onSingleAssemblySource = RxJavaPlugins.getOnSingleAssembly();
RxJavaPlugins.setOnSingleAssembly(
compose(
onSingleAssemblySource,
single -> new SingleOnAssemblyScheduled(Context.current(), single)
)
);
onObservableAssemblySource = RxJavaPlugins.getOnObservableAssembly();
RxJavaPlugins.setOnObservableAssembly(
compose(
onObservableAssemblySource,
observable -> new ObservableOnAssemblyScheduled(Context.current(), (observable))
)
);
onFlowableAssemblySource = RxJavaPlugins.getOnFlowableAssembly();
RxJavaPlugins.setOnFlowableAssembly(
compose(
onFlowableAssemblySource,
flowable -> new FlowableOnAssemblyScheduled<>(Context.current(), (flowable))
)
);
}
}
public static void disable() {
if (lock.compareAndSet(false, true)) {
RxJavaPlugins.setOnCompletableAssembly(onCompletableAssemblySource);
RxJavaPlugins.setOnSingleAssembly(onSingleAssemblySource);
RxJavaPlugins.setOnMaybeAssembly(onMaybeAssemblySource);
RxJavaPlugins.setOnObservableAssembly(onObservableAssemblySource);
RxJavaPlugins.setOnFlowableAssembly(onFlowableAssemblySource);
lock.set(false);
}
}
private static <R> Function<? super R, ? extends R> compose(
Function<? super R, ? extends R> before,
Function<? super R, ? extends R> after) {
if (before == null) {
return after;
}
return (R v) -> after.apply(before.apply(v));
}
private static class SingleOnAssemblyScheduled<T> extends Single<T> {
final Context context;
final SingleSource<T> source;
SingleOnAssemblyScheduled(Context context, SingleSource<T> source) {
this.context = context;
this.source = source;
}
@Override
protected void subscribeActual(SingleObserver<? super T> observer) {
try (Scope ignore = context.makeCurrent()) {
source.subscribe(observer);
}
}
}
private static class ObservableOnAssemblyScheduled<T> extends Observable<T> {
final Context context;
final ObservableSource<T> source;
ObservableOnAssemblyScheduled(Context context, ObservableSource<T> source) {
this.context = context;
this.source = source;
}
@Override
protected void subscribeActual(Observer<? super T> observer) {
try (Scope ignore = context.makeCurrent()) {
source.subscribe(observer);
}
}
}
private static class CompletableOnAssemblyScheduled extends Completable {
final Context context;
final CompletableSource source;
CompletableOnAssemblyScheduled(Context context, CompletableSource source) {
this.context = context;
this.source = source;
}
@Override
protected void subscribeActual(CompletableObserver observer) {
try (Scope ignore = context.makeCurrent()) {
source.subscribe(observer);
}
}
}
private static class MaybeOnAssemblyScheduled<T> extends Maybe<T> {
final Context context;
final MaybeSource<T> source;
MaybeOnAssemblyScheduled(Context context, MaybeSource<T> source) {
this.context = context;
this.source = source;
}
@Override
protected void subscribeActual(MaybeObserver<? super T> observer) {
try (Scope ignore = context.makeCurrent()) {
source.subscribe(observer);
}
}
}
private static class FlowableOnAssemblyScheduled<T> extends Flowable<T> {
final Context context;
final Flowable<T> source;
FlowableOnAssemblyScheduled(Context context, Flowable<T> source) {
this.context = context;
this.source = source;
}
@Override
protected void subscribeActual(Subscriber<? super T> observer) {
try (Scope ignore = context.makeCurrent()) {
source.subscribe(observer);
}
}
}
}