Search in sources :

Example 1 with InstrumentationType

use of com.newrelic.agent.instrumentation.InstrumentationType 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 2 with InstrumentationType

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

the class TraceMatchVisitor method newClassMatchVisitor.

@Override
public ClassVisitor newClassMatchVisitor(ClassLoader loader, Class<?> classBeingRedefined, ClassReader reader, ClassVisitor cv, final InstrumentationContext context) {
    return new ClassVisitor(WeaveUtils.ASM_API_LEVEL, cv) {

        private String source;

        private boolean isWeaveUtilityClass = false;

        @Override
        public void visitSource(String source, String debug) {
            super.visitSource(source, debug);
            this.source = source;
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            AnnotationVisitor av = super.visitAnnotation(desc, visible);
            if (WEAVE_UTILITY_CLASS_DESC.equals(desc)) {
                isWeaveUtilityClass = true;
                av = new AnnotationVisitor(WeaveUtils.ASM_API_LEVEL, av) {

                    @Override
                    public void visit(String name, Object value) {
                        if (WEAVE_UTILITY_CLASS_PACKAGE_NAME.equals(name)) {
                            source = (String) value;
                        }
                        super.visit(name, value);
                    }
                };
            }
            return av;
        }

        @Override
        public MethodVisitor visitMethod(int access, final String methodName, final String methodDesc, String signature, String[] exceptions) {
            return new MethodVisitor(WeaveUtils.ASM_API_LEVEL, super.visitMethod(access, methodName, methodDesc, signature, exceptions)) {

                @Override
                public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                    if (traceAnnotationMatcher.matches(desc) || (TRACE_DESC.equals(desc) && isWeaveUtilityClass)) {
                        return processTraceAnnotation(desc, visible);
                    }
                    if (ignoreApdexAnnotationMatcher.matches(desc)) {
                        context.addIgnoreApdexMethod(methodName, methodDesc);
                    }
                    if (ignoreTransactionAnnotationMatcher.matches(desc)) {
                        context.addIgnoreTransactionMethod(methodName, methodDesc);
                    }
                    return super.visitAnnotation(desc, visible);
                }

                private AnnotationVisitor processTraceAnnotation(final String desc, final boolean visible) {
                    InstrumentationType type = isWeaveUtilityClass ? InstrumentationType.TracedWeaveInstrumentation : InstrumentationType.TraceAnnotation;
                    Annotation node = new Annotation(super.visitAnnotation(desc, visible), TRACE_DESC, TraceDetailsBuilder.newBuilder().setInstrumentationType(type).setInstrumentationSourceName(source)) {

                        @Override
                        public void visitEnd() {
                            context.putTraceAnnotation(new Method(methodName, methodDesc), getTraceDetails(!isWeaveUtilityClass));
                            super.visitEnd();
                        }
                    };
                    return node;
                }
            };
        }
    };
}
Also used : InstrumentationType(com.newrelic.agent.instrumentation.InstrumentationType) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) Method(org.objectweb.asm.commons.Method) Annotation(com.newrelic.agent.instrumentation.tracing.Annotation) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

InstrumentationType (com.newrelic.agent.instrumentation.InstrumentationType)2 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)2 ClassVisitor (org.objectweb.asm.ClassVisitor)2 MethodVisitor (org.objectweb.asm.MethodVisitor)2 Method (org.objectweb.asm.commons.Method)2 InstrumentedMethod (com.newrelic.agent.instrumentation.InstrumentedMethod)1 PointCut (com.newrelic.agent.instrumentation.PointCut)1 WeavedMethod (com.newrelic.agent.instrumentation.WeavedMethod)1 Annotation (com.newrelic.agent.instrumentation.tracing.Annotation)1 TraceDetails (com.newrelic.agent.instrumentation.tracing.TraceDetails)1 ArrayList (java.util.ArrayList)1 Level (java.util.logging.Level)1