通过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);
      }
    }
  }
}
相关推荐
linab11222 分钟前
mybatis中的resultMap的association及collectio的使用
java·开发语言·mybatis
Code哈哈笑3 小时前
【图书管理系统】用户注册系统实现详解
数据库·spring boot·后端·mybatis
Code哈哈笑5 小时前
【基于Spring Boot 的图书购买系统】深度讲解 用户注册的前后端交互,Mapper操作MySQL数据库进行用户持久化
数据库·spring boot·后端·mysql·mybatis·交互
lyrhhhhhhhh7 小时前
MyBatis 延迟加载与缓存
sql·缓存·mybatis
菜鸟蹦迪19 小时前
学习记录:mybatis和jdbc实现数据表作为参数的相关的sql操作
sql·学习·mybatis
芯眼2 天前
STM32启动文件详解(重点)
java·开发语言·c++·stm32·单片机·mybatis
遗憾皆是温柔2 天前
MyBatis—动态 SQL
java·数据库·ide·sql·mybatis
CircleMouse3 天前
springboot如何通过提供的注解方式来操作Redis
java·spring boot·redis·spring·mybatis
荔枝吻3 天前
【抽丝剥茧知识讲解】引入mybtis-plus后,mapper实现方式
java·sql·mybatis