Skip to content

Commit

Permalink
telemetry: add telemetry for savepoint (pingcap#36354)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Jul 20, 2022
1 parent e2ab431 commit e3eea88
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
25 changes: 15 additions & 10 deletions executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ import (
)

var (
stmtNodeCounterUse = metrics.StmtNodeCounter.WithLabelValues("Use")
stmtNodeCounterShow = metrics.StmtNodeCounter.WithLabelValues("Show")
stmtNodeCounterBegin = metrics.StmtNodeCounter.WithLabelValues("Begin")
stmtNodeCounterCommit = metrics.StmtNodeCounter.WithLabelValues("Commit")
stmtNodeCounterRollback = metrics.StmtNodeCounter.WithLabelValues("Rollback")
stmtNodeCounterInsert = metrics.StmtNodeCounter.WithLabelValues("Insert")
stmtNodeCounterReplace = metrics.StmtNodeCounter.WithLabelValues("Replace")
stmtNodeCounterDelete = metrics.StmtNodeCounter.WithLabelValues("Delete")
stmtNodeCounterUpdate = metrics.StmtNodeCounter.WithLabelValues("Update")
stmtNodeCounterSelect = metrics.StmtNodeCounter.WithLabelValues("Select")
stmtNodeCounterUse = metrics.StmtNodeCounter.WithLabelValues("Use")
stmtNodeCounterShow = metrics.StmtNodeCounter.WithLabelValues("Show")
stmtNodeCounterBegin = metrics.StmtNodeCounter.WithLabelValues("Begin")
stmtNodeCounterCommit = metrics.StmtNodeCounter.WithLabelValues("Commit")
stmtNodeCounterRollback = metrics.StmtNodeCounter.WithLabelValues("Rollback")
stmtNodeCounterInsert = metrics.StmtNodeCounter.WithLabelValues("Insert")
stmtNodeCounterReplace = metrics.StmtNodeCounter.WithLabelValues("Replace")
stmtNodeCounterDelete = metrics.StmtNodeCounter.WithLabelValues("Delete")
stmtNodeCounterUpdate = metrics.StmtNodeCounter.WithLabelValues("Update")
stmtNodeCounterSelect = metrics.StmtNodeCounter.WithLabelValues("Select")
stmtNodeCounterSavepoint = metrics.StmtNodeCounter.WithLabelValues("Savepoint")
)

// Compiler compiles an ast.StmtNode to a physical plan.
Expand Down Expand Up @@ -174,6 +175,8 @@ func CountStmtNode(stmtNode ast.StmtNode, inRestrictedSQL bool) {
stmtNodeCounterUpdate.Inc()
case "Select":
stmtNodeCounterSelect.Inc()
case "Savepoint":
stmtNodeCounterSavepoint.Inc()
default:
metrics.StmtNodeCounter.WithLabelValues(typeLabel).Inc()
}
Expand Down Expand Up @@ -411,6 +414,8 @@ func GetStmtLabel(stmtNode ast.StmtNode) string {
return "Trace"
case *ast.ShutdownStmt:
return "Shutdown"
case *ast.SavepointStmt:
return "Savepoint"
}
return "other"
}
5 changes: 5 additions & 0 deletions metrics/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,8 @@ func GetNonTransactionalStmtCounter() NonTransactionalStmtCounter {
DeleteCount: readCounter(NonTransactionalDeleteCount),
}
}

// GetSavepointStmtCounter gets the savepoint statement executed counter.
func GetSavepointStmtCounter() int64 {
return readCounter(StmtNodeCounter.With(prometheus.Labels{LblType: "Savepoint"}))
}
1 change: 1 addition & 0 deletions telemetry/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ func postReportTelemetryData() {
postReportMultiSchemaChangeUsage()
postReportSlowQueryStats()
postReportNonTransactionalCounter()
PostSavepointCount()
}
11 changes: 10 additions & 1 deletion telemetry/data_feature_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ type TxnUsage struct {
MutationCheckerUsed bool `json:"mutationCheckerUsed"`
AssertionLevel string `json:"assertionLevel"`
RcCheckTS bool `json:"rcCheckTS"`
SavepointCounter int64 `json:"SavepointCounter"`
}

var initialTxnCommitCounter metrics.TxnCommitCounter
var initialCTECounter m.CTEUsageCounter
var initialNonTransactionalCounter m.NonTransactionalStmtCounter
var initialMultiSchemaChangeCounter m.MultiSchemaChangeUsageCounter
var initialSavepointStmtCounter int64

// getTxnUsageInfo gets the usage info of transaction related features. It's exported for tests.
func getTxnUsageInfo(ctx sessionctx.Context) *TxnUsage {
Expand All @@ -240,7 +242,9 @@ func getTxnUsageInfo(ctx sessionctx.Context) *TxnUsage {
if val, err := variable.GetGlobalSystemVar(ctx.GetSessionVars(), variable.TiDBRCReadCheckTS); err == nil {
rcCheckTSUsed = val == variable.On
}
return &TxnUsage{asyncCommitUsed, onePCUsed, diff, mutationCheckerUsed, assertionUsed, rcCheckTSUsed}
currSavepointCount := m.GetSavepointStmtCounter()
diffSavepointCount := currSavepointCount - initialSavepointStmtCounter
return &TxnUsage{asyncCommitUsed, onePCUsed, diff, mutationCheckerUsed, assertionUsed, rcCheckTSUsed, diffSavepointCount}
}

func postReportTxnUsage() {
Expand All @@ -251,6 +255,11 @@ func postReportCTEUsage() {
initialCTECounter = m.GetCTECounter()
}

// PostSavepointCount exports for testing.
func PostSavepointCount() {
initialSavepointStmtCounter = m.GetSavepointStmtCounter()
}

// getCTEUsageInfo gets the CTE usages.
func getCTEUsageInfo() *m.CTEUsageCounter {
curr := m.GetCTECounter()
Expand Down
20 changes: 20 additions & 0 deletions telemetry/data_feature_usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,23 @@ func TestPagingUsageInfo(t *testing.T) {
require.NoError(t, err)
require.False(t, usage.EnablePaging)
}

func TestTxnSavepointUsageInfo(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("savepoint sp1")
tk.MustExec("savepoint sp2")
txnUsage := telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(2), txnUsage.SavepointCounter)

tk.MustExec("savepoint sp3")
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(3), txnUsage.SavepointCounter)

telemetry.PostSavepointCount()
tk.MustExec("savepoint sp1")
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(1), txnUsage.SavepointCounter)
}

0 comments on commit e3eea88

Please sign in to comment.