通过mybatis拦截器给sql执行加一个耗时监控

代码没什么内容,直接贴上来吧,其中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";
  @Resource
  private AlertOnTimesOfAverageCostTimeUtil costTimeUtil;

  @Override
  public 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);
      }
    }
  }
}
相关推荐
柒.梧.13 小时前
SSM常见核心面试问题深度解析
java·spring·面试·职场和发展·mybatis
rabbit_pro16 小时前
Java使用Mybatis-Plus封装动态数据源工具类
java·python·mybatis
清风徐来QCQ20 小时前
SpringMvc(Interceptor,Filter)
过滤器·拦截器
IT_Octopus20 小时前
java 实体属性 Map 解决 mybatis-plus wrapper selectone 查mysql json类型为null 问题
java·mysql·mybatis
Dolphin_Home21 小时前
MyBatis 核心属性详解笔记(由浅入深)
笔记·mybatis
一直都在57221 小时前
MyBatis入门:CRUD、参数处理与防 SQL 注入
java·sql·mybatis
while(1){yan}1 天前
MyBatis Generator
数据库·spring boot·java-ee·mybatis
memgLIFE2 天前
mybatis数据库查询
数据库·oracle·mybatis
drebander2 天前
MyBatis-Plus saveBatch 在异步线程中事务未提交问题排查与修复
数据库·mybatis
super_lzb2 天前
mybatis拦截器ResultSetHandler详解
java·spring·mybatis·springboot