Search in sources :

Example 1 with InstrumentationContextClassMatcherHelper

use of com.newrelic.agent.instrumentation.context.InstrumentationContextClassMatcherHelper 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 InstrumentationContextClassMatcherHelper

use of com.newrelic.agent.instrumentation.context.InstrumentationContextClassMatcherHelper 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)

Aggregations

InstrumentationContextClassMatcherHelper (com.newrelic.agent.instrumentation.context.InstrumentationContextClassMatcherHelper)2 ClassRetransformer (com.newrelic.agent.instrumentation.custom.ClassRetransformer)2 ExtensionClassAndMethodMatcher (com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher)1 JmxService (com.newrelic.agent.jmx.JmxService)1 ReinstrumentResult (com.newrelic.agent.reinstrument.ReinstrumentResult)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1