From 24a9caa4d4cd3c2a24642b3c0a723dfbe129f07f Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 19 Feb 2024 18:37:56 +0800 Subject: [PATCH] metrics: add connection and fail metrics by `resource group name` (#49424) (#50124) ref pingcap/tidb#49318 --- pkg/executor/compiler.go | 10 ++-- pkg/metrics/executor.go | 2 +- pkg/metrics/server.go | 10 ++-- pkg/metrics/session.go | 2 +- pkg/metrics/telemetry.go | 2 +- pkg/parser/go.mod | 1 - pkg/parser/go.sum | 2 - pkg/server/BUILD.bazel | 1 + pkg/server/conn.go | 51 +++++++++++++----- pkg/server/conn_test.go | 2 +- .../testserverclient/server_client.go | 4 +- pkg/server/metrics/BUILD.bazel | 1 + pkg/server/metrics/metrics.go | 53 ++++++++++--------- pkg/server/server.go | 40 +++++++++----- pkg/session/session.go | 2 +- 15 files changed, 109 insertions(+), 74 deletions(-) diff --git a/pkg/executor/compiler.go b/pkg/executor/compiler.go index fd724588763b6..f0aa729023e91 100644 --- a/pkg/executor/compiler.go +++ b/pkg/executor/compiler.go @@ -105,9 +105,9 @@ func (c *Compiler) Compile(ctx context.Context, stmtNode ast.StmtNode) (_ *ExecS }) if preparedObj != nil { - CountStmtNode(preparedObj.PreparedAst.Stmt, sessVars.InRestrictedSQL) + CountStmtNode(preparedObj.PreparedAst.Stmt, sessVars.InRestrictedSQL, sessVars.ResourceGroupName) } else { - CountStmtNode(stmtNode, sessVars.InRestrictedSQL) + CountStmtNode(stmtNode, sessVars.InRestrictedSQL, sessVars.ResourceGroupName) } var lowerPriority bool if c.Ctx.GetSessionVars().StmtCtx.Priority == mysql.NoPriority { @@ -187,7 +187,7 @@ func isPhysicalPlanNeedLowerPriority(p plannercore.PhysicalPlan) bool { } // CountStmtNode records the number of statements with the same type. -func CountStmtNode(stmtNode ast.StmtNode, inRestrictedSQL bool) { +func CountStmtNode(stmtNode ast.StmtNode, inRestrictedSQL bool, resourceGroup string) { if inRestrictedSQL { return } @@ -203,11 +203,11 @@ func CountStmtNode(stmtNode ast.StmtNode, inRestrictedSQL bool) { } case config.GetGlobalConfig().Status.RecordDBLabel: for dbLabel := range dbLabels { - metrics.StmtNodeCounter.WithLabelValues(typeLabel, dbLabel).Inc() + metrics.StmtNodeCounter.WithLabelValues(typeLabel, dbLabel, resourceGroup).Inc() } } } else { - metrics.StmtNodeCounter.WithLabelValues(typeLabel, "").Inc() + metrics.StmtNodeCounter.WithLabelValues(typeLabel, "", resourceGroup).Inc() } } diff --git a/pkg/metrics/executor.go b/pkg/metrics/executor.go index a364adf462515..b0295e6ae54d4 100644 --- a/pkg/metrics/executor.go +++ b/pkg/metrics/executor.go @@ -58,7 +58,7 @@ func InitExecutorMetrics() { Subsystem: "executor", Name: "statement_total", Help: "Counter of StmtNode.", - }, []string{LblType, LblDb}) + }, []string{LblType, LblDb, LblResourceGroup}) DbStmtNodeCounter = NewCounterVec( prometheus.CounterOpts{ diff --git a/pkg/metrics/server.go b/pkg/metrics/server.go index 27f1f0c9b8ee6..e6ae6ef9fb978 100644 --- a/pkg/metrics/server.go +++ b/pkg/metrics/server.go @@ -31,7 +31,7 @@ var ( QueryDurationHistogram *prometheus.HistogramVec QueryTotalCounter *prometheus.CounterVec AffectedRowsCounter *prometheus.CounterVec - ConnGauge prometheus.Gauge + ConnGauge *prometheus.GaugeVec DisconnectionCounter *prometheus.CounterVec PreparedStmtGauge prometheus.Gauge ExecuteErrorCounter *prometheus.CounterVec @@ -97,7 +97,7 @@ func InitServerMetrics() { Subsystem: "server", Name: "query_total", Help: "Counter of queries.", - }, []string{LblType, LblResult}) + }, []string{LblType, LblResult, LblResourceGroup}) AffectedRowsCounter = NewCounterVec( prometheus.CounterOpts{ @@ -107,13 +107,13 @@ func InitServerMetrics() { Help: "Counters of server affected rows.", }, []string{LblSQLType}) - ConnGauge = NewGauge( + ConnGauge = NewGaugeVec( prometheus.GaugeOpts{ Namespace: "tidb", Subsystem: "server", Name: "connections", Help: "Number of connections.", - }) + }, []string{LblResourceGroup}) DisconnectionCounter = NewCounterVec( prometheus.CounterOpts{ @@ -136,7 +136,7 @@ func InitServerMetrics() { Subsystem: "server", Name: "execute_error_total", Help: "Counter of execute errors.", - }, []string{LblType, LblDb}) + }, []string{LblType, LblDb, LblResourceGroup}) CriticalErrorCounter = NewCounter( prometheus.CounterOpts{ diff --git a/pkg/metrics/session.go b/pkg/metrics/session.go index 712bd22974b1c..9a744155c8c6a 100644 --- a/pkg/metrics/session.go +++ b/pkg/metrics/session.go @@ -218,7 +218,7 @@ func InitSessionMetrics() { Subsystem: "session", Name: "resource_group_query_total", Help: "Counter of the total number of queries for the resource group", - }, []string{LblName}) + }, []string{LblName, LblResourceGroup}) FairLockingUsageCount = NewCounterVec( prometheus.CounterOpts{ diff --git a/pkg/metrics/telemetry.go b/pkg/metrics/telemetry.go index 0cb6439053eec..4da1d9a3f2778 100644 --- a/pkg/metrics/telemetry.go +++ b/pkg/metrics/telemetry.go @@ -476,7 +476,7 @@ func GetNonTransactionalStmtCounter() NonTransactionalStmtCounter { // GetSavepointStmtCounter gets the savepoint statement executed counter. func GetSavepointStmtCounter() int64 { - return readCounter(StmtNodeCounter.With(prometheus.Labels{LblType: "Savepoint", LblDb: ""})) + return readCounter(StmtNodeCounter.WithLabelValues("Savepoint", "", "default")) } // GetLazyPessimisticUniqueCheckSetCounter returns the counter of setting tidb_constraint_check_in_place_pessimistic to false. diff --git a/pkg/parser/go.mod b/pkg/parser/go.mod index 1ac354e13f806..8bc3326719c0a 100644 --- a/pkg/parser/go.mod +++ b/pkg/parser/go.mod @@ -19,7 +19,6 @@ require ( require ( github.com/benbjohnson/clock v1.3.5 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/klauspost/compress v1.17.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect go.uber.org/atomic v1.11.0 // indirect diff --git a/pkg/parser/go.sum b/pkg/parser/go.sum index 90e703e857ff8..6dbccf4122843 100644 --- a/pkg/parser/go.sum +++ b/pkg/parser/go.sum @@ -13,8 +13,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= -github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g= -github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= diff --git a/pkg/server/BUILD.bazel b/pkg/server/BUILD.bazel index 648adb67278ef..9fd96813b783e 100644 --- a/pkg/server/BUILD.bazel +++ b/pkg/server/BUILD.bazel @@ -24,6 +24,7 @@ go_library( "//pkg/config", "//pkg/domain", "//pkg/domain/infosync", + "//pkg/domain/resourcegroup", "//pkg/errno", "//pkg/executor", "//pkg/executor/mppcoordmanager", diff --git a/pkg/server/conn.go b/pkg/server/conn.go index 6125efdb2374c..482b005ab136f 100644 --- a/pkg/server/conn.go +++ b/pkg/server/conn.go @@ -58,6 +58,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/pkg/config" "github.com/pingcap/tidb/pkg/domain/infosync" + "github.com/pingcap/tidb/pkg/domain/resourcegroup" "github.com/pingcap/tidb/pkg/errno" "github.com/pingcap/tidb/pkg/executor" "github.com/pingcap/tidb/pkg/extension" @@ -338,17 +339,26 @@ func (cc *clientConn) handshake(ctx context.Context) error { func (cc *clientConn) Close() error { cc.server.rwlock.Lock() delete(cc.server.clients, cc.connectionID) - connections := len(cc.server.clients) + resourceGroupName, count := "", 0 + if ctx := cc.getCtx(); ctx != nil { + resourceGroupName = ctx.GetSessionVars().ResourceGroupName + count = cc.server.ConnNumByResourceGroup[resourceGroupName] + if count <= 1 { + delete(cc.server.ConnNumByResourceGroup, resourceGroupName) + } else { + cc.server.ConnNumByResourceGroup[resourceGroupName]-- + } + } cc.server.rwlock.Unlock() - return closeConn(cc, connections) + return closeConn(cc, resourceGroupName, count) } // closeConn is idempotent and thread-safe. // It will be called on the same `clientConn` more than once to avoid connection leak. -func closeConn(cc *clientConn, connections int) error { +func closeConn(cc *clientConn, resourceGroupName string, connections int) error { var err error cc.closeOnce.Do(func() { - metrics.ConnGauge.Set(float64(connections)) + metrics.ConnGauge.WithLabelValues(resourceGroupName).Set(float64(connections)) if cc.connectionID > 0 { cc.server.dom.ReleaseConnID(cc.connectionID) cc.connectionID = 0 @@ -372,7 +382,14 @@ func closeConn(cc *clientConn, connections int) error { func (cc *clientConn) closeWithoutLock() error { delete(cc.server.clients, cc.connectionID) - return closeConn(cc, len(cc.server.clients)) + name := cc.getCtx().GetSessionVars().ResourceGroupName + count := cc.server.ConnNumByResourceGroup[name] + if count <= 1 { + delete(cc.server.ConnNumByResourceGroup, name) + } else { + cc.server.ConnNumByResourceGroup[name]-- + } + return closeConn(cc, name, count-1) } // writeInitialHandshake sends server version, connection ID, server capability, collation, server status @@ -1092,9 +1109,11 @@ func (cc *clientConn) Run(ctx context.Context) { if ctx := cc.getCtx(); ctx != nil { txnMode = ctx.GetSessionVars().GetReadableTxnMode() } - for _, dbName := range session.GetDBNames(cc.getCtx().GetSessionVars()) { - metrics.ExecuteErrorCounter.WithLabelValues(metrics.ExecuteErrorToLabel(err), dbName).Inc() + vars := cc.getCtx().GetSessionVars() + for _, dbName := range session.GetDBNames(vars) { + metrics.ExecuteErrorCounter.WithLabelValues(metrics.ExecuteErrorToLabel(err), dbName, vars.ResourceGroupName).Inc() } + if storeerr.ErrLockAcquireFailAndNoWaitSet.Equal(err) { logutil.Logger(ctx).Debug("Expected error for FOR UPDATE NOWAIT", zap.Error(err)) } else { @@ -1143,20 +1162,25 @@ func (cc *clientConn) addMetrics(cmd byte, startTime time.Time, err error) { return } + vars := cc.getCtx().GetSessionVars() + resourceGroupName := vars.ResourceGroupName var counter prometheus.Counter - if err != nil && int(cmd) < len(server_metrics.QueryTotalCountErr) { - counter = server_metrics.QueryTotalCountErr[cmd] - } else if err == nil && int(cmd) < len(server_metrics.QueryTotalCountOk) { - counter = server_metrics.QueryTotalCountOk[cmd] + if len(resourceGroupName) == 0 || resourceGroupName == resourcegroup.DefaultResourceGroupName { + if err != nil && int(cmd) < len(server_metrics.QueryTotalCountErr) { + counter = server_metrics.QueryTotalCountErr[cmd] + } else if err == nil && int(cmd) < len(server_metrics.QueryTotalCountOk) { + counter = server_metrics.QueryTotalCountOk[cmd] + } } + if counter != nil { counter.Inc() } else { label := strconv.Itoa(int(cmd)) if err != nil { - metrics.QueryTotalCounter.WithLabelValues(label, "Error").Inc() + metrics.QueryTotalCounter.WithLabelValues(label, "Error", resourceGroupName).Inc() } else { - metrics.QueryTotalCounter.WithLabelValues(label, "OK").Inc() + metrics.QueryTotalCounter.WithLabelValues(label, "OK", resourceGroupName).Inc() } } @@ -1182,7 +1206,6 @@ func (cc *clientConn) addMetrics(cmd byte, startTime time.Time, err error) { server_metrics.AffectedRowsCounterUpdate.Add(float64(affectedRows)) } - vars := cc.getCtx().GetSessionVars() for _, dbName := range session.GetDBNames(vars) { metrics.QueryDurationHistogram.WithLabelValues(sqlType, dbName, vars.StmtCtx.ResourceGroupName).Observe(cost.Seconds()) } diff --git a/pkg/server/conn_test.go b/pkg/server/conn_test.go index 679e87b019cb9..57add48d4d1d7 100644 --- a/pkg/server/conn_test.go +++ b/pkg/server/conn_test.go @@ -2033,7 +2033,7 @@ func TestCloseConn(t *testing.T) { for i := 0; i < numGoroutines; i++ { go func() { defer wg.Done() - err := closeConn(cc, 1) + err := closeConn(cc, "default", 1) require.NoError(t, err) }() } diff --git a/pkg/server/internal/testserverclient/server_client.go b/pkg/server/internal/testserverclient/server_client.go index be26180ae8ca8..b09291db6d6dd 100644 --- a/pkg/server/internal/testserverclient/server_client.go +++ b/pkg/server/internal/testserverclient/server_client.go @@ -2265,7 +2265,7 @@ func (cli *TestServerClient) getMetrics(t *testing.T) []byte { func getStmtCnt(content string) (stmtCnt map[string]int) { stmtCnt = make(map[string]int) - r := regexp.MustCompile("tidb_executor_statement_total{db=\"\",type=\"([A-Z|a-z|-]+)\"} (\\d+)") + r := regexp.MustCompile("tidb_executor_statement_total{db=\"\",resource_group=\".*\",type=\"([A-Z|a-z|-]+)\"} (\\d+)") matchResult := r.FindAllStringSubmatch(content, -1) for _, v := range matchResult { cnt, _ := strconv.Atoi(v[2]) @@ -2276,7 +2276,7 @@ func getStmtCnt(content string) (stmtCnt map[string]int) { func getDBStmtCnt(content, dbName string) (stmtCnt map[string]int) { stmtCnt = make(map[string]int) - r := regexp.MustCompile(fmt.Sprintf("tidb_executor_statement_total{db=\"%s\",type=\"([A-Z|a-z|-]+)\"} (\\d+)", dbName)) + r := regexp.MustCompile(fmt.Sprintf("tidb_executor_statement_total{db=\"%s\",resource_group=\".*\",type=\"([A-Z|a-z|-]+)\"} (\\d+)", dbName)) matchResult := r.FindAllStringSubmatch(content, -1) for _, v := range matchResult { cnt, _ := strconv.Atoi(v[2]) diff --git a/pkg/server/metrics/BUILD.bazel b/pkg/server/metrics/BUILD.bazel index 7e84c7ec918d2..f3bb2fb5e6f03 100644 --- a/pkg/server/metrics/BUILD.bazel +++ b/pkg/server/metrics/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "github.com/pingcap/tidb/pkg/server/metrics", visibility = ["//visibility:public"], deps = [ + "//pkg/domain/resourcegroup", "//pkg/metrics", "//pkg/parser/mysql", "@com_github_prometheus_client_golang//prometheus", diff --git a/pkg/server/metrics/metrics.go b/pkg/server/metrics/metrics.go index c07faadf7616d..3710e8c915476 100644 --- a/pkg/server/metrics/metrics.go +++ b/pkg/server/metrics/metrics.go @@ -15,6 +15,7 @@ package metrics import ( + "github.com/pingcap/tidb/pkg/domain/resourcegroup" "github.com/pingcap/tidb/pkg/metrics" "github.com/pingcap/tidb/pkg/parser/mysql" "github.com/prometheus/client_golang/prometheus" @@ -48,34 +49,34 @@ func init() { // InitMetricsVars init server metrics vars. func InitMetricsVars() { QueryTotalCountOk = []prometheus.Counter{ - mysql.ComSleep: metrics.QueryTotalCounter.WithLabelValues("Sleep", "OK"), - mysql.ComQuit: metrics.QueryTotalCounter.WithLabelValues("Quit", "OK"), - mysql.ComInitDB: metrics.QueryTotalCounter.WithLabelValues("InitDB", "OK"), - mysql.ComQuery: metrics.QueryTotalCounter.WithLabelValues("Query", "OK"), - mysql.ComPing: metrics.QueryTotalCounter.WithLabelValues("Ping", "OK"), - mysql.ComFieldList: metrics.QueryTotalCounter.WithLabelValues("FieldList", "OK"), - mysql.ComStmtPrepare: metrics.QueryTotalCounter.WithLabelValues("StmtPrepare", "OK"), - mysql.ComStmtExecute: metrics.QueryTotalCounter.WithLabelValues("StmtExecute", "OK"), - mysql.ComStmtFetch: metrics.QueryTotalCounter.WithLabelValues("StmtFetch", "OK"), - mysql.ComStmtClose: metrics.QueryTotalCounter.WithLabelValues("StmtClose", "OK"), - mysql.ComStmtSendLongData: metrics.QueryTotalCounter.WithLabelValues("StmtSendLongData", "OK"), - mysql.ComStmtReset: metrics.QueryTotalCounter.WithLabelValues("StmtReset", "OK"), - mysql.ComSetOption: metrics.QueryTotalCounter.WithLabelValues("SetOption", "OK"), + mysql.ComSleep: metrics.QueryTotalCounter.WithLabelValues("Sleep", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComQuit: metrics.QueryTotalCounter.WithLabelValues("Quit", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComInitDB: metrics.QueryTotalCounter.WithLabelValues("InitDB", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComQuery: metrics.QueryTotalCounter.WithLabelValues("Query", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComPing: metrics.QueryTotalCounter.WithLabelValues("Ping", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComFieldList: metrics.QueryTotalCounter.WithLabelValues("FieldList", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtPrepare: metrics.QueryTotalCounter.WithLabelValues("StmtPrepare", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtExecute: metrics.QueryTotalCounter.WithLabelValues("StmtExecute", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtFetch: metrics.QueryTotalCounter.WithLabelValues("StmtFetch", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtClose: metrics.QueryTotalCounter.WithLabelValues("StmtClose", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtSendLongData: metrics.QueryTotalCounter.WithLabelValues("StmtSendLongData", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtReset: metrics.QueryTotalCounter.WithLabelValues("StmtReset", "OK", resourcegroup.DefaultResourceGroupName), + mysql.ComSetOption: metrics.QueryTotalCounter.WithLabelValues("SetOption", "OK", resourcegroup.DefaultResourceGroupName), } QueryTotalCountErr = []prometheus.Counter{ - mysql.ComSleep: metrics.QueryTotalCounter.WithLabelValues("Sleep", "Error"), - mysql.ComQuit: metrics.QueryTotalCounter.WithLabelValues("Quit", "Error"), - mysql.ComInitDB: metrics.QueryTotalCounter.WithLabelValues("InitDB", "Error"), - mysql.ComQuery: metrics.QueryTotalCounter.WithLabelValues("Query", "Error"), - mysql.ComPing: metrics.QueryTotalCounter.WithLabelValues("Ping", "Error"), - mysql.ComFieldList: metrics.QueryTotalCounter.WithLabelValues("FieldList", "Error"), - mysql.ComStmtPrepare: metrics.QueryTotalCounter.WithLabelValues("StmtPrepare", "Error"), - mysql.ComStmtExecute: metrics.QueryTotalCounter.WithLabelValues("StmtExecute", "Error"), - mysql.ComStmtFetch: metrics.QueryTotalCounter.WithLabelValues("StmtFetch", "Error"), - mysql.ComStmtClose: metrics.QueryTotalCounter.WithLabelValues("StmtClose", "Error"), - mysql.ComStmtSendLongData: metrics.QueryTotalCounter.WithLabelValues("StmtSendLongData", "Error"), - mysql.ComStmtReset: metrics.QueryTotalCounter.WithLabelValues("StmtReset", "Error"), - mysql.ComSetOption: metrics.QueryTotalCounter.WithLabelValues("SetOption", "Error"), + mysql.ComSleep: metrics.QueryTotalCounter.WithLabelValues("Sleep", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComQuit: metrics.QueryTotalCounter.WithLabelValues("Quit", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComInitDB: metrics.QueryTotalCounter.WithLabelValues("InitDB", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComQuery: metrics.QueryTotalCounter.WithLabelValues("Query", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComPing: metrics.QueryTotalCounter.WithLabelValues("Ping", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComFieldList: metrics.QueryTotalCounter.WithLabelValues("FieldList", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtPrepare: metrics.QueryTotalCounter.WithLabelValues("StmtPrepare", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtExecute: metrics.QueryTotalCounter.WithLabelValues("StmtExecute", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtFetch: metrics.QueryTotalCounter.WithLabelValues("StmtFetch", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtClose: metrics.QueryTotalCounter.WithLabelValues("StmtClose", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtSendLongData: metrics.QueryTotalCounter.WithLabelValues("StmtSendLongData", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComStmtReset: metrics.QueryTotalCounter.WithLabelValues("StmtReset", "Error", resourcegroup.DefaultResourceGroupName), + mysql.ComSetOption: metrics.QueryTotalCounter.WithLabelValues("SetOption", "Error", resourcegroup.DefaultResourceGroupName), } DisconnectNormal = metrics.DisconnectionCounter.WithLabelValues(metrics.LblOK) diff --git a/pkg/server/server.go b/pkg/server/server.go index cac07242d354d..e0dd235e0bc5b 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -116,8 +116,9 @@ type Server struct { socket net.Listener concurrentLimiter *TokenLimiter - rwlock sync.RWMutex - clients map[uint64]*clientConn + rwlock sync.RWMutex + clients map[uint64]*clientConn + ConnNumByResourceGroup map[string]int capability uint32 dom *domain.Domain @@ -235,14 +236,15 @@ func (s *Server) newConn(conn net.Conn) *clientConn { // NewServer creates a new Server. func NewServer(cfg *config.Config, driver IDriver) (*Server, error) { s := &Server{ - cfg: cfg, - driver: driver, - concurrentLimiter: NewTokenLimiter(cfg.TokenLimit), - clients: make(map[uint64]*clientConn), - internalSessions: make(map[interface{}]struct{}, 100), - health: uatomic.NewBool(true), - inShutdownMode: uatomic.NewBool(false), - printMDLLogTime: time.Now(), + cfg: cfg, + driver: driver, + concurrentLimiter: NewTokenLimiter(cfg.TokenLimit), + clients: make(map[uint64]*clientConn), + ConnNumByResourceGroup: make(map[string]int), + internalSessions: make(map[interface{}]struct{}, 100), + health: uatomic.NewBool(true), + inShutdownMode: uatomic.NewBool(false), + printMDLLogTime: time.Now(), } s.capability = defaultCapability setTxnScope() @@ -596,17 +598,27 @@ func (s *Server) Close() { func (s *Server) registerConn(conn *clientConn) bool { s.rwlock.Lock() defer s.rwlock.Unlock() - connections := len(s.clients) + connections := make(map[string]int, 0) + for _, conn := range s.clients { + resourceGroup := conn.getCtx().GetSessionVars().ResourceGroupName + connections[resourceGroup]++ + } logger := logutil.BgLogger() if s.inShutdownMode.Load() { logger.Info("close connection directly when shutting down") - terror.Log(closeConn(conn, connections)) + for resourceGroupName, count := range s.ConnNumByResourceGroup { + metrics.ConnGauge.WithLabelValues(resourceGroupName).Set(float64(count)) + } + terror.Log(closeConn(conn, "", 0)) return false } s.clients[conn.connectionID] = conn - connections = len(s.clients) - metrics.ConnGauge.Set(float64(connections)) + s.ConnNumByResourceGroup[conn.getCtx().GetSessionVars().ResourceGroupName]++ + + for name, count := range s.ConnNumByResourceGroup { + metrics.ConnGauge.WithLabelValues(name).Set(float64(count)) + } return true } diff --git a/pkg/session/session.go b/pkg/session/session.go index b8960ede6d19a..7ccfa9367d26d 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -2279,7 +2279,7 @@ func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex // current session is attached with a resource group. resourceGroupName := s.GetSessionVars().StmtCtx.ResourceGroupName if len(resourceGroupName) > 0 { - metrics.ResourceGroupQueryTotalCounter.WithLabelValues(resourceGroupName).Inc() + metrics.ResourceGroupQueryTotalCounter.WithLabelValues(resourceGroupName, resourceGroupName).Inc() } if err != nil {