Skip to content

Commit

Permalink
Adding tracking of game server states as gauges
Browse files Browse the repository at this point in the history
Signed-off-by: Ken Haines <1144092+khaines@users.noreply.github.com>
  • Loading branch information
khaines committed Dec 16, 2021
1 parent 17550ff commit 71c0547
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
18 changes: 11 additions & 7 deletions nodeagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ func main() {
// starts the server in a goroutine
startHttpServer(srv)

// create a channel and wait for SIGINT or SIGTERM
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
log.Info("Shutting down")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// wait for SIGINT or SIGTERM
ctx, cancel := waitForShutdownSignal()
defer cancel()

// shut down gracefully, but wait no longer than 5 seconds before halting
Expand All @@ -65,8 +61,16 @@ func healthzHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}

func waitForShutdownSignal() (context.Context, context.CancelFunc) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
log.Info("Shutting down")
return context.WithTimeout(context.Background(), 5*time.Second)
}

func getEnv(key string, required bool) string {
if value, ok := os.LookupEnv(key); !ok {
if value, ok := os.LookupEnv(key); ok {
return value
}
if required {
Expand Down
14 changes: 14 additions & 0 deletions nodeagent/nodeagentmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -32,6 +34,13 @@ const (
LabelNodeName = "NodeName"
)

var (
GameServerStates = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "game_server_states",
Help: "Game server states",
}, []string{"name", "state"})
)

type NodeAgentManager struct {
gameServerMap *sync.Map // we use a sync map instead of a regular map since this will be updated by multiple goroutines
dynamicClient dynamic.Interface
Expand Down Expand Up @@ -142,6 +151,11 @@ func (n *NodeAgentManager) gameServerUpdated(oldObj, newObj interface{}) {

gsd := gsdi.(*GameServerDetails)

GameServerStates.WithLabelValues(gameServerName, newState).Set(1)
if gsd.PreviousGameState != "" {
GameServerStates.WithLabelValues(gameServerName, string(gsd.PreviousGameState)).Set(0)
}

// we're only interested if the game server was allocated
if gsd.PreviousGameState == GameStateStandingBy && newState == string(GameStateActive) {
sessionID, sessionCookie := n.parseSessionDetails(new, gameServerName, gameServerNamespace)
Expand Down

0 comments on commit 71c0547

Please sign in to comment.