Search in sources :

Example 1 with ExactMethodMatcher

use of com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher 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 ExactMethodMatcher

use of com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher in project newrelic-java-agent by newrelic.

the class InstrumentationMetricsTest method testMultiplePointcutsTransformationMetrics.

@Test
public void testMultiplePointcutsTransformationMetrics() throws Exception {
    ServiceFactory.getClassTransformerService().getClassTransformer().getClassNameFilter().addIncludeClass(getClass().getName().replace('.', '/') + "$ClassToRetransform2");
    Map<String, Integer> metricData = InstrumentTestUtils.getAndClearMetricData();
    // This will load the class also:
    String className = ClassToRetransform2Object.class.getName();
    String constructorMetric = MetricNames.SUPPORTABILITY_INIT + className + "/<init>(I)V";
    String methodMetric1 = MetricNames.SUPPORTABILITY_INIT + className + "/getVal()I";
    String methodMetric2 = MetricNames.SUPPORTABILITY_INIT + className + "/getVal2()I";
    Assert.assertEquals(1, new ClassToRetransform2Object(1).getVal());
    Assert.assertEquals(2, new ClassToRetransform2Object(1).getVal2());
    metricData = InstrumentTestUtils.getAndClearMetricData();
    Assert.assertNull(metricData.get(constructorMetric));
    Assert.assertNull(metricData.get(methodMetric1));
    Assert.assertNull(metricData.get(methodMetric2));
    ExtensionClassAndMethodMatcher pc1 = new ExtensionClassAndMethodMatcher(null, null, "Custom", new ExactClassMatcher(className), new ExactMethodMatcher("getVal", "()I"), false, false, false, false, null);
    ExtensionClassAndMethodMatcher pc2 = new ExtensionClassAndMethodMatcher(null, null, "Custom", new ExactClassMatcher(className), new ExactMethodMatcher("getVal2", "()I"), false, false, false, false, null);
    List<ExtensionClassAndMethodMatcher> list = new LinkedList<>();
    list.add(pc1);
    list.add(pc2);
    ServiceFactory.getClassTransformerService().getLocalRetransformer().appendClassMethodMatchers(list);
    InstrumentTestUtils.retransformClass(className);
    Assert.assertEquals(1, new ClassToRetransform2Object(1).getVal());
    Assert.assertEquals(2, new ClassToRetransform2Object(1).getVal2());
    metricData = InstrumentTestUtils.getAndClearMetricData();
    Assert.assertNull(metricData.get(constructorMetric));
    Assert.assertEquals(Integer.valueOf(1), metricData.get(methodMetric1));
    Assert.assertEquals(Integer.valueOf(1), metricData.get(methodMetric2));
}
Also used : ExactClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with ExactMethodMatcher

use of com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher 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 ExactMethodMatcher

use of com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher in project newrelic-java-agent by newrelic.

the class InstrumentationConstructorTest method exactMethodMatcherTwoSignatures.

@Test
public void exactMethodMatcherTwoSignatures() throws Exception {
    ExactMethodMatcher methodMatcher = load("!exact_method_matcher [ 'getTracerFactoryClassName', '()Ljava/lang/String;', '()V' ]");
    Assert.assertEquals(new ExactMethodMatcher("getTracerFactoryClassName", "()Ljava/lang/String;", "()V"), methodMatcher);
}
Also used : ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher) Test(org.junit.Test)

Example 5 with ExactMethodMatcher

use of com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher in project newrelic-java-agent by newrelic.

the class InstrumentationConstructorTest method exactMethodMatcher.

@Test
public void exactMethodMatcher() throws Exception {
    ExactMethodMatcher methodMatcher = load("!exact_method_matcher [ 'getTracerFactoryClassName', '()Ljava/lang/String;' ]");
    Assert.assertEquals(new ExactMethodMatcher("getTracerFactoryClassName", "()Ljava/lang/String;"), methodMatcher);
}
Also used : ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher) Test(org.junit.Test)

Aggregations

ExactMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher)14 Test (org.junit.Test)11 ExactClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher)7 MethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher)6 ExtensionClassAndMethodMatcher (com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher)5 DefaultClassAndMethodMatcher (com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher)3 OrMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.OrMethodMatcher)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 HashSafeClassAndMethodMatcher (com.newrelic.agent.instrumentation.classmatchers.HashSafeClassAndMethodMatcher)2 AccessMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AccessMethodMatcher)2 AnnotationMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher)2 NameMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher)2 List (java.util.List)2 ClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ClassMatcher)1 Match (com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match)1 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)1 AllMethodsMatcher (com.newrelic.agent.instrumentation.methodmatchers.AllMethodsMatcher)1 ExactReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher)1 LinkedList (java.util.LinkedList)1