Skip to content

Commit

Permalink
server: fix the missing type of query total counter for resource grou…
Browse files Browse the repository at this point in the history
…ps above 2 (#52606) (#52637)

close #52605
  • Loading branch information
ti-chi-bot committed Apr 22, 2024
1 parent 83440bc commit e406d57
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ go_test(
"//pkg/util/syncutil",
"//pkg/util/topsql/state",
"@com_github_docker_go_units//:go-units",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_pingcap_kvproto//pkg/metapb",
"@com_github_prometheus_client_golang//prometheus/testutil",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//error",
"@com_github_tikv_client_go_v2//testutils",
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ func (cc *clientConn) addMetrics(cmd byte, startTime time.Time, err error) {
if counter != nil {
counter.Inc()
} else {
label := strconv.Itoa(int(cmd))
label := server_metrics.CmdToString(cmd)
if err != nil {
metrics.QueryTotalCounter.WithLabelValues(label, "Error", resourceGroupName).Inc()
} else {
Expand Down
40 changes: 40 additions & 0 deletions pkg/server/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import (
"testing"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/extension"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser/auth"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/server/internal"
Expand All @@ -52,6 +54,7 @@ import (
"github.com/pingcap/tidb/pkg/util/arena"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/dbterror/exeerrors"
promtestutils "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
tikverr "github.com/tikv/client-go/v2/error"
"github.com/tikv/client-go/v2/testutils"
Expand Down Expand Up @@ -2039,3 +2042,40 @@ func TestCloseConn(t *testing.T) {
}
wg.Wait()
}

func TestConnAddMetrics(t *testing.T) {
store := testkit.CreateMockStore(t)
re := require.New(t)
tk := testkit.NewTestKit(t, store)
cc := &clientConn{
alloc: arena.NewAllocator(1024),
chunkAlloc: chunk.NewAllocator(),
pkt: internal.NewPacketIOForTest(bufio.NewWriter(bytes.NewBuffer(nil))),
}
tk.MustExec("use test")
cc.SetCtx(&TiDBContext{Session: tk.Session(), stmts: make(map[int]*TiDBStatement)})

// default
cc.addMetrics(mysql.ComQuery, time.Now(), nil)
counter := metrics.QueryTotalCounter
v := promtestutils.ToFloat64(counter.WithLabelValues("Query", "OK", "default"))
require.Equal(t, 1.0, v)

// rg1
cc.getCtx().GetSessionVars().ResourceGroupName = "test_rg1"
cc.addMetrics(mysql.ComQuery, time.Now(), nil)
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("Query", "OK", "default")), 1.0)
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("Query", "OK", "test_rg1")), 1.0)
/// inc the counter again
cc.addMetrics(mysql.ComQuery, time.Now(), nil)
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("Query", "OK", "test_rg1")), 2.0)

// rg2
cc.getCtx().GetSessionVars().ResourceGroupName = "test_rg2"
// error
cc.addMetrics(mysql.ComQuery, time.Now(), errors.New("unknown error"))
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("Query", "Error", "test_rg2")), 1.0)
// ok
cc.addMetrics(mysql.ComStmtExecute, time.Now(), nil)
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("StmtExecute", "OK", "test_rg2")), 1.0)
}
35 changes: 35 additions & 0 deletions pkg/server/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package metrics

import (
"strconv"

"github.com/pingcap/tidb/pkg/domain/resourcegroup"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser/mysql"
Expand Down Expand Up @@ -46,6 +48,39 @@ func init() {
InitMetricsVars()
}

// CmdToString convert command type to string.
func CmdToString(cmd byte) string {
switch cmd {
case mysql.ComSleep:
return "Sleep"
case mysql.ComQuit:
return "Quit"
case mysql.ComInitDB:
return "InitDB"
case mysql.ComQuery:
return "Query"
case mysql.ComPing:
return "Ping"
case mysql.ComFieldList:
return "FieldList"
case mysql.ComStmtPrepare:
return "StmtPrepare"
case mysql.ComStmtExecute:
return "StmtExecute"
case mysql.ComStmtFetch:
return "StmtFetch"
case mysql.ComStmtClose:
return "StmtClose"
case mysql.ComStmtSendLongData:
return "StmtSendLongData"
case mysql.ComStmtReset:
return "StmtReset"
case mysql.ComSetOption:
return "SetOption"
}
return strconv.Itoa(int(cmd))
}

// InitMetricsVars init server metrics vars.
func InitMetricsVars() {
QueryTotalCountOk = []prometheus.Counter{
Expand Down

0 comments on commit e406d57

Please sign in to comment.