Search in sources :

Example 26 with ExtensionClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class ExtensionServiceTest method testParamAttributes.

/**
 * Tests reading in the xml extensions for one file.
 */
@Test
public void testParamAttributes() throws Exception {
    setupFiles(true, XML_FILE_PATH_1);
    ExtensionService extService = new ExtensionService(configService, ExtensionsLoadedListener.NOOP);
    serviceManager.setExtensionService(extService);
    extService.start();
    List<ParameterAttributeName> parameterAttributeNames = null;
    List<ExtensionClassAndMethodMatcher> pcs = extensionService.getEnabledPointCuts();
    for (ExtensionClassAndMethodMatcher matcher : pcs) {
        if (matcher.getClassMatcher().getClassNames().contains("test/CustomExtensionTest") && matcher.getMethodMatcher().matches(0, "test2", "(Ljava/lang/String;J)V", null)) {
            parameterAttributeNames = matcher.getTraceDetails().getParameterAttributeNames();
        }
    }
    assertNotNull(parameterAttributeNames);
    assertEquals(2, parameterAttributeNames.size());
}
Also used : ParameterAttributeName(com.newrelic.agent.instrumentation.tracing.ParameterAttributeName) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) Test(org.junit.Test)

Example 27 with ExtensionClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class ExtensionServiceTest method testFindExtensions.

@Test
public void testFindExtensions() throws Exception {
    try {
        extensionService.start();
        List<ExtensionClassAndMethodMatcher> e = extensionService.getEnabledPointCuts();
        int beforeSize = e.size();
        File extDir = new File("/tmp/agentTestExtensions");
        if (!extDir.exists()) {
            extDir.mkdirs();
        }
        File extFile = new File(extDir, "test.yml");
        InputStream is = getClass().getResourceAsStream("test_extension.yml");
        FileOutputStream fileOutputStream = new FileOutputStream(extFile);
        Streams.copy(is, fileOutputStream);
        fileOutputStream.close();
        is.close();
        System.setProperty("newrelic.config.extensions.dir", extDir.getAbsolutePath());
        extensionService = new ExtensionService(serviceManager.getConfigService(), ExtensionsLoadedListener.NOOP);
        extensionService.start();
        e = extensionService.getEnabledPointCuts();
        assertEquals(beforeSize + 1, e.size());
    } finally {
        System.clearProperty("newrelic.config.extensions.dir");
    }
}
Also used : ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Example 28 with ExtensionClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class ExtensionServiceTest method testXMLExtensionOneFile.

/**
 * Tests reading in the xml extensions for one file.
 */
@Test
public void testXMLExtensionOneFile() throws Exception {
    setupFiles(true, XML_FILE_PATH_1);
    List<ExtensionClassAndMethodMatcher> pcs = extensionService.getEnabledPointCuts();
    assertNotNull(pcs);
    assertEquals(3, pcs.size());
    List<String> classNames = new ArrayList<>();
    List<MethodMatcher> mms = new ArrayList<>();
    for (ExtensionClassAndMethodMatcher pc : pcs) {
        classNames.addAll(pc.getClassMatcher().getClassNames());
        mms.add(pc.getMethodMatcher());
    }
    // verify that classes
    assertEquals(3, classNames.size());
    assertTrue(classNames.contains("test/CustomExtensionTest$1"));
    assertTrue(classNames.contains("test/AbstractCustomExtensionTest"));
    assertTrue(classNames.contains("test/CustomExtensionTest"));
    // verify the methods
    assertEquals(3, mms.size());
    verifyMatch("run", "()V", mms);
    verifyMatch("abstractTest1", "()V", mms);
    verifyMatch("abstractTest2", "()V", mms);
    verifyMatch("test2", "(Ljava/lang/String;J)V", mms);
}
Also used : ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) ArrayList(java.util.ArrayList) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) MethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher) Test(org.junit.Test)

Example 29 with ExtensionClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class ExtensionDomParserTest method testDomOnePointCutWithReadString.

@Test
public void testDomOnePointCutWithReadString() throws Exception {
    File file = getFile(FILE_PATH_1);
    Extension ext = ExtensionDomParser.readStringCatchException(readInFile(file));
    Assert.assertNotNull(ext);
    // ext attributes
    Assert.assertEquals("test1", ext.getName());
    Assert.assertEquals(1.0, ext.getVersion(), .001);
    Assert.assertTrue(ext.isEnabled());
    Instrumentation inst = ext.getInstrumentation();
    Assert.assertEquals("PREFIX", inst.getMetricPrefix());
    List<Pointcut> thePcs = inst.getPointcut();
    Assert.assertEquals(1, thePcs.size());
    Pointcut pc = thePcs.get(0);
    Assert.assertTrue(pc.isTransactionStartPoint());
    Assert.assertFalse(pc.isIgnoreTransaction());
    Assert.assertFalse(pc.isExcludeFromTransactionTrace());
    Assert.assertNull(pc.getMetricNameFormat());
    Assert.assertEquals("test.CustomExampleTest", pc.getClassName().getValue());
    List<Method> methods = pc.getMethod();
    Assert.assertEquals(2, methods.size());
    Assert.assertEquals("run", methods.get(0).getName());
    Assert.assertEquals("finish", methods.get(1).getName());
    Assert.assertEquals("(Ljava/lang/String;Ljava/lang/String;)", MethodParameters.getDescriptor(methods.get(0).getParameters()));
    Assert.assertEquals("(F)", MethodParameters.getDescriptor(methods.get(1).getParameters()));
    List<ExtensionClassAndMethodMatcher> pcs = ExtensionConversionUtility.convertToPointCutsForValidation(ext);
    Assert.assertNotNull(pcs);
    Assert.assertEquals(1, pcs.size());
    ExtensionClassAndMethodMatcher actual = pcs.get(0);
    // test method matching
    Assert.assertTrue(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "run", "(Ljava/lang/String;Ljava/lang/String;)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(Ljava/lang/String;Ljava/lang/String;)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(F)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "run", "(F)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(B)V", com.google.common.collect.ImmutableSet.<String>of()));
    // test class matching
    Assert.assertEquals(1, actual.getClassMatcher().getClassNames().size());
    Assert.assertEquals("test/CustomExampleTest", actual.getClassMatcher().getClassNames().toArray()[0]);
}
Also used : Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) AgentHelper.getFile(com.newrelic.agent.AgentHelper.getFile) File(java.io.File) Test(org.junit.Test)

Example 30 with ExtensionClassAndMethodMatcher

use of com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher in project newrelic-java-agent by newrelic.

the class ExtensionDomParserTest method testDomOnePointCutNoPrefix.

@Test
public void testDomOnePointCutNoPrefix() throws Exception {
    Extension ext = ExtensionDomParser.readFile(getFile(FILE_PATH_1_NO_PREFIX));
    // ext attributes
    Assert.assertEquals("test1", ext.getName());
    Assert.assertEquals(1.0, ext.getVersion(), .001);
    Assert.assertTrue(ext.isEnabled());
    Instrumentation inst = ext.getInstrumentation();
    Assert.assertEquals("PREFIX", inst.getMetricPrefix());
    List<Pointcut> thePcs = inst.getPointcut();
    Assert.assertEquals(1, thePcs.size());
    Pointcut pc = thePcs.get(0);
    Assert.assertTrue(pc.isTransactionStartPoint());
    Assert.assertFalse(pc.isIgnoreTransaction());
    Assert.assertFalse(pc.isExcludeFromTransactionTrace());
    Assert.assertFalse(pc.isLeaf());
    Assert.assertNull(pc.getMetricNameFormat());
    Assert.assertEquals("test.CustomExampleTest", pc.getClassName().getValue());
    List<Method> methods = pc.getMethod();
    Assert.assertEquals(2, methods.size());
    Assert.assertEquals("run", methods.get(0).getName());
    Assert.assertEquals("finish", methods.get(1).getName());
    Assert.assertEquals("(Ljava/lang/String;Ljava/lang/String;)", MethodParameters.getDescriptor(methods.get(0).getParameters()));
    Assert.assertEquals("(F)", MethodParameters.getDescriptor(methods.get(1).getParameters()));
    List<ExtensionClassAndMethodMatcher> pcs = ExtensionConversionUtility.convertToPointCutsForValidation(ext);
    Assert.assertNotNull(pcs);
    Assert.assertEquals(1, pcs.size());
    ExtensionClassAndMethodMatcher actual = pcs.get(0);
    // test method matching
    Assert.assertTrue(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "run", "(Ljava/lang/String;Ljava/lang/String;)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(Ljava/lang/String;Ljava/lang/String;)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(F)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "run", "(F)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(B)V", com.google.common.collect.ImmutableSet.<String>of()));
    // test class matching
    Assert.assertEquals(1, actual.getClassMatcher().getClassNames().size());
    Assert.assertEquals("test/CustomExampleTest", actual.getClassMatcher().getClassNames().toArray()[0]);
}
Also used : Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Test(org.junit.Test)

Aggregations

ExtensionClassAndMethodMatcher (com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher)36 Test (org.junit.Test)26 Extension (com.newrelic.agent.extension.beans.Extension)11 MethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher)10 ArrayList (java.util.ArrayList)10 Pointcut (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut)9 Instrumentation (com.newrelic.agent.extension.beans.Extension.Instrumentation)8 File (java.io.File)6 AgentHelper.getFile (com.newrelic.agent.AgentHelper.getFile)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 LinkedList (java.util.LinkedList)5 Method (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method)4 ExactClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher)3 ExactMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher)3 ParameterAttributeName (com.newrelic.agent.instrumentation.tracing.ParameterAttributeName)3 ChildClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ChildClassMatcher)2 ClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ClassMatcher)2 BaseConfig (com.newrelic.agent.config.BaseConfig)1 Config (com.newrelic.agent.config.Config)1 AllClassesMatcher (com.newrelic.agent.instrumentation.classmatchers.AllClassesMatcher)1