|
| 1 | +package datadog.trace.bootstrap; |
| 2 | + |
| 3 | +import java.lang.invoke.MethodHandle; |
| 4 | +import java.lang.invoke.MethodHandles; |
| 5 | +import java.lang.invoke.MethodType; |
| 6 | + |
| 7 | +/** |
| 8 | + * Thread safe wrapper around BootstrapInitializationTelemetry used inside the Datadog ClassLoader. |
| 9 | + * |
| 10 | + * <p>Right now, this is needed because of the build separation between the two portions of the |
| 11 | + * bootstrap. We should consider adjusting the build to allow Agent et al to reference |
| 12 | + * BootstrapInitializationTelemetry, then we could remove this proxy. |
| 13 | + */ |
| 14 | +public abstract class InitializationTelemetry { |
| 15 | + /** Returns a proxy around a BoostrapInitializationTelemetry object */ |
| 16 | + public static final InitializationTelemetry proxy(Object bootstrapInitTelemetry) { |
| 17 | + if (bootstrapInitTelemetry == null) { |
| 18 | + return InitializationTelemetry.noOpInstance(); |
| 19 | + } else { |
| 20 | + return new BootstrapProxy(bootstrapInitTelemetry); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /** Returns a singleton of the no op InitializationTelemetry */ |
| 25 | + public static final InitializationTelemetry noOpInstance() { |
| 26 | + return NoOp.INSTANCE; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Indicates that an abort condition occurred during the bootstrapping process. Abort conditions |
| 31 | + * are assumed to leave the bootstrapping process incomplete. {@link #markIncomplete()} |
| 32 | + */ |
| 33 | + public abstract void onAbort(String reasonCode); |
| 34 | + |
| 35 | + /** |
| 36 | + * Indicates that an exception occurred during the bootstrapping process By default the exception |
| 37 | + * is assumed to NOT have fully stopped the initialization of the tracer. |
| 38 | + * |
| 39 | + * <p>If this exception stops the core bootstrapping of the tracer, then {@link #markIncomplete()} |
| 40 | + * should also be called. |
| 41 | + */ |
| 42 | + public abstract void onError(Throwable t); |
| 43 | + |
| 44 | + /** |
| 45 | + * Indicates an exception that occurred during the bootstrapping process that left initialization |
| 46 | + * incomplete. Equivalent to calling {@link #onError(Throwable)} and {@link #markIncomplete()} |
| 47 | + */ |
| 48 | + public abstract void onFatalError(Throwable t); |
| 49 | + |
| 50 | + /** |
| 51 | + * Indicates that an exception conditional occurred during the bootstrapping process. By default |
| 52 | + * the exceptional condition is assumed to NOT have fully stopped the initialization of the |
| 53 | + * tracer. |
| 54 | + * |
| 55 | + * <p>If this exception stops the core bootstrapping of the tracer, then {@link #markIncomplete()} |
| 56 | + * should also be called. |
| 57 | + */ |
| 58 | + public abstract void onError(String reasonCode); |
| 59 | + |
| 60 | + /** |
| 61 | + * Marks bootstrapping of tracer as an incomplete Should only be called when a core (e.g. |
| 62 | + * non-optional) component fails to initialize |
| 63 | + */ |
| 64 | + public abstract void markIncomplete(); |
| 65 | + |
| 66 | + /** No telemetry - used for delayed initialization outside bootstrap invocation */ |
| 67 | + static final class NoOp extends InitializationTelemetry { |
| 68 | + static final NoOp INSTANCE = new NoOp(); |
| 69 | + |
| 70 | + NoOp() {} |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onAbort(String reasonCode) {} |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onError(String reasonCode) {} |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onError(Throwable t) {} |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onFatalError(Throwable t) {} |
| 83 | + |
| 84 | + @Override |
| 85 | + public void markIncomplete() {} |
| 86 | + } |
| 87 | + |
| 88 | + /** Reflective proxy to BootstrapInitializationTelemetry */ |
| 89 | + static final class BootstrapProxy extends InitializationTelemetry { |
| 90 | + private final Object bootstrapInitTelemetry; |
| 91 | + private volatile MethodHandle bmh_onAbortString; |
| 92 | + private volatile MethodHandle bmh_onErrorString; |
| 93 | + private volatile MethodHandle bmh_onErrorThrowable; |
| 94 | + private volatile MethodHandle bmh_onFatalErrorThrowable; |
| 95 | + private volatile MethodHandle bmh_markIncomplete; |
| 96 | + |
| 97 | + // DQH - Decided not to eager access MethodHandles, since exceptions are uncommon |
| 98 | + // However, MethodHandles are cached on lookup |
| 99 | + |
| 100 | + /** @param bootstrapInitTelemetry - non-null BootstrapInitializationTelemetry */ |
| 101 | + BootstrapProxy(final Object bootstrapInitTelemetry) { |
| 102 | + this.bootstrapInitTelemetry = bootstrapInitTelemetry; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onAbort(String reasonCode) { |
| 107 | + MethodHandle bmh_onAbortString = this.bmh_onAbortString; |
| 108 | + if (bmh_onAbortString == null) { |
| 109 | + bmh_onAbortString = findBoundHandle("onAbort", String.class); |
| 110 | + this.bmh_onAbortString = bmh_onAbortString; |
| 111 | + } |
| 112 | + if (bmh_onAbortString != null) { |
| 113 | + try { |
| 114 | + bmh_onAbortString.invokeExact(reasonCode); |
| 115 | + } catch (Throwable t) { |
| 116 | + // ignore |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void onError(String reasonCode) { |
| 123 | + MethodHandle bmh_onErrorString = this.bmh_onErrorString; |
| 124 | + if (bmh_onErrorString == null) { |
| 125 | + bmh_onErrorString = findBoundHandle("onError", String.class); |
| 126 | + this.bmh_onErrorString = bmh_onErrorString; |
| 127 | + } |
| 128 | + if (bmh_onErrorString != null) { |
| 129 | + try { |
| 130 | + bmh_onErrorString.invokeExact(reasonCode); |
| 131 | + } catch (Throwable t) { |
| 132 | + // ignore |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void onError(Throwable cause) { |
| 139 | + MethodHandle bmh_onErrorThrowable = this.bmh_onErrorThrowable; |
| 140 | + if (bmh_onErrorThrowable == null) { |
| 141 | + bmh_onErrorThrowable = findBoundHandle("onError", Throwable.class); |
| 142 | + this.bmh_onErrorThrowable = bmh_onErrorThrowable; |
| 143 | + } |
| 144 | + if (bmh_onErrorThrowable != null) { |
| 145 | + try { |
| 146 | + bmh_onErrorThrowable.invokeExact(cause); |
| 147 | + } catch (Throwable t) { |
| 148 | + // ignore |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void onFatalError(Throwable cause) { |
| 155 | + MethodHandle bmh_onFatalErrorThrowable = this.bmh_onFatalErrorThrowable; |
| 156 | + if (bmh_onFatalErrorThrowable == null) { |
| 157 | + bmh_onFatalErrorThrowable = findBoundHandle("onFatalError", Throwable.class); |
| 158 | + this.bmh_onFatalErrorThrowable = bmh_onFatalErrorThrowable; |
| 159 | + } |
| 160 | + if (bmh_onFatalErrorThrowable != null) { |
| 161 | + try { |
| 162 | + bmh_onFatalErrorThrowable.invokeExact(cause); |
| 163 | + } catch (Throwable t) { |
| 164 | + // ignore |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void markIncomplete() { |
| 171 | + MethodHandle bmh_markIncomplete = this.bmh_markIncomplete; |
| 172 | + if (bmh_markIncomplete == null) { |
| 173 | + bmh_markIncomplete = findBoundHandle("markIncomplete"); |
| 174 | + this.bmh_markIncomplete = bmh_markIncomplete; |
| 175 | + } |
| 176 | + if (bmh_markIncomplete != null) { |
| 177 | + try { |
| 178 | + bmh_markIncomplete.invokeExact(); |
| 179 | + } catch (Throwable t) { |
| 180 | + // ignore |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private final MethodHandle findBoundHandle(String name, Class<?> paramType) { |
| 186 | + try { |
| 187 | + MethodHandle virtualHandle = |
| 188 | + MethodHandles.publicLookup() |
| 189 | + .findVirtual( |
| 190 | + bootstrapInitTelemetry.getClass(), |
| 191 | + name, |
| 192 | + MethodType.methodType(void.class, paramType)); |
| 193 | + |
| 194 | + return virtualHandle.bindTo(bootstrapInitTelemetry); |
| 195 | + } catch (NoSuchMethodException | IllegalAccessException e) { |
| 196 | + return null; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private final MethodHandle findBoundHandle(String name) { |
| 201 | + try { |
| 202 | + MethodHandle virtualHandle = |
| 203 | + MethodHandles.publicLookup() |
| 204 | + .findVirtual( |
| 205 | + bootstrapInitTelemetry.getClass(), name, MethodType.methodType(void.class)); |
| 206 | + |
| 207 | + return virtualHandle.bindTo(bootstrapInitTelemetry); |
| 208 | + } catch (NoSuchMethodException | IllegalAccessException e) { |
| 209 | + return null; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments