use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.
the class ClassLoaderClassTransformer method buildBaseClassLoaderPatcherPackage.
private WeavePackage buildBaseClassLoaderPatcherPackage() {
WeavePackageConfig baseClassloaderPatcher = WeavePackageConfig.builder().name("base-agent-classloader-patcher").errorHandleClassNode(LogAndReturnOriginal.ERROR_HANDLER_NODE).extensionClassTemplate(extensionTemplate).build();
List<byte[]> baseWeavePackageBytes = new ArrayList<>();
try {
// 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[] baseClassloaderPatcherInstrumentationBytes = WeaveUtils.getClassBytesFromClassLoaderResource(AgentClassLoaderBaseInstrumentation.class.getName(), ClassLoaderClassTransformer.class.getClassLoader());
baseWeavePackageBytes.add(baseClassloaderPatcherInstrumentationBytes);
} catch (IOException e) {
Agent.LOG.log(Level.FINE, e, "Unable to initialize agent classloader instrumentation");
}
return new WeavePackage(baseClassloaderPatcher, baseWeavePackageBytes);
}
use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.
the class ClassWeaverService method loadInternalWeavePackages.
/**
* Load all the weave packages embedded in the agent jar.
*/
private Collection<ClassMatchVisitorFactory> loadInternalWeavePackages() {
final Collection<ClassMatchVisitorFactory> matchers = Sets.newConcurrentHashSet();
Collection<String> jarFileNames = AgentJarHelper.findAgentJarFileNames(Pattern.compile("instrumentation\\/(.*).jar"));
if (jarFileNames.isEmpty()) {
LOG.log(Level.SEVERE, "No instrumentation packages were found in the agent.");
} else {
LOG.log(Level.FINE, "Loading {0} instrumentation packages", jarFileNames.size());
}
int partitions = (jarFileNames.size() < PARTITIONS) ? jarFileNames.size() : PARTITIONS;
// Note: An ExecutorService would be better suited for this work but we are
// specifically not using it here to prevent the ConcurrentCallablePointCut
// from being loaded too early
final CountDownLatch executorCountDown = new CountDownLatch(partitions);
List<Set<String>> weavePackagePartitions = partitionInstrumentationJars(jarFileNames, partitions);
for (final Set<String> weavePackageJars : weavePackagePartitions) {
Runnable loadWeavePackagesRunnable = new Runnable() {
@Override
public void run() {
try {
for (final String name : weavePackageJars) {
URL instrumentationUrl = BootstrapAgent.class.getResource('/' + name);
if (instrumentationUrl == null) {
Agent.LOG.error("Unable to find instrumentation jar: " + name);
} else {
try (InputStream inputStream = instrumentationUrl.openStream()) {
WeavePackage internalWeavePackage = createWeavePackage(inputStream, instrumentationUrl.toExternalForm());
if (null == internalWeavePackage) {
LOG.log(Level.FINEST, "internal weave package: {0} was null", instrumentationUrl.toExternalForm());
continue;
} else if (internalWeavePackage.getPackageViolations().size() > 0) {
LOG.log(Level.FINER, "skip loading weave package: {0}", internalWeavePackage.getName());
for (WeaveViolation violation : internalWeavePackage.getPackageViolations()) {
LOG.log(Level.FINER, "\t violation: {0}", violation);
}
} else {
LOG.log(Level.FINER, "adding weave package: {0}", internalWeavePackage.getName());
internalWeavePackages.add(internalWeavePackage.getName());
weavePackageManager.register(internalWeavePackage);
}
} catch (Throwable t) {
LOG.log(Level.FINER, t, "unable to load weave package jar {0}", instrumentationUrl);
}
}
}
} catch (Throwable t) {
LOG.log(Level.FINER, t, "A thread loading weaved packages threw an error");
} finally {
executorCountDown.countDown();
}
}
};
new Thread(loadWeavePackagesRunnable).start();
}
try {
// Wait for all partitions to complete
executorCountDown.await();
LOG.log(Level.FINE, "Loaded {0} internal instrumentation packages", internalWeavePackages.size());
} catch (InterruptedException e) {
LOG.log(Level.FINE, e, "Interrupted while waiting for instrumentation packages.");
}
return matchers;
}
use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.
the class ClassWeaverService method unloadExternalWeavePackages.
/**
* Unload the given external instrumentation packages
*/
private Collection<ClassMatchVisitorFactory> unloadExternalWeavePackages(Set<String> removedFilePaths) {
LOG.log(Level.INFO, "ClassWeaveService removing {0} weave packages.", removedFilePaths.size());
Collection<ClassMatchVisitorFactory> matchers = Sets.newHashSetWithExpectedSize(removedFilePaths.size());
for (String removedFilePath : removedFilePaths) {
String weavePackageName = this.externalWeavePackages.get(removedFilePath);
if (this.internalWeavePackages.contains(weavePackageName)) {
Agent.LOG.log(Level.FINER, "Attempted to unload internal weave package {0} -- {1}. Ignoring request.", weavePackageName, removedFilePath);
continue;
}
WeavePackage externalPackage = weavePackageManager.deregister(weavePackageName);
if (null == externalPackage) {
Agent.LOG.log(Level.FINER, "Attempted to unload non-existent weave package {0} -- {1}. Ignoring request.", weavePackageName, removedFilePath);
} else {
externalWeavePackages.remove(removedFilePath);
}
}
return matchers;
}
Aggregations