Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weaver jax rs issue #234

Merged
merged 5 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.WeaveIntoAllMethods;
import com.newrelic.api.agent.weaver.WeavePriorityOrder;
import com.newrelic.api.agent.weaver.WeaveWithAnnotation;
import com.newrelic.api.agent.weaver.Weaver;

Expand All @@ -27,6 +28,7 @@
import java.util.Queue;
import java.util.logging.Level;

@WeavePriorityOrder(1) // Lower priority than JavaxWsRsApi_Subresource_Instrumentation in WeavePackage.processWeaveBytes
@WeaveWithAnnotation(annotationClasses = { "javax.ws.rs.Path" }, type = MatchType.Interface)
public class JavaxWsRsApi_Instrumentation {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.newrelic.agent.bridge.TransactionNamePriority;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.WeaveIntoAllMethods;
import com.newrelic.api.agent.weaver.WeavePriorityOrder;
import com.newrelic.api.agent.weaver.WeaveWithAnnotation;
import com.newrelic.api.agent.weaver.Weaver;

Expand Down Expand Up @@ -42,6 +43,7 @@
* Case 3: Class annotation = @Path, method annotation = @Path
* This can't happen.
*/
@WeavePriorityOrder(0) // Higher priority than JavaxWsRsApi_Instrumentation in WeavePackage.processWeaveBytes
public class JavaxWsRsApi_Subresource_Instrumentation {

@WeaveWithAnnotation(annotationClasses = { "javax.ws.rs.PUT", "javax.ws.rs.POST", "javax.ws.rs.GET",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.newrelic.api.agent.weaver;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Sets the priority order that a weave instrumentation class should be processed in when processing all classes in a given {@code WeavePackage}.
*
* This can be used to force weave instrumentation classes to be processed in a particular order, if necessary.
*
jasonjkeller marked this conversation as resolved.
Show resolved Hide resolved
* Usage: @WeavePriorityOrder(n)
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface WeavePriorityOrder {

/**
* The priority order. Lower numbers are considered higher in priority. Defaults to 0.
*/
int value() default 0;

}
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,22 @@ public static boolean hasRequiredAnnotations(Set<String> methodAnnotationClassNa
public static boolean isMethodWeNeverInstrument(MethodNode originalMethod) {
return METHODS_WE_NEVER_INSTRUMENT.contains(new MethodKey(originalMethod.name, originalMethod.desc));
}

/**
* Checks if a {@code ClassNode} is annotated with {@code @WeavePriorityOrder} and returns the priority value if it exists.
*
* @param classNode ClassNode to inspect
* @return Integer indicating the WeavePriorityOrder value, or Integer.MIN_VALUE if there isn't one
*/
public static Integer getWeavePriorityOrderIfExists(ClassNode classNode) {
// Uses SynchronizedClassNode and SynchronizedAnnotationNode
final List<AnnotationNode> visibleAnnotations = classNode.visibleAnnotations;

for (AnnotationNode annotationNode : visibleAnnotations) {
if (annotationNode.desc.contains("WeavePriorityOrder")) {
return (Integer) annotationNode.values.get(1);
}
}
return Integer.MIN_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,25 @@ protected final List<WeaveViolation> processWeaveBytes(List<byte[]> weavePackage
requiredClassAnnotationsLookup.put(weaveNode.name, weave.getRequiredClassAnnotations());
}
if (isMethodAnnotationMatch) {
jasonjkeller marked this conversation as resolved.
Show resolved Hide resolved
final Integer weavePriorityOrder = WeaveUtils.getWeavePriorityOrderIfExists(weaveNode);
final boolean weavePriorityOrderExists = weavePriorityOrder != Integer.MIN_VALUE;

for (String requiredAnnotation : weave.getRequiredMethodAnnotations()) {
allMethodAnnotationWeaves.put(requiredAnnotation, weaveNode);
// order of instrumentation class processing doesn't matter, add annotation to the map
if (!weavePriorityOrderExists) {
allMethodAnnotationWeaves.put(requiredAnnotation, weaveNode);
// order matters and the instrumentation class is annotated with @WeavePriorityOrder
} else if (!allMethodAnnotationWeaves.containsKey(requiredAnnotation)) {
// annotation hasn't yet been added to the map, so just add it
allMethodAnnotationWeaves.put(requiredAnnotation, weaveNode);
} else {
final Integer oldWeavePriorityOrder = WeaveUtils.getWeavePriorityOrderIfExists(allMethodAnnotationWeaves.get(requiredAnnotation));
final boolean oldWeavePriorityOrderExists = oldWeavePriorityOrder != Integer.MIN_VALUE;
// annotation is already in the map, compare weavePriorityOrder to determine if the weaveNode should be overwritten or not
if (oldWeavePriorityOrderExists && (weavePriorityOrder < oldWeavePriorityOrder)) {
allMethodAnnotationWeaves.put(requiredAnnotation, weaveNode);
}
}
}
requiredMethodAnnotationsLookup.put(weaveNode.name, weave.getRequiredMethodAnnotations());
}
Expand Down