generated from newrelic-experimental/java-instrumentation-template
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #22 from newrelic-experimental/camel-updates
Camel updates
- Loading branch information
Showing
58 changed files
with
1,306 additions
and
676 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
48 changes: 48 additions & 0 deletions
48
...java/com/newrelic/instrumentation/labs/apache/camel/processors/CamelClassTransformer.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,48 @@ | ||
package com.newrelic.instrumentation.labs.apache.camel.processors; | ||
|
||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.security.ProtectionDomain; | ||
import java.util.logging.Level; | ||
|
||
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.methodmatchers.MethodMatcher; | ||
import com.newrelic.agent.instrumentation.tracing.TraceDetailsBuilder; | ||
import com.newrelic.api.agent.NewRelic; | ||
|
||
|
||
public class CamelClassTransformer implements ContextClassTransformer { | ||
|
||
private ClassMatchVisitorFactory matcher = null; | ||
|
||
public ClassMatchVisitorFactory getMatcher() { | ||
return matcher; | ||
} | ||
|
||
public CamelClassTransformer() { | ||
matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new ProcessorClassMethodMatcher()).build(); | ||
} | ||
|
||
@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))) { | ||
NewRelic.getAgent().getLogger().log(Level.FINE, "Instrumenting the Processor class {0} and method {1}",className,method.getName()); | ||
context.putTraceAnnotation(method, TraceDetailsBuilder.newBuilder().build()); | ||
} | ||
} | ||
|
||
} | ||
return null; | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...main/java/com/newrelic/instrumentation/labs/apache/camel/processors/CamelCorePremain.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,84 @@ | ||
package com.newrelic.instrumentation.labs.apache.camel.processors; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.logging.Level; | ||
|
||
import com.newrelic.agent.instrumentation.ClassTransformerService; | ||
import com.newrelic.agent.instrumentation.context.InstrumentationContextManager; | ||
import com.newrelic.agent.service.ServiceFactory; | ||
import com.newrelic.api.agent.NewRelic; | ||
|
||
public class CamelCorePremain { | ||
|
||
private static ExecutorService executor = null; | ||
|
||
public static void premain(String s, Instrumentation inst) { | ||
initialize(); | ||
} | ||
|
||
public static void initialize() { | ||
ClassTransformerService classTransformerService = ServiceFactory.getClassTransformerService(); | ||
if(classTransformerService != null) { | ||
InstrumentationContextManager contextMgr = classTransformerService.getContextManager(); | ||
|
||
if(contextMgr != null) { | ||
CamelClassTransformer classTransformer = new CamelClassTransformer(); | ||
NewRelic.getAgent().getLogger().log(Level.FINE, "Constructed CamelClassTransformer: {0}, matcher: {1}", classTransformer, classTransformer.getMatcher()); | ||
contextMgr.addContextClassTransformer(classTransformer.getMatcher(), classTransformer); | ||
} else { | ||
NewRelic.getAgent().getLogger().log(Level.FINE, "Could not load matcher because ClassTransformerService is null"); | ||
startExecutor(); | ||
} | ||
} else { | ||
NewRelic.getAgent().getLogger().log(Level.FINE, "Could not load matcher because InstrumentationContextManager is null"); | ||
startExecutor(); | ||
} | ||
|
||
} | ||
|
||
private static void startExecutor() { | ||
executor = Executors.newSingleThreadExecutor(); | ||
RunCheck runCheck = new RunCheck(); | ||
executor.submit(runCheck); | ||
NewRelic.getAgent().getLogger().log(Level.FINE, "Submit RunCheck to executor"); | ||
} | ||
|
||
private static void shutdownExecutor() { | ||
if (executor != null) { | ||
executor.shutdown(); | ||
NewRelic.getAgent().getLogger().log(Level.FINE, "CamelPremain executor has shut down"); | ||
} | ||
} | ||
|
||
|
||
private static class RunCheck implements Runnable { | ||
|
||
@Override | ||
public void run() { | ||
boolean done = false; | ||
while(!done) { | ||
ClassTransformerService classTransformerService = ServiceFactory.getClassTransformerService(); | ||
if(classTransformerService != null) { | ||
InstrumentationContextManager contextMgr = classTransformerService.getContextManager(); | ||
|
||
if(contextMgr != null) { | ||
CamelClassTransformer classTransformer = new CamelClassTransformer(); | ||
NewRelic.getAgent().getLogger().log(Level.FINE, "Constructed CamelClassTransformer: {0}, matcher: {1}", classTransformer, classTransformer.getMatcher()); | ||
contextMgr.addContextClassTransformer(classTransformer.getMatcher(), classTransformer); | ||
done = true; | ||
} | ||
} else { | ||
try { | ||
Thread.sleep(2000L); | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
} | ||
shutdownExecutor(); | ||
} | ||
|
||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
.../java/com/newrelic/instrumentation/labs/apache/camel/processors/ExcludedClassMatcher.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,42 @@ | ||
package com.newrelic.instrumentation.labs.apache.camel.processors; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import com.newrelic.agent.deps.org.objectweb.asm.ClassReader; | ||
import com.newrelic.agent.instrumentation.classmatchers.ClassMatcher; | ||
|
||
public class ExcludedClassMatcher extends ClassMatcher { | ||
|
||
private static final List<String> excluded; | ||
|
||
static { | ||
excluded = new ArrayList<>(); | ||
excluded.add("com.nr.instrumentation.apache.camel.NRProcessorStart"); | ||
excluded.add("com.nr.instrumentation.apache.camel.NRAsyncProcessorStart"); | ||
excluded.add("com.nr.instrumentation.apache.camel.NRProcessorWrapper"); | ||
excluded.add("com.nr.instrumentation.apache.camel.NRAsyncProcessorWrapper"); | ||
excluded.add("org.apache.camel.impl.engine.SharedCamelInternalProcessor"); | ||
excluded.add("org.apache.camel.impl.engine.CamelInternalProcessor"); | ||
} | ||
|
||
@Override | ||
public boolean isMatch(ClassLoader loader, ClassReader cr) { | ||
|
||
return !excluded.contains(cr.getClassName()); | ||
} | ||
|
||
@Override | ||
public boolean isMatch(Class<?> clazz) { | ||
return !excluded.contains(clazz.getName()); | ||
} | ||
|
||
@Override | ||
public Collection<String> getClassNames() { | ||
|
||
return Collections.emptyList(); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...java/com/newrelic/instrumentation/labs/apache/camel/processors/ProcessorClassMatcher.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,36 @@ | ||
package com.newrelic.instrumentation.labs.apache.camel.processors; | ||
|
||
import java.util.Collection; | ||
|
||
import com.newrelic.agent.deps.org.objectweb.asm.ClassReader; | ||
import com.newrelic.agent.instrumentation.classmatchers.AndClassMatcher; | ||
import com.newrelic.agent.instrumentation.classmatchers.ClassMatcher; | ||
import com.newrelic.agent.instrumentation.classmatchers.InterfaceMatcher; | ||
|
||
public class ProcessorClassMatcher extends ClassMatcher { | ||
|
||
private AndClassMatcher matcher = null; | ||
private static final String interfaceName = "org.apache.camel.Processor"; | ||
|
||
public ProcessorClassMatcher() { | ||
ExcludedClassMatcher excluded = new ExcludedClassMatcher(); | ||
InterfaceMatcher interfaceMatcher = new InterfaceMatcher(interfaceName); | ||
matcher = new AndClassMatcher(excluded,interfaceMatcher); | ||
} | ||
|
||
@Override | ||
public boolean isMatch(ClassLoader loader, ClassReader cr) { | ||
return matcher.isMatch(loader, cr); | ||
} | ||
|
||
@Override | ||
public boolean isMatch(Class<?> clazz) { | ||
return matcher.isMatch(clazz); | ||
} | ||
|
||
@Override | ||
public Collection<String> getClassNames() { | ||
return matcher.getClassNames(); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...om/newrelic/instrumentation/labs/apache/camel/processors/ProcessorClassMethodMatcher.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,31 @@ | ||
package com.newrelic.instrumentation.labs.apache.camel.processors; | ||
|
||
import com.newrelic.agent.instrumentation.classmatchers.ClassAndMethodMatcher; | ||
import com.newrelic.agent.instrumentation.classmatchers.ClassMatcher; | ||
import com.newrelic.agent.instrumentation.classmatchers.InterfaceMatcher; | ||
import com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher; | ||
import com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher; | ||
|
||
public class ProcessorClassMethodMatcher implements ClassAndMethodMatcher { | ||
|
||
private ProcessorClassMatcher classMatcher; | ||
private NameMethodMatcher methodMatcher; | ||
private static final String methodName = "process"; | ||
|
||
public ProcessorClassMethodMatcher() { | ||
classMatcher = new ProcessorClassMatcher(); | ||
methodMatcher = new NameMethodMatcher(methodName); | ||
} | ||
|
||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return classMatcher; | ||
} | ||
|
||
@Override | ||
public MethodMatcher getMethodMatcher() { | ||
return methodMatcher; | ||
} | ||
|
||
|
||
} |
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
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
Oops, something went wrong.