forked from github-mirrorer/quake-kube
Add prometheus instrumentation, count active ws conns (#8)
* Add prometheus instrumentation, count active ws conns * Stop bum ticker * Moving things around * Handle unspecified host for ws proxy * Add config reload counter * metrics: rename conns -> players
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
quakenet "github.com/criticalstack/quake-kube/internal/quake/net"
|
||||
)
|
||||
@ -52,6 +53,8 @@ func NewRouter(cfg *Config) (*echo.Echo, error) {
|
||||
})
|
||||
})
|
||||
|
||||
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
|
||||
|
||||
e.GET("/info", func(c echo.Context) error {
|
||||
m, err := quakenet.GetInfo(cfg.ServerAddr)
|
||||
if err != nil {
|
||||
|
||||
@ -32,7 +32,16 @@ func (s *Server) Serve(l net.Listener) error {
|
||||
}
|
||||
}()
|
||||
|
||||
wsproxy, err := NewProxy(s.ServerAddr)
|
||||
host, port, err := net.SplitHostPort(s.ServerAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proxyTarget := s.ServerAddr
|
||||
if net.ParseIP(host).IsUnspecified() {
|
||||
// handle case where host is 0.0.0.0
|
||||
proxyTarget = net.JoinHostPort("127.0.0.1", port)
|
||||
}
|
||||
wsproxy, err := NewProxy(proxyTarget)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -9,11 +9,33 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
quakenet "github.com/criticalstack/quake-kube/internal/quake/net"
|
||||
"github.com/criticalstack/quake-kube/internal/util/exec"
|
||||
)
|
||||
|
||||
var (
|
||||
actrvePlayers = promauto.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "quake_active_players",
|
||||
Help: "The current number of active players",
|
||||
})
|
||||
scores = promauto.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Name: "quake_player_scores",
|
||||
Help: "Current scores by player, by map",
|
||||
}, []string{"player", "map"})
|
||||
pings = promauto.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Name: "quake_player_pings",
|
||||
Help: "Current ping by player",
|
||||
}, []string{"player"})
|
||||
configReloads = promauto.NewCounter(prometheus.CounterOpts{
|
||||
Name: "quake_config_reloads",
|
||||
Help: "Config file reload count",
|
||||
})
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Dir string
|
||||
WatchInterval time.Duration
|
||||
@ -68,6 +90,32 @@ func (s *Server) Start(ctx context.Context) error {
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
addr := s.Addr
|
||||
if net.ParseIP(host).IsUnspecified() {
|
||||
addr = net.JoinHostPort("127.0.0.1", port)
|
||||
}
|
||||
tick := time.NewTicker(5 * time.Second)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
status, err := quakenet.GetStatus(addr)
|
||||
if err != nil {
|
||||
log.Printf("metrics: get status failed %v", err)
|
||||
continue
|
||||
}
|
||||
actrvePlayers.Set(float64(len(status.Players)))
|
||||
for _, p := range status.Players {
|
||||
scores.WithLabelValues(p.Name, status.Configuration["mapname"]).Set(float64(p.Score))
|
||||
pings.WithLabelValues(p.Name).Set(float64(p.Ping))
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
ch, err := s.watch(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -79,6 +127,7 @@ func (s *Server) Start(ctx context.Context) error {
|
||||
if err := s.reload(); err != nil {
|
||||
return err
|
||||
}
|
||||
configReloads.Inc()
|
||||
if err := cmd.Restart(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user