Skip to content

Commit 3f39a97

Browse files
author
flike
committed
log slow sql
1 parent 2a538aa commit 3f39a97

File tree

9 files changed

+29
-19
lines changed

9 files changed

+29
-19
lines changed

README_ZH.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ kingshard是一个由Go开发高性能MySQL Proxy项目,kingshard在满足基
2121
12. 支持order by,limit等操作。
2222
13. 支持主流语言(java,php,python,C/C++,Go)SDK的mysql的prepare特性。
2323
14. 支持到后端DB的最大连接数限制。
24+
15. 支持SQL日志及慢日志输出。
2425

2526
## kinshard详细说明
2627

backend/node.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (n *Node) GetSlaveConn() (*BackendConn, error) {
8383
}
8484

8585
if db == nil {
86-
return nil, errors.ErrNoSlaveDb
86+
return nil, errors.ErrNoSlaveDB
8787
}
8888
if atomic.LoadInt32(&(db.state)) == Down {
8989
return nil, errors.ErrSlaveDown
@@ -184,7 +184,7 @@ func (n *Node) DeleteSlave(addr string) error {
184184
defer n.Unlock()
185185
slaveCount := len(n.Slave)
186186
if slaveCount == 0 {
187-
return errors.ErrNoSlaveDb
187+
return errors.ErrNoSlaveDB
188188
} else if slaveCount == 1 {
189189
n.Slave = nil
190190
n.SlaveWeights = nil
@@ -260,7 +260,7 @@ func (n *Node) UpSlave(addr string) error {
260260
func (n *Node) DownMaster(addr string) error {
261261
db := n.Master
262262
if db == nil || db.addr != addr {
263-
return errors.ErrNoMasterDb
263+
return errors.ErrNoMasterDB
264264
}
265265
db.Close()
266266
atomic.StoreInt32(&(db.state), Down)
@@ -271,7 +271,7 @@ func (n *Node) DownSlave(addr string) error {
271271
n.Lock()
272272
if n.Slave == nil {
273273
n.Unlock()
274-
return errors.ErrNoSlaveDb
274+
return errors.ErrNoSlaveDB
275275
}
276276
slaves := make([]*DB, len(n.Slave))
277277
copy(slaves, n.Slave)
@@ -291,7 +291,7 @@ func (n *Node) DownSlave(addr string) error {
291291
func (n *Node) ParseMaster(masterStr string) error {
292292
var err error
293293
if len(masterStr) == 0 {
294-
return errors.ErrNoMasterDb
294+
return errors.ErrNoMasterDB
295295
}
296296

297297
n.Master, err = n.OpenDB(masterStr)

config/config.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ import (
88

99
//整个config文件对应的结构
1010
type Config struct {
11-
Addr string `yaml:"addr"`
12-
User string `yaml:"user"`
13-
Password string `yaml:"password"`
14-
LogPath string `yaml:"log_path"`
15-
LogLevel string `yaml:"log_level"`
16-
17-
AllowIps string `yaml:"allow_ips"`
18-
LogSql string `yaml:"log_sql"`
19-
20-
Nodes []NodeConfig `yaml:"nodes"`
11+
Addr string `yaml:"addr"`
12+
User string `yaml:"user"`
13+
Password string `yaml:"password"`
14+
LogPath string `yaml:"log_path"`
15+
LogLevel string `yaml:"log_level"`
16+
LogSql string `yaml:"log_sql"`
17+
SlowLogTime int `yaml:"slow_log_time"`
18+
AllowIps string `yaml:"allow_ips"`
19+
Nodes []NodeConfig `yaml:"nodes"`
2120

2221
Schemas []SchemaConfig `yaml:"schemas"`
2322
}

core/errors/errors.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var (
88
ErrNoMasterConn = errors.New("no master connection")
99
ErrNoSlaveConn = errors.New("no slave connection")
1010
ErrNoDefaultNode = errors.New("no default node")
11-
ErrNoMasterDb = errors.New("no master database")
12-
ErrNoSlaveDb = errors.New("no slave database")
11+
ErrNoMasterDB = errors.New("no master database")
12+
ErrNoSlaveDB = errors.New("no slave database")
1313
ErrNoDatabase = errors.New("no database")
1414

1515
ErrMasterDown = errors.New("master is down")

doc/KingDoc/how_to_use_kingshard.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ password : kingshard
3232
log_level : debug
3333
# 打开SQL日志,设置为on;关闭SQL日志,设置为off
3434
log_sql : on
35+
#如果设置了该项,则只输出SQL执行时间超过slow_log_time(ms)的SQL日志,不设置则输出全部SQL日志
36+
slow_log_time : 100
3537
#日志文件路径,如果不配置则会输出到终端。
3638
log_path : /Users/flike/log
3739
# 只允许下面的IP列表连接kingshard,如果不配置则对连接kingshard的IP不做限制。

doc/KingDoc/how_to_use_kingshard_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ log_path : /Users/flike/log
3333
log_level : debug
3434
#if set log_sql(on|off) off,the sql log will not output
3535
log_sql: off
36+
#only log the query that take more than slow_log_time ms
37+
#slow_log_time : 100
3638
# only allow this ip list ip to connect kingshard
3739
#allow_ips: 127.0.0.1
3840

doc/KingDoc/kingshard_install_document.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ password : kingshard
2323
log_level : debug
2424
# 打开SQL日志,设置为on;关闭SQL日志,设置为off
2525
log_sql : on
26+
#如果设置了该项,则只输出SQL执行时间超过slow_log_time(单位毫秒)的SQL日志,不设置则输出全部SQL日志
27+
slow_log_time : 100
2628
#日志文件路径,如果不配置则会输出到终端。
2729
log_path : /Users/flike/log
2830
# 只允许下面的IP列表连接kingshard,如果不配置则对连接kingshard的IP不做限制。

etc/ks.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ password : kingshard
1313
log_level : debug
1414
#if set log_sql(on|off) off,the sql log will not output
1515
log_sql: off
16+
#only log the query that take more than slow_log_time ms
17+
#slow_log_time : 100
1618
# only allow this ip list ip to connect kingshard
1719
#allow_ips: 127.0.0.1
1820

proxy/server/conn_query.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ func (c *ClientConn) executeInNode(conn *backend.BackendConn, sql string, args [
174174
state = "INFO"
175175
}
176176
execTime := float64(time.Now().UnixNano()-startTime) / float64(time.Millisecond)
177-
if strings.ToLower(c.proxy.cfg.LogSql) != golog.LogSqlOff {
177+
if strings.ToLower(c.proxy.cfg.LogSql) != golog.LogSqlOff &&
178+
execTime > float64(c.proxy.cfg.SlowLogTime) {
178179
golog.OutputSql(state, "%.1fms - %s->%s:%s",
179180
execTime,
180181
c.c.RemoteAddr(),
@@ -227,7 +228,8 @@ func (c *ClientConn) executeInMultiNodes(conns map[string]*backend.BackendConn,
227228
rs[i] = r
228229
}
229230
execTime := float64(time.Now().UnixNano()-startTime) / float64(time.Millisecond)
230-
if c.proxy.cfg.LogSql != golog.LogSqlOff {
231+
if c.proxy.cfg.LogSql != golog.LogSqlOff &&
232+
execTime > float64(c.proxy.cfg.SlowLogTime) {
231233
golog.OutputSql(state, "%.1fms - %s->%s:%s",
232234
execTime,
233235
c.c.RemoteAddr(),

0 commit comments

Comments
 (0)