diff --git a/go.mod b/go.mod index e81358d3..d9f7508f 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/giongto35/cloud-game/v3 go 1.26 require ( + github.com/VictoriaMetrics/metrics v1.44.0 github.com/fsnotify/fsnotify v1.10.1 github.com/go-gst/go-gst v1.4.0 github.com/gofrs/flock v0.13.0 @@ -50,6 +51,8 @@ require ( github.com/pion/transport/v4 v4.0.2 // indirect github.com/pion/turn/v5 v5.0.12 // indirect github.com/tinylib/msgp v1.6.4 // indirect + github.com/valyala/fastrand v1.1.0 // indirect + github.com/valyala/histogram v1.2.0 // indirect github.com/wlynxg/anet v0.0.5 // indirect github.com/zeebo/xxh3 v1.1.0 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect diff --git a/go.sum b/go.sum index 9dde1c8c..1573a1c3 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/VictoriaMetrics/metrics v1.44.0 h1:Fr8yqQSV+ZfYaDD/anqk1E8e9YPgfleSleJmAI0M0Tw= +github.com/VictoriaMetrics/metrics v1.44.0/go.mod h1:xDM82ULLYCYdFRgQ2JBxi8Uf1+8En1So9YUwlGTOqTc= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -105,6 +107,10 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ= github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA= +github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8= +github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= +github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ= +github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY= github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU= github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA= github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= diff --git a/pkg/config/config.yaml b/pkg/config/config.yaml index a55461e8..61ee209d 100644 --- a/pkg/config/config.yaml +++ b/pkg/config/config.yaml @@ -52,6 +52,7 @@ coordinator: port: 6601 # enable Go profiler HTTP server profilingEnabled: false + metricEnabled: false urlPrefix: /coordinator # a custom Origins for incoming Websocket connections: # "" -- checks same origin policy @@ -104,6 +105,8 @@ worker: # monitoring server port port: 6602 profilingEnabled: false + # monitoring server URL prefix + metricEnabled: false urlPrefix: /worker server: address: :9000 diff --git a/pkg/config/shared.go b/pkg/config/shared.go index 9e705763..e49eb3ce 100644 --- a/pkg/config/shared.go +++ b/pkg/config/shared.go @@ -25,10 +25,11 @@ func (l Library) GetSupportedExtensions() []string { return l.Supported } type Monitoring struct { Port int URLPrefix string + MetricEnabled bool `json:"metric_enabled"` ProfilingEnabled bool `json:"profiling_enabled"` } -func (c *Monitoring) IsEnabled() bool { return c.ProfilingEnabled } +func (c *Monitoring) IsEnabled() bool { return c.MetricEnabled || c.ProfilingEnabled } type Server struct { Address string diff --git a/pkg/monitoring/monitoring.go b/pkg/monitoring/monitoring.go index da1d4232..6ed7d3da 100644 --- a/pkg/monitoring/monitoring.go +++ b/pkg/monitoring/monitoring.go @@ -6,12 +6,14 @@ import ( "net/http/pprof" "strconv" + "github.com/VictoriaMetrics/metrics" "github.com/giongto35/cloud-game/v3/pkg/config" "github.com/giongto35/cloud-game/v3/pkg/logger" "github.com/giongto35/cloud-game/v3/pkg/network/httpx" ) const debugEndpoint = "/debug/pprof" +const metricsEndpoint = "/metrics" type Monitoring struct { conf config.Monitoring @@ -40,6 +42,12 @@ func New(conf config.Monitoring, baseAddr string, log *logger.Logger) *Monitorin Handle("/mutex", pprof.Handler("mutex")). Handle("/threadcreate", pprof.Handler("threadcreate")) } + if conf.MetricEnabled { + h.Prefix(conf.URLPrefix) + h.HandleFunc(metricsEndpoint, func(w httpx.ResponseWriter, _ *httpx.Request) { + metrics.WritePrometheus(w, true) + }) + } h.Prefix("") return h }, @@ -67,6 +75,10 @@ func (m *Monitoring) String() string { return fmt.Sprintf("monitoring::%s:%d", m.conf.URLPrefix, m.conf.Port) } +func (m *Monitoring) GetMetricsPublicAddress() string { + return m.server.GetProtocol() + "://" + m.server.Addr + m.conf.URLPrefix + metricsEndpoint +} + func (m *Monitoring) GetProfilingAddress() string { return m.server.GetProtocol() + "://" + m.server.Addr + m.conf.URLPrefix + debugEndpoint } @@ -76,5 +88,8 @@ func (m *Monitoring) printInfo() { if m.conf.ProfilingEnabled { message = message.Str("profiler", m.GetProfilingAddress()) } + if m.conf.MetricEnabled { + message = message.Str("prometheus", m.GetMetricsPublicAddress()) + } message.Msg("Monitoring") }