Search in sources :

Example 16 with InterceptContext

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

the class DubboIT method doProfiling.

/**
 * collect all ServiceBeans for later profiling
 *
 * @param args
 */
@SuppressWarnings("rawtypes")
public void doProfiling(Object[] args) {
    InterceptContext ic = InterceptSupport.instance().getThreadLocalContext(Event.WEBCONTAINER_STARTED);
    @SuppressWarnings("unchecked") Map<String, DubboServiceProfileInfo> list = (Map<String, DubboServiceProfileInfo>) ic.get(HookConstants.DUBBO_PROFILE_LIST);
    if (null == list) {
        list = new HashMap<String, DubboServiceProfileInfo>();
        ic.put(HookConstants.DUBBO_PROFILE_LIST, list);
    }
    ServiceBean sb = (ServiceBean) args[0];
    String serviceClass = sb.getInterface();
    String serviceImplClass = sb.getRef().getClass().getName();
    // the refclass is enhance by SpringCGLIB, className like "ServiceImplClass$$EnhancerBySpringCGLIB$$2a862c3d"
    if (serviceImplClass.indexOf("$$") > -1) {
        serviceImplClass = serviceImplClass.substring(0, serviceImplClass.indexOf("$$"));
    }
    String group = (sb.getGroup() == null) ? "" : sb.getGroup();
    if (sb.getGroup() == null && sb.getProvider() != null && sb.getProvider().getGroup() != null) {
        group = sb.getProvider().getGroup();
    }
    String version = (sb.getVersion() == null) ? "" : sb.getVersion();
    if (sb.getVersion() == null && sb.getProvider() != null && sb.getProvider().getVersion() != null) {
        version = sb.getProvider().getVersion();
    }
    String serviceKey = serviceClass;
    if (!"".equals(group) && !"".equals(version)) {
        serviceKey += ":" + group + ":" + version;
    } else if (!"".equals(group) && "".equals(version)) {
        serviceKey += ":" + group;
    } else if ("".equals(group) && !"".equals(version)) {
        serviceKey += ":none:" + version;
    }
    if (list.containsKey(serviceKey)) {
        return;
    }
    DubboServiceProfileInfo dspi = new DubboServiceProfileInfo();
    String dbAppId = sb.getApplication().getName();
    if (sb.getApplication().getVersion() != null) {
        dbAppId += "-" + sb.getApplication().getVersion();
    }
    dspi.setAppId(appId);
    dspi.setDbAppId(dbAppId);
    dspi.setServiceClass(serviceClass);
    dspi.setServiceImplClass(serviceImplClass);
    dspi.setGroup(group);
    dspi.setVersion(version);
    @SuppressWarnings("unchecked") List<URL> urlList = sb.getExportedUrls();
    if (urlList != null) {
        for (URL url : urlList) {
            DubboServiceProfileInfo.Protocol pro = new DubboServiceProfileInfo.Protocol();
            pro.setPort(url.getPort());
            pro.setpName(url.getProtocol());
            int pathLength = url.getPath().length();
            // 长度加1为了去除反斜杠/
            int interfaceLength = url.getServiceInterface().length() + 1;
            String contextpath = null;
            if (pathLength > interfaceLength + 1) {
                contextpath = url.getPath().substring(0, pathLength - interfaceLength);
            }
            pro.setContextpath(contextpath);
            pro.setCharset(url.getParameter("charset"));
            // dubbo当前默认的序列化方式为hessian2,不知道日后会不会变,故此处不进行默认赋值
            pro.setSerialization(url.getParameter("serialization"));
            dspi.addProtocol(pro);
        }
    }
    list.put(serviceKey, dspi);
}
Also used : DubboServiceProfileInfo(com.creditease.uav.profiling.handlers.dubbo.DubboServiceProfileInfo) URL(com.alibaba.dubbo.common.URL) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) ServiceBean(com.alibaba.dubbo.config.spring.ServiceBean) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with InterceptContext

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

the class SpringBootTomcatPlusIT method onSpringFinishRefresh.

/**
 * ComponentProfile will be done after springboot finish it's context's refresh, cause every pre-profile(like dubbo)
 * is ready.
 */
public void onSpringFinishRefresh(Object arg) {
    if (!isWebServerContext(arg)) {
        return;
    }
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_STARTED);
    iSupport.doIntercept(context);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport)

Example 18 with InterceptContext

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

the class SpringBootTomcatPlusIT method onAppInit.

@Override
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 19 with InterceptContext

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

the class SpringBootTomcatPlusIT method onAppStop.

/**
 * onAppStop
 *
 * @param args
 */
@Override
public void onAppStop(Object... args) {
    StandardContext sc = (StandardContext) args[0];
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.createInterceptContext(Event.WEBCONTAINER_STOPPED);
    if (null == context || null == sc) {
        return;
    }
    /**
     * NOTE: spring boot will reset tomcat webappclassloader to null when shutdown, we may use the currentThread's
     * classloader as the classloader
     */
    context.put(InterceptConstants.WEBAPPLOADER, Thread.currentThread().getContextClassLoader());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    String contextPath = (String) ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true);
    context.put(InterceptConstants.CONTEXTPATH, contextPath);
    context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
    context.put(InterceptConstants.SERVLET_CONTEXT, ReflectionHelper.getField(StandardContext.class, sc, "context", true));
    iSupport.doIntercept(context);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext)

Example 20 with InterceptContext

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

the class SpringBootTomcatPlusIT method onAppStart.

/**
 * onAppStart
 *
 * @param args
 */
@Override
public void onAppStart(Object... args) {
    StandardContext sc = (StandardContext) args[0];
    String contextPath = (String) ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true);
    // springboot use threadlocalContext to store the WEBCONTAINER_STARTED Event context, just return when it's uav's inner app in case of rewriting the real app's context
    if ("/com.creditease.uav".equals(contextPath)) {
        return;
    }
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_STARTED);
    /**
     * NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do
     * anything on this we may use its webappclassloader's parent as the classloader
     */
    context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader().getParent());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    context.put(InterceptConstants.CONTEXTPATH, contextPath);
    if (context.get(InterceptConstants.APPNAME) == null) {
        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);
    String basePath = sContext.getRealPath("");
    /*
         * NOTE: springboot couldn't get the basePath through method "getRealPath", temporary process
         */
    if (basePath == null) {
        basePath = "";
    } else if (basePath.lastIndexOf("/") == (basePath.length() - 1) || basePath.lastIndexOf("\\") == (basePath.length() - 1)) {
        basePath = basePath.substring(0, basePath.length() - 1);
    }
    context.put(InterceptConstants.BASEPATH, basePath);
// we don't doIntercept here cause some pre-profile(like dubbo) not happen yet, profile will be done after
// finishRefresh
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) ServletContext(javax.servlet.ServletContext)

Aggregations

InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)66 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 LogProfileInfo (com.creditease.uav.profiling.handlers.log.LogProfileInfo)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 ServletException (javax.servlet.ServletException)3 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)3