com.github.dennisit.vplus.data.metric.spring.InvokeJMetricIntercept Maven / Gradle / Ivy
/*--------------------------------------------------------------------------
* Copyright (c) 2010-2020, Elon.su All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the elon developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Elon.su, you can also mail [email protected]
*--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.metric.spring;
import com.github.dennisit.vplus.data.metric.annotation.JMetric;
import com.github.dennisit.vplus.data.metric.metric.JMetricClient;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
/**
* 应用耗时采集器
* @author Elon.su
*/
public class InvokeJMetricIntercept implements MethodInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(InvokeJMetricIntercept.class);
private Object[] defensiveCopyOfArgs;
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Object result = null;
Method method = methodInvocation.getMethod();
Method targetMethod = methodInvocation.getThis().getClass().getMethod(method.getName(),method.getParameterTypes());
String className = method.getDeclaringClass().getSimpleName();
String classFullName = method.getDeclaringClass().getName();
String packageName = StringUtils.substringBeforeLast(classFullName,".");
//处理内部类
int innerClassIndex = StringUtils.indexOf(classFullName, "$");
if (innerClassIndex > 0) {
className = StringUtils.substringAfterLast(classFullName, ".");
}
StringBuilder classInfo = new StringBuilder();
classInfo.append(className).append(".").append(method.getName());
JMetric metric = null;
if (method.isAnnotationPresent(JMetric.class)) {
metric = method.getAnnotation(JMetric.class);
} else if (targetMethod.isAnnotationPresent(JMetric.class)) {
metric = targetMethod.getAnnotation(JMetric.class);
}
if (null != metric && StringUtils.isNotBlank(metric.metric())) {
packageName = metric.metric();
}
long inTime = System.currentTimeMillis();
try {
result = methodInvocation.proceed();
} catch (Exception e) {
throw e;
} finally {
if (metric != null) {
String metricKey = StringUtils.isNotBlank(metric.metric())? metric.metric(): packageName + "." + classInfo.toString();
if(StringUtils.isNoneBlank(metricKey) && metric.monitor()){
JMetricClient.add(metric.metric(), System.currentTimeMillis() - inTime);
}
}
}
return result;
}
}