mirror of
https://github.com/Octops/quake-kube.git
synced 2026-04-05 09:10:34 +00:00
Extract metrics reporter from Server
This commit is contained in:
@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/criticalstack/quake-kube/pkg/reporters/metrics"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
@ -9,36 +10,12 @@ 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
|
||||
@ -112,13 +89,7 @@ func (s *Server) Start(ctx context.Context) error {
|
||||
log.Printf("metrics: get status failed %v", err)
|
||||
continue
|
||||
}
|
||||
actrvePlayers.Set(float64(len(status.Players)))
|
||||
for _, p := range status.Players {
|
||||
if mapname, ok := status.Configuration["mapname"]; ok {
|
||||
scores.WithLabelValues(p.Name, mapname).Set(float64(p.Score))
|
||||
}
|
||||
pings.WithLabelValues(p.Name).Set(float64(p.Ping))
|
||||
}
|
||||
s.reportStatusMetrics(status)
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
@ -136,7 +107,7 @@ func (s *Server) Start(ctx context.Context) error {
|
||||
if err := s.reload(); err != nil {
|
||||
return err
|
||||
}
|
||||
configReloads.Inc()
|
||||
metrics.ConfigReloads().Inc()
|
||||
if err := cmd.Restart(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -151,6 +122,16 @@ func (s *Server) Start(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) reportStatusMetrics(status *quakenet.StatusResponse) {
|
||||
statusMetrics := &metrics.StatusMetrics{
|
||||
Players: status.Players,
|
||||
}
|
||||
if mapName, ok := status.Configuration["mapname"]; ok {
|
||||
statusMetrics.MapName = mapName
|
||||
}
|
||||
metrics.Report(statusMetrics)
|
||||
}
|
||||
|
||||
func (s *Server) reload() error {
|
||||
data, err := ioutil.ReadFile(s.ConfigFile)
|
||||
if err != nil {
|
||||
|
||||
60
pkg/reporters/metrics/metrics_reporter.go
Normal file
60
pkg/reporters/metrics/metrics_reporter.go
Normal file
@ -0,0 +1,60 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/criticalstack/quake-kube/internal/quake/net"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
||||
var (
|
||||
activePlayers = 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 StatusMetrics struct {
|
||||
MapName string
|
||||
Players []net.Player
|
||||
}
|
||||
|
||||
func ConfigReloads() prometheus.Counter {
|
||||
return configReloads
|
||||
}
|
||||
|
||||
func Pings() *prometheus.GaugeVec {
|
||||
return pings
|
||||
}
|
||||
|
||||
func Scores() *prometheus.GaugeVec {
|
||||
return scores
|
||||
}
|
||||
|
||||
func ActivePlayers() prometheus.Gauge {
|
||||
return activePlayers
|
||||
}
|
||||
|
||||
func Report(metrics *StatusMetrics) {
|
||||
ActivePlayers().Set(float64(len(metrics.Players)))
|
||||
for _, p := range metrics.Players {
|
||||
if len(metrics.MapName) > 0 {
|
||||
Scores().WithLabelValues(p.Name, metrics.MapName).Set(float64(p.Score))
|
||||
}
|
||||
Pings().WithLabelValues(p.Name).Set(float64(p.Ping))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user