Search in sources :

Example 1 with ClassRetransformer

use of com.newrelic.agent.instrumentation.custom.ClassRetransformer in project newrelic-java-agent by newrelic.

the class ExtensionService method reloadCustomExtensionsIfModified.

private void reloadCustomExtensionsIfModified() {
    File[] xmlFiles = getExtensionFiles(ExtensionFileTypes.XML.getFilter());
    File[] ymlFiles = getExtensionFiles(ExtensionFileTypes.YML.getFilter());
    // element count start at -1 to ensure fileModified is true the first time
    boolean fileModified = (xmlFiles.length + ymlFiles.length) != elementCount;
    if (!fileModified) {
        for (File file : xmlFiles) {
            fileModified |= (file.lastModified() <= System.currentTimeMillis() && lastReloaded < file.lastModified());
        }
        for (File file : ymlFiles) {
            fileModified |= (file.lastModified() <= System.currentTimeMillis() && lastReloaded < file.lastModified());
        }
    }
    // if you are changing be sure to test without an extensions directory
    if (fileModified) {
        lastReloaded = System.currentTimeMillis();
        elementCount = xmlFiles.length + ymlFiles.length;
        pointCuts.clear();
        HashMap<String, Extension> allExtensions = new HashMap<>(internalExtensions);
        loadValidExtensions(xmlFiles, extensionParsers.getXmlParser(), allExtensions);
        loadValidExtensions(ymlFiles, extensionParsers.getYamlParser(), allExtensions);
        Set<Extension> externalExtensions = new HashSet<>(allExtensions.values());
        externalExtensions.removeAll(internalExtensions.values());
        Set<Extension> oldExtensions = extensions;
        extensions = Collections.unmodifiableSet(externalExtensions);
        JmxService jmxService = ServiceFactory.getJmxService();
        if (jmxService != null) {
            jmxService.reloadExtensions(oldExtensions, extensions);
        }
        for (Extension extension : allExtensions.values()) {
            pointCuts.addAll(extension.getInstrumentationMatchers());
        }
        ClassRetransformer retransformer = ServiceFactory.getClassTransformerService().getLocalRetransformer();
        if (retransformer != null) {
            Class<?>[] allLoadedClasses = ServiceFactory.getCoreService().getInstrumentation().getAllLoadedClasses();
            retransformer.setClassMethodMatchers(pointCuts);
            InstrumentationContextClassMatcherHelper matcherHelper = new InstrumentationContextClassMatcherHelper();
            Set<Class<?>> classesToRetransform = ClassesMatcher.getMatchingClasses(retransformer.getMatchers(), matcherHelper, allLoadedClasses);
            ReinstrumentUtils.checkClassExistsAndRetransformClasses(new ReinstrumentResult(), Collections.<ExtensionClassAndMethodMatcher>emptyList(), null, classesToRetransform);
        }
    }
}
Also used : ClassRetransformer(com.newrelic.agent.instrumentation.custom.ClassRetransformer) InstrumentationContextClassMatcherHelper(com.newrelic.agent.instrumentation.context.InstrumentationContextClassMatcherHelper) HashMap(java.util.HashMap) JmxService(com.newrelic.agent.jmx.JmxService) File(java.io.File) HashSet(java.util.HashSet) ReinstrumentResult(com.newrelic.agent.reinstrument.ReinstrumentResult)

Example 2 with ClassRetransformer

use of com.newrelic.agent.instrumentation.custom.ClassRetransformer in project newrelic-java-agent by newrelic.

the class ClassTransformerServiceImpl method startClassTransformer.

private PointCutClassTransformer startClassTransformer(InstrumentationProxy instrProxy) throws Exception {
    boolean retransformSupported = isRetransformationSupported(instrProxy);
    PointCutClassTransformer classTransformer = new PointCutClassTransformer(instrProxy, retransformSupported);
    contextManager = InstrumentationContextManager.create(classLoaderClassTransformer, instrProxy, AgentBridge.class.getClassLoader() == null);
    contextManager.addContextClassTransformer(classTransformer.getMatcher(), classTransformer);
    for (PointCut pc : classTransformer.getPointcuts()) {
        Agent.LOG.log(Level.FINER, "pointcut {0} active", pc);
        pc.noticeTransformerStarted(classTransformer);
    }
    Agent.LOG.log(Level.FINE, "enabled {0} pointcuts", classTransformer.getPointcuts().size());
    // create the class transformer prepopulated with the extensions loaded from disk
    localRetransformer = new ClassRetransformer(contextManager);
    localRetransformer.setClassMethodMatchers(ServiceFactory.getExtensionService().getEnabledPointCuts());
    // create the retransformer for instrumentation we receive from the remote instrumentation service
    remoteRetransformer = new ClassRetransformer(contextManager);
    traceMatchTransformer = new TraceMatchTransformer(contextManager);
    StartableClassFileTransformer[] startableClassTransformers = new StartableClassFileTransformer[] { // new NewRelicClassLoaderClassTransformer(classTransformer.getClassReaderFlags()),
    new InterfaceMixinClassTransformer(classTransformer.getClassReaderFlags()) // new ClassLoaderClassTransformer(classTransformer.getClassReaderFlags())
    };
    for (StartableClassFileTransformer transformer : startableClassTransformers) {
        transformer.start(instrProxy, retransformSupported);
        classTransformers.add(transformer);
    }
    for (StartableClassFileTransformer transformer : InterfaceImplementationClassTransformer.getClassTransformers(classTransformer)) {
        transformer.start(instrProxy, retransformSupported);
        classTransformers.add(transformer);
    }
    return classTransformer;
}
Also used : ClassRetransformer(com.newrelic.agent.instrumentation.custom.ClassRetransformer) AgentBridge(com.newrelic.agent.bridge.AgentBridge)

Example 3 with ClassRetransformer

use of com.newrelic.agent.instrumentation.custom.ClassRetransformer in project newrelic-java-agent by newrelic.

the class RemoteInstrumentationServiceImpl method updateJvmWithExtension.

private void updateJvmWithExtension(Extension ext, ReinstrumentResult result) {
    List<ExtensionClassAndMethodMatcher> pointCuts = null;
    if (ext == null || !ext.isEnabled()) {
        // remove all if the extension is empty or disabled
        pointCuts = Collections.emptyList();
    } else {
        pointCuts = ExtensionConversionUtility.convertToEnabledPointCuts(Arrays.asList(ext), true, InstrumentationType.RemoteCustomXml, isLiveAttributesEnabled);
    }
    result.setPointCutsSpecified(pointCuts.size());
    // this set of classes to retransform will include any classes that matched the last set of matchers AND
    // classes matched with the new matchers
    ClassRetransformer remoteRetransformer = ServiceFactory.getClassTransformerService().getRemoteRetransformer();
    remoteRetransformer.setClassMethodMatchers(pointCuts);
    Class<?>[] allLoadedClasses = ServiceFactory.getCoreService().getInstrumentation().getAllLoadedClasses();
    InstrumentationContextClassMatcherHelper matcherHelper = new InstrumentationContextClassMatcherHelper();
    Set<Class<?>> classesToRetransform = ClassesMatcher.getMatchingClasses(remoteRetransformer.getMatchers(), matcherHelper, allLoadedClasses);
    ReinstrumentUtils.checkClassExistsAndRetransformClasses(result, pointCuts, ext, classesToRetransform);
}
Also used : ClassRetransformer(com.newrelic.agent.instrumentation.custom.ClassRetransformer) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) InstrumentationContextClassMatcherHelper(com.newrelic.agent.instrumentation.context.InstrumentationContextClassMatcherHelper)

Example 4 with ClassRetransformer

use of com.newrelic.agent.instrumentation.custom.ClassRetransformer in project newrelic-java-agent by newrelic.

the class ExtensionServiceTest method setUpAgent.

@Before
public void setUpAgent() throws Exception {
    Map<String, Object> configMap = new HashMap<>();
    configMap.put(AgentConfigImpl.APP_NAME, EXTENSION_NAME);
    AgentConfig config = AgentConfigImpl.createAgentConfig(configMap);
    configService = ConfigServiceFactory.createConfigService(config, Collections.<String, Object>emptyMap());
    serviceManager = new MockServiceManager(configService);
    ServiceFactory.setServiceManager(serviceManager);
    final InstrumentationProxy instrumentationProxy = Mockito.mock(InstrumentationProxy.class);
    serviceManager.setCoreService(new MockCoreService() {

        @Override
        public InstrumentationProxy getInstrumentation() {
            return instrumentationProxy;
        }
    });
    Mockito.when(instrumentationProxy.isRetransformClassesSupported()).thenReturn(true);
    Mockito.when(instrumentationProxy.getAllLoadedClasses()).thenReturn(new Class[] {});
    extensionService = new ExtensionService(configService, ExtensionsLoadedListener.NOOP);
    serviceManager.setExtensionService(extensionService);
    serviceManager.setJmxService(Mockito.mock(JmxService.class));
    ClassTransformerService classTransformerService = serviceManager.getClassTransformerService();
    ClassRetransformer mockRetransformer = Mockito.mock(ClassRetransformer.class);
    Mockito.when(classTransformerService.getLocalRetransformer()).thenReturn(mockRetransformer);
    InstrumentationContextManager mockContextManager = Mockito.mock(InstrumentationContextManager.class);
    Mockito.when(classTransformerService.getContextManager()).thenReturn(mockContextManager);
    ClassWeaverService mockWeaverService = Mockito.mock(ClassWeaverService.class);
    Mockito.when(mockContextManager.getClassWeaverService()).thenReturn(mockWeaverService);
    Mockito.when(mockWeaverService.reloadExternalWeavePackages(Mockito.<File>anyCollection(), Mockito.<File>anyCollection())).thenReturn(new Runnable() {

        @Override
        public void run() {
        }
    });
}
Also used : ClassRetransformer(com.newrelic.agent.instrumentation.custom.ClassRetransformer) HashMap(java.util.HashMap) InstrumentationProxy(com.newrelic.agent.InstrumentationProxy) JmxService(com.newrelic.agent.jmx.JmxService) InstrumentationContextManager(com.newrelic.agent.instrumentation.context.InstrumentationContextManager) AgentConfig(com.newrelic.agent.config.AgentConfig) MockServiceManager(com.newrelic.agent.MockServiceManager) ClassTransformerService(com.newrelic.agent.instrumentation.ClassTransformerService) MockCoreService(com.newrelic.agent.MockCoreService) ClassWeaverService(com.newrelic.agent.instrumentation.weaver.ClassWeaverService) Before(org.junit.Before)

Aggregations

ClassRetransformer (com.newrelic.agent.instrumentation.custom.ClassRetransformer)4 InstrumentationContextClassMatcherHelper (com.newrelic.agent.instrumentation.context.InstrumentationContextClassMatcherHelper)2 JmxService (com.newrelic.agent.jmx.JmxService)2 HashMap (java.util.HashMap)2 InstrumentationProxy (com.newrelic.agent.InstrumentationProxy)1 MockCoreService (com.newrelic.agent.MockCoreService)1 MockServiceManager (com.newrelic.agent.MockServiceManager)1 AgentBridge (com.newrelic.agent.bridge.AgentBridge)1 AgentConfig (com.newrelic.agent.config.AgentConfig)1 ClassTransformerService (com.newrelic.agent.instrumentation.ClassTransformerService)1 InstrumentationContextManager (com.newrelic.agent.instrumentation.context.InstrumentationContextManager)1 ExtensionClassAndMethodMatcher (com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher)1 ClassWeaverService (com.newrelic.agent.instrumentation.weaver.ClassWeaverService)1 ReinstrumentResult (com.newrelic.agent.reinstrument.ReinstrumentResult)1 File (java.io.File)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1