More error catching in safe matcher and reduce log level

This commit is contained in:
Andrew Kent 2018-08-02 22:30:07 -07:00
parent 75a6cee068
commit 35c6fa2343
3 changed files with 47 additions and 18 deletions

View File

@ -2,13 +2,15 @@ package datadog.trace.agent.tooling;
import static net.bytebuddy.matcher.ElementMatchers.erasure;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.build.HashCodeAndEqualsPlugin;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
@ -89,7 +91,7 @@ public class ByteBuddyElementMatchers {
// in {@code getSuperClass} calls
TypeDefinition typeDefinition = target;
while (typeDefinition != null) {
if (matcher.matches(typeDefinition.asGenericType())
if (safeMatches(typeDefinition.asGenericType())
|| hasInterface(typeDefinition, checkedInterfaces)) {
return true;
}
@ -98,11 +100,20 @@ public class ByteBuddyElementMatchers {
return false;
}
private boolean safeMatches(TypeDescription.Generic target) {
try {
return matcher.matches(target);
} catch (final Exception e) {
log.debug(matcher + ": Exception trying to get match generic type description:", e);
}
return false;
}
private TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
try {
return typeDefinition.getSuperClass();
} catch (final Exception e) {
log.info("Exception trying to get next type definition:", e);
log.debug("Exception trying to get next type definition:", e);
return null;
}
}
@ -118,7 +129,7 @@ public class ByteBuddyElementMatchers {
final TypeDefinition typeDefinition, final Set<TypeDescription> checkedInterfaces) {
for (final TypeDefinition interfaceType : safeGetInterfaces(typeDefinition)) {
if (checkedInterfaces.add(interfaceType.asErasure())
&& (matcher.matches(interfaceType.asGenericType())
&& (safeMatch(interfaceType.asGenericType())
|| hasInterface(interfaceType, checkedInterfaces))) {
return true;
}
@ -126,13 +137,27 @@ public class ByteBuddyElementMatchers {
return false;
}
private TypeList.Generic safeGetInterfaces(final TypeDefinition typeDefinition) {
private boolean safeMatch(TypeDescription.Generic target) {
try {
return typeDefinition.getInterfaces();
return matcher.matches(target);
} catch (final Exception e) {
log.info("Exception trying to get interfaces:", e);
return new TypeList.Generic.Empty();
log.debug(matcher + ": Exception while matching:", e);
}
return false;
}
private List<TypeDefinition> safeGetInterfaces(final TypeDefinition typeDefinition) {
final List<TypeDefinition> interfaceTypes = new ArrayList<>();
try {
final Iterator<TypeDescription.Generic> interfaceIter =
typeDefinition.getInterfaces().iterator();
while (interfaceIter.hasNext()) {
interfaceTypes.add(interfaceIter.next());
}
} catch (final Exception e) {
log.debug("Exception trying to get interfaces:", e);
}
return interfaceTypes;
}
@Override

View File

@ -9,22 +9,25 @@ import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.pool.TypePool;
public class DDCachingPoolStrategy implements PoolStrategy {
private static final Map<ClassLoader, TypePool> typePoolCache =
Collections.synchronizedMap(new WeakHashMap<ClassLoader, TypePool>());
private static final Map<ClassLoader, TypePool.CacheProvider> typePoolCache =
Collections.synchronizedMap(new WeakHashMap<ClassLoader, TypePool.CacheProvider>());
@Override
public TypePool typePool(ClassFileLocator classFileLocator, ClassLoader classLoader) {
final ClassLoader key = null == classLoader ? Utils.getBootstrapProxy() : classLoader;
TypePool cachedPool = typePoolCache.get(key);
if (null == cachedPool) {
TypePool.CacheProvider cache = typePoolCache.get(key);
if (null == cache) {
synchronized (key) {
cachedPool = typePoolCache.get(key);
if (null == cachedPool) {
cachedPool = Default.FAST.typePool(classFileLocator, classLoader);
typePoolCache.put(key, cachedPool);
cache = typePoolCache.get(key);
if (null == cache) {
// new TypePool.Default.WithLazyResolution(TypePool.CacheProvider.Simple.withObjectType(),
// classFileLocator, TypePool.Default.ReaderMode.FAST);
cache = TypePool.CacheProvider.Simple.withObjectType();
typePoolCache.put(key, cache);
}
}
}
return cachedPool;
return new TypePool.Default.WithLazyResolution(
cache, classFileLocator, TypePool.Default.ReaderMode.FAST);
}
}

View File

@ -140,7 +140,8 @@ public abstract class AgentTestRunner extends Specification {
@After
public void afterTest() {
ERROR_LISTENER.deactivateTest(this);
assert INSTRUMENTATION_ERROR_COUNT.get() == 0 : "Instrumentation errors during test";
assert INSTRUMENTATION_ERROR_COUNT.get() == 0
: INSTRUMENTATION_ERROR_COUNT.get() + " Instrumentation errors during test";
}
@AfterClass