Skip to content

Commit

Permalink
chore: add prometheus metrics to gameserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Emyrk committed Mar 25, 2024
1 parent 8962782 commit 0d31c1e
Show file tree
Hide file tree
Showing 5 changed files with 1,776 additions and 7 deletions.
5 changes: 5 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ port = 443
cpu_kind = "shared"
cpus = 1
memory_mb = 1024

[[metrics]]
port = 9999
path = "/metrics"

59 changes: 56 additions & 3 deletions gameserver/gameserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,69 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
)

type gameServerMetrics struct {
GCRoomsCloseCounter prometheus.Counter
GCRoomLifetime prometheus.Histogram
OpenRooms prometheus.Gauge
PlayerJoinEventCount prometheus.Gauge
}

type GameServer struct {
HTTPServer *http.Server
Mux *mux.Router
ctx context.Context
registry prometheus.Registerer
metrics *gameServerMetrics

roomLock sync.RWMutex
Rooms map[string]*Room
}

func NewGameServer() *GameServer {
func NewGameServer(reg prometheus.Registerer) *GameServer {
gs := new(GameServer)
gs.HTTPServer = &http.Server{}
gs.Rooms = make(map[string]*Room)
gs.ctx = context.Background()
gs.registry = reg

fact := promauto.With(gs.registry)
gs.metrics = &gameServerMetrics{
GCRoomsCloseCounter: fact.NewCounter(prometheus.CounterOpts{
Namespace: "unbrewed",
Subsystem: "gameserver",
Name: "gc_rooms_closed",
Help: "Total count of rooms closed",
ConstLabels: nil,
}),

GCRoomLifetime: fact.NewHistogram(prometheus.HistogramOpts{
Namespace: "unbrewed",
Subsystem: "gameserver",
Name: "gc_room_lifetime",
Help: "Lifetime of a room before it is closed due to inactivity",
// 24hrs, 12hrs, 6hrs, 1hr, 40m, 20m, 10m, 5m, 1m
Buckets: []float64{60, 5 * 60, 10 * 60, 20 * 60, 40 * 60, 60 * 60, 60 * 60 * 12, 60 * 60 * 24},
}),

OpenRooms: fact.NewGauge(prometheus.GaugeOpts{
Namespace: "unbrewed",
Subsystem: "gameserver",
Name: "open_rooms_count",
Help: "Total count of open rooms",
}),

PlayerJoinEventCount: fact.NewGauge(prometheus.GaugeOpts{
Namespace: "unbrewed",
Subsystem: "gameserver",
Name: "player_join_event_count",
Help: "Total count of player join events",
}),
}

return gs
}
Expand Down Expand Up @@ -96,6 +142,7 @@ func (gs *GameServer) WSHandler(w http.ResponseWriter, r *http.Request) {
}
name := strings.Join(values["name"], " ")

gs.metrics.PlayerJoinEventCount.Inc()
err = room.PlayerJoin(c, name)
if err != nil {
_, _ = fmt.Fprintf(w, "player failed to join: %s", err.Error())
Expand All @@ -116,6 +163,7 @@ func (gs *GameServer) CreateLobby(gid string) (bool, error) {
}

gs.Rooms[gid] = NewRoom(gid, gs.ctx)
gs.metrics.OpenRooms.Set(float64(len(gs.Rooms)))

return true, nil
}
Expand All @@ -138,6 +186,8 @@ func (gs *GameServer) GarbageCollector(ctx context.Context) {
// Close the room to kill any active go routines, all clients
// will be disconnected if present.
room.Close()
gs.metrics.GCRoomsCloseCounter.Inc()
gs.metrics.GCRoomLifetime.Observe(room.FieldState.LastUpdate.Sub(room.openedAt).Seconds())
delete(gs.Rooms, gid)
log.WithFields(log.Fields{
"time": start,
Expand All @@ -157,6 +207,7 @@ func (gs *GameServer) GarbageCollector(ctx context.Context) {
"closed_count": closed,
}).Info("GC Run")
}
gs.metrics.OpenRooms.Set(float64(len(gs.Rooms)))
}
}

Expand Down Expand Up @@ -190,8 +241,9 @@ type Room struct {

mutex sync.Mutex

ctx context.Context
stop context.CancelFunc
openedAt time.Time
ctx context.Context
stop context.CancelFunc
}

func NewRoom(gid string, ctx context.Context) *Room {
Expand All @@ -204,6 +256,7 @@ func NewRoom(gid string, ctx context.Context) *Room {
}}
r.ctx, r.stop = context.WithCancel(ctx)
r.Clients = make(map[string]*PlayerConn)
r.openedAt = time.Now()

return r
}
Expand Down
5 changes: 2 additions & 3 deletions gameserver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.4
github.com/gorilla/websocket v1.4.1
github.com/sirupsen/logrus v1.5.0
github.com/stretchr/testify v1.4.0 // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/sirupsen/logrus v1.6.0
)
Loading

0 comments on commit 0d31c1e

Please sign in to comment.