feat 在线人数埋点

This commit is contained in:
2026-01-08 13:36:08 +08:00
parent 2145b07e69
commit 265e522aff
3 changed files with 18 additions and 0 deletions

View File

@@ -6,6 +6,8 @@ import (
"fmt" "fmt"
"git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-gateway/config" "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" "github.com/prometheus/client_golang/prometheus/promhttp"
"net/http" "net/http"
"sync" "sync"
@@ -19,6 +21,13 @@ type ModulePrometheus struct {
func (m *ModulePrometheus) init() error { func (m *ModulePrometheus) init() error {
m.wg = &sync.WaitGroup{} 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 return nil
} }

View File

@@ -1,5 +1,7 @@
package global package global
import "github.com/prometheus/client_golang/prometheus"
const ( const (
KeyGatewayAccessToken = "gateway:token:access:%v" KeyGatewayAccessToken = "gateway:token:access:%v"
KeyGatewayRefreshToken = "gateway:token:refresh:%v" KeyGatewayRefreshToken = "gateway:token:refresh:%v"
@@ -17,3 +19,7 @@ const (
var PublicPaths = []string{ var PublicPaths = []string{
"/user/info", "/user/info",
} }
var (
OnlineUsersGauge prometheus.Gauge
)

View File

@@ -1,6 +1,7 @@
package client package client
import ( import (
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"sync" "sync"
) )
@@ -21,12 +22,14 @@ func (m *userManager) Add(usn int64, client *Client) {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
m.userMap[usn] = client m.userMap[usn] = client
global.OnlineUsersGauge.Inc()
} }
func (m *userManager) Delete(usn int64) { func (m *userManager) Delete(usn int64) {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
delete(m.userMap, usn) delete(m.userMap, usn)
global.OnlineUsersGauge.Dec()
} }
func (m *userManager) GetAll() map[int64]*Client { func (m *userManager) GetAll() map[int64]*Client {