当前位置:网站首页>Talk about how to use p6spy for SQL monitoring
Talk about how to use p6spy for SQL monitoring
2022-07-03 05:19:00 【Linyb geek Road】
Preface
1、p6spy brief introduction
P6Spy It's a framework , No code changes need to be made to existing applications , You can seamlessly intercept and record database data . adopt P6Spy We can SQL Statement to intercept , Equivalent to one SQL Statement logger , In this way, we can use it for relevant analysis , For example, performance analysis
2、 Realization principle
p6spy Hijack the data source of the application , The application operation database is actually calling p6spy Data source ,p6spy Hijack to what needs to be executed sql perhaps hql After statements like , He calls one himself realDatasource, Then operate the database
3、 Relevant official documents
github:https://github.com/p6spy/p6spy
Official website :https://p6spy.readthedocs.io/en/latest/index.html
p6spy Use
1、 In the project pom Introduce related GAV
<dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>${p6spy.version}</version> </dependency>2、 Switch between in the project jdbc Drivers and data sources
Change to the following
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: ${DRIVER_CALSS_NAME:com.p6spy.engine.spy.P6SpyDriver} url: ${DATASOURCE_URL:jdbc:p6spy:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai} username: ${DATASOURCE_USERNAME:root} password: ${DATASOURCE_PWD:123456}3、 stay resource Add under directory spy.properties
The configuration is as shown in the following example
#3.2.1 Above use modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory#3.2.1 The following uses or does not configure #modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory# Custom log printing #logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLoggerlogMessageFormat=com.github.lybgeek.p6spy.extentsion.CustomP6SpyLogger# Log output to the console #appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger# Use the logging system to record sql#appender=com.p6spy.engine.spy.appender.Slf4JLoggerappender=com.github.lybgeek.p6spy.extentsion.CustomStdoutLogger# Set up p6spy driver agent deregisterdrivers=true# Cancel JDBC URL Prefix useprefix=true# Configuration record Log exception , The result sets that can be removed are error,info,batch,debug,statement,commit,rollback,result,resultset.excludecategories=info,debug,result,commit,resultset# Date format dateformat=yyyy-MM-dd HH:mm:ss# The actual drive can be multiple #driverlist=org.h2.Driver# Whether to turn on slowly SQL Record outagedetection=true# slow SQL Record standard 2 second outagedetectioninterval=24、 Custom log format 【 Optional 】
public class CustomP6SpyLogger implements MessageFormattingStrategy { /** * Sql Log formatting * * @param connectionId: Connect ID * @param now: current time * @param elapsed: Spend time * @param category: Category * @param prepared: precompile SQL * @param sql: Final executed SQL * @param url: Database connection address * @return Format log results */ @Override public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { return StringUtils.isNotBlank(sql) ? " Time consuming :" + elapsed + " ms " + now + "\n perform SQL:" + sql.replaceAll("[\\s]+", " ") + "\n" : ""; }}stay spy.properties Configure custom log format in
logMessageFormat=com.github.lybgeek.p6spy.extentsion.CustomP6SpyLogger5、 Custom log output 【 Optional 】
public class CustomStdoutLogger extends com.p6spy.engine.spy.appender.StdoutLogger{ @Override public void logText(String text) { System.out.println("sql:" + text); }}stay spy.properties Configure custom log output
appender=com.github.lybgeek.p6spy.extentsion.CustomStdoutLogger6、 Test and observe the console output
sql: Time consuming :1 ms 2022-05-10 11:38:34 perform SQL:SELECT id,username,password,fullname,mobile,email FROM t_usersummary
p6spy According to sql Execution efficiency analysis of sql Yes sql To optimize , But because p6spy Will have a certain impact on performance , Therefore, it is not suitable for use in a formal environment . Besides, it's about p6spy For more detailed configuration, see the following links https://p6spy.readthedocs.io/en/latest/configandusage.html
demo link
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-p6spy
边栏推荐
- Go language interface learning notes Continued
- study hard and make progress every day
- cookie session jwt
- Introduction to deep learning (II) -- univariate linear regression
- Go practice -- closures in golang (anonymous functions, closures)
- Altaro VM backup getting started
- BIO、NIO、AIO区别
- Actual combat 8051 drives 8-bit nixie tube
- leetcode452. Detonate the balloon with the minimum number of arrows
- XML配置文件
猜你喜欢

2022-02-11 daily clock in: problem fine brush

穀歌 | 蛋白序列的深度嵌入和比對

appium1.22. Appium inspector after X version needs to be installed separately

leetcode452. Detonate the balloon with the minimum number of arrows

Why is go language particularly popular in China

Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)

Ueditor, FCKeditor, kindeditor editor vulnerability

JS dynamic table creation

Technical analysis of qianyuantong multi card aggregation router

BTC-密码学原理
随机推荐
appium1.22.x 版本后的 appium inspector 需单独安装
ROS Compilation Principle
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)
Making coco datasets
编译GCC遇到的“pthread.h” not found问题
Progressive multi grasp detection using grasp path for rgbd images
(完美解决)matplotlib图例(legend)如何自由设置其位置
appium1.22. Appium inspector after X version needs to be installed separately
Gbase8s composite index (I)
My first Smartphone
XML配置文件
JS scope
Redis 过期淘汰机制
1119 pre- and post order traversals (30 points)
Web APIs exclusivity
1103 integer factorization (30 points)
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
1110 complete binary tree (25 points)
Introduction to rust Foundation (basic type)