Search in sources :

Example 6 with WeavePackageConfig

use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.

the class WeavingClassLoader method processClassPath.

private void processClassPath() throws IOException {
    // scan for @Weave or @SkipIfPresent classes and the classes they shadow
    Set<String> weaveResourceNames = new HashSet<>();
    List<byte[]> weavePackageClasses = new ArrayList<>();
    Set<URL> weavePackageURLs = new HashSet<>();
    // also scan for NewRelic classes and AgentBridge class
    // we use this later to prevent the API JAR's NewRelic class from shadowing the bridge JAR's NewRelic class
    Set<ClassResource> newRelicApiClasses = new HashSet<>();
    URL agentBridgeSourceURL = null;
    List<ClassResource> classResources = ClassResource.fromClassLoader((URLClassLoader) getParent());
    for (ClassResource classResource : classResources) {
        String resourceName = classResource.resourceName;
        String className = resourceName.replace('/', '.').replace(".class", "");
        if (weaveIncludes.isIncluded(className)) {
            byte[] bytes = classResource.read();
            if (usesWeaver(bytes) || isInstrumentationUtilityClass(classResource)) {
                weaveResourceNames.add(resourceName);
                weavePackageClasses.add(bytes);
                weavePackageURLs.add(classResource.sourceURL);
            } else {
                // Everything else
                shadowedClassResources.put(resourceName, classResource);
            }
        } else if (weaveIncludes.classUnderTestName.startsWith(className)) {
            weavePackageURLs.add(classResource.sourceURL);
        } else if (className.equals(NEWRELIC_API_CLASS)) {
            newRelicApiClasses.add(classResource);
        } else if (className.equals(AGENT_BRIDGE_CLASS)) {
            agentBridgeSourceURL = classResource.sourceURL;
        }
    }
    // remove resources that aren't actually shadowed
    shadowedClassResources.keySet().retainAll(weaveResourceNames);
    // this is necessary to support @SkipIfPresent
    for (String name : weaveResourceNames) {
        if (!shadowedClassResources.containsKey(name)) {
            shadowedClassResources.put(name, NO_RESOURCE);
        }
    }
    // create weave package for testing
    // @formatter:off
    WeavePackageConfig config = WeavePackageConfig.builder().name(TEST_WEAVE_PACKAGE_NAME).source(TEST_WEAVE_PACKAGE_SOURCE).errorHandleClassNode(errorTrapClassNode).weavePreprocessor(weavePreprocessor).weavePostprocessor(weavePostprocessor).build();
    // @formatter:on
    WeavePackage weavePackage = new WeavePackage(config, weavePackageClasses);
    weavePackageManager.register(weavePackage);
    // handle NewRelic API class shadowing
    if (agentBridgeSourceURL != null) {
        for (ClassResource newRelicApiClass : newRelicApiClasses) {
            if (agentBridgeSourceURL.equals(newRelicApiClass.sourceURL)) {
                shadowedClassResources.put(newRelicApiClass.resourceName, newRelicApiClass);
            }
        }
    }
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ArrayList(java.util.ArrayList) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) URL(java.net.URL) HashSet(java.util.HashSet)

Example 7 with WeavePackageConfig

use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.

the class EnumNoNewFieldsTest method testEnumInvalidNewFields.

@Test
public void testEnumInvalidNewFields() throws IOException {
    List<byte[]> weaveBytes = new ArrayList<>();
    WeavePackageConfig config = WeavePackageConfig.builder().name("weave_unittest").source("com.newrelic.weave.weavepackage.testclasses").build();
    weaveBytes.add(WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumNoNewFieldsTest$Day_Weave"));
    WeavePackage weavePackage = new WeavePackage(config, weaveBytes);
    ClassCache cache = new ClassCache(new ClassLoaderFinder(EnumNoNewFieldsTest.class.getClassLoader()));
    PackageValidationResult result = weavePackage.validate(cache);
    WeaveTestUtils.expectViolations(result, new WeaveViolation(WeaveViolationType.ENUM_NEW_FIELD, "com/newrelic/weave/EnumNoNewFieldsTest$Day"));
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) ArrayList(java.util.ArrayList) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) ClassCache(com.newrelic.weave.utils.ClassCache) PackageValidationResult(com.newrelic.weave.weavepackage.PackageValidationResult) Test(org.junit.Test)

Example 8 with WeavePackageConfig

use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.

the class WeavePackageVerifier method getWeavePackage.

/**
 * Create a {@link WeavePackage} for the specified instrumentation JAR
 *
 * @param instrumentationJar instrumentation JAR
 * @return {@link WeavePackage} for the specified instrumentation JAR
 */
private WeavePackage getWeavePackage(String instrumentationJar) throws Exception {
    URL instrumentationJarUrl = new File(instrumentationJar).toURI().toURL();
    JarInputStream jarStream = null;
    try {
        WeavePackageConfig config = WeavePackageConfig.builder().url(instrumentationJarUrl).build();
        jarStream = new JarInputStream(instrumentationJarUrl.openStream());
        return CachedWeavePackage.createWeavePackage(jarStream, config);
    } finally {
        if (null != jarStream) {
            jarStream.close();
        }
    }
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) JarInputStream(java.util.jar.JarInputStream) File(java.io.File) URL(java.net.URL)

Example 9 with WeavePackageConfig

use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.

the class ClassTransformerConfigImpl method isWeavePackageEnabled.

@Override
public boolean isWeavePackageEnabled(WeavePackageConfig weavePackageConfig) {
    // user can override with traditional instrumentation module name
    String moduleName = weavePackageConfig.getName();
    Config instrumentationConfig = getInstrumentationConfig(moduleName);
    Boolean moduleNameEnabled = instrumentationConfig.getProperty("enabled");
    if (moduleNameEnabled == null) {
        if (DEFAULT_DISABLED_WEAVE_PACKAGES.contains(moduleName)) {
            moduleNameEnabled = false;
        }
    }
    // ..or, an alias for backwards compatibility with legacy pointcut configurations, if available
    String aliasName = weavePackageConfig.getAlias();
    Boolean aliasEnabled = null;
    if (aliasName != null) {
        Config aliasConfig = getInstrumentationConfig(aliasName);
        if (aliasConfig.getProperty("enabled") != null) {
            Agent.LOG.log(Level.INFO, "Using deprecated configuration setting {0} for instrumentation {1}", aliasName, moduleName);
            aliasEnabled = aliasConfig.getProperty("enabled");
        }
    }
    Agent.LOG.log(Level.FINEST, " ### Considering instrumentation: {0}({1}) enabled?{2}({3})", moduleName, aliasName, moduleNameEnabled, aliasEnabled);
    if (moduleNameEnabled == null && aliasEnabled == null && !isDefaultInstrumentationEnabled) {
        Agent.LOG.log(Level.FINEST, " Instrumentation is disabled by default. Skipping: {0} because it is not explicitly enabled.", moduleName);
        // no configuration and instrumentation_disabled is true
        return false;
    } else if (moduleNameEnabled == null && aliasEnabled == null && isDefaultInstrumentationEnabled) {
        // no configuration, return default from weave package
        return weavePackageConfig.isEnabled();
    } else if (moduleNameEnabled == null) {
        // only alias was configured
        return aliasEnabled;
    } else if (aliasEnabled == null) {
        // only module name was configured
        return moduleNameEnabled;
    } else {
        // both were configured
        return aliasEnabled && moduleNameEnabled;
    }
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig)

Example 10 with WeavePackageConfig

use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.

the class ClassLoaderClassTransformer method buildClassLoaderPatcherPackage.

private WeavePackage buildClassLoaderPatcherPackage() {
    WeavePackageConfig classloaderPatcher = WeavePackageConfig.builder().name("agent-classloader-patcher").errorHandleClassNode(LogAndReturnOriginal.ERROR_HANDLER_NODE).extensionClassTemplate(extensionTemplate).build();
    List<byte[]> weavePackageBytes = new ArrayList<>();
    try {
        // Grab the bytes of our Agent Classloader instrumentation class. Note: This call uses "findResource" but
        // this is ok because we are not under a classloader lock at this point, we are still in the premain()
        byte[] classloaderPatcherInstrumentationBytes = WeaveUtils.getClassBytesFromClassLoaderResource(AgentClassLoaderInstrumentation.class.getName(), ClassLoaderClassTransformer.class.getClassLoader());
        weavePackageBytes.add(classloaderPatcherInstrumentationBytes);
    } catch (IOException e) {
        Agent.LOG.log(Level.FINE, e, "Unable to initialize agent classloader instrumentation");
    }
    return new WeavePackage(classloaderPatcher, weavePackageBytes);
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) AgentClassLoaderInstrumentation(com.newrelic.agent.instrumentation.builtin.AgentClassLoaderInstrumentation) ArrayList(java.util.ArrayList) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) IOException(java.io.IOException)

Aggregations

WeavePackageConfig (com.newrelic.weave.weavepackage.WeavePackageConfig)22 Test (org.junit.Test)10 WeavePackage (com.newrelic.weave.weavepackage.WeavePackage)9 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)4 ClassTransformerConfig (com.newrelic.agent.config.ClassTransformerConfig)3 URL (java.net.URL)3 JarInputStream (java.util.jar.JarInputStream)3 AgentConfig (com.newrelic.agent.config.AgentConfig)2 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)2 CachedWeavePackage (com.newrelic.weave.weavepackage.CachedWeavePackage)2 File (java.io.File)2 HashSet (java.util.HashSet)2 AgentClassLoaderBaseInstrumentation (com.newrelic.agent.instrumentation.builtin.AgentClassLoaderBaseInstrumentation)1 AgentClassLoaderInstrumentation (com.newrelic.agent.instrumentation.builtin.AgentClassLoaderInstrumentation)1 ClassLoaderPackageAccessInstrumentation (com.newrelic.agent.instrumentation.builtin.ClassLoaderPackageAccessInstrumentation)1 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)1 AgentPostprocessors (com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPostprocessors)1 AgentPreprocessors (com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessors)1