Search in sources :

Example 1 with Annotation

use of com.newrelic.agent.instrumentation.tracing.Annotation 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)

Example 2 with Annotation

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

the class AgentPreprocessors method gatherTraceInfo.

/**
 * Visit the methods of weave classes which have {@link Trace} annotations. <br/>
 * Methods which have tracers will be stored in the tracedWeaveInstrumentationDetails map.
 */
ClassVisitor gatherTraceInfo(ClassVisitor cv) {
    // weave package name -> WeaverTraceDetails
    Set<TracedWeaveInstrumentationTracker> initial = Sets.newConcurrentHashSet();
    tracedWeaveInstrumentationDetails.putIfAbsent(weavePackageName, initial);
    return new ClassVisitor(WeaveUtils.ASM_API_LEVEL, cv) {

        private boolean isWeave = false;

        private boolean isWeaveWithAnnotation = false;

        private String originalName;

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

        /**
         * Get the name of the original class specified in @Weave(originalName="...")
         */
        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            AnnotationVisitor av = super.visitAnnotation(desc, visible);
            if (Type.getDescriptor(Weave.class).equals(desc)) {
                isWeave = true;
                return new AnnotationVisitor(WeaveUtils.ASM_API_LEVEL, av) {

                    @Override
                    public void visit(String name, Object value) {
                        if (name.equals("originalName")) {
                            originalName = ((String) value).replace('.', '/');
                        }
                        super.visit(name, value);
                    }
                };
            }
            if (Type.getDescriptor(WeaveWithAnnotation.class).equals(desc)) {
                isWeaveWithAnnotation = true;
            }
            return av;
        }

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

                @Override
                public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                    AnnotationVisitor av = super.visitAnnotation(desc, visible);
                    if (Type.getDescriptor(Trace.class).equals(desc)) {
                        Agent.LOG.log(Level.FINER, "Storing TracedWeaveInstrumentation: {0} - {1}.{2}({3})", weavePackageName, originalName, methodName, methodDesc);
                        TraceDetailsBuilder builder = TraceDetailsBuilder.newBuilder().setInstrumentationType(InstrumentationType.TracedWeaveInstrumentation).setInstrumentationSourceName(weavePackageName);
                        av = new Annotation(av, desc, builder) {

                            @Override
                            public void visitEnd() {
                                tracedWeaveInstrumentationDetails.get(weavePackageName).add(new TracedWeaveInstrumentationTracker(weavePackageName, originalName, new Method(methodName, methodDesc), isWeaveWithAnnotation, getTraceDetails(false)));
                                super.visitEnd();
                            }
                        };
                    }
                    return av;
                }
            };
        }
    };
}
Also used : TraceDetailsBuilder(com.newrelic.agent.instrumentation.tracing.TraceDetailsBuilder) ClassVisitor(org.objectweb.asm.ClassVisitor) Method(org.objectweb.asm.commons.Method) Annotation(com.newrelic.agent.instrumentation.tracing.Annotation) WeaveWithAnnotation(com.newrelic.api.agent.weaver.WeaveWithAnnotation) MethodVisitor(org.objectweb.asm.MethodVisitor) Trace(com.newrelic.api.agent.Trace) WeaveWithAnnotation(com.newrelic.api.agent.weaver.WeaveWithAnnotation) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) Weave(com.newrelic.api.agent.weaver.Weave)

Aggregations

Annotation (com.newrelic.agent.instrumentation.tracing.Annotation)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 InstrumentationType (com.newrelic.agent.instrumentation.InstrumentationType)1 TraceDetailsBuilder (com.newrelic.agent.instrumentation.tracing.TraceDetailsBuilder)1 Trace (com.newrelic.api.agent.Trace)1 Weave (com.newrelic.api.agent.weaver.Weave)1 WeaveWithAnnotation (com.newrelic.api.agent.weaver.WeaveWithAnnotation)1