通过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);
      }
    }
  }
}
相关推荐
misL NITL12 小时前
idea、mybatis报错Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
tomcat·intellij-idea·mybatis
是宇写的啊20 小时前
MyBatis-Plus
java·开发语言·mybatis
工作log2 天前
Spring Boot 3.5 + MyBatis Plus + RabbitMQ:打造 AI 驱动的慢 SQL 监控与优化系统
spring boot·mybatis·java-rabbitmq
河阿里2 天前
MyBatis-Plus:MyBatis的进阶开发
数据库·mybatis
橙子圆1232 天前
Mybatis之动态sql
sql·tomcat·mybatis
冷小鱼3 天前
MyBatis 与 MyBatis-Plus:从入门到精通的完整指南
java·tomcat·mybatis
1.14(java)3 天前
Spring拦截器实战:统一登录校验与异常处理
拦截器
香香甜甜的辣椒炒肉3 天前
SpringMVC高级应用和MyBatis-Plus的概念和使用
mybatis
鸡蛋灌Bean3 天前
mybatis分页深入了解
java·数据库·mybatis
看腻了那片水4 天前
开源一个对业务代码零侵入的透明数据治理框架 —— 【sangsang】
java·mybatis