|
7 | 7 |
|
8 | 8 | import static io.opentelemetry.instrumentation.netty.v4_1.internal.client.HttpClientRequestTracingHandler.HTTP_CLIENT_REQUEST;
|
9 | 9 |
|
10 |
| -import com.twitter.finagle.context.Contexts; |
11 |
| -import com.twitter.finagle.context.LocalContext; |
12 |
| -import com.twitter.util.Future; |
13 | 10 | import com.twitter.util.Local;
|
14 | 11 | import io.netty.channel.Channel;
|
15 | 12 | import io.netty.channel.ChannelHandler;
|
|
25 | 22 | import io.opentelemetry.instrumentation.netty.v4_1.internal.server.HttpServerTracingHandler;
|
26 | 23 | import io.opentelemetry.javaagent.instrumentation.netty.v4_1.NettyHttpServerResponseBeforeCommitHandler;
|
27 | 24 | import io.opentelemetry.javaagent.instrumentation.netty.v4_1.NettyServerSingletons;
|
28 |
| -import java.lang.invoke.MethodHandle; |
29 | 25 | import java.util.Deque;
|
30 |
| -import java.util.concurrent.Callable; |
31 |
| -import scala.Function0; |
32 |
| -import scala.Option; |
33 | 26 |
|
34 | 27 | public class Helpers {
|
35 | 28 |
|
36 | 29 | private Helpers() {}
|
37 | 30 |
|
38 |
| - public static final LocalCallGuard LOOP_GUARD = new LocalCallGuard(); |
39 |
| - |
40 |
| - public static final LocalCallGuard WRITE_GUARD = new LocalCallGuard(); |
41 |
| - |
42 |
| - // used for finagle's LocalContext: carries a reference to the Context observed at the start |
43 |
| - // of the server request processing or the client Service call |
44 |
| - static class ContextRef { |
45 |
| - |
46 |
| - private final LocalContext.Key<Context> key; |
47 |
| - public static final ContextRef INSTANCE = new ContextRef(Contexts.local().newKey()); |
48 |
| - |
49 |
| - private ContextRef(LocalContext.Key<Context> var1) { |
50 |
| - this.key = var1; |
51 |
| - } |
52 |
| - |
53 |
| - public LocalContext.Key<Context> getKey() { |
54 |
| - return this.key; |
55 |
| - } |
56 |
| - } |
57 |
| - |
58 |
| - // uses twitter util's Local bc the finagle/twitter stack is essentially incompatible with |
59 |
| - // java-native TLVs; |
60 |
| - // as these are referenced across twitter Future compositions, no assumptions are made and they |
61 |
| - // are used to adhere strictly to the interface, allowing the guard test to succeed at execution |
62 |
| - // time |
63 |
| - public static class LocalCallGuard { |
64 |
| - |
65 |
| - private final Local<Object> guard = new Local<>(); |
66 |
| - |
67 |
| - // is the current thread presently inside a call guarded by this LocalCallGuard? |
68 |
| - public boolean isRecursed() { |
69 |
| - return guard.apply().isDefined(); |
70 |
| - } |
71 |
| - |
72 |
| - // safely guard the Function0 call; |
73 |
| - // (Function0 -> Java Supplier, in our cases -- it wraps existing calls) |
74 |
| - public <T> T guardedCall(Function0<T> f, T defaultVal) { |
75 |
| - if (isRecursed()) { |
76 |
| - return defaultVal; |
77 |
| - } |
78 |
| - return guard.let(null, f); |
79 |
| - } |
80 |
| - } |
81 |
| - |
82 |
| - @SuppressWarnings({"ThrowSpecificExceptions", "CheckedExceptionNotThrown"}) |
83 |
| - public static Future<?> loopAdviceExit(MethodHandle handle, Future<?> ret) { |
84 |
| - return LOOP_GUARD.guardedCall( |
85 |
| - () -> |
86 |
| - Contexts.local() |
87 |
| - .let( |
88 |
| - ContextRef.INSTANCE.getKey(), |
89 |
| - // this works bc at this point in the server evaluation, the netty |
90 |
| - // instrumentation has already gone to work and assigned the context to the |
91 |
| - // local thread; |
92 |
| - // |
93 |
| - // this works specifically in finagle's netty stack bc at this point the loop() |
94 |
| - // method is running on a netty thread with the necessary access to the |
95 |
| - // java-native ThreadLocal where the Context is stored |
96 |
| - Context.current(), |
97 |
| - () -> { |
98 |
| - try { |
99 |
| - // all access to Context.current() from this point forward should now |
100 |
| - // succeed as expected |
101 |
| - return (Future<?>) handle.invoke(); |
102 |
| - } catch (Throwable e) { |
103 |
| - throw new RuntimeException(e); |
104 |
| - } |
105 |
| - }), |
106 |
| - ret); |
107 |
| - } |
108 |
| - |
109 |
| - @SuppressWarnings("ThrowSpecificExceptions") |
110 |
| - public static Future<?> callWrite(MethodHandle handle, Future<?> ret) { |
111 |
| - return WRITE_GUARD.guardedCall( |
112 |
| - () -> { |
113 |
| - Option<Context> ref = Contexts.local().get(ContextRef.INSTANCE.getKey()); |
114 |
| - Callable<Future<?>> call = |
115 |
| - () -> { |
116 |
| - try { |
117 |
| - return (Future<?>) handle.invoke(); |
118 |
| - } catch (Exception e) { |
119 |
| - // don't wrap needlessly |
120 |
| - throw e; |
121 |
| - } catch (Throwable e) { |
122 |
| - throw new RuntimeException(e); |
123 |
| - } |
124 |
| - }; |
125 |
| - if (ref.isDefined()) { |
126 |
| - // wrap the call if ContextRef contains a set Context |
127 |
| - call = ref.get().wrap(call); |
128 |
| - } |
129 |
| - try { |
130 |
| - return call.call(); |
131 |
| - } catch (Exception e) { |
132 |
| - throw new RuntimeException(e); |
133 |
| - } |
134 |
| - }, |
135 |
| - ret); |
136 |
| - } |
| 31 | + public static final Local<Context> CONTEXT_LOCAL = new Local<>(); |
137 | 32 |
|
138 | 33 | public static <C extends Channel> ChannelInitializer<C> wrapServer(ChannelInitializer<C> inner) {
|
139 | 34 | return new ChannelInitializerDelegate<C>(inner) {
|
|
0 commit comments