Search in sources :

Example 16 with WeavePackage

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

the class ClassLoaderClassTransformer method buildBaseClassLoaderPatcherPackage.

private WeavePackage buildBaseClassLoaderPatcherPackage() {
    WeavePackageConfig baseClassloaderPatcher = WeavePackageConfig.builder().name("base-agent-classloader-patcher").errorHandleClassNode(LogAndReturnOriginal.ERROR_HANDLER_NODE).extensionClassTemplate(extensionTemplate).build();
    List<byte[]> baseWeavePackageBytes = 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[] baseClassloaderPatcherInstrumentationBytes = WeaveUtils.getClassBytesFromClassLoaderResource(AgentClassLoaderBaseInstrumentation.class.getName(), ClassLoaderClassTransformer.class.getClassLoader());
        baseWeavePackageBytes.add(baseClassloaderPatcherInstrumentationBytes);
    } catch (IOException e) {
        Agent.LOG.log(Level.FINE, e, "Unable to initialize agent classloader instrumentation");
    }
    return new WeavePackage(baseClassloaderPatcher, baseWeavePackageBytes);
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) AgentClassLoaderBaseInstrumentation(com.newrelic.agent.instrumentation.builtin.AgentClassLoaderBaseInstrumentation) ArrayList(java.util.ArrayList) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) IOException(java.io.IOException)

Example 17 with WeavePackage

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

the class ClassWeaverService method loadInternalWeavePackages.

/**
 * Load all the weave packages embedded in the agent jar.
 */
private Collection<ClassMatchVisitorFactory> loadInternalWeavePackages() {
    final Collection<ClassMatchVisitorFactory> matchers = Sets.newConcurrentHashSet();
    Collection<String> jarFileNames = AgentJarHelper.findAgentJarFileNames(Pattern.compile("instrumentation\\/(.*).jar"));
    if (jarFileNames.isEmpty()) {
        LOG.log(Level.SEVERE, "No instrumentation packages were found in the agent.");
    } else {
        LOG.log(Level.FINE, "Loading {0} instrumentation packages", jarFileNames.size());
    }
    int partitions = (jarFileNames.size() < PARTITIONS) ? jarFileNames.size() : PARTITIONS;
    // Note: An ExecutorService would be better suited for this work but we are
    // specifically not using it here to prevent the ConcurrentCallablePointCut
    // from being loaded too early
    final CountDownLatch executorCountDown = new CountDownLatch(partitions);
    List<Set<String>> weavePackagePartitions = partitionInstrumentationJars(jarFileNames, partitions);
    for (final Set<String> weavePackageJars : weavePackagePartitions) {
        Runnable loadWeavePackagesRunnable = new Runnable() {

            @Override
            public void run() {
                try {
                    for (final String name : weavePackageJars) {
                        URL instrumentationUrl = BootstrapAgent.class.getResource('/' + name);
                        if (instrumentationUrl == null) {
                            Agent.LOG.error("Unable to find instrumentation jar: " + name);
                        } else {
                            try (InputStream inputStream = instrumentationUrl.openStream()) {
                                WeavePackage internalWeavePackage = createWeavePackage(inputStream, instrumentationUrl.toExternalForm());
                                if (null == internalWeavePackage) {
                                    LOG.log(Level.FINEST, "internal weave package: {0} was null", instrumentationUrl.toExternalForm());
                                    continue;
                                } else if (internalWeavePackage.getPackageViolations().size() > 0) {
                                    LOG.log(Level.FINER, "skip loading weave package: {0}", internalWeavePackage.getName());
                                    for (WeaveViolation violation : internalWeavePackage.getPackageViolations()) {
                                        LOG.log(Level.FINER, "\t violation: {0}", violation);
                                    }
                                } else {
                                    LOG.log(Level.FINER, "adding weave package: {0}", internalWeavePackage.getName());
                                    internalWeavePackages.add(internalWeavePackage.getName());
                                    weavePackageManager.register(internalWeavePackage);
                                }
                            } catch (Throwable t) {
                                LOG.log(Level.FINER, t, "unable to load weave package jar {0}", instrumentationUrl);
                            }
                        }
                    }
                } catch (Throwable t) {
                    LOG.log(Level.FINER, t, "A thread loading weaved packages threw an error");
                } finally {
                    executorCountDown.countDown();
                }
            }
        };
        new Thread(loadWeavePackagesRunnable).start();
    }
    try {
        // Wait for all partitions to complete
        executorCountDown.await();
        LOG.log(Level.FINE, "Loaded {0} internal instrumentation packages", internalWeavePackages.size());
    } catch (InterruptedException e) {
        LOG.log(Level.FINE, e, "Interrupted while waiting for instrumentation packages.");
    }
    return matchers;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL)

Example 18 with WeavePackage

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

the class ClassWeaverService method unloadExternalWeavePackages.

/**
 * Unload the given external instrumentation packages
 */
private Collection<ClassMatchVisitorFactory> unloadExternalWeavePackages(Set<String> removedFilePaths) {
    LOG.log(Level.INFO, "ClassWeaveService removing {0} weave packages.", removedFilePaths.size());
    Collection<ClassMatchVisitorFactory> matchers = Sets.newHashSetWithExpectedSize(removedFilePaths.size());
    for (String removedFilePath : removedFilePaths) {
        String weavePackageName = this.externalWeavePackages.get(removedFilePath);
        if (this.internalWeavePackages.contains(weavePackageName)) {
            Agent.LOG.log(Level.FINER, "Attempted to unload internal weave package {0} -- {1}. Ignoring request.", weavePackageName, removedFilePath);
            continue;
        }
        WeavePackage externalPackage = weavePackageManager.deregister(weavePackageName);
        if (null == externalPackage) {
            Agent.LOG.log(Level.FINER, "Attempted to unload non-existent weave package {0} -- {1}. Ignoring request.", weavePackageName, removedFilePath);
        } else {
            externalWeavePackages.remove(removedFilePath);
        }
    }
    return matchers;
}
Also used : ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage)

Aggregations

WeavePackage (com.newrelic.weave.weavepackage.WeavePackage)18 WeavePackageConfig (com.newrelic.weave.weavepackage.WeavePackageConfig)9 CachedWeavePackage (com.newrelic.weave.weavepackage.CachedWeavePackage)8 ArrayList (java.util.ArrayList)7 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)4 IOException (java.io.IOException)4 ClassTransformerConfig (com.newrelic.agent.config.ClassTransformerConfig)3 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)3 ClassCache (com.newrelic.weave.utils.ClassCache)3 ClassLoaderFinder (com.newrelic.weave.utils.ClassLoaderFinder)3 PackageValidationResult (com.newrelic.weave.weavepackage.PackageValidationResult)3 URL (java.net.URL)3 HashSet (java.util.HashSet)3 JarInputStream (java.util.jar.JarInputStream)3 AgentConfig (com.newrelic.agent.config.AgentConfig)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Map (java.util.Map)2 Test (org.junit.Test)2 AgentClassLoaderBaseInstrumentation (com.newrelic.agent.instrumentation.builtin.AgentClassLoaderBaseInstrumentation)1