|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.jetty.httpclient.v12_0.internal; |
| 7 | + |
| 8 | +import io.opentelemetry.context.Context; |
| 9 | +import io.opentelemetry.context.Scope; |
| 10 | +import java.lang.reflect.InvocationTargetException; |
| 11 | +import java.lang.reflect.Proxy; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import org.eclipse.jetty.client.Response; |
| 15 | + |
| 16 | +/** |
| 17 | + * This class is internal and is hence not for public use. Its APIs are unstable and can change at |
| 18 | + * any time. |
| 19 | + */ |
| 20 | +public final class JettyClientWrapUtil { |
| 21 | + |
| 22 | + private JettyClientWrapUtil() {} |
| 23 | + |
| 24 | + /** |
| 25 | + * Utility to wrap the response listeners only, this includes the important CompleteListener. |
| 26 | + * |
| 27 | + * @param context top level context that is above the Jetty client span context |
| 28 | + * @param listener listener passed to Jetty client send() method |
| 29 | + * @return wrapped listener |
| 30 | + */ |
| 31 | + public static Response.CompleteListener wrapTheListener( |
| 32 | + Response.CompleteListener listener, Context context) { |
| 33 | + if (listener == null) { |
| 34 | + return listener; |
| 35 | + } |
| 36 | + |
| 37 | + Class<?> listenerClass = listener.getClass(); |
| 38 | + List<Class<?>> interfaces = new ArrayList<>(); |
| 39 | + for (Class<?> type : Response.Listener.class.getInterfaces()) { |
| 40 | + if (type.isInstance(listener)) { |
| 41 | + interfaces.add(type); |
| 42 | + } |
| 43 | + } |
| 44 | + if (interfaces.isEmpty()) { |
| 45 | + return listener; |
| 46 | + } |
| 47 | + |
| 48 | + return (Response.CompleteListener) |
| 49 | + Proxy.newProxyInstance( |
| 50 | + listenerClass.getClassLoader(), |
| 51 | + interfaces.toArray(new Class<?>[0]), |
| 52 | + (proxy, method, args) -> { |
| 53 | + try (Scope ignored = context.makeCurrent()) { |
| 54 | + return method.invoke(listener, args); |
| 55 | + } catch (InvocationTargetException exception) { |
| 56 | + throw exception.getCause(); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | +} |
0 commit comments