Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Muzzle match only once in each class loader #4543

Merged
merged 4 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ AgentBuilder install(
private static class MuzzleMatcher implements AgentBuilder.RawMatcher {
private final InstrumentationModule instrumentationModule;
private final AtomicBoolean initialized = new AtomicBoolean(false);
private final Cache<ClassLoader, Boolean> matchingResult =
Cache.builder().setWeakKeys().build();
private final Cache<ClassLoader, Boolean> matchCache = Cache.builder().setWeakKeys().build();
private volatile ReferenceMatcher referenceMatcher;

private MuzzleMatcher(InstrumentationModule instrumentationModule) {
Expand All @@ -140,16 +139,14 @@ public boolean matches(
JavaModule module,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain) {
ReferenceMatcher muzzle = getReferenceMatcher();
if (classLoader == BOOTSTRAP_LOADER) {
classLoader = Utils.getBootstrapProxy();
}
return matchCache.computeIfAbsent(classLoader, this::doesMatch);
}

Boolean cachedResult = matchingResult.get(classLoader);
if (cachedResult != null) {
return cachedResult.booleanValue();
}

private boolean doesMatch(ClassLoader classLoader) {
ReferenceMatcher muzzle = getReferenceMatcher();
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
boolean isMatch = muzzle.matches(classLoader);

if (!isMatch) {
Expand All @@ -174,8 +171,6 @@ public boolean matches(
}
}

matchingResult.put(classLoader, isMatch);

return isMatch;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

import io.opentelemetry.instrumentation.api.caching.Cache;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.muzzle.references.ClassRef;
import io.opentelemetry.javaagent.tooling.muzzle.references.FieldRef;
Expand All @@ -31,7 +30,6 @@
/** Matches a set of references against a classloader. */
public final class ReferenceMatcher {

private final Cache<ClassLoader, Boolean> mismatchCache = Cache.builder().setWeakKeys().build();
private final Map<String, ClassRef> references;
private final Set<String> helperClassNames;
private final HelperClassPredicate helperClassPredicate;
Expand All @@ -55,16 +53,11 @@ public static ReferenceMatcher of(InstrumentationModule instrumentationModule) {
/**
* Matcher used by ByteBuddy. Fails fast and only caches empty results, or complete results
*
* @param userClassLoader Classloader to validate against (cannot be {@code null}, must pass
* "bootstrap proxy" instead of bootstrap class loader)
* @param loader Classloader to validate against (cannot be {@code null}, must pass "bootstrap
* proxy" instead of bootstrap class loader)
* @return true if all references match the classpath of loader
*/
public boolean matches(ClassLoader userClassLoader) {
return mismatchCache.computeIfAbsent(userClassLoader, this::doesMatch);
}

// loader cannot be null, must pass "bootstrap proxy" instead of bootstrap class loader
private boolean doesMatch(ClassLoader loader) {
public boolean matches(ClassLoader loader) {
TypePool typePool = createTypePool(loader);
for (ClassRef reference : references.values()) {
if (!checkMatch(reference, typePool, loader).isEmpty()) {
Expand Down