Search in sources :

Example 1 with InstrumentationContextManager

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);
    }
}
Also used : InstrumentationContextManager(com.newrelic.agent.instrumentation.context.InstrumentationContextManager) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with InstrumentationContextManager

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()));
}
Also used : HashMap(java.util.HashMap) InstrumentationContextManager(com.newrelic.agent.instrumentation.context.InstrumentationContextManager) InstrumentationProxy(com.newrelic.agent.InstrumentationProxy) JarCollectorService(com.newrelic.agent.service.module.JarCollectorService) MockServiceManager(com.newrelic.agent.MockServiceManager) Test(org.junit.Test)

Example 3 with InstrumentationContextManager

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));
}
Also used : HashMap(java.util.HashMap) InstrumentationContextManager(com.newrelic.agent.instrumentation.context.InstrumentationContextManager) Test(org.junit.Test)

Example 4 with InstrumentationContextManager

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));
}
Also used : InstrumentationContextManager(com.newrelic.agent.instrumentation.context.InstrumentationContextManager) Test(org.junit.Test)

Example 5 with InstrumentationContextManager

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>());
}
Also used : InstrumentationContextManager(com.newrelic.agent.instrumentation.context.InstrumentationContextManager) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

InstrumentationContextManager (com.newrelic.agent.instrumentation.context.InstrumentationContextManager)8 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 InstrumentationProxy (com.newrelic.agent.InstrumentationProxy)2 MockServiceManager (com.newrelic.agent.MockServiceManager)2 File (java.io.File)2 HashSet (java.util.HashSet)2 MockCoreService (com.newrelic.agent.MockCoreService)1 AgentConfig (com.newrelic.agent.config.AgentConfig)1 ClassTransformerService (com.newrelic.agent.instrumentation.ClassTransformerService)1 ClassRetransformer (com.newrelic.agent.instrumentation.custom.ClassRetransformer)1 ClassWeaverService (com.newrelic.agent.instrumentation.weaver.ClassWeaverService)1 JmxService (com.newrelic.agent.jmx.JmxService)1 JarCollectorService (com.newrelic.agent.service.module.JarCollectorService)1 FileNotFoundException (java.io.FileNotFoundException)1 Before (org.junit.Before)1