From 265e522affddd19fd90e8f5173b4fd8d550a0c52 Mon Sep 17 00:00:00 2001 From: "DESKTOP-V763RJ7\\Administrator" <835606593@qq.com> Date: Thu, 8 Jan 2026 13:36:08 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=9C=A8=E7=BA=BF=E4=BA=BA=E6=95=B0?= =?UTF-8?q?=E5=9F=8B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/prometheus.go | 9 +++++++++ internal/global/global.go | 6 ++++++ internal/handler/ws_handler/client/manager.go | 3 +++ 3 files changed, 18 insertions(+) diff --git a/app/prometheus.go b/app/prometheus.go index b024985..bd7e991 100644 --- a/app/prometheus.go +++ b/app/prometheus.go @@ -6,6 +6,8 @@ import ( "fmt" "git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-gateway/config" + "git.hlsq.asia/mmorpg/service-gateway/internal/global" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" "sync" @@ -19,6 +21,13 @@ type ModulePrometheus struct { func (m *ModulePrometheus) init() error { m.wg = &sync.WaitGroup{} + + global.OnlineUsersGauge = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "gateway_online_users_total", + Help: "Total number of online users in gateway", + }) + prometheus.MustRegister(global.OnlineUsersGauge) + return nil } diff --git a/internal/global/global.go b/internal/global/global.go index 54e86ff..25afc27 100644 --- a/internal/global/global.go +++ b/internal/global/global.go @@ -1,5 +1,7 @@ package global +import "github.com/prometheus/client_golang/prometheus" + const ( KeyGatewayAccessToken = "gateway:token:access:%v" KeyGatewayRefreshToken = "gateway:token:refresh:%v" @@ -17,3 +19,7 @@ const ( var PublicPaths = []string{ "/user/info", } + +var ( + OnlineUsersGauge prometheus.Gauge +) diff --git a/internal/handler/ws_handler/client/manager.go b/internal/handler/ws_handler/client/manager.go index 769c15a..c46537c 100644 --- a/internal/handler/ws_handler/client/manager.go +++ b/internal/handler/ws_handler/client/manager.go @@ -1,6 +1,7 @@ package client import ( + "git.hlsq.asia/mmorpg/service-gateway/internal/global" "sync" ) @@ -21,12 +22,14 @@ func (m *userManager) Add(usn int64, client *Client) { m.Lock() defer m.Unlock() m.userMap[usn] = client + global.OnlineUsersGauge.Inc() } func (m *userManager) Delete(usn int64) { m.Lock() defer m.Unlock() delete(m.userMap, usn) + global.OnlineUsersGauge.Dec() } func (m *userManager) GetAll() map[int64]*Client {