diff --git a/executor/adapter.go b/executor/adapter.go index b324fdb331a69..1723d0f069f50 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -856,8 +856,7 @@ func FormatSQL(sql string) stringutil.StringerFunc { var ( sessionExecuteRunDurationInternal = metrics.SessionExecuteRunDuration.WithLabelValues(metrics.LblInternal) sessionExecuteRunDurationGeneral = metrics.SessionExecuteRunDuration.WithLabelValues(metrics.LblGeneral) - totalTiFlashQueryFailCounter = metrics.TiFlashQueryTotalCounter.WithLabelValues(metrics.LblError) - totalTiFlashQuerySuccCounter = metrics.TiFlashQueryTotalCounter.WithLabelValues(metrics.LblOK) + totalTiFlashQuerySuccCounter = metrics.TiFlashQueryTotalCounter.WithLabelValues("", metrics.LblOK) ) // FinishExecuteStmt is used to record some information after `ExecStmt` execution finished: @@ -866,7 +865,7 @@ var ( // 3. record execute duration metric. // 4. update the `PrevStmt` in session variable. // 5. reset `DurationParse` in session variable. -func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, succ bool, hasMoreResults bool) { +func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, err error, hasMoreResults bool) { sessVars := a.Ctx.GetSessionVars() execDetail := sessVars.StmtCtx.GetExecDetails() // Attach commit/lockKeys runtime stats to executor runtime stats. @@ -885,6 +884,7 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, succ bool, hasMoreResults boo // Only record the read keys in write statement which affect row more than 0. a.Ctx.GetTxnWriteThroughputSLI().AddReadKeys(execDetail.ScanDetail.ProcessedKeys) } + succ := err == nil // `LowSlowQuery` and `SummaryStmt` must be called before recording `PrevStmt`. a.LogSlowQuery(txnTS, succ, hasMoreResults) a.SummaryStmt(succ) @@ -892,7 +892,7 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, succ bool, hasMoreResults boo if succ { totalTiFlashQuerySuccCounter.Inc() } else { - totalTiFlashQueryFailCounter.Inc() + metrics.TiFlashQueryTotalCounter.WithLabelValues(metrics.ExecuteErrorToLabel(err), metrics.LblError).Inc() } } sessVars.PrevStmt = FormatSQL(a.GetTextToLog()) @@ -909,7 +909,7 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, succ bool, hasMoreResults boo // CloseRecordSet will finish the execution of current statement and do some record work func (a *ExecStmt) CloseRecordSet(txnStartTS uint64, lastErr error) { - a.FinishExecuteStmt(txnStartTS, lastErr == nil, false) + a.FinishExecuteStmt(txnStartTS, lastErr, false) a.logAudit() // Detach the Memory and disk tracker for the previous stmtCtx from GlobalMemoryUsageTracker and GlobalDiskUsageTracker if stmtCtx := a.Ctx.GetSessionVars().StmtCtx; stmtCtx != nil { diff --git a/metrics/server.go b/metrics/server.go index 3f01eec30bea8..f83c7afee2732 100644 --- a/metrics/server.go +++ b/metrics/server.go @@ -227,7 +227,7 @@ var ( Subsystem: "server", Name: "tiflash_query_total", Help: "Counter of TiFlash queries.", - }, []string{LblResult}) + }, []string{LblType, LblResult}) ) // ExecuteErrorToLabel converts an execute error to label. diff --git a/server/conn.go b/server/conn.go index 76b4f8d32dd1d..303ef0defb8e5 100644 --- a/server/conn.go +++ b/server/conn.go @@ -1723,7 +1723,7 @@ func (cc *clientConn) handleStmt(ctx context.Context, stmt ast.StmtNode, warns [ if handled { execStmt := cc.ctx.Value(session.ExecStmtVarKey) if execStmt != nil { - execStmt.(*executor.ExecStmt).FinishExecuteStmt(0, err == nil, false) + execStmt.(*executor.ExecStmt).FinishExecuteStmt(0, err, false) } } if err != nil { diff --git a/session/session.go b/session/session.go index fe6fef23bf019..c7d006bb9a767 100644 --- a/session/session.go +++ b/session/session.go @@ -1669,7 +1669,7 @@ func runStmt(ctx context.Context, se *session, s sqlexec.Statement) (rs sqlexec. } else { // If it is not a select statement or special query, we record its slow log here, // then it could include the transaction commit time. - s.(*executor.ExecStmt).FinishExecuteStmt(origTxnCtx.StartTS, err == nil, false) + s.(*executor.ExecStmt).FinishExecuteStmt(origTxnCtx.StartTS, err, false) } return nil, err }