通过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);
      }
    }
  }
}
相关推荐
独断万古他化8 小时前
【SSM开发实战:博客系统】(三)核心业务功能开发与安全加密实现
spring boot·spring·mybatis·博客系统·加密
fengxin_rou12 小时前
[Redis从零到精通|第四篇]:缓存穿透、雪崩、击穿
java·redis·缓存·mybatis·idea·多线程
老毛肚1 天前
MyBatis插件原理及Spring集成
java·spring·mybatis
马尔代夫哈哈哈1 天前
MyBatis 入门与实战:从配置到CRUD一站式指南
mybatis
Jul1en_1 天前
【MyBatis/plus】核心配置、插件与 MyBatis-Plus 构造器 Wrapper
mybatis
LiZhen7981 天前
SpringBoot 实现动态切换数据源
java·spring boot·mybatis
我是Superman丶1 天前
在 PostgreSQL 中使用 JSONB 类型并结合 MyBatis-Plus 实现自动注入,主要有以下几种方案
数据库·postgresql·mybatis
Pluto_CSND1 天前
基于mybatis-generator插件生成指定数据表的实体类、xml文件和dao层接口
mybatis
indexsunny1 天前
互联网大厂Java面试实战:微服务与Spring生态技术解析
java·spring boot·redis·kafka·mybatis·hibernate·microservices
手握风云-1 天前
JavaEE 进阶第十六期:MyBatis,查询请求的生命周期全景图(一)
java·java-ee·mybatis