Search in sources :

Example 6 with ClassMatchVisitorFactory

use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory in project newrelic-java-agent by newrelic.

the class OptimizedClassMatcherTest method testSkipObjectMethods_looseMatch.

@Test
public void testSkipObjectMethods_looseMatch() throws IOException {
    ClassMatchVisitorFactory matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new DefaultClassAndMethodMatcher(new AllClassesMatcher(), new AllMethodsMatcher())).build();
    Match match = getMatch(matcher, MyObject.class);
    Assert.assertNotNull(match);
    for (Method m : OBJECT_METHODS) {
        Assert.assertFalse(match.getMethods().contains(m));
    }
}
Also used : ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) Method(org.objectweb.asm.commons.Method) AllMethodsMatcher(com.newrelic.agent.instrumentation.methodmatchers.AllMethodsMatcher) Match(com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match) Test(org.junit.Test)

Example 7 with ClassMatchVisitorFactory

use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory 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 8 with ClassMatchVisitorFactory

use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory 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)

Example 9 with ClassMatchVisitorFactory

use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory in project newrelic-java-agent by newrelic.

the class JarCollectorInputs method build.

public static JarCollectorInputs build(boolean jarCollectorEnabled, JarAnalystFactory jarAnalystFactory, ExecutorService executorService, Logger jarCollectorLogger) {
    ClassMatchVisitorFactory classNoticingFactory = jarCollectorEnabled ? new ClassNoticingFactory(jarAnalystFactory, executorService, jarCollectorLogger) : ClassMatchVisitorFactory.NO_OP_FACTORY;
    ExtensionsLoadedListener extensionAnalysisProducer = jarCollectorEnabled ? new ExtensionAnalysisProducer(jarAnalystFactory, executorService, jarCollectorLogger) : ExtensionsLoadedListener.NOOP;
    return new JarCollectorInputs(extensionAnalysisProducer, classNoticingFactory);
}
Also used : ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) ExtensionsLoadedListener(com.newrelic.agent.extension.ExtensionsLoadedListener)

Aggregations

ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)9 Test (org.junit.Test)5 Method (org.objectweb.asm.commons.Method)5 HashSet (java.util.HashSet)4 Match (com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match)3 InstrumentationContext (com.newrelic.agent.instrumentation.context.InstrumentationContext)3 CachedWeavePackage (com.newrelic.weave.weavepackage.CachedWeavePackage)3 WeavePackage (com.newrelic.weave.weavepackage.WeavePackage)3 RequireMethodsAdapter (com.newrelic.agent.instrumentation.RequireMethodsAdapter)2 ClassMatcherTest (com.newrelic.agent.instrumentation.classmatchers.ClassMatcherTest)2 ExactReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher)2 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 JarInputStream (java.util.jar.JarInputStream)2 ClassReader (org.objectweb.asm.ClassReader)2 ClassVisitor (org.objectweb.asm.ClassVisitor)2 AgentConfig (com.newrelic.agent.config.AgentConfig)1 ClassTransformerConfig (com.newrelic.agent.config.ClassTransformerConfig)1 ExtensionsLoadedListener (com.newrelic.agent.extension.ExtensionsLoadedListener)1