-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
edagarli
committed
Apr 4, 2016
1 parent
dbd12cc
commit f0141a2
Showing
6 changed files
with
428 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>twodfire-monitor</artifactId> | ||
<groupId>com.twodfire</groupId> | ||
<packaging>jar</packaging> | ||
<version>1.0.0</version> | ||
|
||
<properties> | ||
<org.springframework-version>4.0.6.RELEASE</org.springframework-version> | ||
<org.springframework-web-version>4.0.6.RELEASE</org.springframework-web-version> | ||
<spring-test-version>4.0.6.RELEASE</spring-test-version> | ||
<logback.version>1.1.2</logback.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-core</artifactId> | ||
<version>${logback.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>${logback.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.janino</groupId> | ||
<artifactId>janino</artifactId> | ||
<version>2.7.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${spring-test-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>${org.springframework-version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-aop</artifactId> | ||
<version>${org.springframework-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-beans</artifactId> | ||
<version>${org.springframework-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context-support</artifactId> | ||
<version>${org.springframework-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>${org.springframework-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-expression</artifactId> | ||
<version>${org.springframework-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-aspects</artifactId> | ||
<version>${org.springframework-version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.aspectj</groupId> | ||
<artifactId>aspectjrt</artifactId> | ||
<version>1.8.5</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib</artifactId> | ||
<version>2.1_3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.aspectj</groupId> | ||
<artifactId>aspectjweaver</artifactId> | ||
<version>1.8.5</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.2.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>log4j-over-slf4j</artifactId> | ||
<version>1.7.7</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/twodfire/annotation/MethodAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.twodfire.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
/** | ||
* User: edagarli | ||
* Email: lizhi@edagarli.com | ||
* Date: 2015/6/5 | ||
* Time: 14:36 | ||
* Desc: 在方法名上添加了下面的 annotation,就可以进行相关的东西拦截了。 | ||
*/ | ||
|
||
@java.lang.annotation.Target({ElementType.METHOD}) | ||
@java.lang.annotation.Retention(RetentionPolicy.RUNTIME) | ||
public @interface MethodAnnotation { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.twodfire.aspect; | ||
|
||
import com.google.gson.Gson; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* User: edagarli | ||
* Email: lizhi@edagarli.com | ||
* Date: 2015/6/5 | ||
* Time: 14:34 | ||
* Desc: 定义环绕通知切面 | ||
*/ | ||
|
||
@Service | ||
@Aspect | ||
public class MethodAspect { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(MethodAspect.class); | ||
|
||
@Around("@annotation(com.twodfire.annotation.MethodAnnotation)") | ||
public Object validator(ProceedingJoinPoint joinPoint) throws Throwable { | ||
Gson gson=new Gson(); | ||
String cName = joinPoint.getTarget().getClass().getName(); | ||
Method method = ((MethodSignature)joinPoint.getSignature()).getMethod(); | ||
Object[]args=getArgs(joinPoint); | ||
long sTime=System.nanoTime(); | ||
Object result= joinPoint.proceed(); | ||
long eTime=System.nanoTime(); | ||
StringBuffer buffer=new StringBuffer(); | ||
buffer.append(cName). | ||
append("."). | ||
append(method.getName()). | ||
append(" "). | ||
append(gson.toJson(args)). | ||
append(" result "). | ||
append(gson.toJson(result)). | ||
append(" consumeTime: "). | ||
append((eTime-sTime)/1000000). | ||
append(" ms"); | ||
logger.info(buffer.toString()); | ||
System.out.println(buffer.append("监控信息").toString()); | ||
return null; | ||
} | ||
|
||
public Object[] getArgs(ProceedingJoinPoint joinPoint){ | ||
Object[] arguments = joinPoint.getArgs(); | ||
return arguments; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/twodfire/timerMonitor/monitor/MonitorAround.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.twodfire.timerMonitor.monitor; | ||
|
||
import com.twodfire.timerMonitor.util.MethodInfo; | ||
import com.twodfire.timerMonitor.util.QuestInfo; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.LinkedList; | ||
|
||
|
||
/** | ||
* User: edagarli | ||
* Email: lizhi@edagarli.com | ||
* 输出格式为 | ||
* A[0-50]50ms | ||
*---B[20-30]30ms | ||
*-----C[25-28]3ms | ||
*/ | ||
public class MonitorAround { | ||
|
||
Logger loggerMonitor = LoggerFactory.getLogger("com.twodfire.timerMonitor");//打印超时的方法栈的日志 | ||
|
||
private int maxTime = 300; | ||
private ThreadLocal<QuestInfo> myThreadLocal = new ThreadLocal(); | ||
|
||
public Object watchPerformance(ProceedingJoinPoint joinpoint) throws Throwable { | ||
Object result = null; | ||
try { | ||
QuestInfo questInfo = myThreadLocal.get(); | ||
if (questInfo == null) { | ||
questInfo = new QuestInfo(new LinkedList<MethodInfo>()); | ||
questInfo.setStartTime(System.nanoTime()); | ||
myThreadLocal.set(questInfo); | ||
} | ||
|
||
questInfo.increaseLevel(); | ||
long startTime = System.nanoTime(); | ||
result = joinpoint.proceed(); | ||
long endTime = System.nanoTime(); | ||
questInfo.decreaseLevel(); | ||
MethodInfo methodInfo=new MethodInfo( | ||
questInfo.getLevel(),/*层次*/ | ||
joinpoint.getSignature().toString(),/*方法名字*/ | ||
startTime,/*开始时间*/ | ||
endTime);/*结束时间*/ | ||
|
||
questInfo.getLinkedList().add(methodInfo); | ||
if (questInfo.getLevel() == 0) { | ||
myThreadLocal.remove(); | ||
if (( endTime-startTime) > maxTime*1000000) { | ||
StringBuilder sbOut=new StringBuilder("TimeMonitor Warn:TimeOut:\n"); | ||
sbOut.append(questInfo.toString()); | ||
loggerMonitor.warn(sbOut.toString()); | ||
} | ||
} | ||
} catch (Throwable e) { | ||
myThreadLocal.remove(); | ||
throw e; | ||
} | ||
return result; | ||
} | ||
|
||
public int getMaxTime() { | ||
return maxTime; | ||
} | ||
|
||
public void setMaxTime(int maxTimer) { | ||
this.maxTime = maxTimer; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/twodfire/timerMonitor/util/MethodInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.twodfire.timerMonitor.util; | ||
|
||
/** | ||
* User: edagarli | ||
* Email: lizhi@edagarli.com | ||
*/ | ||
public class MethodInfo { | ||
private int level; | ||
private String methodName; | ||
private Long startTime; | ||
private Long endTime; | ||
|
||
public MethodInfo(int level, String methodName, Long startTime, Long endTime) { | ||
this.level = level; | ||
this.methodName = methodName; | ||
this.startTime = startTime; | ||
this.endTime = endTime; | ||
} | ||
|
||
public int getLevel() { | ||
return level; | ||
} | ||
|
||
public void setLevel(int level) { | ||
this.level = level; | ||
} | ||
|
||
public String getMethodName() { | ||
return methodName; | ||
} | ||
|
||
public void setMethodName(String methodName) { | ||
this.methodName = methodName; | ||
} | ||
|
||
public Long getStartTime() { | ||
return startTime; | ||
} | ||
|
||
public void setStartTime(Long startTime) { | ||
this.startTime = startTime; | ||
} | ||
|
||
public Long getEndTime() { | ||
return endTime; | ||
} | ||
|
||
public void setEndTime(Long endTime) { | ||
this.endTime = endTime; | ||
} | ||
} |
Oops, something went wrong.