Search in sources :

Example 1 with DefaultClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class InstrumentationImpl method instrument.

@Override
public void instrument(Method methodToInstrument, String metricPrefix) {
    if (methodToInstrument.isAnnotationPresent(InstrumentedMethod.class)) {
        return;
    }
    if (OptimizedClassMatcher.METHODS_WE_NEVER_INSTRUMENT.contains(org.objectweb.asm.commons.Method.getMethod(methodToInstrument))) {
        return;
    }
    int modifiers = methodToInstrument.getModifiers();
    if (Modifier.isNative(modifiers) || Modifier.isAbstract(modifiers)) {
        // Can't instrument, so don't bother.
        return;
    }
    Class<?> declaringClass = methodToInstrument.getDeclaringClass();
    DefaultClassAndMethodMatcher matcher = new HashSafeClassAndMethodMatcher(new ExactClassMatcher(declaringClass.getName()), new ExactMethodMatcher(methodToInstrument.getName(), Type.getMethodDescriptor(methodToInstrument)));
    boolean shouldRetransform = ServiceFactory.getClassTransformerService().addTraceMatcher(matcher, metricPrefix);
    if (shouldRetransform) {
        logger.log(Level.FINE, "Retransforming {0} for instrumentation.", methodToInstrument);
        PeriodicRetransformer.INSTANCE.queueRetransform(Sets.<Class<?>>newHashSet(declaringClass));
    }
}
Also used : ExactClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher) DefaultClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher) HashSafeClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.HashSafeClassAndMethodMatcher) ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher)

Example 2 with DefaultClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class InstrumentationImpl method instrument.

@Override
public void instrument(String className, String metricPrefix) {
    DefaultClassAndMethodMatcher matcher = new HashSafeClassAndMethodMatcher(new ExactClassMatcher(className), AndMethodMatcher.getMethodMatcher(new AccessMethodMatcher(Opcodes.ACC_PUBLIC), new NotMethodMatcher(GetterSetterMethodMatcher.getGetterSetterMethodMatcher())));
    ServiceFactory.getClassTransformerService().addTraceMatcher(matcher, metricPrefix);
}
Also used : ExactClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher) AccessMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.AccessMethodMatcher) DefaultClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher) HashSafeClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.HashSafeClassAndMethodMatcher) NotMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.NotMethodMatcher)

Example 3 with DefaultClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class JVMClassTransformerTest method testConcurrentTransforms.

@Test
public void testConcurrentTransforms() throws Exception {
    final int numClasses = 300;
    final int concurrencyLevel = 30;
    List<Future<Class<?>>> futures = new ArrayList<>();
    ExecutorService executor = Executors.newFixedThreadPool(concurrencyLevel);
    for (int i = 0; i < numClasses; ++i) {
        final String generatedClassname = TestClass.class.getName() + i;
        Callable<Class<?>> callable = new Callable<Class<?>>() {

            @Override
            public Class<?> call() throws Exception {
                // make a new class
                byte[] newClassBytes = cloneClassWithNewName(TestClass.class, generatedClassname);
                for (String methodname : TestClass.methodsToTrace) {
                    DefaultClassAndMethodMatcher matcher = new HashSafeClassAndMethodMatcher(new ExactClassMatcher(generatedClassname), new ExactMethodMatcher(methodname, Type.getMethodDescriptor(Type.VOID_TYPE)));
                    ServiceFactory.getClassTransformerService().addTraceMatcher(matcher, "FunctionalTest");
                }
                // run the new class through the transformer
                newClassBytes = transformer.transform(classloader, generatedClassname, null, classloader.getClass().getProtectionDomain(), newClassBytes);
                if (null == newClassBytes) {
                    Assert.fail("Class transformer returned null for class " + generatedClassname);
                }
                // load the new class. This will cause the transformer to run again
                addToContextClassloader(generatedClassname, newClassBytes);
                Class<?> newClass = classloader.loadClass(generatedClassname);
                return newClass;
            }
        };
        futures.add(executor.submit(callable));
    }
    List<Class<?>> toRetransform = new ArrayList<>(futures.size());
    for (Future<Class<?>> future : futures) {
        toRetransform.add(future.get());
    }
    ServiceFactory.getClassTransformerService().getContextManager().getInstrumentation().retransformClasses(toRetransform.toArray(new Class[0]));
    String uninstrumentedClassMethodNames = "";
    int numUninstrumented = 0;
    for (Future<Class<?>> future : futures) {
        Class<?> clazz = future.get();
        if (null == clazz) {
            numUninstrumented++;
            continue;
        }
        for (String methodname : TestClass.methodsToTrace) {
            if (!isInstrumented(clazz.getMethod(methodname))) {
                if (numUninstrumented == 0) {
                    uninstrumentedClassMethodNames = clazz.getName() + ":" + methodname;
                } else {
                    uninstrumentedClassMethodNames += "," + clazz.getName() + ":" + methodname;
                }
                numUninstrumented++;
            }
        }
    }
    if (numUninstrumented > 0) {
        Assert.fail(numUninstrumented + " generated methods were not instrumented: " + uninstrumentedClassMethodNames);
    }
    executor.shutdown();
}
Also used : ExactClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher) ArrayList(java.util.ArrayList) DefaultClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher) HashSafeClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.HashSafeClassAndMethodMatcher) ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) BeforeClass(org.junit.BeforeClass) Test(org.junit.Test)

Example 4 with DefaultClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class ClassesMatcherTest method getMatchingClasses_Simple.

@Test
public void getMatchingClasses_Simple() {
    ClassMatchVisitorFactory matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new DefaultClassAndMethodMatcher(new ExactClassMatcher(Type.getType(ArrayList.class).getInternalName()), new ExactMethodMatcher("size", "()I"))).build();
    Set<Class<?>> matchingClasses = ClassesMatcher.getMatchingClasses(Collections.singletonList(matcher), matcherHelper, ArrayList.class);
    assertFalse(matchingClasses.isEmpty());
    assertTrue(matchingClasses.contains(ArrayList.class));
}
Also used : ExactClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher) DefaultClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher) ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher) Test(org.junit.Test)

Example 5 with DefaultClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class ClassesMatcherTest method getMatchingClasses_ReturnType.

@Trace
@Test
public void getMatchingClasses_ReturnType() {
    ClassMatchVisitorFactory matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new DefaultClassAndMethodMatcher(new AllClassesMatcher(), new ExactReturnTypeMethodMatcher(Type.getType(List.class)))).build();
    Set<Class<?>> matchingClasses = ClassesMatcher.getMatchingClasses(Collections.singletonList(matcher), matcherHelper, Arrays.class, ArrayList.class, HashMap.class, TestClass.class, Proxy.class);
    assertEquals(3, matchingClasses.size());
    assertTrue(matchingClasses.contains(ArrayList.class));
    assertTrue(matchingClasses.contains(TestClass.class));
    assertTrue(matchingClasses.contains(Arrays.class));
}
Also used : ExactReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher) DefaultClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher) AllClassesMatcher(com.newrelic.agent.instrumentation.classmatchers.AllClassesMatcher) Trace(com.newrelic.api.agent.Trace) Test(org.junit.Test)

Aggregations

DefaultClassAndMethodMatcher (com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher)7 ExactClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher)5 Test (org.junit.Test)5 HashSafeClassAndMethodMatcher (com.newrelic.agent.instrumentation.classmatchers.HashSafeClassAndMethodMatcher)3 ExactMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher)3 AllClassesMatcher (com.newrelic.agent.instrumentation.classmatchers.AllClassesMatcher)2 Trace (com.newrelic.api.agent.Trace)2 AccessMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AccessMethodMatcher)1 AllMethodsMatcher (com.newrelic.agent.instrumentation.methodmatchers.AllMethodsMatcher)1 AnnotationMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher)1 ExactReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher)1 NotMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.NotMethodMatcher)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 BeforeClass (org.junit.BeforeClass)1