CatAspect.java
3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import com.dianping.cat.Cat;
import com.dianping.cat.message.Transaction;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Aspect
public class CatAspect {
@Around("@annotation(catTransaction)")
public Object catTransactionProcess(ProceedingJoinPoint pjp, CatTransaction catTransaction) throws Throwable {
String transName = pjp.getSignature().getDeclaringType().getSimpleName() + "." + pjp.getSignature().getName();
if(StringUtils.isNotBlank(catTransaction.name())){
transName = catTransaction.name();
}
Transaction t = Cat.newTransaction(catTransaction.type(), transName);
try {
Object result = pjp.proceed();
t.setStatus(Transaction.SUCCESS);
return result;
} catch (Throwable e) {
t.setStatus(e);
throw e;
}finally{
t.complete();
}
}
@Around("@annotation(catCacheTransaction)")
public Object catCacheTransactionProcess(ProceedingJoinPoint pjp, CatCacheTransaction catCacheTransaction) throws Throwable {
String transName = pjp.getSignature().getName();
if(StringUtils.isNotBlank(catCacheTransaction.name())){
transName = catCacheTransaction.name();
}
Transaction t = Cat.newTransaction("Cache.Redis",transName);
try {
Cat.logEvent("Cache.Server", catCacheTransaction.server());
Object result = pjp.proceed();
t.setStatus(Transaction.SUCCESS);
return result;
} catch (Throwable e) {
Cat.logEvent("Cache.Server", catCacheTransaction.server(), "-1", null);
t.setStatus(e);
throw e;
}finally{
t.complete();
}
}
@Around("@annotation(catDubboClientTransaction)")
public Object catDubboServerTransactionProcess(ProceedingJoinPoint pjp, CatDubboClientTransaction catDubboClientTransaction) throws Throwable {
String transName = pjp.getSignature().getName();
if(StringUtils.isNotBlank(catDubboClientTransaction.name())){
transName = catDubboClientTransaction.name();
}
Transaction t = Cat.newTransaction("Call",transName);
try {
Cat.logEvent("Call.app", catDubboClientTransaction.callApp());
Cat.logEvent("Call.server", catDubboClientTransaction.callServer());
Object result = pjp.proceed();
t.setStatus(Transaction.SUCCESS);
return result;
} catch (Throwable e) {
t.setStatus(e);
throw e;
}finally{
t.complete();
}
}
@After("@annotation(catHttpRequestTransaction)")
public void catHttpRequestProcess(CatHttpRequestTransaction catHttpRequestTransaction) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
if(StringUtils.isNotBlank(catHttpRequestTransaction.name())){
String transName = catHttpRequestTransaction.name();
request.setAttribute("cat-page-uri", transName);
}
}
}