当前位置:网站首页>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=2
4、 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.CustomP6SpyLogger
5、 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.CustomStdoutLogger
6、 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_user
summary
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
边栏推荐
- Principles of BTC cryptography
- 乾元通多卡聚合路由器的技术解析
- SimpleITK学习笔记
- Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
- 1114 family property (25 points)
- 编译GCC遇到的“pthread.h” not found问题
- appium1.22.x 版本後的 appium inspector 需單獨安裝
- Progressive multi grasp detection using grasp path for rgbd images
- Go language interface learning notes Continued
- Chapter II program design of circular structure
猜你喜欢
Skip table: principle introduction, advantages and disadvantages of skiplist
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
[practical project] autonomous web server
Web APIs exclusivity
appium1.22. Appium inspector after X version needs to be installed separately
微服务常见面试题
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
Differences among bio, NiO and AIO
谷歌 | 蛋白序列的深度嵌入和比对
随机推荐
leetcode452. Detonate the balloon with the minimum number of arrows
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
1087 all roads lead to Rome (30 points)
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
Common interview questions of microservice
appium1.22.x 版本后的 appium inspector 需单独安装
Go practice -- use redis in golang (redis and go redis / redis)
Go language interface learning notes
Pytorch through load_ state_ Dict load weight
Burp suite plug-in based on actual combat uses tips
Progressive multi grasp detection using grasp path for rgbd images
Dynamic programming - related concepts, (tower problem)
Gan network thought
Classification and discussion of plane grab detection methods based on learning
Go language interface learning notes Continued
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
Botu uses peek and poke for IO mapping
Actual combat 8051 drives 8-bit nixie tube
[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)
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit