mybatis 攔截器實(shí)現(xiàn) prometheus 監(jiān)控 sql 層

本文采用 Java 的實(shí)現(xiàn)方式針對(duì) prometheus 監(jiān)控

需要新建 Mybatis 的代理類

import java.util.Properties;

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 org.springframework.stereotype.Component;

import com.didapinche.server.commons.common.metrics.InterfaceMonitor;
import com.didapinche.server.commons.common.metrics.base.CommonUtils;

import io.prometheus.client.SimpleTimer;
import io.prometheus.client.Summary;

@Intercepts({
    @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 }),
    @Signature(type = Executor.class, method = "query",  args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class })
})
@Component
public class MybatisExecuteInterceptor implements Interceptor {

    private static final String MYBATIS = "mybatis";
    
    private static final Summary sqlLatency = Summary
            .build("server_sql_duration_seconds", "about sql execute duration")
            .labelNames("host", "app", "method")
            .quantile(0.8, 0.01).quantile(0.9, 0.01).quantile(0.99, 0.01)
            .register();

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        
        MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
        String id = ms.getId();
        String[] split = id.split("\\.");
        String methodName = split[split.length - 2] + ":" + split[split.length -1];

        InterfaceMonitor.getInstance().addTotal(methodName, MYBATIS);
        SimpleTimer st = new SimpleTimer();
        
        Object proceed = null;
        try {
            proceed = invocation.proceed();
        } catch (Exception e) {
            InterfaceMonitor.getInstance().addFail(methodName, MYBATIS);
            throw e;
        }
        sqlLatency.labels(CommonUtils.getHostName(), CommonUtils.getAppName(), methodName).observe(st.elapsedSeconds());
        return proceed;
    }

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

    @Override
    public void setProperties(Properties properties) {
    }

}

Grafana 監(jiān)控如下

image.png
  • Counter 統(tǒng)計(jì)


    image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容