通过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);
      }
    }
  }
}
相关推荐
龙猫蓝图5 小时前
wrapper+ xml文件进行SQL编写
mybatis
ss2736 小时前
手写MyBatis第104弹:SqlSession从工厂构建到执行器选择的深度剖析
java·开发语言·后端·mybatis
一路向北_Coding12 小时前
MyBatis Generator让你优雅的写SQL
mysql·mybatis
莫陌尛.13 小时前
SSM(Spring+SpringMVC+Mybatis)整合
java·spring·mybatis
2301_7976042415 小时前
d41:MyBatisPlus入门,注解,配置,条件构造器,自定义SQL,IService
sql·mybatis
色空大师15 小时前
【mybatisPlus详解】
java·mybatis·mybatisplus
尘下吹霜15 小时前
【鉴权架构】SpringBoot + Sa-Token + MyBatis + MySQL + Redis 实现用户鉴权、角色管理、权限管理
spring boot·mysql·mybatis
小陈爱coding1 天前
SaaS多租户数据隔离实战:MyBatis拦截器实现行级安全方案
安全·云原生·mybatis·多租户
lang201509281 天前
MyBatis入门指南:从零掌握数据库操作
mybatis
222you1 天前
Mybatis(1)
java·tomcat·mybatis