Skip to content

Commit 6660de7

Browse files
authored
server: fix the missing type of query total counter for resource groups above 2 (#52606)
close #52605
1 parent 0778297 commit 6660de7

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

pkg/server/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ go_test(
200200
"@com_github_pingcap_errors//:errors",
201201
"@com_github_pingcap_failpoint//:failpoint",
202202
"@com_github_pingcap_kvproto//pkg/metapb",
203+
"@com_github_prometheus_client_golang//prometheus/testutil",
203204
"@com_github_stretchr_testify//require",
204205
"@com_github_tikv_client_go_v2//error",
205206
"@com_github_tikv_client_go_v2//testutils",

pkg/server/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ func (cc *clientConn) addMetrics(cmd byte, startTime time.Time, err error) {
12061206
if counter != nil {
12071207
counter.Inc()
12081208
} else {
1209-
label := strconv.Itoa(int(cmd))
1209+
label := server_metrics.CmdToString(cmd)
12101210
if err != nil {
12111211
metrics.QueryTotalCounter.WithLabelValues(label, "Error", resourceGroupName).Inc()
12121212
} else {

pkg/server/conn_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ import (
3030
"testing"
3131
"time"
3232

33+
"github.com/pingcap/errors"
3334
"github.com/pingcap/failpoint"
3435
"github.com/pingcap/kvproto/pkg/metapb"
3536
"github.com/pingcap/tidb/pkg/config"
3637
"github.com/pingcap/tidb/pkg/domain"
3738
"github.com/pingcap/tidb/pkg/extension"
39+
"github.com/pingcap/tidb/pkg/metrics"
3840
"github.com/pingcap/tidb/pkg/parser/auth"
3941
"github.com/pingcap/tidb/pkg/parser/mysql"
4042
"github.com/pingcap/tidb/pkg/server/internal"
@@ -54,6 +56,7 @@ import (
5456
"github.com/pingcap/tidb/pkg/util/dbterror/exeerrors"
5557
"github.com/pingcap/tidb/pkg/util/plancodec"
5658
"github.com/pingcap/tidb/pkg/util/sqlkiller"
59+
promtestutils "github.com/prometheus/client_golang/prometheus/testutil"
5760
"github.com/stretchr/testify/require"
5861
tikverr "github.com/tikv/client-go/v2/error"
5962
"github.com/tikv/client-go/v2/testutils"
@@ -2085,3 +2088,40 @@ func TestCloseConn(t *testing.T) {
20852088
}
20862089
wg.Wait()
20872090
}
2091+
2092+
func TestConnAddMetrics(t *testing.T) {
2093+
store := testkit.CreateMockStore(t)
2094+
re := require.New(t)
2095+
tk := testkit.NewTestKit(t, store)
2096+
cc := &clientConn{
2097+
alloc: arena.NewAllocator(1024),
2098+
chunkAlloc: chunk.NewAllocator(),
2099+
pkt: internal.NewPacketIOForTest(bufio.NewWriter(bytes.NewBuffer(nil))),
2100+
}
2101+
tk.MustExec("use test")
2102+
cc.SetCtx(&TiDBContext{Session: tk.Session(), stmts: make(map[int]*TiDBStatement)})
2103+
2104+
// default
2105+
cc.addMetrics(mysql.ComQuery, time.Now(), nil)
2106+
counter := metrics.QueryTotalCounter
2107+
v := promtestutils.ToFloat64(counter.WithLabelValues("Query", "OK", "default"))
2108+
require.Equal(t, 1.0, v)
2109+
2110+
// rg1
2111+
cc.getCtx().GetSessionVars().ResourceGroupName = "test_rg1"
2112+
cc.addMetrics(mysql.ComQuery, time.Now(), nil)
2113+
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("Query", "OK", "default")), 1.0)
2114+
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("Query", "OK", "test_rg1")), 1.0)
2115+
/// inc the counter again
2116+
cc.addMetrics(mysql.ComQuery, time.Now(), nil)
2117+
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("Query", "OK", "test_rg1")), 2.0)
2118+
2119+
// rg2
2120+
cc.getCtx().GetSessionVars().ResourceGroupName = "test_rg2"
2121+
// error
2122+
cc.addMetrics(mysql.ComQuery, time.Now(), errors.New("unknown error"))
2123+
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("Query", "Error", "test_rg2")), 1.0)
2124+
// ok
2125+
cc.addMetrics(mysql.ComStmtExecute, time.Now(), nil)
2126+
re.Equal(promtestutils.ToFloat64(counter.WithLabelValues("StmtExecute", "OK", "test_rg2")), 1.0)
2127+
}

pkg/server/metrics/metrics.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package metrics
1616

1717
import (
18+
"strconv"
19+
1820
"github.com/pingcap/tidb/pkg/domain/resourcegroup"
1921
"github.com/pingcap/tidb/pkg/metrics"
2022
"github.com/pingcap/tidb/pkg/parser/mysql"
@@ -46,6 +48,39 @@ func init() {
4648
InitMetricsVars()
4749
}
4850

51+
// CmdToString convert command type to string.
52+
func CmdToString(cmd byte) string {
53+
switch cmd {
54+
case mysql.ComSleep:
55+
return "Sleep"
56+
case mysql.ComQuit:
57+
return "Quit"
58+
case mysql.ComInitDB:
59+
return "InitDB"
60+
case mysql.ComQuery:
61+
return "Query"
62+
case mysql.ComPing:
63+
return "Ping"
64+
case mysql.ComFieldList:
65+
return "FieldList"
66+
case mysql.ComStmtPrepare:
67+
return "StmtPrepare"
68+
case mysql.ComStmtExecute:
69+
return "StmtExecute"
70+
case mysql.ComStmtFetch:
71+
return "StmtFetch"
72+
case mysql.ComStmtClose:
73+
return "StmtClose"
74+
case mysql.ComStmtSendLongData:
75+
return "StmtSendLongData"
76+
case mysql.ComStmtReset:
77+
return "StmtReset"
78+
case mysql.ComSetOption:
79+
return "SetOption"
80+
}
81+
return strconv.Itoa(int(cmd))
82+
}
83+
4984
// InitMetricsVars init server metrics vars.
5085
func InitMetricsVars() {
5186
QueryTotalCountOk = []prometheus.Counter{

0 commit comments

Comments
 (0)