trace spring
Posted 阿旭代码之路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } } }
以上是关于trace spring的主要内容,如果未能解决你的问题,请参考以下文章
自定义 Spring Boot HTTP TRACE disabled 错误响应
使用spring boot在undertow中禁用http TRACK/TRACE
Spring-Cloud-Gateway 创建的初始 Trace 都命名为“/”,无论路径如何
Spring 框架:返回所有 METHODS 的 HTTP OPTIONS(get、put、post、delete、trace、head、options)
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段