mirror of
https://gitee.com/huoyo/ko-time.git
synced 2025-12-06 16:58:26 +08:00
监测范围配置化
This commit is contained in:
parent
f64991e6f2
commit
577db78d0c
12
README.md
12
README.md
@ -26,7 +26,7 @@ http://huoyo.gitee.io/ko-time/
|
||||
<dependency>
|
||||
<groupId>cn.langpy</groupId>
|
||||
<artifactId>ko-time</artifactId>
|
||||
<version>1.3</version>
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
```
|
||||
2. 配置信息
|
||||
@ -37,10 +37,14 @@ spring.profiles.active=koTime
|
||||
koTime.log.enable=false # 是否开启控制输出,非必填,默认false
|
||||
koTime.log.language=chinese # 控制台输出语言(english/chinese)非必填,默认chinese
|
||||
koTime.time.threshold=800.0 # 时间阈值,用于前端展示,大于阈值显示红色,小于阈值显示绿色,非必填,默认800
|
||||
koTime.pointcut=execution(* com.huoyo..*.*(..)) 需要监测的切面范围,参考aop的@pointcut v1.4开始加入的功能,用来替代下面的步骤3
|
||||
```
|
||||
|
||||
|
||||
3. 新建一个类,实现ComputeTimeHandlerInterface,并在 @Pointcut 写入 需要监测的范围
|
||||
|
||||
`注意:v1.3之前需要该步骤,v1.4及以上使用koTime.pointcut配置即可,无需此步骤`
|
||||
|
||||
```java
|
||||
@Component
|
||||
@Aspect
|
||||
@ -90,4 +94,10 @@ public class RunTimeHandler implements ComputeTimeHandlerInterface {
|
||||
|
||||
> V1.3:添加日志、时间阈值可配置
|
||||
|
||||
> V1.4:添加koTime.pointcut配置
|
||||
|
||||
#### 特别说明
|
||||
|
||||
本项目使用java8开发,其他版本未曾试验,如有什么bug还请告知!
|
||||
|
||||
|
||||
|
||||
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>cn.langpy</groupId>
|
||||
<artifactId>ko-time</artifactId>
|
||||
<version>1.3</version>
|
||||
<version>1.4</version>
|
||||
<name>koTime</name>
|
||||
<description>koTime</description>
|
||||
<licenses>
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
package cn.langpy.kotime.config;
|
||||
|
||||
|
||||
import cn.langpy.kotime.handler.RunTimeHandler;
|
||||
import cn.langpy.kotime.model.KoTimeConfig;
|
||||
import cn.langpy.kotime.util.Context;
|
||||
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
@ -16,6 +19,8 @@ public class DefaultConfig {
|
||||
private Boolean logEnable;
|
||||
@Value("${koTime.time.threshold:800.0}")
|
||||
private Double timeThreshold;
|
||||
@Value("${koTime.pointcut:execution(* cn.langpy.kotime.controller.KoTimeController.*(..))}")
|
||||
private String pointcut;
|
||||
|
||||
@PostConstruct
|
||||
public void function() {
|
||||
@ -26,6 +31,14 @@ public class DefaultConfig {
|
||||
Context.setConfig(config);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AspectJExpressionPointcutAdvisor configurabledvisor() {
|
||||
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
|
||||
advisor.setExpression(pointcut);
|
||||
advisor.setAdvice(new RunTimeHandler());
|
||||
return advisor;
|
||||
}
|
||||
|
||||
public Double getTimeThreshold() {
|
||||
return timeThreshold;
|
||||
}
|
||||
|
||||
@ -8,7 +8,6 @@ import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
|
||||
@Aspect
|
||||
public interface ComputeTimeHandlerInterface {
|
||||
@Pointcut("")
|
||||
void prog();
|
||||
|
||||
21
src/main/java/cn/langpy/kotime/handler/RunTimeHandler.java
Normal file
21
src/main/java/cn/langpy/kotime/handler/RunTimeHandler.java
Normal file
@ -0,0 +1,21 @@
|
||||
package cn.langpy.kotime.handler;
|
||||
|
||||
import cn.langpy.kotime.model.RunTimeNode;
|
||||
import cn.langpy.kotime.service.InvokeService;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
public class RunTimeHandler implements MethodInterceptor {
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
long begin = System.nanoTime();
|
||||
Object obj=invocation.proceed();
|
||||
long end =System.nanoTime();
|
||||
String packName = invocation.getThis().getClass().getPackage().getName();
|
||||
RunTimeNode parent = InvokeService.getParentRunTimeNode(packName);
|
||||
RunTimeNode current = InvokeService.getCurrentRunTimeNode(invocation,((end-begin)/1000000.0));
|
||||
InvokeService.createGraph(parent,current);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@ package cn.langpy.kotime.service;
|
||||
import cn.langpy.kotime.model.RunTimeNode;
|
||||
import cn.langpy.kotime.util.Common;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -30,6 +31,18 @@ public class InvokeService {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public static RunTimeNode getCurrentRunTimeNode(MethodInvocation pjp, Double runTime) {
|
||||
String className = pjp.getThis().getClass().getName();
|
||||
String methodName = pjp.getMethod().getName();
|
||||
RunTimeNode current = new RunTimeNode();
|
||||
current.setName(className.substring(className.lastIndexOf(".")+1)+"."+methodName);
|
||||
current.setClassName(className);
|
||||
current.setMethodName(methodName);
|
||||
current.setAvgRunTime(runTime);
|
||||
current.setChildren(new ArrayList<>());
|
||||
current.setMethodType(Common.getMethodType(pjp));
|
||||
return current;
|
||||
}
|
||||
public static RunTimeNode getCurrentRunTimeNode(ProceedingJoinPoint pjp, Double runTime) {
|
||||
String className = pjp.getTarget().getClass().getName();
|
||||
String methodName = pjp.getSignature().getName();
|
||||
|
||||
@ -2,6 +2,7 @@ package cn.langpy.kotime.util;
|
||||
|
||||
import cn.langpy.kotime.model.RunTimeNode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.stereotype.Repository;
|
||||
@ -25,6 +26,26 @@ public class Common {
|
||||
}
|
||||
|
||||
|
||||
public static MethodType getMethodType(MethodInvocation pjp) {
|
||||
Class<?> targetClass = pjp.getThis().getClass();
|
||||
if (targetClass.getAnnotation(Controller.class)!=null || targetClass.getAnnotation(RestController.class)!=null) {
|
||||
return MethodType.Controller;
|
||||
}else if (targetClass.getAnnotation(Service.class)!=null) {
|
||||
return MethodType.Service;
|
||||
}else if (targetClass.getAnnotation(Repository.class)!=null) {
|
||||
return MethodType.Dao;
|
||||
}
|
||||
String className = pjp.getThis().getClass().getName().toLowerCase();
|
||||
if (className.contains("controller")) {
|
||||
return MethodType.Controller;
|
||||
}else if (className.contains("service")) {
|
||||
return MethodType.Service;
|
||||
}else if (className.contains("dao") || className.contains("mapper")|| className.contains( "com.sun.proxy.$Proxy")) {
|
||||
return MethodType.Dao;
|
||||
}else{
|
||||
return MethodType.Others;
|
||||
}
|
||||
}
|
||||
public static MethodType getMethodType(ProceedingJoinPoint pjp) {
|
||||
Class<?> targetClass = pjp.getTarget().getClass();
|
||||
if (targetClass.getAnnotation(Controller.class)!=null || targetClass.getAnnotation(RestController.class)!=null) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user