Skip to content

Commit

Permalink
add basic proxy metrics (#30)
Browse files Browse the repository at this point in the history
* add go pprof metrics
* add basic proxy metrics
  • Loading branch information
teckick authored Dec 17, 2020
1 parent fdd28f7 commit 3873c2f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pkg/proxy/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package proxy
import (
"net"
"net/http"
"net/http/pprof"

"github.com/gin-gonic/gin"
"github.com/pingcap-incubator/weir/pkg/config"
"github.com/pingcap-incubator/weir/pkg/configcenter"
"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"
)

Expand Down Expand Up @@ -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
}
Expand Down
51 changes: 50 additions & 1 deletion pkg/proxy/server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}
}
}

0 comments on commit 3873c2f

Please sign in to comment.