Skip to content

Commit

Permalink
IndyProxy > InstrumentationProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Nov 27, 2024
1 parent bf1beca commit 9def1a6
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import instrumentation.TestHelperClass;
import io.opentelemetry.javaagent.bootstrap.IndyProxy;
import io.opentelemetry.javaagent.bootstrap.InstrumentationProxy;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldAccessorMarker;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldInstalledMarker;
import java.io.ObjectStreamClass;
Expand Down Expand Up @@ -57,7 +57,7 @@ void testInjectedClassProxyUnwrap() throws Exception {
.isNotNull();

Object instance = helperType.getConstructor().newInstance();
if (IndyProxy.class.isAssignableFrom(helperType)) {
if (InstrumentationProxy.class.isAssignableFrom(helperType)) {
// indy advice: must be an indy proxy

for (Method method : helperType.getMethods()) {
Expand All @@ -69,18 +69,20 @@ void testInjectedClassProxyUnwrap() throws Exception {
for (Class<?> interfaceType : helperType.getInterfaces()) {
assertThat(interfaceType)
.describedAs("indy proxy interface must be hidden from reflection")
.isNotEqualTo(IndyProxy.class);
.isNotEqualTo(InstrumentationProxy.class);
}

assertThat(instance).isInstanceOf(IndyProxy.class);
assertThat(instance).isInstanceOf(InstrumentationProxy.class);

Object proxyDelegate = ((IndyProxy) instance).__getIndyProxyDelegate();
assertThat(proxyDelegate).isNotInstanceOf(IndyProxy.class);
Object proxyDelegate = ((InstrumentationProxy) instance).__getIndyProxyDelegate();
assertThat(proxyDelegate).isNotInstanceOf(InstrumentationProxy.class);

} else {
// inline advice: must be of the expected type
assertThat(helperType).isEqualTo(TestHelperClass.class);
assertThat(instance).isInstanceOf(TestHelperClass.class).isNotInstanceOf(IndyProxy.class);
assertThat(instance)
.isInstanceOf(TestHelperClass.class)
.isNotInstanceOf(InstrumentationProxy.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.internal.reflection;

import io.opentelemetry.javaagent.bootstrap.IndyProxy;
import io.opentelemetry.javaagent.bootstrap.InstrumentationProxy;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldAccessorMarker;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldDetector;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldInstalledMarker;
Expand Down Expand Up @@ -71,7 +71,7 @@ public static Class<?>[] filterInterfaces(Class<?>[] interfaces, Class<?> contai
// filter out virtual field marker and accessor interfaces
if (interfaceClass == VirtualFieldInstalledMarker.class) {
continue;
} else if (interfaceClass == IndyProxy.class) {
} else if (interfaceClass == InstrumentationProxy.class) {
continue;
} else if (VirtualFieldAccessorMarker.class.isAssignableFrom(interfaceClass)
&& interfaceClass.isSynthetic()
Expand All @@ -89,6 +89,6 @@ public static Class<?>[] filterInterfaces(Class<?>[] interfaces, Class<?> contai

private static boolean noInterfaceToHide(Class<?> containingClass) {
return !VirtualFieldInstalledMarker.class.isAssignableFrom(containingClass)
&& !IndyProxy.class.isAssignableFrom(containingClass);
&& !InstrumentationProxy.class.isAssignableFrom(containingClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.IndyProxyHelper;
import io.opentelemetry.javaagent.bootstrap.InstrumentationProxyHelper;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.List;
Expand Down Expand Up @@ -68,7 +68,7 @@ public static void afterRefresh(
}
Object bean = springCtx.getBean("otelAutoDispatcherFilter");
OpenTelemetryHandlerMappingFilter filter =
IndyProxyHelper.unwrapIfNeeded(bean, OpenTelemetryHandlerMappingFilter.class);
InstrumentationProxyHelper.unwrapIfNeeded(bean, OpenTelemetryHandlerMappingFilter.class);
filter.setHandlerMappings(handlerMappings);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.IndyProxyHelper;
import io.opentelemetry.javaagent.bootstrap.InstrumentationProxyHelper;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.List;
Expand Down Expand Up @@ -69,7 +69,7 @@ public static void afterRefresh(

Object bean = springCtx.getBean("otelAutoDispatcherFilter");
OpenTelemetryHandlerMappingFilter filter =
IndyProxyHelper.unwrapIfNeeded(bean, OpenTelemetryHandlerMappingFilter.class);
InstrumentationProxyHelper.unwrapIfNeeded(bean, OpenTelemetryHandlerMappingFilter.class);
filter.setHandlerMappings(handlerMappings);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package io.opentelemetry.javaagent.bootstrap;

/** Interface added to indy proxies to allow unwrapping the proxy object */
public interface IndyProxy {
public interface InstrumentationProxy {

/**
* Unwraps the proxy delegate instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package io.opentelemetry.javaagent.bootstrap;

public class IndyProxyHelper {
public class InstrumentationProxyHelper {

private IndyProxyHelper() {}
private InstrumentationProxyHelper() {}

/**
* Unwraps and casts an indy proxy, or just casts if it's not an indy proxy.
Expand All @@ -21,8 +21,8 @@ private IndyProxyHelper() {}
* expected type
*/
public static <T> T unwrapIfNeeded(Object o, Class<T> type) {
if (o instanceof IndyProxy) {
Object delegate = ((IndyProxy) o).__getIndyProxyDelegate();
if (o instanceof InstrumentationProxy) {
Object delegate = ((InstrumentationProxy) o).__getIndyProxyDelegate();
if (type.isAssignableFrom(delegate.getClass())) {
return type.cast(delegate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@

import org.junit.jupiter.api.Test;

class IndyProxyHelperTest {
class InstrumentationProxyHelperTest {

@Test
void wrongType() {
assertThrows(
IllegalArgumentException.class, () -> IndyProxyHelper.unwrapIfNeeded("", Integer.class));
IllegalArgumentException.class,
() -> InstrumentationProxyHelper.unwrapIfNeeded("", Integer.class));
assertThrows(
IllegalArgumentException.class,
() -> IndyProxyHelper.unwrapIfNeeded(proxy(""), Integer.class));
() -> InstrumentationProxyHelper.unwrapIfNeeded(proxy(""), Integer.class));
}

@Test
void unwrap() {

// no wrapping
Number number = IndyProxyHelper.unwrapIfNeeded(42, Number.class);
Number number = InstrumentationProxyHelper.unwrapIfNeeded(42, Number.class);
assertThat(number).isEqualTo(42);

// unwrap needed
String string = IndyProxyHelper.unwrapIfNeeded(proxy("hello"), String.class);
String string = InstrumentationProxyHelper.unwrapIfNeeded(proxy("hello"), String.class);
assertThat(string).isEqualTo("hello");
}

private static IndyProxy proxy(Object delegate) {
private static InstrumentationProxy proxy(Object delegate) {
return () -> delegate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.javaagent.tooling.instrumentation.indy;

import io.opentelemetry.javaagent.bootstrap.IndyProxy;
import io.opentelemetry.javaagent.bootstrap.InstrumentationProxy;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand Down Expand Up @@ -96,7 +96,7 @@ public DynamicType.Unloaded<?> generateProxy(
TypeDescription classToProxy, String proxyClassName) {
TypeDescription.Generic superClass = classToProxy.getSuperClass();
List<TypeDefinition> interfaces = new ArrayList<>(classToProxy.getInterfaces());
interfaces.add(TypeDescription.ForLoadedType.of(IndyProxy.class));
interfaces.add(TypeDescription.ForLoadedType.of(InstrumentationProxy.class));
DynamicType.Builder<?> builder =
new ByteBuddy()
.subclass(superClass, ConstructorStrategy.Default.NO_CONSTRUCTORS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.javaagent.bootstrap.IndyProxy;
import io.opentelemetry.javaagent.bootstrap.InstrumentationProxy;
import io.opentelemetry.javaagent.tooling.instrumentation.indy.dummies.DummyAnnotation;
import java.lang.invoke.CallSite;
import java.lang.invoke.ConstantCallSite;
Expand Down Expand Up @@ -329,14 +329,16 @@ void verifyNonPublicMembersIgnored() {
@Test
void verifyProxyClass() throws Exception {
Class<?> proxyType = generateProxy(ProxyUnwrapTest.class);
assertThat(proxyType).isNotInstanceOf(IndyProxy.class).isNotInstanceOf(ProxyUnwrapTest.class);
assertThat(proxyType)
.isNotInstanceOf(InstrumentationProxy.class)
.isNotInstanceOf(ProxyUnwrapTest.class);

assertThat(IndyProxy.class.isAssignableFrom(proxyType))
assertThat(InstrumentationProxy.class.isAssignableFrom(proxyType))
.describedAs("proxy class can be cast to IndyProxy")
.isTrue();

Object proxyInstance = proxyType.getConstructor().newInstance();
Object proxyDelegate = ((IndyProxy) proxyInstance).__getIndyProxyDelegate();
Object proxyDelegate = ((InstrumentationProxy) proxyInstance).__getIndyProxyDelegate();
assertThat(proxyDelegate).isInstanceOf(ProxyUnwrapTest.class);
}

Expand Down

0 comments on commit 9def1a6

Please sign in to comment.