generated from newrelic-experimental/java-instrumentation-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from newrelic-experimental/distributed2
Distributed2
- Loading branch information
Showing
21 changed files
with
892 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
.java-version | ||
.git | ||
.github | ||
.settings | ||
.classpath | ||
.project | ||
bin | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
// Build.gradle generated for instrumentation module rmi-stubs | ||
|
||
apply plugin: 'java' | ||
|
||
dependencies { | ||
// Declare a dependency on each JAR you want to instrument | ||
// Example: | ||
// implementation 'javax.servlet:servlet-api:2.5' | ||
|
||
// New Relic Java Agent dependencies | ||
implementation 'com.newrelic.agent.java:newrelic-agent:6.4.0' | ||
implementation 'com.newrelic.agent.java:newrelic-api:6.4.0' | ||
implementation fileTree(include: ['*.jar'], dir: '../libs') | ||
implementation fileTree(include: ['*.jar'], dir: '../test-lib') | ||
} | ||
|
||
jar { | ||
manifest { | ||
attributes 'Implementation-Title': 'com.newrelic.instrumentation.rmi-stubs' | ||
attributes 'Implementation-Vendor': 'New Relic' | ||
attributes 'Implementation-Vendor-Id': 'com.newrelic' | ||
attributes 'Implementation-Version': 1.0 | ||
} | ||
} | ||
|
||
verifyInstrumentation { | ||
// Verifier plugin documentation: | ||
// https://github.com/newrelic/newrelic-gradle-verify-instrumentation | ||
// Example: | ||
// passes 'javax.servlet:servlet-api:[2.2,2.5]' | ||
// exclude 'javax.servlet:servlet-api:2.4.public_draft' | ||
} |
30 changes: 30 additions & 0 deletions
30
rmi-stubs/src/main/java/com/newrelic/instrumentation/rmi/stubs/RMIStubsClassMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.newrelic.instrumentation.rmi.stubs; | ||
|
||
import com.newrelic.agent.deps.org.objectweb.asm.ClassReader; | ||
import com.newrelic.agent.instrumentation.classmatchers.ChildClassMatcher; | ||
|
||
public class RMIStubsClassMatcher extends ChildClassMatcher { | ||
|
||
public RMIStubsClassMatcher() { | ||
super("java.rmi.server.RemoteStub"); | ||
} | ||
|
||
@Override | ||
public boolean isMatch(ClassLoader loader, ClassReader cr) { | ||
boolean isChild = super.isMatch(loader, cr); | ||
String tmp = cr.getClassName().replace('/', '.'); | ||
boolean isSystem = tmp.startsWith("java.rmi") || tmp.startsWith("sun.rmi"); | ||
return isChild && !isSystem; | ||
} | ||
|
||
@Override | ||
public boolean isMatch(Class<?> clazz) { | ||
boolean isChild = super.isMatch(clazz); | ||
String classname = clazz.getName(); | ||
boolean isSystem = classname.startsWith("java.rmi") || classname.startsWith("sun.rmi"); | ||
|
||
return isChild && !isSystem; | ||
} | ||
|
||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...tubs/src/main/java/com/newrelic/instrumentation/rmi/stubs/RMIStubsClassMethodMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.newrelic.instrumentation.rmi.stubs; | ||
|
||
import com.newrelic.agent.instrumentation.classmatchers.ClassAndMethodMatcher; | ||
import com.newrelic.agent.instrumentation.classmatchers.ClassMatcher; | ||
import com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher; | ||
|
||
public class RMIStubsClassMethodMatcher implements ClassAndMethodMatcher { | ||
|
||
private ClassMatcher classMatcher = new RMIStubsClassMatcher(); | ||
private MethodMatcher methodMatcher = new RMIStubsMethodMatcher(); | ||
|
||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return classMatcher; | ||
} | ||
|
||
@Override | ||
public MethodMatcher getMethodMatcher() { | ||
return methodMatcher; | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
rmi-stubs/src/main/java/com/newrelic/instrumentation/rmi/stubs/RMIStubsClassTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.newrelic.instrumentation.rmi.stubs; | ||
|
||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.security.ProtectionDomain; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.newrelic.agent.InstrumentationProxy; | ||
import com.newrelic.agent.deps.org.objectweb.asm.commons.Method; | ||
import com.newrelic.agent.instrumentation.classmatchers.ClassAndMethodMatcher; | ||
import com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match; | ||
import com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcherBuilder; | ||
import com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory; | ||
import com.newrelic.agent.instrumentation.context.ContextClassTransformer; | ||
import com.newrelic.agent.instrumentation.context.InstrumentationContext; | ||
import com.newrelic.agent.instrumentation.context.InstrumentationContextManager; | ||
import com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher; | ||
import com.newrelic.agent.instrumentation.tracing.TraceDetailsBuilder; | ||
|
||
public class RMIStubsClassTransformer implements ContextClassTransformer { | ||
|
||
private final InstrumentationContextManager contextManager; | ||
private final Map<String, ClassMatchVisitorFactory> matchers = new HashMap<String, ClassMatchVisitorFactory>(); | ||
|
||
public RMIStubsClassTransformer(InstrumentationContextManager mgr,InstrumentationProxy pInstrumentation) { | ||
contextManager = mgr; | ||
} | ||
|
||
protected ClassMatchVisitorFactory addMatcher(ClassAndMethodMatcher matcher) { | ||
OptimizedClassMatcherBuilder builder = OptimizedClassMatcherBuilder.newBuilder(); | ||
builder.addClassMethodMatcher(matcher); | ||
ClassMatchVisitorFactory matchVisitor = builder.build(); | ||
matchers.put(matcher.getClass().getSimpleName(), matchVisitor); | ||
contextManager.addContextClassTransformer(matchVisitor, this); | ||
return matchVisitor; | ||
} | ||
|
||
protected void removeMatcher(ClassAndMethodMatcher matcher) { | ||
ClassMatchVisitorFactory matchVisitor = matchers.get(matcher.getClass().getSimpleName()); | ||
if(matchVisitor != null) { | ||
contextManager.removeMatchVisitor(matchVisitor); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, | ||
ProtectionDomain protectionDomain, byte[] classfileBuffer, InstrumentationContext context, Match match) | ||
throws IllegalClassFormatException { | ||
for (Method method : match.getMethods()) { | ||
for (ClassAndMethodMatcher matcher : match.getClassMatches().keySet()) { | ||
if (matcher.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, method.getName(), | ||
method.getDescriptor(), match.getMethodAnnotations(method))) { | ||
int index = className.lastIndexOf('/'); | ||
if(index == -1) { | ||
index = className.lastIndexOf('.'); | ||
} | ||
String simpleClass = index > -1 ? className.substring(index+1) : className; | ||
simpleClass = simpleClass.replace("_Stub", ""); | ||
String metricName = "Custom/RMI/Stub/" + simpleClass + "/" + method; | ||
context.putTraceAnnotation(method, TraceDetailsBuilder.newBuilder().setDispatcher(true).setMetricName(metricName).build()); | ||
} | ||
} | ||
|
||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
rmi-stubs/src/main/java/com/newrelic/instrumentation/rmi/stubs/RMIStubsMethodMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.newrelic.instrumentation.rmi.stubs; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import com.newrelic.agent.deps.org.objectweb.asm.commons.Method; | ||
import com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher; | ||
|
||
public class RMIStubsMethodMatcher implements MethodMatcher { | ||
|
||
private static final List<String> methodsToSkipped = new ArrayList<>(); | ||
|
||
static { | ||
methodsToSkipped.add("getRef"); | ||
methodsToSkipped.add("equals"); | ||
methodsToSkipped.add("hashcode"); | ||
methodsToSkipped.add("toString"); | ||
methodsToSkipped.add("toStub"); | ||
methodsToSkipped.add("<init>"); | ||
} | ||
|
||
@Override | ||
public boolean matches(int access, String name, String desc, Set<String> annotations) { | ||
boolean isPublic = 1 == access || -1 == access; | ||
boolean b = isPublic && !methodsToSkipped.contains(name); | ||
return b; | ||
} | ||
|
||
@Override | ||
public Method[] getExactMethods() { | ||
return null; | ||
} | ||
|
||
} |
Oops, something went wrong.