通过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);
      }
    }
  }
}
相关推荐
不羁。。1 小时前
【撸靶笔记】第八关:GET - Blind - Boolian Based - Single Quotes
数据库·sql·mybatis
西红柿维生素8 小时前
MyBatis SqlCommand+MethodSignature源码探究
mybatis
HeyZoeHey2 天前
Mybatis执行sql流程(一)
java·sql·mybatis
青川入梦2 天前
MyBatis极速通关上篇:Spring Boot环境搭建+用户管理实战
java·开发语言·mybatis
33255_40857_280592 天前
掌握分页艺术:MyBatis与MyBatis-Plus实战指南(10年Java亲授)
java·mybatis
勿在浮沙筑高台3 天前
无法获取实体类com.example.springdemo2.entity.po.UserPO对应的表名!
java·spring boot·mybatis
柯南二号3 天前
【Java后端】MyBatis-Plus 原理解析
java·开发语言·mybatis
Easocen3 天前
Mybatis学习笔记(五)
笔记·学习·mybatis
qq_三哥啊4 天前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
柯南二号4 天前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis