Skip to content

Commit

Permalink
Disable matcher cache for running muzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Mar 17, 2023
1 parent bb929f5 commit 153b713
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class MuzzleGradlePluginUtil {

val matcherClass = agentClassLoader.loadClass("io.opentelemetry.javaagent.tooling.muzzle.ClassLoaderMatcher")

// We cannot reference Mismatch class directly here, because we are loaded from a different
// class loader.

// We cannot reference Mismatch class directly here, because we are loaded from a different
// class loader.
val allMismatches = matcherClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import net.bytebuddy.matcher.ElementMatcher;

class ClassLoaderHasClassesNamedMatcher extends ElementMatcher.Junction.AbstractBase<ClassLoader> {
// caching is disabled for build time muzzle checks
// this field is set via reflection from ClassLoaderMatcher
static boolean useCache = true;
private static final AtomicInteger counter = new AtomicInteger();

private final String[] resources;
Expand All @@ -25,7 +28,9 @@ class ClassLoaderHasClassesNamedMatcher extends ElementMatcher.Junction.Abstract
for (int i = 0; i < resources.length; i++) {
resources[i] = resources[i].replace(".", "/") + ".class";
}
Manager.INSTANCE.add(this);
if (useCache) {
Manager.INSTANCE.add(this);
}
}

@Override
Expand All @@ -34,7 +39,11 @@ public boolean matches(ClassLoader cl) {
// Can't match the bootstrap class loader.
return false;
}
return Manager.INSTANCE.match(this, cl);
if (useCache) {
return Manager.INSTANCE.match(this, cl);
} else {
return hasResources(cl, resources);
}
}

private static boolean hasResources(ClassLoader cl, String... resources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.HelperInjector;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -32,6 +33,8 @@ public class ClassLoaderMatcher {
*/
public static Map<String, List<Mismatch>> matchesAll(
ClassLoader classLoader, boolean injectHelpers, Set<String> excludedInstrumentationNames) {
disableMatcherCache();

Map<String, List<Mismatch>> result = new HashMap<>();
ServiceLoader.load(InstrumentationModule.class, ClassLoaderMatcher.class.getClassLoader())
.forEach(
Expand Down Expand Up @@ -101,5 +104,19 @@ private static List<Mismatch> checkHelperInjection(
return mismatches;
}

private static void disableMatcherCache() {
try {
Class<?> matcherClass =
Class.forName(
"io.opentelemetry.javaagent.extension.matcher.ClassLoaderHasClassesNamedMatcher");
Field field = matcherClass.getDeclaredField("useCache");
field.setAccessible(true);
field.setBoolean(null, false);
} catch (Exception exception) {
throw new IllegalStateException(
"Failed to disable cache for ClassLoaderHasClassesNamedMatcher", exception);
}
}

private ClassLoaderMatcher() {}
}

0 comments on commit 153b713

Please sign in to comment.