Search in sources :

Example 6 with ProfileElementInstance

use of com.creditease.uav.profiling.spi.ProfileElementInstance in project uavstack by uavorg.

the class MSCPProfileHandler method doProfiling.

@Override
public void doProfiling(ProfileElement elem, ProfileContext context) {
    UAVServer.ServerVendor sv = (UAVServer.ServerVendor) UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR);
    // only support MSCP Application
    if (sv != UAVServer.ServerVendor.MSCP) {
        return;
    }
    if (!ProfileConstants.PROELEM_COMPONENT.equals(elem.getElemId())) {
        return;
    }
    InterceptContext ic = context.get(InterceptContext.class);
    if (ic == null) {
        this.logger.warn("Profile:Annotation FAILs as No InterceptContext available", null);
        return;
    }
    /**
     * 1. get webappclassloader
     */
    ClassLoader webappclsLoader = (ClassLoader) ic.get(InterceptConstants.WEBAPPLOADER);
    if (null == webappclsLoader) {
        this.logger.warn("Profile:JARS FAILs as No webappclsLoader available", null);
        return;
    }
    Collection<ClassLoader> clList = ConfigurationManager.getInstance().getFeatureClassLoader();
    clList.add(webappclsLoader);
    ClassLoader[] allcl = new ClassLoader[clList.size()];
    allcl = clList.toArray(allcl);
    /**
     * 2. see what kind of components we could get via annotations or interface or parentClass
     */
    FastClasspathScanner fcs = new FastClasspathScanner(allcl, "com.creditease.uav", "com.creditease.agent", "org.uavstack");
    fcs.scan();
    /**
     * 3. get MSCPServlets profile info
     */
    InterceptContext itContext = context.get(InterceptContext.class);
    String appRootPath = (String) itContext.get(InterceptConstants.BASEPATH);
    String appName = (String) itContext.get(InterceptConstants.APPNAME);
    for (String componentClassName : componentClassMap.keySet()) {
        // set the instance id = simple name of the annotation class
        ProfileElementInstance inst = elem.getInstance(componentClassName);
        ComponentProcessor cp = componentClassMap.get(componentClassName);
        cp.handle(componentClassName, appName, inst, fcs);
    }
    /**
     * 4. load application info
     */
    loadAppInfo(elem, appRootPath, appName);
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) FastClasspathScanner(io.github.lukehutch.fastclasspathscanner.FastClasspathScanner) UAVServer(com.creditease.monitor.UAVServer)

Example 7 with ProfileElementInstance

use of com.creditease.uav.profiling.spi.ProfileElementInstance in project uavstack by uavorg.

the class ClientProfileHandler method getTargetURIInst.

/**
 * @param clientTargetURI
 * @return
 */
private ProfileElementInstance getTargetURIInst(URI clientTargetURI, ProfileElement elem) {
    String host = clientTargetURI.getHost();
    int port = clientTargetURI.getPort();
    // String dnsName = null;
    // 
    // if (!NetworkHelper.isIPV4(host)) {
    // String tmp = NetworkHelper.getIPByDNS(host);
    // if (tmp != null) {
    // dnsName = host;
    // host = tmp;
    // }
    // }
    String uri = clientTargetURI.getScheme() + "://" + host;
    if (port > 0) {
        uri += ":" + port;
    }
    // http://xxxxx/yyyyy@client type
    ProfileElementInstance pei = elem.getInstance(uri);
    return pei;
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance)

Example 8 with ProfileElementInstance

use of com.creditease.uav.profiling.spi.ProfileElementInstance in project uavstack by uavorg.

the class ClientProfileHandler method doProfiling.

@SuppressWarnings("unchecked")
@Override
public void doProfiling(ProfileElement elem, ProfileContext context) {
    if (!ProfileConstants.PROELEM_CLIENT.equals(elem.getElemId())) {
        return;
    }
    String clientURL = (String) context.get(ProfileConstants.PC_ARG_CLIENT_URL);
    if (clientURL == null) {
        return;
    }
    long curTime = System.currentTimeMillis();
    String type = (String) context.get(ProfileConstants.PC_ARG_CLIENT_TYPE);
    ProfileElementInstance pei = null;
    String actionPath = null;
    // for http://
    if (clientURL.indexOf("http://") == 0) {
        URI clientTargetURI = null;
        try {
            clientTargetURI = new URI(clientURL);
        } catch (URISyntaxException e) {
            return;
        }
        pei = getTargetURIInst(clientTargetURI, elem);
        actionPath = clientTargetURI.getPath();
    } else // for none jdbc /redis /mongo
    {
        pei = elem.getInstance(clientURL);
        actionPath = "ap";
    }
    pei.setValue("ts", curTime);
    String ctype = (String) pei.getValues().get("type");
    if (ctype == null) {
        pei.setValue("type", type);
    } else {
        if (ctype.indexOf(type) == -1) {
            pei.setValue("type", ctype + "," + type);
        }
    }
    /**
     * NOTE: sometimes the ngnix or proxys or tomcat will tell us the server type in reponse header
     */
    String server = (String) context.get(ProfileConstants.PC_ARG_CLIENT_TARGETSERVER);
    if (!StringHelper.isEmpty(server)) {
        pei.setValue("svr", server);
    }
    Map<String, Object> urls = (Map<String, Object>) pei.getValues().get("urls");
    if (urls == null) {
        int limit = DataConvertHelper.toInt(System.getProperty("com.creditease.uav.profile.eleminst.client.urls.limit"), 100);
        urls = new LimitLinkedHashMap<String, Object>(limit);
        pei.setValue("urls", urls);
    }
    Map<String, Object> urlAttrs = (Map<String, Object>) urls.get(actionPath);
    if (urlAttrs == null) {
        urlAttrs = new HashMap<String, Object>();
        urls.put(actionPath, urlAttrs);
    }
    urlAttrs.put("ts", curTime);
    String action = (String) context.get(ProfileConstants.PC_ARG_CLIENT_ACTION);
    Integer rc = (Integer) context.get(ProfileConstants.PC_ARG_CLIENT_RC);
    if (rc == 1) {
        urlAttrs.put(MonitorServerUtil.getActionTag(action), curTime);
    } else if (rc == -1) {
        urlAttrs.put(MonitorServerUtil.getActionErrorTag(action), curTime);
    }
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HashMap(java.util.HashMap) LimitLinkedHashMap(com.creditease.uav.util.LimitLinkedHashMap) Map(java.util.Map)

Example 9 with ProfileElementInstance

use of com.creditease.uav.profiling.spi.ProfileElementInstance in project uavstack by uavorg.

the class ComponentProfileHandler method loadCommonInfoFromWebXML.

/**
 * loadCommonInfoFromWebXML
 *
 * @param elem
 * @param dpInstances
 * @param webAppRoot
 */
protected void loadCommonInfoFromWebXML(ProfileElement elem, Map<String, DescriptorProcessor> dpInstances, String webAppRoot, ProfileContext context) {
    // append a "webapp" instance to collect web.xml related common info
    ProfileElementInstance inst = elem.getInstance("webapp");
    DescriptorProcessor webxmlDP = dpInstances.get(WebXmlProcessor.class.getName());
    InterceptContext ic = context.get(InterceptContext.class);
    String contextpath = (String) ic.get(InterceptConstants.CONTEXTPATH);
    // get the web application display name as the application name
    String appname = (String) ic.get(InterceptConstants.APPNAME);
    if (appname != null && appname.length() > 0) {
        inst.setValue("appname", appname);
    } else {
        webxmlDP.parseToPEIWithValueKey(inst, "appname", webAppRoot, "/web-app/display-name", XMLNodeType.ELEMENT_NODE);
    }
    // get the web application description as app description
    webxmlDP.parseToPEIWithValueKey(inst, "appdes", webAppRoot, "/web-app/description", XMLNodeType.ELEMENT_NODE);
    // get the spring context config location
    webxmlDP.parseToPEIWithValueKey(inst, "springctx", webAppRoot, "/web-app/context-param[param-name='contextConfigLocation']/param-value", XMLNodeType.ELEMENT_NODE);
    // get the real path of application context root
    inst.setValue("webapproot", webAppRoot);
    // get the app Http URL
    inst.setValue("appurl", getServiceURI(contextpath));
    // get the vender
    inst.setValue("vender", UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR));
    // get app group
    getAppGroup(inst);
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext)

Example 10 with ProfileElementInstance

use of com.creditease.uav.profiling.spi.ProfileElementInstance in project uavstack by uavorg.

the class ComponentProfileHandler method doProfiling.

@Override
public void doProfiling(ProfileElement elem, ProfileContext context) {
    UAVServer.ServerVendor sv = (UAVServer.ServerVendor) UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR);
    // only support JEE Application, not support MSCP Application
    if (sv == UAVServer.ServerVendor.MSCP) {
        return;
    }
    if (!ProfileConstants.PROELEM_COMPONENT.equals(elem.getElemId())) {
        return;
    }
    InterceptContext ic = context.get(InterceptContext.class);
    if (ic == null) {
        this.logger.warn("Profile:Annotation FAILs as No InterceptContext available", null);
        return;
    }
    /**
     * 1.get webappclassloader
     */
    ClassLoader webappclsLoader = (ClassLoader) ic.get(InterceptConstants.WEBAPPLOADER);
    if (null == webappclsLoader) {
        this.logger.warn("Profile:JARS FAILs as No webappclsLoader available", null);
        return;
    }
    /**
     * 1.5 for other none JEE or Spring tech profiling
     */
    dubboProfileHandler.doProfiling(elem, context);
    /**
     * 2.load available annotation classes
     */
    Map<String, Class<?>> annoAvailableClasses = new HashMap<String, Class<?>>();
    for (String annoClsName : componentClassNames) {
        try {
            Class<?> c = webappclsLoader.loadClass(annoClsName);
            annoAvailableClasses.put(annoClsName, c);
        } catch (ClassNotFoundException e) {
            // ignore
            if (this.logger.isDebugable()) {
                this.logger.warn("Annotation Class [" + annoClsName + "] is not found in web application [" + elem.getRepository().getProfile().getId() + "]", e);
            }
        }
    }
    /**
     * 2.1 load available interface classes
     */
    Map<String, Class<?>> interfaceAvailableClasses = new HashMap<String, Class<?>>();
    for (String interfaceClsName : componentInterfaceNames) {
        try {
            Class<?> c = webappclsLoader.loadClass(interfaceClsName);
            interfaceAvailableClasses.put(interfaceClsName, c);
        } catch (ClassNotFoundException e) {
            // ignore
            if (this.logger.isDebugable()) {
                this.logger.warn("Interface Class [" + interfaceClsName + "] is not found in web application [" + elem.getRepository().getProfile().getId() + "]", e);
            }
        }
    }
    /**
     * 3. see what kind of components we could get via annotations
     */
    UAVServer.ServerVendor vendor = (ServerVendor) UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR);
    /**
     * NOTE: currently for spring boot, we use its base classloader as the webappclassloader, and should scan all
     * packages
     */
    ClassLoader[] classLoaders = null;
    if (vendor != UAVServer.ServerVendor.SPRINGBOOT) {
        classLoaders = new ClassLoader[] { webappclsLoader };
        String scanPackages = System.getProperty("com.creditease.uav.uavmof.profile.package.header");
        if (StringHelper.isEmpty(scanPackages)) {
            scanPackage[0] = "com";
        } else {
            scanPackage = scanPackages.split(",");
        }
    } else {
        scanPackage[0] = "";
    }
    FastClasspathScanner fcs = new FastClasspathScanner(classLoaders, scanPackage);
    fcs.scan();
    // store FastClasspathScanner instance into ProfileContext
    context.put(FastClasspathScanner.class, fcs);
    /**
     * 4.tide components we get from annotations & deployment descriptors
     */
    // store the DescriptorProcessor instance for better performance
    Map<String, DescriptorProcessor> dpInstances = new LinkedHashMap<String, DescriptorProcessor>();
    // get web application root
    InterceptContext itContext = context.get(InterceptContext.class);
    String webAppRoot = (String) itContext.get(InterceptConstants.BASEPATH);
    for (String componentClassName : componentClassNames) {
        // set the instance id = simple name of the annotation class
        ProfileElementInstance inst = elem.getInstance(componentClassName);
        // load componentsByAnno first
        loadComponentsByAnno(context, webappclsLoader, fcs, annoAvailableClasses, componentClassName, inst);
    }
    for (String componentClassName : componentInterfaceNames) {
        // set the instance id = simple name of the annotation class
        ProfileElementInstance inst = elem.getInstance(componentClassName);
        // load componentsByInterface
        loadComponentsByInterface(context, webappclsLoader, fcs, interfaceAvailableClasses, componentClassName, inst);
    }
    String[] allComponentNames = new String[componentClassNames.length + componentInterfaceNames.length];
    System.arraycopy(componentClassNames, 0, allComponentNames, 0, componentClassNames.length);
    System.arraycopy(componentInterfaceNames, 0, allComponentNames, componentClassNames.length, componentInterfaceNames.length);
    for (String componentClassName : allComponentNames) {
        ProfileElementInstance inst = elem.getInstance(componentClassName);
        // try to load componentsByDescriptor
        loadComponentsByDescriptor(dpInstances, webAppRoot, context, componentClassName, inst);
        // try to load componentsByDynamic Creation, currently is for servlet 3.x and webservice
        loadComponentsByDynamic(itContext, componentClassName, inst, context, annoAvailableClasses, fcs);
        /**
         * NOTE: in order to control the monitor data url, we need collect ther servlet url to help to identify if
         * an url is a service url
         */
        if (componentClassName.equalsIgnoreCase("javax.servlet.annotation.WebServlet")) {
            collectServletInfoForMonitor(inst);
        }
        /**
         * collectProfileServiceMap
         */
        collectProfileServiceMap(componentClassName, inst);
    }
    /**
     * 4.1 add common info instance from descriptor to profile element
     */
    // web.xml related common info
    loadCommonInfoFromWebXML(elem, dpInstances, webAppRoot, context);
    /**
     * 5.confirm there is update
     */
    elem.getRepository().setUpdate(true);
    // release resources quickly
    dpInstances.clear();
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance) FastClasspathScanner(io.github.lukehutch.fastclasspathscanner.FastClasspathScanner) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UAVServer(com.creditease.monitor.UAVServer) LinkedHashMap(java.util.LinkedHashMap) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) ServerVendor(com.creditease.monitor.UAVServer.ServerVendor) ServerVendor(com.creditease.monitor.UAVServer.ServerVendor)

Aggregations

ProfileElementInstance (com.creditease.uav.profiling.spi.ProfileElementInstance)13 InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)5 UAVServer (com.creditease.monitor.UAVServer)3 Map (java.util.Map)3 FastClasspathScanner (io.github.lukehutch.fastclasspathscanner.FastClasspathScanner)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ServerVendor (com.creditease.monitor.UAVServer.ServerVendor)1 DubboServiceProfileInfo (com.creditease.uav.profiling.handlers.dubbo.DubboServiceProfileInfo)1 LogProfileInfo (com.creditease.uav.profiling.handlers.log.LogProfileInfo)1 ProfileElement (com.creditease.uav.profiling.spi.ProfileElement)1 LimitLinkedHashMap (com.creditease.uav.util.LimitLinkedHashMap)1 File (java.io.File)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 LinkedList (java.util.LinkedList)1