From 51e0722e70efd3c282604efaac094b4942a57d41 Mon Sep 17 00:00:00 2001 From: Cyan Date: Tue, 23 May 2023 10:07:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86Prometheus=E7=9A=84E?= =?UTF-8?q?xporter,=20=E6=96=B0=E5=A2=9E=E4=B8=80=E4=B8=AA=E6=8C=87?= =?UTF-8?q?=E6=A0=87=E7=94=A8=E4=BA=8E=E6=9A=B4=E9=9C=B2=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E7=9A=84=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/collector/collector.go | 38 +++++++++++++++++++++++++++++++++++ src/example_zqpool.conf | 2 ++ src/main.go | 2 ++ src/poolserver/fe_be_logic.go | 8 ++++++++ 4 files changed, 50 insertions(+) create mode 100644 src/collector/collector.go diff --git a/src/collector/collector.go b/src/collector/collector.go new file mode 100644 index 0000000..3bae8cc --- /dev/null +++ b/src/collector/collector.go @@ -0,0 +1,38 @@ +// collector/collector.go +package collector + +import ( + "net/http" + "log" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" + config "csudata.com/zqpool/src/config" +) + +var ( + FrontendConnectCount = promauto.NewGauge(prometheus.GaugeOpts{ + Namespace: "zqpool", + Subsystem: "gauge", + Name: "frontend_connect_count", + Help: "Number of frontend connections.", + }) +) + +func FrontendConnectCountIncrease() { + FrontendConnectCount.Inc() +} + +func FrontendConnectCountDecrease() { + FrontendConnectCount.Dec() +} + +func StartPromServer() { + port := config.Get("exporter_port") + if port == "" { + log.Fatal("exporter_port not set in configuration") + } + + http.Handle("/metrics", promhttp.Handler()) + log.Fatal(http.ListenAndServe(":"+port, nil)) +} diff --git a/src/example_zqpool.conf b/src/example_zqpool.conf index bb1597b..72a0590 100644 --- a/src/example_zqpool.conf +++ b/src/example_zqpool.conf @@ -4,6 +4,8 @@ listen_addr = * mgr_port = 9380 mgr_addr = * +exporter_port=9816 # 指定 Prometheus 监控端口, 即 ZQPool 中 Exporter 暴露数据的端口 + pool.1.fe_max_conns = 3000 pool.1.fe_user=kuafu pool.1.fe_passwd=chase diff --git a/src/main.go b/src/main.go index 9eee471..6016e08 100644 --- a/src/main.go +++ b/src/main.go @@ -15,6 +15,7 @@ import ( config "csudata.com/zqpool/src/config" mgrhttp "csudata.com/zqpool/src/mgrhttp" poolserver "csudata.com/zqpool/src/poolserver" + collector "csudata.com/zqpool/src/collector" ) func main() { @@ -54,6 +55,7 @@ func main() { return } + go collector.StartPromServer() go mgrhttp.StartHttpServer() var listenIp string diff --git a/src/poolserver/fe_be_logic.go b/src/poolserver/fe_be_logic.go index db97cfd..ddf567c 100644 --- a/src/poolserver/fe_be_logic.go +++ b/src/poolserver/fe_be_logic.go @@ -7,6 +7,7 @@ import ( "time" "go.uber.org/zap" + collector "csudata.com/zqpool/src/collector" ) /*获得第一个消息包*/ @@ -508,6 +509,8 @@ func handleConnection(cliConn net.Conn) { ctx.pool = pool ctx.cachedPrepare.Init() + collector.FrontendConnectCountIncrease() + defer ctx.cachedPrepare.Discard() for { @@ -523,27 +526,32 @@ func handleConnection(cliConn net.Conn) { ctx.isGetBackend = false } cliConn.Close() + collector.FrontendConnectCountDecrease() return } if ctx.recvBuf[0] == 'X' { /* 这是Terminate消息*/ ctx.ProcessX() + collector.FrontendConnectCountDecrease() return } else if ctx.recvBuf[0] == 'Q' { /* 这是 Query 简单查询 */ err = ctx.ProcessQ() if err != nil { + collector.FrontendConnectCountDecrease() return } continue } else if ctx.recvBuf[0] == 'P' || ctx.recvBuf[0] == 'B' || ctx.recvBuf[0] == 'D' { err = ctx.ProcessExtendedQuery() if err != nil { + collector.FrontendConnectCountDecrease() return } continue } else if ctx.recvBuf[0] == 'C' { /* 这是Close消息 */ err = ctx.ProcessC() if err != nil { + collector.FrontendConnectCountDecrease() return } continue -- Gitee