监测范围配置化

This commit is contained in:
huoyo 2021-01-20 22:15:31 +08:00
parent f64991e6f2
commit 577db78d0c
7 changed files with 80 additions and 3 deletions

View File

@ -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还请告知

View File

@ -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>

View File

@ -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;
}

View File

@ -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();

View 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;
}
}

View File

@ -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();

View File

@ -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) {