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 field and method matching #422

Merged
merged 10 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ public void logrb(
String msg,
Throwable thrown) {}

public void severe(String msg) {}

public void warning(String msg) {}

public void info(String msg) {}

public void config(String msg) {}

public void fine(String msg) {}

public void finer(String msg) {}

public void finest(String msg) {}

public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {}

public void setLevel(Level newLevel) throws SecurityException {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

@Slf4j
public class AgentInstaller {
public static final DDLocationStrategy LOCATION_STRATEGY = new DDLocationStrategy();
private static volatile Instrumentation INSTRUMENTATION;

public static Instrumentation getInstrumentation() {
Expand All @@ -44,7 +45,7 @@ public static ResettableClassFileTransformer installBytebuddyAgent(
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentBuilder.DescriptionStrategy.Default.POOL_ONLY)
.with(new LoggingListener())
.with(new DDLocationStrategy())
.with(LOCATION_STRATEGY)
.ignore(any(), skipClassLoader())
.or(nameStartsWith("datadog.trace."))
.or(nameStartsWith("datadog.opentracing."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
* reached.
*/
public class DDLocationStrategy implements AgentBuilder.LocationStrategy {
public ClassFileLocator classFileLocator(ClassLoader classLoader) {
return classFileLocator(classLoader, null);
}

@Override
public ClassFileLocator classFileLocator(ClassLoader classLoader, final JavaModule javaModule) {
final List<ClassFileLocator> locators = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,23 @@ public boolean matches(
final List<Reference.Mismatch> mismatches =
muzzle.getMismatchedReferenceSources(classLoader);
if (mismatches.size() > 0) {
if (log.isDebugEnabled()) {
log.debug(
"Instrumentation muzzled: {} -- {} on {}",
instrumentationPrimaryName,
this.getClass().getName(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this thises would get wiped out by our current IDEA config - but I guess not by gradle formatter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing for consistency.

classLoader);
for (Reference.Mismatch mismatch : mismatches) {
log.debug("-- {}", mismatch);
}
}
} else {
log.debug(
"Instrumentation muzzled: {} -- {} on {}",
"Applying instrumentation: {} -- {} on {}",
instrumentationPrimaryName,
getClass().getName(),
this.getClass().getName(),
classLoader);
}
for (final Reference.Mismatch mismatch : mismatches) {
log.debug("-- {}", mismatch);
}
return mismatches.size() == 0;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datadog.trace.bootstrap.DatadogClassLoader;
import datadog.trace.bootstrap.DatadogClassLoader.BootstrapClassLoaderProxy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;

Expand Down Expand Up @@ -74,6 +75,30 @@ public static String getClassName(final String resourceName) {
return resourceName.replaceAll("\\.class\\$", "").replace('/', '.');
}

/** com.foo.Bar -> com/foo/Bar */
public static String getInternalName(final String resourceName) {
return resourceName.replaceAll("\\.class\\$", "").replace('.', '/');
}

public static boolean isClassLoaded(final String className, final ClassLoader classLoader) {
Class<?> loadedClass = findLoadedClass(className, classLoader);
return loadedClass != null && loadedClass.getClassLoader() == classLoader;
}

public static Class<?> findLoadedClass(final String className, ClassLoader classLoader) {
if (classLoader == ClassLoaderMatcher.BOOTSTRAP_CLASSLOADER) {
classLoader = ClassLoader.getSystemClassLoader();
}
try {
findLoadedClassMethod.setAccessible(true);
return (Class<?>) findLoadedClassMethod.invoke(classLoader, className);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException(e);
} finally {
findLoadedClassMethod.setAccessible(false);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn 't the plan to remove these methods to avoid Java10 problems?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I brought that back when I resolved a merge conflict. I forgot that we intentionally removed it.


static boolean getConfigEnabled(final String name, final boolean fallback) {
final String property =
System.getProperty(
Expand Down