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);
}
}
}
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;
}
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);
}
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() {
}
});
}
Aggregations