mybatis(5) -自定义sql拦截统计执行耗时

通过前文了解到 真正执行的Executor是一个invocationHandler为包装了Interceptor的Plugin的代理类

Plugin源码如下:

d407e1c3502b051541f837160d5d9dbadee.jpg

想通过自定义sql执行拦截器来统一打印sql的执行耗时

配置sqlSessionFactory增加Interceptor:

   @Bean(name = "sqlSessionFactory")
    private SqlSessionFactoryBean sqlSessionFactory(TDataSource mysqlTddlDataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(mysqlTddlDataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/*Mapper.xml"));
        sqlSessionFactoryBean.setPlugins(new Interceptor[] { new SqlStatementInterceptor(ServiceNameConstant.ENGINE)});
        return sqlSessionFactoryBean;
    }

拦截器: @Signature标识哪样的类在什么样的方法上进行切面。 详见: mybatis(6) - 自定义拦截器

package com.noob.interceptor;

import java.util.Properties;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.noob.common.constant.CommonConstants;
import com.noob.common.constant.CommonConstants.MonitorLogKey;
import com.noob.util.CommonUtil;
import com.noob.util.NetUtil;

/**
 * 数据库操作性能拦截器,记录耗时
 */
@Intercepts(value = {
        @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
        @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
                RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class }),
        @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
                RowBounds.class, ResultHandler.class }) })
@NoArgsConstructor
@Slf4j
public class SqlStatementInterceptor implements Interceptor {
    private Properties properties;
    private String     hostIp;

    public SqlStatementInterceptor(String appName) {
        properties = new Properties();
        properties.setProperty(MonitorLogKey.PROJECT_NAME, appName);
        hostIp = NetUtil.getLocalAddress();
    }

    @Override
    public Object intercept(Invocation arg0) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) arg0.getArgs()[0];
        String sqlId = mappedStatement.getId();
        Object returnValue;

        long start = System.currentTimeMillis();
        returnValue = arg0.proceed();
        long end = System.currentTimeMillis();
        log(sqlId, end - start);
        return returnValue;
    }

    private void log(String sqlId, long costTime) {
        try {
            JSONObject logObj = CommonUtil.createGrafanaLog();
            logObj.put(MonitorLogKey.LOG_TYPE, CommonConstants.MonitorType.SQL_MONITOR);//sql 耗时
            logObj.put(MonitorLogKey.PROJECT_NAME, properties.get(MonitorLogKey.PROJECT_NAME));
            logObj.put(MonitorLogKey.HOST_IP, hostIp);
            logObj.put("sql", sqlId);
            logObj.put("costTime", costTime);
            log.info(JSONObject.toJSONString(logObj, SerializerFeature.UseISO8601DateFormat));
        } catch (Exception e) {
            log.warn("output sql costTime fail.", e);
        }
    }

    @Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }

    @Override
    public void setProperties(Properties arg0) {
        this.properties = arg0;
    }
}

 

转载于:https://my.oschina.net/u/3434392/blog/3010600

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值