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 all commits
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 @@ -10,6 +10,7 @@
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;

import io.opentelemetry.instrumentation.api.caching.Cache;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.tooling.HelperInjector;
Expand Down Expand Up @@ -124,6 +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> matchCache = Cache.builder().setWeakKeys().build();
private volatile ReferenceMatcher referenceMatcher;

private MuzzleMatcher(InstrumentationModule instrumentationModule) {
Expand All @@ -137,10 +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);
}

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 Down Expand Up @@ -168,10 +174,8 @@ public boolean matches(
return isMatch;
}

// ReferenceMatcher internally caches the muzzle check results per classloader, that's why we
// keep its instance in a field
// it is lazily created to avoid unnecessarily loading the muzzle references from the module
// during the agent setup
// ReferenceMatcher is lazily created to avoid unnecessarily loading the muzzle references from
// the module during the agent setup
private ReferenceMatcher getReferenceMatcher() {
if (initialized.compareAndSet(false, true)) {
referenceMatcher = ReferenceMatcher.of(instrumentationModule);
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 @@ -53,18 +51,14 @@ public static ReferenceMatcher of(InstrumentationModule instrumentationModule) {
}

/**
* Matcher used by ByteBuddy. Fails fast and only caches empty results, or complete results
* Matcher used by ByteBuddy. Caller is expected to cache the result if this method is called
* multiple times for given class loader.
*
* @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 All @@ -75,7 +69,7 @@ private boolean doesMatch(ClassLoader loader) {
}

/**
* Loads the full list of mismatches. Used in debug contexts only
* Loads the full list of mismatches. Used in debug contexts only.
*
* @param loader Classloader to validate against (cannot be {@code null}, must pass "bootstrap *
* proxy" instead of bootstrap class loader)
Expand Down