建设网站的总结,重庆航运建设发展有限公司网站,青岛做网站定制,做网站刷QQ会员网站文章目录背景具体实现MySQL5MySQL6MySQL8使用方法测试结果背景 公司的一个需求#xff0c;公司既有的链路追踪日志组件要支持MySQL的sql执行时间打印#xff0c;要实现链路追踪常用的手段就是实现第三方框架或工具提供的拦截器接口或者是过滤器接口#xff0c;对于MySQL也不…
文章目录背景具体实现MySQL5MySQL6MySQL8使用方法测试结果背景 公司的一个需求公司既有的链路追踪日志组件要支持MySQL的sql执行时间打印要实现链路追踪常用的手段就是实现第三方框架或工具提供的拦截器接口或者是过滤器接口对于MySQL也不例外实际上就是实现了MySQL驱动的拦截器接口而已。
具体实现 MySQL的渠道有不同的版本不同版本的拦截器接口是不同的所以要针对你所使用的不同版本的MySQL驱动去实现响应的拦截器接下来分别介绍下MySQL渠道5,6,8版本的实现方式。
MySQL5 这里以MySQL渠道5.1.18版本为例实现实现StatementInterceptorV2接口主要实现逻辑在preProcess和postProcess方法这两个方法是sql执行前后要执行的方法我所使用的框架是logback这里使用MDC来记录sql执行前的一个时间戳代码在postProcess方法MDC.put(sql_exec_time, start);自己也可以使用ThreadLocal等来实现然后在postProcess方法中使用MDC.get(sql_exec_time)将记录的sql执行前的时间取出来最后再用当前时间戳减去sql执行前的时间就算出了sql执行的时间。
import static net.logstash.logback.marker.Markers.append;import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSetInternalMethods;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.StatementInterceptorV2;
import com.redick.util.LogUtil;
import java.sql.SQLException;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;/*** author Redick01*/
Slf4j
public class Mysql5StatementInterceptor implements StatementInterceptorV2 {Overridepublic void init(Connection connection, Properties properties) throws SQLException {}Overridepublic ResultSetInternalMethods preProcess(String s, Statement statement, Connection connection)throws SQLException {String start String.valueOf(System.currentTimeMillis());MDC.put(sql_exec_time, start);log.info(LogUtil.customizeMarker(LogUtil.kLOG_KEY_TRACE_TAG, sql_exec_before), 开始执行sql);return null;}Overridepublic boolean executeTopLevelOnly() {return false;}Overridepublic void destroy() {}Overridepublic ResultSetInternalMethods postProcess(String s, Statement statement,ResultSetInternalMethods resultSetInternalMethods, Connection connection, int i,boolean b, boolean b1, SQLException e) throws SQLException {long start Long.parseLong(MDC.get(sql_exec_time));long end System.currentTimeMillis();log.info(LogUtil.customizeMarker(LogUtil.kLOG_KEY_TRACE_TAG, sql_exec_after).and(append(LogUtil.kLOG_KEY_SQL_EXEC_DURATION, end - start)), 结束执行sql);return null;}
}MySQL6 MySQL6和MySQL5基本一样只是接口不是同一个直接放代码
import static net.logstash.logback.marker.Markers.append;import com.mysql.cj.api.MysqlConnection;
import com.mysql.cj.api.jdbc.Statement;
import com.mysql.cj.api.jdbc.interceptors.StatementInterceptor;
import com.mysql.cj.api.log.Log;
import com.mysql.cj.api.mysqla.result.Resultset;
import com.redick.util.LogUtil;
import java.sql.SQLException;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;/*** author Redick01*/
Slf4j
public class Mysql6StatementInterceptor implements StatementInterceptor {Overridepublic StatementInterceptor init(MysqlConnection mysqlConnection, Properties properties,Log log) {return null;}Overridepublic T extends Resultset T preProcess(String s, Statement statement) throws SQLException {String start String.valueOf(System.currentTimeMillis());MDC.put(sql_exec_time, start);log.info(LogUtil.customizeMarker(LogUtil.kLOG_KEY_TRACE_TAG, sql_exec_before), 开始执行sql);return null;}Overridepublic boolean executeTopLevelOnly() {return false;}Overridepublic void destroy() {}Overridepublic T extends Resultset T postProcess(String s, Statement statement, T t, int i, boolean b,boolean b1, Exception e) throws SQLException {long start Long.parseLong(MDC.get(sql_exec_time));long end System.currentTimeMillis();log.info(LogUtil.customizeMarker(LogUtil.kLOG_KEY_TRACE_TAG, sql_exec_after).and(append(LogUtil.kLOG_KEY_SQL_EXEC_DURATION, end - start)), 结束执行sql);return null;}
}MySQL8 MySQL8和MySQL5/6的拦截器接口又不一样了MySQL8的拦截器接口是com.mysql.cj.interceptors.QueryInterceptor统计sql执行时间的方式还是一样的代码如下
import static net.logstash.logback.marker.Markers.append;import com.mysql.cj.MysqlConnection;
import com.mysql.cj.Query;
import com.mysql.cj.interceptors.QueryInterceptor;
import com.mysql.cj.log.Log;
import com.mysql.cj.protocol.Resultset;
import com.mysql.cj.protocol.ServerSession;
import com.redick.util.LogUtil;
import java.util.Properties;
import java.util.function.Supplier;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;/*** author Redick01*/
Slf4j
public class Mysql8QueryInterceptor implements QueryInterceptor {Overridepublic QueryInterceptor init(MysqlConnection mysqlConnection, Properties properties, Log log) {return null;}Overridepublic T extends Resultset T preProcess(SupplierString supplier, Query query) {String start String.valueOf(System.currentTimeMillis());MDC.put(sql_exec_time, start);log.info(LogUtil.customizeMarker(LogUtil.kLOG_KEY_TRACE_TAG, sql_exec_before), 开始执行sql);return null;}Overridepublic boolean executeTopLevelOnly() {return false;}Overridepublic void destroy() {}Overridepublic T extends Resultset T postProcess(SupplierString supplier, Query query, T t,ServerSession serverSession) {long start Long.parseLong(MDC.get(sql_exec_time));long end System.currentTimeMillis();log.info(LogUtil.customizeMarker(LogUtil.kLOG_KEY_TRACE_TAG, sql_exec_after).and(append(LogUtil.kLOG_KEY_SQL_EXEC_DURATION, end - start)), 结束执行sql);return null;}
}使用方法 MySQL5和6的使用方式一样在数据库链接的url中增加如下statementInterceptors参数例如 url: jdbc:mysql://127.0.0.1:3316/log-helper?useUnicodetruecharacterEncodingUTF8statementInterceptorscom.redick.support.mysql.Mysql5StatementInterceptorserverTimezoneCSTMySQL8则是在url中增加queryInterceptors参数例如 url: jdbc:mysql://127.0.0.1:3316/log-helper?useUnicodetruecharacterEncodingUTF8queryInterceptorscom.redick.support.mysql.Mysql8QueryInterceptorserverTimezoneCST测试结果 sql执行前日志
{timestamp:2023-02-28T17:16:29.23408:00,version:0.0.1,message:开始执行sql,logger_name:com.redick.support.mysql.Mysql5StatementInterceptor,thread_name:http-nio-3321-exec-4,level:INFO,level_value:20000,traceId:9ed930dc-4cc6-4719-bf33-9fcb618fd65b,spanId:1,request_type:getName,parentId:0,trace_tag:sql_exec_before}sql执行后日志sql_duration标识执行sql耗时3ms
{timestamp:2023-02-28T17:16:29.23708:00,version:0.0.1,message:结束执行sql,logger_name:com.redick.support.mysql.Mysql5StatementInterceptor,thread_name:http-nio-3321-exec-4,level:INFO,level_value:20000,traceId:9ed930dc-4cc6-4719-bf33-9fcb618fd65b,spanId:1,request_type:getName,parentId:0,trace_tag:sql_exec_after,sql_duration:3}具体实现代码参考基于Spring AOP logstash-logback-encoder日志链路追踪工具LogHelper 文章转载自: http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.qzqjz.cn.gov.cn.qzqjz.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.xjmpg.cn.gov.cn.xjmpg.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.kdbcx.cn.gov.cn.kdbcx.cn http://www.morning.3dcb8231.cn.gov.cn.3dcb8231.cn http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.fbxdp.cn.gov.cn.fbxdp.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.rhmt.cn.gov.cn.rhmt.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.hous-e.com.gov.cn.hous-e.com http://www.morning.mgfnt.cn.gov.cn.mgfnt.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.srjbs.cn.gov.cn.srjbs.cn http://www.morning.jncxr.cn.gov.cn.jncxr.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.xmttd.cn.gov.cn.xmttd.cn http://www.morning.mbfkt.cn.gov.cn.mbfkt.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.gnmhy.cn.gov.cn.gnmhy.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.lskrg.cn.gov.cn.lskrg.cn http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn