Search in sources :

Example 1 with TraceDetailsBuilder

use of com.newrelic.agent.instrumentation.tracing.TraceDetailsBuilder 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)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 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1 Method (org.objectweb.asm.commons.Method)1