拦截Redis所有日志已经记录耗时,
@Aspect
@Component
public class RedisLogAop {
/**
* redis 监控切点
*/
@Pointcut(value = "execution(* com.xxx.operate.get.cache.RedisService.*(*)) && args(key)", argNames = "key")
public void redisCut(String key) {
// 切点定义无具体操作,具体操作由引用处方法实现
}
@Around(value = "redisCut(key)", argNames = "joinPoint,key")
public Object around(ProceedingJoinPoint joinPoint, String key) {
long startTimeMillis = System.currentTimeMillis();
String method = joinPoint.getSignature().getName();
Object result;
try {
result = joinPoint.proceed();
} catch (Throwable e) {
ElkLogger.error(ElkBean.builder().requestInfo(key).method("Redis/" + method).cost(ElkBean.calculateTime(startTimeMillis)).build(), e);
throw new RuntimeException(e.getMessage());
}
ElkLogger.info(ElkBean.builder().requestInfo(key).method("Redis/" + method).cost(ElkBean.calculateTime(startTimeMillis)).build());
return result;
}
}
拦截指定包下的所有的方法
@Aspect
@Component
public class GrpcLogAop {
@Pointcut(value = "execution(* com.xxx.operate.get.grpc.*.*(..)) && args(requestInfo,..)", argNames = "requestInfo")
public void traceIDCut(RequestInfo requestInfo) {
}
@Before("traceIDCut(requestInfo)")
public void doBeforeRunning(RequestInfo requestInfo) {
//这里获取二个 测试用的
String traceID = ThreadLocalUtil.getTraceID();
if (StringUtils.isBlank(traceID)) {
traceID = String.format("%s%s", "N", UUID.randomUUID().toString().replaceAll("-", ""));
}
if (requestInfo instanceof RequestPageInfo) {
//设置分页
RequestPageInfo requestPageInfo = (RequestPageInfo) requestInfo;
}
requestInfo.setTraceID(traceID);
}
}