use of com.newrelic.agent.instrumentation.context.InstrumentationContextManager in project newrelic-java-agent by newrelic.
the class ExtensionService method reloadWeaveInstrumentationIfModified.
private void reloadWeaveInstrumentationIfModified() {
File[] jarFiles = getExtensionFiles(ExtensionFileTypes.JAR.getFilter());
Collection<File> weaveFiles = Collections2.filter(Arrays.asList(jarFiles), new Predicate<File>() {
@Override
public boolean apply(File extension) {
boolean isWeave = JarExtension.isWeaveInstrumentation(extension);
return isWeave;
}
});
Collection<File> newWeaveFiles = new HashSet<>();
Collection<File> removedWeaveFiles = new HashSet<>();
for (File file : weaveFiles) {
Long timestamp = weaveExtensions.get(file);
if (timestamp == null || (timestamp <= System.currentTimeMillis() && timestamp != file.lastModified())) {
newWeaveFiles.add(file);
}
}
for (File file : weaveExtensions.keySet()) {
if (!weaveFiles.contains(file)) {
removedWeaveFiles.add(file);
}
}
if (newWeaveFiles.size() > 0 || removedWeaveFiles.size() > 0) {
weaveExtensions.clear();
for (File file : weaveFiles) {
weaveExtensions.put(file, file.lastModified());
}
InstrumentationContextManager contextManager = ServiceFactory.getClassTransformerService().getContextManager();
if (contextManager != null) {
contextManager.getClassWeaverService().reloadExternalWeavePackages(newWeaveFiles, removedWeaveFiles).run();
}
Agent.LOG.finer("Weave extension jars: " + weaveExtensions);
}
}
use of com.newrelic.agent.instrumentation.context.InstrumentationContextManager in project newrelic-java-agent by newrelic.
the class ClassTransformerConfigImplTest method classloaderExcludes.
@Test
public void classloaderExcludes() throws Exception {
Map<String, Object> classTransformerMap = new HashMap<>();
final String prefix = ClassTransformerConfigImplTest.class.getName();
classTransformerMap.put(ClassTransformerConfigImpl.CLASSLOADER_EXCLUDES, prefix + "$BadClassLoader, " + prefix + "$BadClassLoaderOther");
Map<String, Object> configMap = new HashMap<>();
configMap.put("class_transformer", classTransformerMap);
configMap.put(AgentConfigImpl.APP_NAME, "unittest");
AgentConfig config = AgentConfigImpl.createAgentConfig(configMap);
ClassTransformerConfig classTransformerConfig = config.getClassTransformerConfig();
classTransformerConfig = ClassTransformerConfigImpl.createClassTransformerConfig(classTransformerMap, true, false);
Set<String> exclusions = classTransformerConfig.getClassloaderExclusions();
// 6 included by default ClassTransformerConfigImpl#initializeClassloaderExcludes
Assert.assertEquals(8, exclusions.size());
ConfigService configService = ConfigServiceFactory.createConfigService(config, Collections.<String, Object>emptyMap());
MockServiceManager serviceManager = new MockServiceManager(configService);
ServiceFactory.setServiceManager(serviceManager);
JarCollectorService mockJarCollector = serviceManager.getJarCollectorService();
when(mockJarCollector.getSourceVisitor()).thenReturn(ClassMatchVisitorFactory.NO_OP_FACTORY);
InstrumentationContextManager icm = new InstrumentationContextManager(Mockito.mock(InstrumentationProxy.class));
Assert.assertFalse(icm.isClassloaderExcluded(new GoodClassLoader()));
assertTrue(icm.isClassloaderExcluded(new BadClassLoader()));
assertTrue(icm.isClassloaderExcluded(new BadClassLoaderOther()));
}
use of com.newrelic.agent.instrumentation.context.InstrumentationContextManager in project newrelic-java-agent by newrelic.
the class IncludeExcludeTest method testExcludes.
@Test
public void testExcludes() throws Exception {
Map<String, Object> configMap = createConfigMap();
Map<String, Object> classTransformerConfigMap = new HashMap<>();
classTransformerConfigMap.put("excludes", "^my/custom/.*");
configMap.put("class_transformer", classTransformerConfigMap);
createServiceManager(configMap);
InstrumentationContextManager manager = new InstrumentationContextManager(null);
// exclude file bundled with agent
final String builtInExcludeFile = "/META-INF/excludes";
Assert.assertFalse(builtInExcludeFile + " classes should be skipped.", manager.shouldTransform("org/objectweb/asm/FunWithBytecode", SOME_CLASSLOADER));
Assert.assertFalse(builtInExcludeFile + " classes should be skipped.", manager.shouldTransform("some/class/created/ByCGLIB$$/Fun", SOME_CLASSLOADER));
Assert.assertFalse("config exclude should be skipped", manager.shouldTransform("my/custom/exclude", SOME_CLASSLOADER));
}
use of com.newrelic.agent.instrumentation.context.InstrumentationContextManager in project newrelic-java-agent by newrelic.
the class IncludeExcludeTest method testCrypto.
@Test
public void testCrypto() throws Exception {
createServiceManager(createConfigMap());
InstrumentationContextManager manager = new InstrumentationContextManager(null);
Assert.assertTrue("Only javax/crypto/ classes should be skipped.", manager.shouldTransform("javax/cryptonotactuallycrypto", SOME_CLASSLOADER));
Assert.assertFalse("javax/crypto/ classes should be skipped.", manager.shouldTransform("javax/crypto/SomeCryptoClass", SOME_CLASSLOADER));
Assert.assertFalse("javax/crypto/ classes should be skipped.", manager.shouldTransform("javax/crypto/foo/bar/baz/AnotherCryptoClass", SOME_CLASSLOADER));
}
use of com.newrelic.agent.instrumentation.context.InstrumentationContextManager in project newrelic-java-agent by newrelic.
the class FunctionalWeaveTestUtils method loadWeaveTestInstrumentation.
public static void loadWeaveTestInstrumentation() throws Exception {
InstrumentationContextManager contextManager = ServiceFactory.getClassTransformerService().getContextManager();
Collection<File> weaveExtensions = new HashSet<>();
File file = new File("weave_test/build/libs/weave_test-1.0.jar");
if (!file.exists()) {
throw new FileNotFoundException(file.getAbsolutePath());
}
weaveExtensions.add(file);
Agent.LOG.log(Level.FINER, "Adding Weave Test package");
contextManager.getClassWeaverService().reloadExternalWeavePackages(weaveExtensions, new HashSet<File>());
}
Aggregations