Search in sources :

Example 56 with InterceptContext

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

the class JarProfileHandler method doProfiling.

@Override
public void doProfiling(ProfileElement pe, ProfileContext context) {
    /**
     * handler jars
     */
    if (!ProfileConstants.PROELEM_JARS.equals(pe.getElemId())) {
        return;
    }
    InterceptContext ic = context.get(InterceptContext.class);
    if (ic == null) {
        this.logger.warn("Profile:JARS FAILs as No InterceptContext available", null);
        return;
    }
    ClassLoader webappclsLoader = (ClassLoader) ic.get(InterceptConstants.WEBAPPLOADER);
    if (null == webappclsLoader) {
        this.logger.warn("Profile:JARS FAILs as No webappclsLoader available", null);
        return;
    }
    ProfileElementInstance pei = pe.getInstance(ProfileConstants.PEI_LIB);
    if (!URLClassLoader.class.isAssignableFrom(webappclsLoader.getClass())) {
        this.logger.warn("Profile:JARS FAILs as the webappclassloader is not a URLClassloader", null);
        return;
    }
    URL[] urls = ((URLClassLoader) webappclsLoader).getURLs();
    if (urls == null) {
        this.logger.warn("Profile:JARS FAILs as No JAR URLs from webappclassloader", null);
        return;
    }
    for (URL url : urls) {
        String path = url.getPath();
        String[] pinfo = path.split("/");
        pei.setValue(pinfo[pinfo.length - 1], url.toString());
    }
    /**
     * confirm there is update
     */
    pe.getRepository().setUpdate(true);
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 57 with InterceptContext

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

the class HookFactory method stopHook.

/**
 * stop application framework hook
 *
 * @param webapploader
 * @param proxyClass
 * @param cntx
 */
public void stopHook(ClassLoader webapploader, String proxyClass, HookContext cntx, boolean shouldRemove) {
    try {
        if (log.isLogEnabled()) {
            log.info("STOP hookproxy[" + proxyClass + "] ");
        }
        InterceptContext context = (InterceptContext) cntx.get(HookConstants.INTERCEPTCONTEXT);
        String contextPath = (String) context.get(InterceptConstants.CONTEXTPATH);
        String proxyKey = getHookProxyKey(contextPath, proxyClass);
        HookProxy proxy = (!shouldRemove) ? hooks.get(proxyKey) : hooks.remove(proxyKey);
        if (proxy == null) {
            if (log.isDebugable()) {
                log.debug("no hookproxy[" + proxyClass + "] should be STOP", null);
            }
            return;
        }
        // run stop hookproxy
        proxy.stop(cntx, webapploader);
        if (log.isDebugable()) {
            log.debug("STOP hookproxy[" + proxyClass + "]  END", null);
        }
    } catch (Exception e) {
        log.error("STOP hookproxy[" + proxyClass + "] FAIL. ", e);
        return;
    }
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext)

Example 58 with InterceptContext

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

the class HookFactory method startHook.

/**
 * start application framework hook
 *
 * @param webapploader
 *            AppServer WebAppClassLoader
 * @param detectClass
 *            the class is used to defect if the application framework exists
 * @param loaderName
 *            the loader tag helps to load the hook jars
 * @param proxyClass
 *            the hook proxy class to run hook logic
 */
@SuppressWarnings("rawtypes")
public void startHook(ClassLoader webapploader, String detectClass, String loaderName, String proxyClass, Map hookConfig, HookContext cntx) {
    if (webapploader == null || detectClass == null || loaderName == null || proxyClass == null) {
        return;
    }
    // see if the app framework is there
    try {
        webapploader.loadClass(detectClass);
    } catch (ClassNotFoundException e) {
        // ignore
        if (log.isDebugable()) {
            log.debug("LOAD detectclass[" + detectClass + "] FAIL. ", e);
        }
        return;
    }
    InterceptContext context = (InterceptContext) cntx.get(HookConstants.INTERCEPTCONTEXT);
    Event evt = cntx.get(Event.class);
    if (log.isLogEnabled()) {
        log.info("START hook for proxy=" + proxyClass + ",detect=" + detectClass + ",jar=" + loaderName + ",evt=" + evt.toString());
    }
    /**
     * NOTE: there is only one HookProxy instance for each proxyClass and each webapploader
     */
    String contextPath = (String) context.get(InterceptConstants.CONTEXTPATH);
    String proxyKey = getHookProxyKey(contextPath, proxyClass);
    HookProxy proxy = null;
    boolean isNew = false;
    if (hooks.containsKey(proxyKey)) {
        proxy = hooks.get(proxyKey);
    } else {
        @SuppressWarnings("unchecked") List<String> supports = (List<String>) hookConfig.get("supports");
        // install hook jars
        this.installHookJars(webapploader, loaderName, supports);
        // new hookproxy instance
        try {
            Class<?> hookproxyClass = webapploader.loadClass(proxyClass);
            Constructor<?> con = hookproxyClass.getConstructor(new Class<?>[] { String.class, Map.class });
            proxy = (HookProxy) con.newInstance(new Object[] { proxyKey, hookConfig });
            isNew = true;
        } catch (Exception e) {
            log.error("CREATE hookproxy[" + proxyClass + "] instance FAIL. ", e);
            return;
        }
    }
    try {
        if (log.isDebugable()) {
            log.debug("START hookproxy[" + proxyClass + "] ", null);
        }
        // start hookproxy
        proxy.start(cntx, webapploader);
        // only start SUCCESS to store hookproxy
        if (isNew == true) {
            hooks.put(proxyKey, proxy);
        }
        if (log.isDebugable()) {
            log.debug("START hookproxy[" + proxyClass + "] END", null);
        }
    } catch (Exception e) {
        log.error("START hookproxy[" + proxyClass + "] FAIL. ", e);
    } catch (Error e) {
        log.error("START hookproxy[" + proxyClass + "] FAIL. ", e);
    }
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) Event(com.creditease.monitor.interceptframework.spi.InterceptContext.Event) List(java.util.List)

Example 59 with InterceptContext

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

the class StandardInterceptContextHelper method get.

private InterceptContext get(Event event) {
    if (event == null)
        return null;
    InterceptContext context = threadInterceptContext.get(event);
    if (context == null) {
        context = new StandardInterceptContext(event);
        threadInterceptContext.put(event, context);
    }
    return context;
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext)

Example 60 with InterceptContext

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

the class GlobalFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    InterceptSupport iSupport = InterceptSupport.instance();
    /**
     * Step 1: request
     */
    InterceptContext context = iSupport.createInterceptContext(Event.GLOBAL_FILTER_REQUEST);
    context.put(InterceptConstants.HTTPREQUEST, request);
    context.put(InterceptConstants.HTTPRESPONSE, response);
    context.put(InterceptConstants.FILTERCHAIN, chain);
    iSupport.doIntercept(context);
    // NOTE: get the response, it is a chance for global filter handler to replace response object
    HttpServletResponse tmpResponse = (HttpServletResponse) context.get(InterceptConstants.HTTPRESPONSE);
    // NOTE: get the request, it is a chance for global filter handler to replace request object
    HttpServletRequest tmpRequest = (HttpServletRequest) context.get(InterceptConstants.HTTPREQUEST);
    /**
     * check if get the stop request token,if yes then stop the request
     */
    Object token = context.get(InterceptConstants.STOPREQUEST);
    if (token == null) {
        /**
         * Step 2: run service
         */
        try {
            chain.doFilter(tmpRequest, tmpResponse);
        } catch (Throwable e) {
            inteceptResponse(iSupport, context);
            throw new RuntimeException(e);
        }
    }
    /**
     * Step 3: response
     */
    inteceptResponse(iSupport, context);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) HttpServletResponse(javax.servlet.http.HttpServletResponse)

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