代码没什么内容,直接贴上来吧,其中costTimeUtil可以看我的另一篇博文:java实现一个不带次数变量的加权平均值算法-CSDN博客
@Slf4j
@Intercepts({@Signature(type = StatementHandler.class,method = "query",args = {Statement.class, ResultHandler.class}
), @Signature(type = StatementHandler.class,method = "queryCursor",args = {Statement.class}
), @Signature(type = StatementHandler.class,method = "update",args = {Statement.class}
), @Signature(type = StatementHandler.class,method = "batch",args = {Statement.class}
)})
public class MetricOnDbQueryInterceptor implements Interceptor {private static final String MAPPER_SLOW_SQL_METRIC = "MetricOnDbQueryInterceptor_cost";@Resourceprivate AlertOnTimesOfAverageCostTimeUtil costTimeUtil;@Overridepublic Object intercept(Invocation invocation) throws Throwable {long start = System.currentTimeMillis();try {return invocation.proceed();} finally {String sql = ((StatementHandler) invocation.getTarget()).getBoundSql().getSql();long costTime = System.currentTimeMillis() - start;if (costTimeUtil.shouldDoTheMetricWork(sql, costTime)) {//这里可以做一些监控打点的功能log.warn("it is a slow sql query:{}, costTime:{}", sql, costTime);}}}
}