当前位置:网站首页>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
边栏推荐
- Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in
- Introduction to rust Foundation (basic type)
- 动态规划——相关概念,(数塔问题)
- Differences among bio, NiO and AIO
- "Pthread.h" not found problem encountered in compiling GCC
- SimpleITK学习笔记
- [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)
- Calculation method of AUC
- [backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
- leetcode860. Lemonade change
猜你喜欢

(perfect solution) how to set the position of Matplotlib legend freely

leetcode452. Detonate the balloon with the minimum number of arrows

小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计

How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in

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

Go practice - gorilla / handlers used by gorilla web Toolkit

XML配置文件

Class loading mechanism (detailed explanation of the whole process)

"Hands on deep learning" pytorch edition Chapter II exercise

Overview of basic knowledge of C language
随机推荐
Common methods of JS array
Brief introduction of realsense d435i imaging principle
ES7 easy mistakes in index creation
50 practical applications of R language (36) - data visualization from basic to advanced
1115 counting nodes in a BST (30 points)
Yolov5 input (II) | CSDN creative punch in
Common interview questions of microservice
6.23星期四库作业
Messy change of mouse style in win system
Introduction to redis and explanation of data types
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
[practical project] autonomous web server
study hard and make progress every day
(subplots用法)matplotlib如何绘制多个子图(轴域)
1110 complete binary tree (25 points)
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
谷歌 | 蛋白序列的深度嵌入和比对
Go language interface learning notes Continued
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
Gbase8s composite index (I)