use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.
the class ClassLoaderClassTransformer method buildCheckAccessPatcherPackage.
private WeavePackage buildCheckAccessPatcherPackage() {
WeavePackageConfig checkAccessPatcher = WeavePackageConfig.builder().name("check-access-patcher").errorHandleClassNode(LogAndReturnOriginal.ERROR_HANDLER_NODE).extensionClassTemplate(extensionTemplate).build();
List<byte[]> checkAccessPackageBytes = new ArrayList<>();
try {
// us to selectively get around security manager checks from instrumentation modules (if config is enabled)
if (System.getSecurityManager() != null) {
ClassTransformerConfig config = ServiceFactory.getConfigService().getDefaultAgentConfig().getClassTransformerConfig();
if (config.isGrantPackageAccess()) {
// Grab the bytes of our Agent Classloader instrumentation class. Note: This call uses "findResource" but
// this is ok because we are not under a classloader lock at this point, we are still in the premain()
byte[] classloaderPackageAccessInstrumentationBytes = WeaveUtils.getClassBytesFromClassLoaderResource(ClassLoaderPackageAccessInstrumentation.class.getName(), ClassLoaderClassTransformer.class.getClassLoader());
checkAccessPackageBytes.add(classloaderPackageAccessInstrumentationBytes);
return new WeavePackage(checkAccessPatcher, checkAccessPackageBytes);
}
}
} catch (IOException e) {
Agent.LOG.log(Level.FINE, e, "Unable to initialize agent classloader instrumentation");
}
return null;
}
use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.
the class ClassWeaverService method loadExternalWeavePackages.
/**
* Load new instrumentation packages from disk and put any new matchers in the matchers collection.
*
* @param weaveExtensions
*/
private Collection<ClassMatchVisitorFactory> loadExternalWeavePackages(Collection<File> weaveExtensions) {
Collection<ClassMatchVisitorFactory> matchers = new HashSet<>();
for (File weaveExtension : weaveExtensions) {
try (JarInputStream stream = new JarInputStream(new FileInputStream(weaveExtension))) {
AgentConfig agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
WeavePackageConfig weaveConfig = createWeavePackageConfig(stream, weaveExtension.getAbsolutePath(), instrumentation, WeavePackageType.CUSTOM, agentConfig);
ClassTransformerConfig classTransformerConfig = agentConfig.getClassTransformerConfig();
String instrName = weaveConfig.getName();
if (weavePackageManager.isRegistered(instrName)) {
weavePackageManager.deregister(instrName);
this.externalWeavePackages.remove(weaveExtension.getAbsolutePath());
}
if (!classTransformerConfig.isWeavePackageEnabled(weaveConfig)) {
if (weaveConfig.isEnabled()) {
// Only log this if the module has been explicitly disabled (not if it is disabled by default)
LOG.log(Level.INFO, "Instrumentation {0} is disabled. Skipping.", instrName);
}
continue;
}
WeavePackage externalPackage = WeavePackage.createWeavePackage(stream, weaveConfig);
if (externalPackage.getPackageViolations().size() > 0) {
LOG.log(Level.FINER, "skip loading external weave package: {0}", instrName);
for (WeaveViolation violation : externalPackage.getPackageViolations()) {
LOG.log(Level.FINER, "\t{0}", violation);
}
} else {
weavePackageManager.register(externalPackage);
externalWeavePackages.put(weaveExtension.getAbsolutePath(), weaveConfig.getName());
}
} catch (Exception e) {
LOG.log(Level.FINE, e, "Error reading weave extension {0}", weaveExtension.getAbsolutePath());
}
}
return matchers;
}
use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.
the class ClassWeaverService method createWeavePackage.
/**
* Create a weave package from a jar source.
*
* @param inputStream The JarInputStream to read from.
* @param source URL where the jar was read from.
*/
private WeavePackage createWeavePackage(InputStream inputStream, String source) throws Exception {
JarInputStream jarStream = new JarInputStream(inputStream);
AgentConfig agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
WeavePackageConfig weavePackageConfig = createWeavePackageConfig(jarStream, source, instrumentation, WeavePackageType.INTERNAL, agentConfig);
ClassTransformerConfig classTransformerConfig = agentConfig.getClassTransformerConfig();
String weavePackageName = weavePackageConfig.getName();
if (!classTransformerConfig.isWeavePackageEnabled(weavePackageConfig)) {
if (weavePackageConfig.isEnabled()) {
// Only log this if the module has been explicitly disabled (not if it is disabled by default)
LOG.log(Level.INFO, "Instrumentation {0} is disabled. Skipping.", weavePackageName);
}
return null;
}
if ("com.newrelic.instrumentation.jcache-1.0.0".equals(weavePackageName)) {
Boolean jcacheDatastoreEnabled = agentConfig.getValue("class_transformer.com.newrelic.instrumentation.jcache-datastore-1.0.0.enabled", Boolean.FALSE);
if (jcacheDatastoreEnabled) {
LOG.log(Level.INFO, " Instrumentation {0} is disabled since {1} is enabled. Skipping.", weavePackageName, "com.newrelic.instrumentation.jcache-datastore-1.0.0");
return null;
}
}
WeavePackage weavePackage = CachedWeavePackage.createWeavePackage(new URL(source), jarStream, weavePackageConfig);
return weavePackage;
}
use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.
the class ClassWeaverService method registerInstrumentationCloseable.
/**
* Register a closable which will run if/when a {@link WeavePackage} is deregistered.
*/
public void registerInstrumentationCloseable(String instrumentationName, Closeable closeable) {
WeavePackage weavePackage = weavePackageManager.getWeavePackage(instrumentationName);
listener.registerInstrumentationCloseable(instrumentationName, weavePackage, closeable);
}
use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.
the class TestCachedWeavePackage method testCachedWeavePackage.
/**
* Test to make sure internal WeavePackage caching is working.
*/
@Test
public void testCachedWeavePackage() {
WeavePackage internalPackage = manager.getWeavePackage("com.newrelic.instrumentation.servlet-2.4");
Assert.assertNotNull(internalPackage);
Assert.assertTrue("Class type is " + internalPackage.getClass().getName(), internalPackage instanceof CachedWeavePackage);
WeavePackage externalPackage = manager.getWeavePackage("Weave Test");
Assert.assertNotNull(externalPackage);
Assert.assertFalse(externalPackage instanceof CachedWeavePackage);
}
Aggregations