Search in sources :

Example 1 with InstrumentedMethod

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

the class ProfiledMethodFactory method getMethodInfo.

public MethodInfo getMethodInfo(ClassMethodSignature methodSignature) {
    MethodInfo methodInfo = methodInfos.get(methodSignature);
    if (null == methodInfo) {
        Type[] argumentTypes = Type.getArgumentTypes(methodSignature.getMethodDesc());
        List<String> arguments = new ArrayList<>(argumentTypes.length);
        for (Type t : argumentTypes) {
            arguments.add(t.getClassName());
        }
        methodInfo = new ExactMethodInfo(arguments, (InstrumentedMethod) null);
        MethodInfo previous = methodInfos.putIfAbsent(methodSignature, methodInfo);
        if (previous != null) {
            return previous;
        }
    }
    return methodInfo;
}
Also used : Type(org.objectweb.asm.Type) ExactMethodInfo(com.newrelic.agent.profile.method.ExactMethodInfo) ArrayList(java.util.ArrayList) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) ExactMethodInfo(com.newrelic.agent.profile.method.ExactMethodInfo) MethodInfo(com.newrelic.agent.profile.method.MethodInfo)

Example 2 with InstrumentedMethod

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

the class WebServiceTest method testTracedMethods.

@Test
public void testTracedMethods() throws NoSuchMethodException, SecurityException {
    Assert.assertTrue(WsExample.class.isAnnotationPresent(InstrumentedClass.class));
    Method method = WsExample.class.getMethod("run");
    Assert.assertNotNull(method);
    Assert.assertFalse(method.isAnnotationPresent(InstrumentedMethod.class));
    method = WsExample.class.getMethod("getWebMethod", String.class);
    Assert.assertNotNull(method);
    Assert.assertTrue(method.isAnnotationPresent(InstrumentedMethod.class));
    InstrumentedMethod annotation = method.getAnnotation(InstrumentedMethod.class);
    Assert.assertEquals("com.newrelic.agent.instrumentation.webservices.WebServiceVisitor", annotation.instrumentationNames()[0]);
    Assert.assertEquals(InstrumentationType.BuiltIn, annotation.instrumentationTypes()[0]);
}
Also used : InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) Method(java.lang.reflect.Method) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) InstrumentedClass(com.newrelic.agent.instrumentation.InstrumentedClass) Test(org.junit.Test)

Example 3 with InstrumentedMethod

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

the class ReinstrumentAnnotationTest method testAnnotation.

@Test
public void testAnnotation() throws NoSuchMethodException, SecurityException {
    String transactionMetric = "OtherTransaction/Custom/" + InstrumentMeObj2.class.getName() + "/getName";
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    sb.append("<extension xmlns=\"https://newrelic.com/docs/java/xsd/v1.0\"");
    sb.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    sb.append(" xsi:schemaLocation=\"newrelic-extension extension.xsd \" name=\"testing 123\">");
    sb.append("<instrumentation>");
    sb.append("<pointcut transactionStartPoint=\"true\">");
    sb.append("<className>com.newrelic.agent.reinstrument.InstrumentMeObj2");
    sb.append("</className>");
    sb.append("<method>");
    sb.append("<name>getName</name>");
    sb.append("<parameters>");
    sb.append("<type>java.lang.String</type>");
    sb.append("</parameters>");
    sb.append("</method>");
    sb.append("</pointcut>");
    sb.append("</instrumentation>");
    sb.append("</extension>");
    InstrumentMeObj2 obj = new InstrumentMeObj2();
    obj.getName("mr");
    InstrumentTestUtils.verifyMetricNotPresent(transactionMetric);
    // reinstrument for the first time
    ServiceFactory.getRemoteInstrumentationService().processXml(sb.toString());
    InstrumentedMethod annotation = InstrumentMeObj2.class.getDeclaredMethod("getName", String.class).getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(annotation);
    Assert.assertEquals(annotation.instrumentationNames().length, annotation.instrumentationTypes().length);
    Assert.assertEquals(InstrumentationType.RemoteCustomXml, annotation.instrumentationTypes()[0]);
    Assert.assertEquals("testing 123", annotation.instrumentationNames()[0]);
    Assert.assertTrue(annotation.dispatcher());
}
Also used : InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) Test(org.junit.Test)

Example 4 with InstrumentedMethod

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

the class ReinstrumentAnnotationTest method testWeavePlusXml.

@Test
public void testWeavePlusXml() throws NoSuchMethodException, SecurityException {
    class JspTest implements HttpJspPage {

        @Override
        public void jspDestroy() {
        }

        @Override
        public void jspInit() {
        }

        @Override
        public void init(ServletConfig config) throws ServletException {
        }

        @Override
        public ServletConfig getServletConfig() {
            return null;
        }

        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        }

        @Override
        public String getServletInfo() {
            return null;
        }

        @Override
        public void destroy() {
        }

        @Override
        public void _jspService(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        }
    }
    JspTest test = new JspTest();
    InstrumentedMethod annotation = JspTest.class.getDeclaredMethod("_jspService", HttpServletRequest.class, HttpServletResponse.class).getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(annotation);
    Assert.assertEquals(2, annotation.instrumentationTypes().length);
    for (int i = 0; i < annotation.instrumentationTypes().length; i++) {
        Assert.assertEquals("com.newrelic.instrumentation.jsp-2.4", annotation.instrumentationNames()[i]);
        Assert.assertTrue(annotation.instrumentationTypes()[i] == InstrumentationType.TracedWeaveInstrumentation || annotation.instrumentationTypes()[i] == InstrumentationType.WeaveInstrumentation);
    }
    // reinstrument with more instrumentation
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    sb.append("<extension xmlns=\"https://newrelic.com/docs/java/xsd/v1.0\"");
    sb.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    sb.append(" xsi:schemaLocation=\"newrelic-extension extension.xsd \" name=\"Not A Pointcut\">");
    sb.append("<instrumentation>");
    sb.append("<pointcut transactionStartPoint=\"true\" >");
    sb.append("<className>" + JspTest.class.getName());
    sb.append("</className>");
    sb.append("<method>");
    sb.append("<name>_jspService</name>");
    sb.append("</method>");
    sb.append("</pointcut>");
    sb.append("</instrumentation>");
    sb.append("</extension>");
    // reinstrument for the first time
    ServiceFactory.getRemoteInstrumentationService().processXml(sb.toString());
    annotation = JspTest.class.getDeclaredMethod("_jspService", HttpServletRequest.class, HttpServletResponse.class).getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(annotation);
    Assert.assertEquals(3, annotation.instrumentationTypes().length);
    for (int i = 0; i < annotation.instrumentationTypes().length; i++) {
        if (annotation.instrumentationNames()[i].equals("com.newrelic.instrumentation.jsp-2.4")) {
            Assert.assertTrue(annotation.instrumentationTypes()[i] == InstrumentationType.TracedWeaveInstrumentation || annotation.instrumentationTypes()[i] == InstrumentationType.WeaveInstrumentation);
        } else if (annotation.instrumentationNames()[i].equals("Not A Pointcut")) {
            Assert.assertTrue(annotation.instrumentationTypes()[i] == InstrumentationType.RemoteCustomXml);
        } else {
            Assert.fail("The instrumentation name does not match. Name: " + annotation.instrumentationNames()[i]);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpJspPage(javax.servlet.jsp.HttpJspPage) ServletConfig(javax.servlet.ServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) Test(org.junit.Test)

Example 5 with InstrumentedMethod

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

the class ReinstrumentAnnotationTest method testAnnotationMixedInstrumentationYamlXml.

@Test
public void testAnnotationMixedInstrumentationYamlXml() throws ParserConfigurationException, IOException, SAXException, NoSuchMethodException, SecurityException {
    ServiceFactory.getStatsService().getStatsEngineForHarvest(null).clear();
    Method method = SolrCore.class.getDeclaredMethod("getSearcher", boolean.class, boolean.class, Future[].class);
    InstrumentedMethod annotation = method.getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(annotation);
    Assert.assertEquals(1, annotation.instrumentationTypes().length);
    Assert.assertEquals(InstrumentationType.CustomYaml, annotation.instrumentationTypes()[0]);
    Assert.assertEquals("Solr", annotation.instrumentationNames()[0]);
    // reinstrument with more instrumentation
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    sb.append("<extension xmlns=\"https://newrelic.com/docs/java/xsd/v1.0\"");
    sb.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    sb.append(" xsi:schemaLocation=\"newrelic-extension extension.xsd \" name=\"MySecondSolr\">");
    sb.append("<instrumentation>");
    sb.append("<pointcut transactionStartPoint=\"true\" >");
    sb.append("<className>org.apache.solr.core.SolrCore");
    sb.append("</className>");
    sb.append("<method>");
    sb.append("<name>getSearcher</name>");
    sb.append("</method>");
    sb.append("</pointcut>");
    sb.append("</instrumentation>");
    sb.append("</extension>");
    // reinstrument for the first time
    ServiceFactory.getRemoteInstrumentationService().processXml(sb.toString());
    method = SolrCore.class.getDeclaredMethod("getSearcher", boolean.class, boolean.class, Future[].class);
    annotation = method.getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(annotation);
    Assert.assertEquals(2, annotation.instrumentationTypes().length);
    for (int i = 0; i < annotation.instrumentationTypes().length; i++) {
        if (annotation.instrumentationTypes()[i] == InstrumentationType.CustomYaml) {
            Assert.assertEquals("Solr", annotation.instrumentationNames()[i]);
        } else if (annotation.instrumentationTypes()[i] == InstrumentationType.RemoteCustomXml) {
            Assert.assertEquals("MySecondSolr", annotation.instrumentationNames()[i]);
        } else {
            Assert.fail("The instrumentation type should be custom yaml or remote custom xml. Type: " + annotation.instrumentationTypes()[i]);
        }
    }
}
Also used : SolrCore(org.apache.solr.core.SolrCore) Future(java.util.concurrent.Future) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

InstrumentedMethod (com.newrelic.agent.instrumentation.InstrumentedMethod)18 Test (org.junit.Test)16 Method (java.lang.reflect.Method)8 InstrumentedClass (com.newrelic.agent.instrumentation.InstrumentedClass)3 ArrayList (java.util.ArrayList)2 InstrumentationType (com.newrelic.agent.instrumentation.InstrumentationType)1 PointCut (com.newrelic.agent.instrumentation.PointCut)1 WeavedMethod (com.newrelic.agent.instrumentation.WeavedMethod)1 TraceDetails (com.newrelic.agent.instrumentation.tracing.TraceDetails)1 ExactMethodInfo (com.newrelic.agent.profile.method.ExactMethodInfo)1 MethodInfo (com.newrelic.agent.profile.method.MethodInfo)1 Future (java.util.concurrent.Future)1 Level (java.util.logging.Level)1 ServletConfig (javax.servlet.ServletConfig)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpJspPage (javax.servlet.jsp.HttpJspPage)1 SolrCore (org.apache.solr.core.SolrCore)1