Skip to content
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 @@ -39,6 +39,7 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.annotation.AnnotationSource;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -118,7 +119,17 @@ private AgentBuilder installIndyModule(
.injectClasses(injectedClassesCollector);
}

MuzzleMatcher muzzleMatcher = new MuzzleMatcher(logger, instrumentationModule);
MuzzleMatcher muzzleMatcher =
new MuzzleMatcher(
logger,
instrumentationModule,
cl ->
// In indy modules muzzle searches for types in both application class loader and in
// the agent class loader. Since we allow using agent class in indy instrumentation
// these classes are treated as regular non-helper classes for which muzzle performs
// reference checks.
IndyModuleRegistry.createInstrumentationClassLoaderForMuzzle(
instrumentationModule, cl));

Function<ClassLoader, List<HelperClassDefinition>> helperGenerator =
cl -> {
Expand Down Expand Up @@ -187,7 +198,8 @@ private AgentBuilder installInjectingModule(
return parentAgentBuilder;
}

MuzzleMatcher muzzleMatcher = new MuzzleMatcher(logger, instrumentationModule);
MuzzleMatcher muzzleMatcher =
new MuzzleMatcher(logger, instrumentationModule, UnaryOperator.identity());
AgentBuilder.Transformer helperInjector =
new HelperInjector(
instrumentationModule.instrumentationName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
import io.opentelemetry.javaagent.tooling.TransformSafeLogger;
import io.opentelemetry.javaagent.tooling.Utils;
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
import io.opentelemetry.javaagent.tooling.instrumentation.indy.IndyModuleRegistry;
import io.opentelemetry.javaagent.tooling.instrumentation.indy.InstrumentationModuleClassLoader;
import io.opentelemetry.javaagent.tooling.muzzle.Mismatch;
import io.opentelemetry.javaagent.tooling.muzzle.ReferenceMatcher;
import java.security.ProtectionDomain;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.UnaryOperator;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.bytebuddy.agent.builder.AgentBuilder;
Expand All @@ -38,15 +37,19 @@ class MuzzleMatcher implements AgentBuilder.RawMatcher {

private final TransformSafeLogger instrumentationLogger;
private final InstrumentationModule instrumentationModule;
private final UnaryOperator<ClassLoader> classLoaderTransformer;
private final Level muzzleLogLevel;
private final AtomicBoolean initialized = new AtomicBoolean(false);
private final Cache<ClassLoader, Boolean> matchCache = Cache.weak();
private volatile ReferenceMatcher referenceMatcher;

MuzzleMatcher(
TransformSafeLogger instrumentationLogger, InstrumentationModule instrumentationModule) {
TransformSafeLogger instrumentationLogger,
InstrumentationModule instrumentationModule,
UnaryOperator<ClassLoader> classLoaderTransformer) {
this.instrumentationLogger = instrumentationLogger;
this.instrumentationModule = instrumentationModule;
this.classLoaderTransformer = classLoaderTransformer;
this.muzzleLogLevel = EarlyInitAgentConfig.get().isDebug() ? WARNING : FINE;
}

Expand All @@ -60,18 +63,7 @@ public boolean matches(
if (classLoader == BOOTSTRAP_LOADER) {
classLoader = Utils.getBootstrapProxy();
}
if (instrumentationModule.isIndyModule()) {
return matchCache.computeIfAbsent(
classLoader,
cl -> {
InstrumentationModuleClassLoader moduleCl =
IndyModuleRegistry.createInstrumentationClassLoaderWithoutRegistration(
instrumentationModule, cl);
return doesMatch(moduleCl);
});
} else {
return matchCache.computeIfAbsent(classLoader, this::doesMatch);
}
return matchCache.computeIfAbsent(classLoaderTransformer.apply(classLoader), this::doesMatch);
}

private boolean doesMatch(ClassLoader classLoader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ public static InstrumentationModuleClassLoader getInstrumentationClassLoader(
* modules from the same module group (see {@link #getModuleGroup(InstrumentationModule)}) will
* not be installed in this class loader.
*/
public static InstrumentationModuleClassLoader
createInstrumentationClassLoaderWithoutRegistration(
InstrumentationModule module, ClassLoader instrumentedClassLoader) {
public static InstrumentationModuleClassLoader createInstrumentationClassLoaderForMuzzle(
InstrumentationModule module, ClassLoader instrumentedClassLoader) {
// TODO: remove this method and replace usages with a custom TypePool implementation instead
ClassLoader agentOrExtensionCl = module.getClass().getClassLoader();
InstrumentationModuleClassLoader cl =
Expand Down
Loading