From 3873c2f4ea795df7465c2ba447b87117042b6f8a Mon Sep 17 00:00:00 2001 From: eastfisher Date: Thu, 17 Dec 2020 10:46:49 +0800 Subject: [PATCH] add basic proxy metrics (#30) * add go pprof metrics * add basic proxy metrics --- go.mod | 1 + pkg/proxy/api.go | 12 ++++++++++ pkg/proxy/server/conn.go | 51 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 34e8b562..179f00be 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/pingcap/failpoint v0.0.0-20200702092429-9f69995143ce github.com/pingcap/parser v0.0.0-20200803072748-fdf66528323d github.com/pingcap/tidb v1.1.0-beta.0.20200826081922-9c1c21270001 + github.com/prometheus/client_golang v1.5.1 github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 github.com/siddontang/go-mysql v1.1.0 github.com/stretchr/testify v1.5.1 diff --git a/pkg/proxy/api.go b/pkg/proxy/api.go index c905d05b..ca46b069 100644 --- a/pkg/proxy/api.go +++ b/pkg/proxy/api.go @@ -3,6 +3,7 @@ package proxy import ( "net" "net/http" + "net/http/pprof" "github.com/gin-gonic/gin" "github.com/pingcap-incubator/weir/pkg/config" @@ -10,6 +11,7 @@ import ( "github.com/pingcap-incubator/weir/pkg/proxy/namespace" "github.com/pingcap-incubator/weir/pkg/proxy/server" "github.com/pingcap/tidb/util/logutil" + "github.com/prometheus/client_golang/prometheus/promhttp" "go.uber.org/zap" ) @@ -70,6 +72,16 @@ func CreateHttpApiServer(proxyServer *server.Server, nsmgr *namespace.NamespaceM namespaceHttpHandler := NewNamespaceHttpHandler(apiServer.nsmgr, apiServer.cfgCenter) namespaceHttpHandler.AddHandlersToRouteGroup(namespaceRouteGroup) + metricsRouteGroup := engine.Group("/metrics") + metricsRouteGroup.GET("/", gin.WrapF(promhttp.Handler().ServeHTTP)) + + pprofRouteGroup := engine.Group("/debug/pprof") + pprofRouteGroup.GET("/", gin.WrapF(pprof.Index)) + pprofRouteGroup.GET("/cmdline", gin.WrapF(pprof.Cmdline)) + pprofRouteGroup.GET("/profile", gin.WrapF(pprof.Profile)) + pprofRouteGroup.GET("/symbol", gin.WrapF(pprof.Symbol)) + pprofRouteGroup.GET("/trace", gin.WrapF(pprof.Trace)) + apiServer.engine = engine return apiServer, nil } diff --git a/pkg/proxy/server/conn.go b/pkg/proxy/server/conn.go index 53603af0..feb15977 100644 --- a/pkg/proxy/server/conn.go +++ b/pkg/proxy/server/conn.go @@ -32,6 +32,7 @@ import ( "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/util/arena" "github.com/pingcap/tidb/util/logutil" + "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" ) @@ -42,6 +43,39 @@ const ( connStatusWaitShutdown // Notified by server to close. ) +var ( + 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"), + } + 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"), + } +) + // clientConn represents a connection between server and client, it maintains connection specific state, // handles client query. type clientConn struct { @@ -260,5 +294,20 @@ func (cc *clientConn) String() string { // TODO(eastfisher): implement this function func (cc *clientConn) addMetrics(cmd byte, startTime time.Time, err error) { - return + var counter prometheus.Counter + if err != nil && int(cmd) < len(queryTotalCountErr) { + counter = queryTotalCountErr[cmd] + } else if err == nil && int(cmd) < len(queryTotalCountOk) { + counter = queryTotalCountOk[cmd] + } + if counter != nil { + counter.Inc() + } else { + label := strconv.Itoa(int(cmd)) + if err != nil { + metrics.QueryTotalCounter.WithLabelValues(label, "ERROR").Inc() + } else { + metrics.QueryTotalCounter.WithLabelValues(label, "OK").Inc() + } + } }