Search in sources :

Example 6 with InstrumentedMethod

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

the class WeaveTest method testTraceUtilityClass.

@Test
public void testTraceUtilityClass() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    Class<?> clazz = Class.forName("com.nr.instrumentation.instrumentation.weaver.NotWeaved");
    AgentBridge.instrumentation.retransformUninstrumentedClass(clazz);
    Method method = clazz.getDeclaredMethod("notWeavedButStillTraced");
    InstrumentedMethod annotation = method.getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(annotation);
    Assert.assertEquals(InstrumentationType.TracedWeaveInstrumentation, annotation.instrumentationTypes()[0]);
    Assert.assertEquals("Weave Test", annotation.instrumentationNames()[0]);
}
Also used : InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 7 with InstrumentedMethod

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

the class FinalClassTransformer method addModifiedMethodAnnotation.

private ClassVisitor addModifiedMethodAnnotation(ClassVisitor cv, final InstrumentationContext context, final ClassLoader loader) {
    return new ClassVisitor(WeaveUtils.ASM_API_LEVEL, cv) {

        private String className;

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            className = name;
            super.visit(version, access, name, signature, superName, interfaces);
        }

        /**
         * @see InstrumentedMethod#instrumentationTypes()
         * @see InstrumentedMethod#instrumentationNames()
         * @see InstrumentedMethod#dispatcher()
         */
        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            Method method = new Method(name, desc);
            if (context.isModified(method)) {
                if (loader != null) {
                    TraceDetails traceDetails = context.getTraceInformation().getTraceAnnotations().get(method);
                    boolean dispatcher = false;
                    if (traceDetails != null) {
                        dispatcher = traceDetails.dispatcher();
                    }
                    AnnotationVisitor av = mv.visitAnnotation(Type.getDescriptor(InstrumentedMethod.class), true);
                    av.visit("dispatcher", dispatcher);
                    List<String> instrumentationNames = new ArrayList<>();
                    List<InstrumentationType> instrumentationTypes = new ArrayList<>();
                    Level logLevel = Level.FINER;
                    if (traceDetails != null) {
                        if (traceDetails.instrumentationSourceNames() != null) {
                            instrumentationNames.addAll(traceDetails.instrumentationSourceNames());
                        }
                        if (traceDetails.instrumentationTypes() != null) {
                            for (InstrumentationType type : traceDetails.instrumentationTypes()) {
                                instrumentationTypes.add(type);
                            }
                        }
                        if (traceDetails.isCustom()) {
                            logLevel = Level.FINE;
                        }
                    }
                    PointCut pointCut = context.getOldStylePointCut(method);
                    if (pointCut != null) {
                        instrumentationNames.add(pointCut.getClass().getName());
                        instrumentationTypes.add(InstrumentationType.Pointcut);
                    }
                    Collection<String> instrumentationPackages = context.getMergeInstrumentationPackages(method);
                    if (instrumentationPackages != null && !instrumentationPackages.isEmpty()) {
                        for (String current : instrumentationPackages) {
                            instrumentationNames.add(current);
                            instrumentationTypes.add(InstrumentationType.WeaveInstrumentation);
                        }
                    }
                    if (instrumentationNames.size() == 0) {
                        instrumentationNames.add("Unknown");
                        Agent.LOG.finest("Unknown instrumentation source for " + className + '.' + method);
                    }
                    if (instrumentationTypes.size() == 0) {
                        instrumentationTypes.add(InstrumentationType.Unknown);
                        Agent.LOG.finest("Unknown instrumentation type for " + className + '.' + method);
                    }
                    AnnotationVisitor visitArrayName = av.visitArray("instrumentationNames");
                    for (String current : instrumentationNames) {
                        // the key on this is ignored
                        visitArrayName.visit("", current);
                    }
                    visitArrayName.visitEnd();
                    AnnotationVisitor visitArrayType = av.visitArray("instrumentationTypes");
                    for (InstrumentationType type : instrumentationTypes) {
                        // the key on this is ignored
                        visitArrayType.visitEnum("", Type.getDescriptor(InstrumentationType.class), type.toString());
                    }
                    visitArrayType.visitEnd();
                    av.visitEnd();
                    if (Agent.LOG.isLoggable(logLevel)) {
                        Agent.LOG.log(logLevel, "Instrumented " + Type.getObjectType(className).getClassName() + '.' + method + ", " + instrumentationTypes + ", " + instrumentationNames);
                    }
                }
            }
            return mv;
        }
    };
}
Also used : InstrumentationType(com.newrelic.agent.instrumentation.InstrumentationType) ArrayList(java.util.ArrayList) TraceDetails(com.newrelic.agent.instrumentation.tracing.TraceDetails) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) ClassVisitor(org.objectweb.asm.ClassVisitor) Method(org.objectweb.asm.commons.Method) WeavedMethod(com.newrelic.agent.instrumentation.WeavedMethod) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) MethodVisitor(org.objectweb.asm.MethodVisitor) PointCut(com.newrelic.agent.instrumentation.PointCut) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) Level(java.util.logging.Level)

Example 8 with InstrumentedMethod

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

the class AgentTest method testAnnotations.

@Test
public void testAnnotations() throws SecurityException, NoSuchMethodException {
    InstrumentedClass annotation = HttpServlet.class.getAnnotation(InstrumentedClass.class);
    Assert.assertNotNull(annotation);
    Assert.assertFalse(annotation.legacy());
    Method m = HttpServlet.class.getMethod("service", ServletRequest.class, ServletResponse.class);
    Assert.assertNotNull(m);
    InstrumentedMethod methodAnnotation = m.getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(methodAnnotation);
    Assert.assertEquals(InstrumentationType.TracedWeaveInstrumentation, methodAnnotation.instrumentationTypes()[0]);
// Assert.assertEquals(HttpServletPointCut.class.getName(), methodAnnotation.instrumentationName());
// m = HttpServlet.class.getMethod("service", HttpServletRequest.class, HttpServletResponse.class);
// annotations = m.getAnnotations();
// Assert.assertTrue(annotations.length == 0);
// Assert.assertTrue(Arrays.asList(annotations).toString(), annotations[0] instanceof Instrumented);
}
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 9 with InstrumentedMethod

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

the class WeaveTest method testTraceNoImpl.

@Test
public void testTraceNoImpl() throws NoSuchMethodException, SecurityException {
    Method method = TestClass.class.getDeclaredMethod("fooBar");
    InstrumentedMethod annotation = method.getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(annotation);
    Assert.assertEquals(InstrumentationType.TracedWeaveInstrumentation, annotation.instrumentationTypes()[0]);
    Assert.assertEquals("Weave Test", annotation.instrumentationNames()[0]);
}
Also used : InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) InstrumentedMethod(com.newrelic.agent.instrumentation.InstrumentedMethod) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 10 with InstrumentedMethod

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

the class WeaveTest method annotationNotTraced.

@Test
public void annotationNotTraced() throws NoSuchMethodException, SecurityException {
    Method method = TestClass.class.getDeclaredMethod("integerTest");
    InstrumentedMethod annotation = method.getAnnotation(InstrumentedMethod.class);
    Assert.assertNotNull(annotation);
    Assert.assertEquals(InstrumentationType.WeaveInstrumentation, annotation.instrumentationTypes()[0]);
    Assert.assertEquals("Weave Test", annotation.instrumentationNames()[0]);
}
Also used : 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