Search in sources :

Example 51 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext in project uavstack by uavorg.

the class TomcatPlusIT method onResourceInit.

/**
 * on Resource Init
 *
 * @param args
 */
public void onResourceInit(Object... args) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        /**
         * after tomcat8, tomcat use ParallelWebappClassLoader instead of WebappClassLoader as it's webapp's
         * classloader, both of them are extends WebappClassLoaderBase
         */
        Class<?> cls = cl.loadClass("org.apache.catalina.loader.WebappClassLoaderBase");
        if (!cls.isAssignableFrom(cl.getClass())) {
            return;
        }
    } catch (ClassNotFoundException e) {
        /**
         * before tomcat7.0.64(include), WebappClassLoaderBase doesn't exist
         */
        if (!WebappClassLoader.class.isAssignableFrom(cl.getClass())) {
            return;
        }
    }
    /**
     * for Application Starting's Resource Init
     */
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_INIT);
    if (context == null) {
        return;
    }
    StandardContext sc = (StandardContext) context.get(InterceptConstants.CONTEXTOBJ);
    if (sc == null) {
        return;
    }
    context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    context.put(InterceptConstants.CONTEXTPATH, ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true));
    context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
    ServletContext sContext = (ServletContext) ReflectionHelper.getField(StandardContext.class, sc, "context", true);
    context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
    getBasePath(context, sContext);
    iSupport.doIntercept(context);
    InterceptContext ic = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_INIT);
    ic.put(InterceptConstants.CONTEXTOBJ, sc);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) ServletContext(javax.servlet.ServletContext)

Example 52 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext in project uavstack by uavorg.

the class TomcatPlusIT method onAppInit.

/**
 * onAppInit
 *
 * @param args
 */
public void onAppInit(Object... args) {
    StandardContext sc = (StandardContext) args[0];
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext ic = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_INIT);
    InterceptContext ic2 = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_CREATE);
    /**
     * NOTE: onAppInit, we put the Context Object into threadlocal, then all other later process for
     * PRE_WEBCONTAINER_INIT, which can get the object, as not everywhere we can get the object
     *
     * for example, the DataSource related injection
     */
    ic.put(InterceptConstants.CONTEXTOBJ, sc);
    ic2.put(InterceptConstants.CONTEXTOBJ, sc);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext)

Example 53 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext 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 54 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext 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)

Example 55 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext in project uavstack by uavorg.

the class DubboProfileHandler method doProfiling.

@SuppressWarnings("unchecked")
@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;
    }
    Map<String, DubboServiceProfileInfo> list = (Map<String, DubboServiceProfileInfo>) ic.get(HookConstants.DUBBO_PROFILE_LIST);
    if (list == null) {
        return;
    }
    // set the instance id = simple name of the annotation class
    ProfileElementInstance inst = elem.getInstance("com.alibaba.dubbo.config.spring.ServiceBean");
    for (String servicekey : list.keySet()) {
        DubboServiceProfileInfo dspi = list.get(servicekey);
        Map<String, Object> info = new LinkedHashMap<String, Object>();
        // get service class & dubbo application id
        String serviceClass = dspi.getServiceClass();
        info.put("dubboAppId", dspi.getDbAppId());
        info.put("group", dspi.getGroup());
        info.put("version", dspi.getVersion());
        info.put("servcls", serviceClass);
        info.put("servimplcls", dspi.getServiceImplClass());
        // get protocols
        Map<String, Object> protocols = new LinkedHashMap<String, Object>();
        info.put("protocols", protocols);
        for (DubboServiceProfileInfo.Protocol pro : dspi.getProtocols()) {
            Map<String, Object> pAttrs = new LinkedHashMap<String, Object>();
            addProtocolAttr(pAttrs, "port", pro.getPort());
            addProtocolAttr(pAttrs, "path", pro.getContextpath());
            addProtocolAttr(pAttrs, "ser", pro.getSerialization());
            addProtocolAttr(pAttrs, "char", pro.getCharset());
            protocols.put(pro.getpName(), pAttrs);
        }
        // get methods
        try {
            Class<?> serviceClassI = webappclsLoader.loadClass(serviceClass);
            ComponentProfileHandler.getMethodInfo(serviceClassI, info);
        } catch (ClassNotFoundException e) {
            continue;
        }
        inst.setValue(servicekey, info);
    }
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance) UAVServer(com.creditease.monitor.UAVServer) DubboServiceProfileInfo(com.creditease.uav.profiling.handlers.dubbo.DubboServiceProfileInfo) LinkedHashMap(java.util.LinkedHashMap) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)61 InterceptSupport (com.creditease.monitor.interceptframework.InterceptSupport)28 ServletContext (javax.servlet.ServletContext)12 StandardContext (org.apache.catalina.core.StandardContext)12 Servlet (javax.servlet.Servlet)6 ProfileElementInstance (com.creditease.uav.profiling.spi.ProfileElementInstance)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 StandardWrapper (org.apache.catalina.core.StandardWrapper)4 WebappClassLoader (org.apache.catalina.loader.WebappClassLoader)4 UAVServer (com.creditease.monitor.UAVServer)3 Event (com.creditease.monitor.interceptframework.spi.InterceptContext.Event)3 DynamicProxyProcessor (com.creditease.uav.monitorframework.dproxy.DynamicProxyProcessor)3 DPMethod (com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod)3 DubboServiceProfileInfo (com.creditease.uav.profiling.handlers.dubbo.DubboServiceProfileInfo)3 HashMap (java.util.HashMap)3 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)3 IConfigurationManager (com.creditease.agent.spi.IConfigurationManager)2 LogProfileInfo (com.creditease.uav.profiling.handlers.log.LogProfileInfo)2 WebServiceProfileInfo (com.creditease.uav.profiling.handlers.webservice.WebServiceProfileInfo)2