diff --git a/go.mod b/go.mod index 18d45fa..2270439 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.24.0 require ( bou.ke/monkey v1.0.2 - git.hlsq.asia/mmorpg/service-common v0.0.0-20260116143417-d944729ad0e5 + git.hlsq.asia/mmorpg/service-common v0.0.0-20260116155014-3de703eb83c5 github.com/alicebob/miniredis/v2 v2.35.0 github.com/gin-contrib/cors v1.7.6 github.com/gin-gonic/gin v1.11.0 diff --git a/go.sum b/go.sum index f7498e0..db46963 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI= bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260116143417-d944729ad0e5 h1:gkPNZ/OiXQhtiqRMdLrL5Zw580zgb9082jkZQqP71Fk= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260116143417-d944729ad0e5/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw= +git.hlsq.asia/mmorpg/service-common v0.0.0-20260116155014-3de703eb83c5 h1:b7pTgEyYUKpHtAAt4ea/3EYfX0Y0lYwhjrgf3kTyn0w= +git.hlsq.asia/mmorpg/service-common v0.0.0-20260116155014-3de703eb83c5/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw= github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI= diff --git a/internal/global/metric.go b/internal/global/metric.go index b2c16ec..6b4f36f 100644 --- a/internal/global/metric.go +++ b/internal/global/metric.go @@ -7,13 +7,20 @@ import ( var ( OnlineUsersGauge prometheus.Gauge + FlowCounter prometheus.Counter ) func init() { log.Infof("Init prometheus metric...") OnlineUsersGauge = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "online_users", + Name: "gateway_online_users", Help: "Total number of online users", }) prometheus.MustRegister(OnlineUsersGauge) + + FlowCounter = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "gateway_flow_counter", + Help: "Total number of flow", + }) + prometheus.MustRegister(FlowCounter) } diff --git a/internal/grpc_server/server.go b/internal/grpc_server/server.go index 0a114d9..1a44313 100644 --- a/internal/grpc_server/server.go +++ b/internal/grpc_server/server.go @@ -5,6 +5,7 @@ import ( "git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb" + "git.hlsq.asia/mmorpg/service-gateway/internal/global" "git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client" "google.golang.org/protobuf/proto" "sync" @@ -39,8 +40,10 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error { log.Errorf("ToClient proto.Marshal error: %v", err) continue } + dataLen := float64(len(data)) for _, cli := range client.UserMgr.GetAll() { cli.WriteBytesPreMarshal(data) + global.FlowCounter.Add(dataLen) } //for _, client := range ws_handler.UserMgr.GetAll() { @@ -49,6 +52,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error { } else { if cli := client.UserMgr.GetByUSN(args.USN); cli != nil { cli.WriteBytes(ss_pb.MessageID(args.MessageID), args.Payload) + global.FlowCounter.Add(float64(len(args.Payload))) } } } diff --git a/internal/grpc_server/server_init.go b/internal/grpc_server/server_init.go index 1dfcb6c..eaac01b 100644 --- a/internal/grpc_server/server_init.go +++ b/internal/grpc_server/server_init.go @@ -21,11 +21,16 @@ func NewServer(ttl int64) *Server { EtcdTTL: ttl, }, } + s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption s.Base.OnInit = s.OnInit s.Base.OnClose = s.OnClose return s } +func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption { + return nil +} + func (s *Server) OnInit(serve *grpc.Server) { client.GatewaySID = s.SID grpc_pb.RegisterGatewayServer(serve, s)