-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
metrics: add db QPS metric #9151
Changes from 12 commits
fa3d86b
e2903eb
b5a6013
d15e4bf
00a27ca
cc67cd5
f322489
919d34d
938f0d4
e7e214d
4c043ff
59a172b
98b7ebb
4796e5d
28d4547
c71c774
6a6b882
ac76f16
bc6cf35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ func (c *Compiler) Compile(ctx context.Context, stmtNode ast.StmtNode) (*ExecStm | |
} | ||
|
||
CountStmtNode(stmtNode, c.Ctx.GetSessionVars().InRestrictedSQL) | ||
DbCountStmtNode(stmtNode, c.Ctx.GetSessionVars().InRestrictedSQL) | ||
isExpensive := logExpensiveQuery(stmtNode, finalPlan) | ||
|
||
return &ExecStmt{ | ||
|
@@ -125,6 +126,120 @@ func CountStmtNode(stmtNode ast.StmtNode, inRestrictedSQL bool) { | |
metrics.StmtNodeCounter.WithLabelValues(GetStmtLabel(stmtNode)).Inc() | ||
} | ||
|
||
// DbCountStmtNode records the number of statements with the same type and db. | ||
func DbCountStmtNode(stmtNode ast.StmtNode, inRestrictedSQL bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to refactor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
cfg := config.GetGlobalConfig() | ||
|
||
if inRestrictedSQL || cfg.DbQPSMetricSwitch != 1 { | ||
return | ||
} | ||
|
||
dbLabels := getStmtDbLabel(stmtNode) | ||
typeLabel := GetStmtLabel(stmtNode) | ||
|
||
for _, dbLabel := range dbLabels { | ||
metrics.DbStmtNodeCounter.WithLabelValues(dbLabel, typeLabel).Inc() | ||
} | ||
} | ||
|
||
func getStmtDbLabel(stmtNode ast.StmtNode) []string { | ||
dbLabelMap := make(map[string]int, 0) | ||
iamzhoug37 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
switch x := stmtNode.(type) { | ||
case *ast.AlterTableStmt: | ||
dbLabel := x.Table.Schema.O | ||
dbLabelMap[dbLabel] = 0 | ||
case *ast.CreateIndexStmt: | ||
dbLabel := x.Table.Schema.O | ||
dbLabelMap[dbLabel] = 0 | ||
case *ast.CreateTableStmt: | ||
dbLabel := x.Table.Schema.O | ||
dbLabelMap[dbLabel] = 0 | ||
case *ast.InsertStmt: | ||
iamzhoug37 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dbLabels := getDbFromResultNode(x.Table.TableRefs) | ||
for _, db := range dbLabels { | ||
dbLabelMap[db] = 0 | ||
} | ||
case *ast.DropIndexStmt: | ||
dbLabel := x.Table.Schema.O | ||
dbLabelMap[dbLabel] = 0 | ||
case *ast.DropTableStmt: | ||
tables := x.Tables | ||
for _, table := range tables { | ||
dbLabel := table.Schema.O | ||
if _, ok := dbLabelMap[dbLabel]; !ok { | ||
dbLabelMap[dbLabel] = 0 | ||
} | ||
} | ||
case *ast.SelectStmt: | ||
dbLabels := getDbFromResultNode(x) | ||
for _, db := range dbLabels { | ||
dbLabelMap[db] = 0 | ||
} | ||
case *ast.UpdateStmt: | ||
if x.TableRefs != nil { | ||
dbLabels := getDbFromResultNode(x.TableRefs.TableRefs) | ||
for _, db := range dbLabels { | ||
dbLabelMap[db] = 0 | ||
} | ||
} | ||
case *ast.DeleteStmt: | ||
if x.TableRefs != nil { | ||
dbLabels := getDbFromResultNode(x.TableRefs.TableRefs) | ||
for _, db := range dbLabels { | ||
dbLabelMap[db] = 0 | ||
} | ||
} | ||
} | ||
|
||
dbLabels := make([]string, 0) | ||
iamzhoug37 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for k := range dbLabelMap { | ||
dbLabels = append(dbLabels, k) | ||
} | ||
|
||
return dbLabels | ||
} | ||
|
||
func getDbFromResultNode(resultNode ast.ResultSetNode) []string { //may have duplicate db name | ||
var dbLabels []string | ||
|
||
if resultNode == nil { | ||
return dbLabels | ||
} | ||
|
||
switch x := resultNode.(type) { | ||
case *ast.TableSource: | ||
return getDbFromResultNode(x.Source) | ||
case *ast.SelectStmt: | ||
if x.From != nil { | ||
return getDbFromResultNode(x.From.TableRefs) | ||
} | ||
case *ast.TableName: | ||
dbLabels = append(dbLabels, x.DBInfo.Name.O) | ||
case *ast.Join: | ||
if x.Left != nil { | ||
dbs := getDbFromResultNode(x.Left) | ||
if dbs != nil { | ||
for _, db := range dbs { | ||
dbLabels = append(dbLabels, db) | ||
} | ||
} | ||
} | ||
|
||
if x.Right != nil { | ||
dbs := getDbFromResultNode(x.Right) | ||
if dbs != nil { | ||
for _, db := range dbs { | ||
dbLabels = append(dbLabels, db) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return dbLabels | ||
} | ||
|
||
// GetStmtLabel generates a label for a statement. | ||
func GetStmtLabel(stmtNode ast.StmtNode) string { | ||
switch x := stmtNode.(type) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,6 @@ require ( | |
google.golang.org/genproto v0.0.0-20190108161440-ae2f86662275 // indirect | ||
google.golang.org/grpc v1.17.0 | ||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 | ||
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is not a related change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is go project auto update. |
||
sourcegraph.com/sourcegraph/appdash v0.0.0-20180531100431-4c381bd170b4 | ||
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tidb/config/config.go
Line 143 in 41838ce
RecordQPSbyDB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok