feat Prometheus
This commit is contained in:
@@ -21,11 +21,11 @@ func (p *Program) Init(_ svc.Environment) error {
|
|||||||
}
|
}
|
||||||
p.moduleList = append(p.moduleList, base)
|
p.moduleList = append(p.moduleList, base)
|
||||||
p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB))
|
p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB))
|
||||||
p.moduleList = append(p.moduleList, &ModulePrometheus{})
|
|
||||||
p.moduleList = append(p.moduleList, &ModuleWebServer{})
|
p.moduleList = append(p.moduleList, &ModuleWebServer{})
|
||||||
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
|
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
|
||||||
p.moduleList = append(p.moduleList, &ModuleGrpcServer{})
|
p.moduleList = append(p.moduleList, &ModuleGrpcServer{})
|
||||||
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
|
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
|
||||||
|
p.moduleList = append(p.moduleList, (&module.Prometheus{}).Bind(config.Get().Metric))
|
||||||
p.moduleList = append(p.moduleList, (&module.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameGateway))
|
p.moduleList = append(p.moduleList, (&module.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameGateway))
|
||||||
|
|
||||||
for i, m := range p.moduleList {
|
for i, m := range p.moduleList {
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
|
||||||
"git.hlsq.asia/mmorpg/service-common/module"
|
|
||||||
"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"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ModulePrometheus 普罗米修斯模块
|
|
||||||
type ModulePrometheus struct {
|
|
||||||
wg *sync.WaitGroup
|
|
||||||
server *http.Server
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *ModulePrometheus) Start() error {
|
|
||||||
m.wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
defer m.wg.Done()
|
|
||||||
m.server = &http.Server{
|
|
||||||
Addr: fmt.Sprintf("%v:%v", config.Get().Metric.Prometheus.Address, config.Get().Metric.Prometheus.Port),
|
|
||||||
Handler: promhttp.Handler(),
|
|
||||||
}
|
|
||||||
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
||||||
log.Errorf("prometheus server failed: %v", err.Error())
|
|
||||||
}
|
|
||||||
log.Infof("prometheus server stop.")
|
|
||||||
}()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *ModulePrometheus) Stop() error {
|
|
||||||
if err := m.server.Shutdown(context.Background()); err != nil {
|
|
||||||
log.Errorf("stop prometheus server failed: %v", err)
|
|
||||||
}
|
|
||||||
m.wg.Wait()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *ModulePrometheus) Bind(_ ...any) module.Module {
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.24.0
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
bou.ke/monkey v1.0.2
|
bou.ke/monkey v1.0.2
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260116140135-e59d106700e3
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260116143417-d944729ad0e5
|
||||||
github.com/alicebob/miniredis/v2 v2.35.0
|
github.com/alicebob/miniredis/v2 v2.35.0
|
||||||
github.com/gin-contrib/cors v1.7.6
|
github.com/gin-contrib/cors v1.7.6
|
||||||
github.com/gin-gonic/gin v1.11.0
|
github.com/gin-gonic/gin v1.11.0
|
||||||
|
|||||||
4
go.sum
4
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=
|
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 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260116140135-e59d106700e3 h1:3VOikwIBiZ1npwqfLeK558Uc6panZbtFkH5T1uoWPmk=
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260116143417-d944729ad0e5 h1:gkPNZ/OiXQhtiqRMdLrL5Zw580zgb9082jkZQqP71Fk=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260116140135-e59d106700e3/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw=
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260116143417-d944729ad0e5/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 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
|
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
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"
|
||||||
@@ -14,7 +12,3 @@ const (
|
|||||||
MaxOnlineSize = 100 // 最大在线人数
|
MaxOnlineSize = 100 // 最大在线人数
|
||||||
MaxQueueUpSize = 100 // 最大排队人数
|
MaxQueueUpSize = 100 // 最大排队人数
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
OnlineUsersGauge prometheus.Gauge
|
|
||||||
)
|
|
||||||
|
|||||||
19
internal/global/metric.go
Normal file
19
internal/global/metric.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
OnlineUsersGauge prometheus.Gauge
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.Infof("Init prometheus metric...")
|
||||||
|
OnlineUsersGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
|
Name: "online_users",
|
||||||
|
Help: "Total number of online users",
|
||||||
|
})
|
||||||
|
prometheus.MustRegister(OnlineUsersGauge)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user