forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
359 lines (338 loc) · 9.25 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/plan"
"github.com/prometheus/client_golang/prometheus"
)
var (
stmtNodeCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "executor",
Name: "statement_node_total",
Help: "Counter of StmtNode.",
}, []string{"type"})
expensiveQueryCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "executor",
Name: "expensive_query_total",
Help: "Counter of expensive query.",
}, []string{"type"})
)
func init() {
prometheus.MustRegister(stmtNodeCounter)
prometheus.MustRegister(expensiveQueryCounter)
}
func stmtCount(node ast.StmtNode, p plan.Plan, inRestrictedSQL bool) bool {
var isExpensive bool
stmtLabel := StatementLabel(node, p, &isExpensive)
if !inRestrictedSQL && stmtLabel != IGNORE {
stmtNodeCounter.WithLabelValues(stmtLabel).Inc()
}
return isExpensive
}
const (
// IGNORE is a special label to identify the situations we want to ignore.
IGNORE = "Ignore"
// Select represents select statements.
Select = "Select"
// AlterTable represents alter table statements.
AlterTable = "AlterTable"
// AnalyzeTable represents analyze table statements.
AnalyzeTable = "AnalyzeTable"
// Begin represents begin statements.
Begin = "Begin"
// Commit represents commit statements.
Commit = "Commit"
// CreateDatabase represents create database statements.
CreateDatabase = "CreateDatabase"
// CreateIndex represents create index statements.
CreateIndex = "CreateIndex"
// CreateTable represents create table statements.
CreateTable = "CreateTable"
// CreateUser represents create user statements.
CreateUser = "CreateUser"
// Delete represents delete statements.
Delete = "Delete"
// DropDatabase represents drop database statements.
DropDatabase = "DropDatabase"
// DropIndex represents drop index statements.
DropIndex = "DropIndex"
// DropTable represents drop table statements.
DropTable = "DropTable"
// Explain represents explain statements.
Explain = "Explain"
// Replace represents replace statements.
Replace = "Replace"
// Insert represents insert statements.
Insert = "Insert"
// LoadDataStmt represents load data statements.
LoadDataStmt = "LoadData"
// RollBack represents roll back statements.
RollBack = "RollBack"
// Set represents set statements.
Set = "Set"
// Show represents show statements.
Show = "Show"
// TruncateTable represents truncate table statements.
TruncateTable = "TruncateTable"
// Update represents update statements.
Update = "Update"
// Grant represents grant statements.
Grant = "Grant"
// Revoke represents revoke statements.
Revoke = "Revoke"
)
// StatementLabel generates a label for a statement.
func StatementLabel(node ast.StmtNode, p plan.Plan, isExpensive *bool) string {
switch x := node.(type) {
case *ast.AlterTableStmt:
return AlterTable
case *ast.AnalyzeTableStmt:
return AnalyzeTable
case *ast.BeginStmt:
return Begin
case *ast.CommitStmt:
return Commit
case *ast.CreateDatabaseStmt:
return CreateDatabase
case *ast.CreateIndexStmt:
return CreateIndex
case *ast.CreateTableStmt:
return CreateTable
case *ast.CreateUserStmt:
return CreateUser
case *ast.DeleteStmt:
return getDeleteStmtLabel(x, p, isExpensive)
case *ast.DropDatabaseStmt:
return DropDatabase
case *ast.DropIndexStmt:
return DropIndex
case *ast.DropTableStmt:
return DropTable
case *ast.ExplainStmt:
return Explain
case *ast.InsertStmt:
if x.IsReplace {
return Replace
}
return Insert
case *ast.LoadDataStmt:
return LoadDataStmt
case *ast.RollbackStmt:
return RollBack
case *ast.SelectStmt:
return getSelectStmtLabel(x, p, isExpensive)
case *ast.SetStmt, *ast.SetPwdStmt:
return Set
case *ast.ShowStmt:
return Show
case *ast.TruncateTableStmt:
return TruncateTable
case *ast.UpdateStmt:
return getUpdateStmtLabel(x, p, isExpensive)
case *ast.GrantStmt:
return Grant
case *ast.RevokeStmt:
return Revoke
case *ast.DeallocateStmt, *ast.ExecuteStmt, *ast.PrepareStmt, *ast.UseStmt:
return IGNORE
}
return "other"
}
func getSelectStmtLabel(stmt *ast.SelectStmt, p plan.Plan, isExpensive *bool) string {
var attributes stmtAttributes
attributes.fromSelectStmt(stmt)
attributes.fromPlan(p)
stmtLabel := Select + attributes.toLabel()
attributes.logExpensiveStmt(stmtLabel, stmt.Text(), isExpensive)
return stmtLabel
}
func getDeleteStmtLabel(stmt *ast.DeleteStmt, p plan.Plan, isExpensive *bool) string {
var attributes stmtAttributes
attributes.fromDeleteStmt(stmt)
attributes.fromPlan(p)
stmtLabel := Delete + attributes.toLabel()
attributes.logExpensiveStmt(stmtLabel, stmt.Text(), isExpensive)
return stmtLabel
}
func getUpdateStmtLabel(stmt *ast.UpdateStmt, p plan.Plan, isExpensive *bool) string {
var attributes stmtAttributes
attributes.fromUpdateStmt(stmt)
attributes.fromPlan(p)
stmtLabel := Update + attributes.toLabel()
attributes.logExpensiveStmt(stmtLabel, stmt.Text(), isExpensive)
return stmtLabel
}
type stmtAttributes struct {
hasJoin bool
hasApply bool
hasAggregate bool
hasIndexDouble bool
hasIndexScan bool
hasTableScan bool
hasRange bool
hasOrder bool
hasLimit bool
isSystemTable bool
}
func (pa *stmtAttributes) fromSelectStmt(stmt *ast.SelectStmt) {
if stmt.Limit != nil {
pa.hasLimit = true
}
if stmt.OrderBy != nil {
pa.hasOrder = true
}
}
func (pa *stmtAttributes) fromDeleteStmt(stmt *ast.DeleteStmt) {
if stmt.Limit != nil {
pa.hasLimit = true
}
if stmt.Order != nil {
pa.hasOrder = true
}
}
func (pa *stmtAttributes) fromUpdateStmt(stmt *ast.UpdateStmt) {
if stmt.Limit != nil {
pa.hasLimit = true
}
if stmt.Order != nil {
pa.hasOrder = true
}
}
func (pa *stmtAttributes) fromPlan(p plan.Plan) {
switch x := p.(type) {
case *plan.PhysicalApply:
pa.hasApply = true
case *plan.PhysicalAggregation:
pa.hasAggregate = true
case *plan.PhysicalHashJoin:
pa.hasJoin = true
case *plan.PhysicalTableScan:
pa.hasTableScan = true
if len(x.AccessCondition) > 0 {
pa.hasRange = true
}
pa.setIsSystemTable(x.DBName)
case *plan.PhysicalIndexScan:
pa.hasIndexScan = true
if len(x.AccessCondition) > 0 {
pa.hasRange = true
}
pa.setIsSystemTable(x.DBName)
case *plan.PhysicalHashSemiJoin:
pa.hasJoin = true
case *plan.PhysicalTableReader:
for _, child := range x.TablePlans {
pa.fromPlan(child)
}
case *plan.PhysicalIndexReader:
for _, child := range x.IndexPlans {
pa.fromPlan(child)
}
case *plan.PhysicalIndexLookUpReader:
for _, child := range x.IndexPlans {
pa.fromPlan(child)
}
for _, child := range x.TablePlans {
pa.fromPlan(child)
}
pa.hasIndexDouble = true
}
children := p.Children()
for _, child := range children {
pa.fromPlan(child)
}
}
func (pa *stmtAttributes) setIsSystemTable(dbName model.CIStr) {
if dbName.L == mysql.SystemDB {
pa.isSystemTable = true
}
}
const (
attrSimple = "Simple"
attrFull = "Full"
attrRange = "Range"
attrTable = "Table"
attrIndex = "Index"
attrIndexOnly = "IndexOnly"
attrJoin = "Join"
attrApply = "Apply"
attrLimit = "Limit"
attrOrder = "Order"
attrAggregate = "Agg"
)
// toLabel ...
// Not all attributes is used to create the label, because we don't want too many labels.
func (pa *stmtAttributes) toLabel() string {
var attrs []string
// First attribute.
if pa.hasJoin {
attrs = append(attrs, attrJoin)
} else if pa.hasApply {
attrs = append(attrs, attrApply)
} else if pa.hasAggregate {
attrs = append(attrs, attrAggregate)
} else if pa.hasIndexDouble {
attrs = append(attrs, attrIndex)
} else if pa.hasIndexScan {
attrs = append(attrs, attrIndexOnly)
} else if pa.hasTableScan {
attrs = append(attrs, attrTable)
} else {
attrs = append(attrs, attrSimple)
}
// Second attribute.
if pa.hasRange {
attrs = append(attrs, attrRange)
} else {
attrs = append(attrs, attrFull)
}
// Third attribute, optional.
if pa.hasOrder {
attrs = append(attrs, attrOrder)
}
// Fourth attribute, optional.
if pa.hasLimit {
attrs = append(attrs, attrLimit)
}
return strings.Join(attrs, "")
}
func (pa *stmtAttributes) isExpensiveStmt() bool {
if pa.hasRange || pa.hasLimit || pa.isSystemTable {
return false
}
if !pa.hasIndexScan && !pa.hasTableScan && !pa.hasIndexDouble {
return false
}
return true
}
func (pa *stmtAttributes) logExpensiveStmt(stmtLabel string, sql string, isExpensive *bool) {
if pa.isExpensiveStmt() {
const logSQLLen = 1024
if len(sql) > logSQLLen {
sql = sql[:logSQLLen] + fmt.Sprintf("len(%d)", len(sql))
}
log.Warnf("[EXPENSIVE_QUERY] %s", sql)
expensiveQueryCounter.WithLabelValues(stmtLabel).Inc()
*isExpensive = true
}
}