Search in sources :

Example 1 with ClassMatchVisitorFactory

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

the class ClassWeaverService method loadExternalWeavePackages.

/**
 * Load new instrumentation packages from disk and put any new matchers in the matchers collection.
 *
 * @param weaveExtensions
 */
private Collection<ClassMatchVisitorFactory> loadExternalWeavePackages(Collection<File> weaveExtensions) {
    Collection<ClassMatchVisitorFactory> matchers = new HashSet<>();
    for (File weaveExtension : weaveExtensions) {
        try (JarInputStream stream = new JarInputStream(new FileInputStream(weaveExtension))) {
            AgentConfig agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
            WeavePackageConfig weaveConfig = createWeavePackageConfig(stream, weaveExtension.getAbsolutePath(), instrumentation, WeavePackageType.CUSTOM, agentConfig);
            ClassTransformerConfig classTransformerConfig = agentConfig.getClassTransformerConfig();
            String instrName = weaveConfig.getName();
            if (weavePackageManager.isRegistered(instrName)) {
                weavePackageManager.deregister(instrName);
                this.externalWeavePackages.remove(weaveExtension.getAbsolutePath());
            }
            if (!classTransformerConfig.isWeavePackageEnabled(weaveConfig)) {
                if (weaveConfig.isEnabled()) {
                    // Only log this if the module has been explicitly disabled (not if it is disabled by default)
                    LOG.log(Level.INFO, "Instrumentation {0} is disabled. Skipping.", instrName);
                }
                continue;
            }
            WeavePackage externalPackage = WeavePackage.createWeavePackage(stream, weaveConfig);
            if (externalPackage.getPackageViolations().size() > 0) {
                LOG.log(Level.FINER, "skip loading external weave package: {0}", instrName);
                for (WeaveViolation violation : externalPackage.getPackageViolations()) {
                    LOG.log(Level.FINER, "\t{0}", violation);
                }
            } else {
                weavePackageManager.register(externalPackage);
                externalWeavePackages.put(weaveExtension.getAbsolutePath(), weaveConfig.getName());
            }
        } catch (Exception e) {
            LOG.log(Level.FINE, e, "Error reading weave extension {0}", weaveExtension.getAbsolutePath());
        }
    }
    return matchers;
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) JarInputStream(java.util.jar.JarInputStream) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) FileInputStream(java.io.FileInputStream) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) IOException(java.io.IOException) AgentConfig(com.newrelic.agent.config.AgentConfig) ClassTransformerConfig(com.newrelic.agent.config.ClassTransformerConfig) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with ClassMatchVisitorFactory

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

the class ApiImplementationUpdateTest method testMatcherMissingMethods.

@Test
public void testMatcherMissingMethods() throws Exception {
    ArgumentCaptor<ClassMatchVisitorFactory> arg = ArgumentCaptor.forClass(ClassMatchVisitorFactory.class);
    InstrumentationContext iInstrumentationContext = Mockito.mock(InstrumentationContext.class);
    // remove required methods from Request implementation class
    Class<?> clazz = RequestImpl.class;
    Set<Method> methodsToRemove = new HashSet<>();
    methodsToRemove.add(new Method("getHeaderType", "()Lcom/newrelic/api/agent/HeaderType;"));
    byte[] classBytes = removeMethods(clazz, methodsToRemove);
    // verify missing methods
    RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
    expectMissingMethods(adapter, classBytes);
    ApiImplementationUpdate transformer = new ApiImplementationUpdate();
    ClassMatchVisitorFactory matcher = transformer.getMatcher();
    ClassReader reader = new ClassReader(new ByteArrayInputStream(classBytes));
    ClassVisitor visitor = matcher.newClassMatchVisitor(clazz.getClassLoader(), null, reader, null, iInstrumentationContext);
    reader.accept(visitor, ClassReader.SKIP_CODE);
    Mockito.verify(iInstrumentationContext, Mockito.only()).putMatch(arg.capture(), Mockito.<Match>anyObject());
    Assert.assertSame(matcher, arg.getValue());
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) Method(org.objectweb.asm.commons.Method) ClassVisitor(org.objectweb.asm.ClassVisitor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequireMethodsAdapter(com.newrelic.agent.instrumentation.RequireMethodsAdapter) ClassReader(org.objectweb.asm.ClassReader) HashSet(java.util.HashSet) Test(org.junit.Test) ClassMatcherTest(com.newrelic.agent.instrumentation.classmatchers.ClassMatcherTest)

Example 3 with ClassMatchVisitorFactory

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

the class ApiImplementationUpdateTest method testMatcher.

@Test
public void testMatcher() throws Exception {
    InstrumentationContext iInstrumentationContext = Mockito.mock(InstrumentationContext.class);
    Class<?> clazz = RequestImpl.class;
    Set<Method> methodsToRemove = new HashSet<>();
    byte[] classBytes = removeMethods(clazz, methodsToRemove);
    // verify no missing methods
    RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
    expectNoMissingMethods(adapter, classBytes);
    ApiImplementationUpdate transformer = new ApiImplementationUpdate();
    ClassMatchVisitorFactory matcher = transformer.getMatcher();
    ClassReader reader = new ClassReader(new ByteArrayInputStream(classBytes));
    ClassVisitor visitor = matcher.newClassMatchVisitor(clazz.getClassLoader(), null, reader, null, iInstrumentationContext);
    reader.accept(visitor, ClassReader.SKIP_CODE);
    Mockito.verify(iInstrumentationContext, Mockito.never()).putMatch(Mockito.<ClassMatchVisitorFactory>anyObject(), Mockito.<Match>anyObject());
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) Method(org.objectweb.asm.commons.Method) ClassVisitor(org.objectweb.asm.ClassVisitor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequireMethodsAdapter(com.newrelic.agent.instrumentation.RequireMethodsAdapter) ClassReader(org.objectweb.asm.ClassReader) HashSet(java.util.HashSet) Test(org.junit.Test) ClassMatcherTest(com.newrelic.agent.instrumentation.classmatchers.ClassMatcherTest)

Example 4 with ClassMatchVisitorFactory

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

the class OptimizedClassMatcherTest method testSkipObjectMethods_exactMatch.

@Test
public void testSkipObjectMethods_exactMatch() throws IOException {
    List<MethodMatcher> matchers = new ArrayList<>();
    for (Method m : OBJECT_METHODS) {
        matchers.add(new ExactMethodMatcher(m.getName(), m.getDescriptor()));
    }
    ClassMatchVisitorFactory matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new DefaultClassAndMethodMatcher(new AllClassesMatcher(), OrMethodMatcher.getMethodMatcher(matchers))).build();
    Match match = getMatch(matcher, MyObject.class);
    Assert.assertNull(match);
}
Also used : ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) ArrayList(java.util.ArrayList) Method(org.objectweb.asm.commons.Method) ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher) AnnotationMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher) ExactReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher) OrMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.OrMethodMatcher) MethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher) NameMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher) AccessMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.AccessMethodMatcher) ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher) Match(com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match) Test(org.junit.Test)

Example 5 with ClassMatchVisitorFactory

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

the class OptimizedClassMatcherTest method testReturnTypeMatch.

@Test
public void testReturnTypeMatch() throws IOException {
    ClassMatchVisitorFactory matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new DefaultClassAndMethodMatcher(new AllClassesMatcher(), new ExactReturnTypeMethodMatcher(Type.getType(List.class)))).build();
    InstrumentationContext instrumentationContext = getInstrumentationContext(matcher, Arrays.class);
    Assert.assertFalse(instrumentationContext.getMatches().isEmpty());
    Match match = instrumentationContext.getMatches().values().iterator().next();
    Assert.assertNotNull(match);
    Assert.assertEquals(1, match.getMethods().size());
    Assert.assertTrue(match.getMethods().contains(new Method("asList", "([Ljava/lang/Object;)Ljava/util/List;")));
    Assert.assertEquals(1, match.getClassMatches().size());
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) ExactReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) Method(org.objectweb.asm.commons.Method) Match(com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match) Test(org.junit.Test)

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