zoukankan      html  css  js  c++  java
  • trace spring

    package xx.com.aspect;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RestController;
    
    
    import javax.servlet.http.HttpServletRequest;
    import java.lang.annotation.Annotation;
    import java.util.Date;
    import java.util.Vector;
    
    @Aspect
    @Configuration
    public class performace {
    
        @Autowired
        HttpServletRequest request;
    
        static final ThreadLocal<TraceInfo> localRequest=new ThreadLocal<>();
        static final ThreadLocal<Boolean> localIsChild=new ThreadLocal<>();
    
        public static TraceInfo getLocalRequest() {
            return localRequest.get();
        }
        public static void setParentRequest(TraceInfo traceInfo) {
            localRequest.set(traceInfo);
            localIsChild.set(true);
        }
    
    //    @Around("execution(* xx.com..*(..))")
    //    @Around("!within(is(FinalType)) && execution(* *..*(..))")
        public Object aspectAround(ProceedingJoinPoint point) throws Throwable {
    
    
            String methodName = point.getSignature().getName();
            String type=point.getSignature().getDeclaringType().getName()+":"+methodName;
            TraceInfo traceInfo=new TraceInfo(type,new Date());
            traceInfo.setChilds(new Vector<>());
            if(localIsChild.get()!=null&&localIsChild.get()){
                traceInfo.setChildThread(true);
            }
            boolean isMain=false;
            if(localRequest.get()==null) {
                isMain=true;
                localRequest.set(traceInfo);
            }
            Object ret=point.proceed();
            traceInfo.setEndDate(new Date());
    
            if(isMain) {
                try {
                    request.getAttribute("a");
                } catch (Exception e) {
                    localRequest.remove();
                    System.out.println("*** 非请求执行:" + traceInfo.getType() + " : " + String.valueOf(traceInfo.getEndDate().getTime() - traceInfo.getStartDate().getTime()));
                    return ret;
                }
            }
    
            if(!isMain){
                localRequest.get().getChilds().add(traceInfo);
            }
    
            for (Annotation an : point.getTarget().getClass().getAnnotations()) {
                if (an instanceof RestController) {
                    //end controller
                        trace(localRequest.get(), 0);
                    localRequest.remove();
                    break;
                }
            }
    
    //        traces=new Vector<>();
    //        request.setAttribute("traces", traces);
            return ret;
    
        }
    
        private void trace(TraceInfo traceInfo,int deep){
    
            char[] space=new char[deep];
            for(int i=0;i<deep;i++){
                if(i==deep-1){
                    if(traceInfo.isChildThread()){
                        space[i] = '→';
                    }else {
                        space[i] = '┞';
                    }
                    break;
                }
                space[i]=' ';
            }
            System.out.println(
                    new String(space)+
                    traceInfo.getType()+
                    " : "+
                    String.valueOf(traceInfo.getEndDate().getTime()-traceInfo.getStartDate().getTime()));
            if(traceInfo.getChilds()==null){
                return;
            }
            for(TraceInfo child:traceInfo.getChilds()){
                trace(child,deep+1);
            }
    
        }
    
    
        public static class TraceInfo{
    
    
            private Date startDate;
            private Date endDate;
            private String type;
            private Vector<TraceInfo> childs;
            private boolean childThread=false;
    
            public TraceInfo(String type,Date time){
                this.startDate=time;
                this.type=type;
            }
    
            public Vector<TraceInfo> getChilds() {
                return childs;
            }
    
            public void setChilds(Vector<TraceInfo> childs) {
                this.childs = childs;
            }
    
            public Date getEndDate() {
                return endDate;
            }
    
    
            public void setEndDate(Date endDate) {
                this.endDate = endDate;
            }
    
            public Date getStartDate() {
                return startDate;
            }
    
            public void setStartDate(Date startDate) {
                this.startDate = startDate;
            }
    
            public String getType() {
                return type;
            }
    
            public void setType(String type) {
                this.type = type;
            }
    
            public boolean isChildThread() {
                return childThread;
            }
    
            public void setChildThread(boolean childThread) {
                this.childThread = childThread;
            }
        }
    
    }
  • 相关阅读:
    k8s资源清单创建pod
    Nginx的应用之动静分离
    Nginx的应用之虚拟主机
    Nginx的应用之安装配置
    k8s 基于NFS部署storageclass pv自动供给
    Helm入门
    k8s Pod的自动水平伸缩(HPA)
    k8s资源指标API及metrics-server资源监控
    k8s的资源限制及资源请求
    k8s的高级调度方式
  • 原文地址:https://www.cnblogs.com/a-xu/p/9818353.html
Copyright © 2011-2022 走看看