From 3f2b2b135c45e3092f393ad55524a5337e6c6640 Mon Sep 17 00:00:00 2001 From: Thomas von Rosenberg Date: Wed, 14 Oct 2020 16:38:36 +0200 Subject: [PATCH] Use counters instead of gauges --- ws/event_create.go | 2 ++ ws/event_disconnected.go | 9 +++++---- ws/event_join.go | 1 + ws/event_stop_share.go | 2 +- ws/prometheus.go | 30 +++++++++++++++++++++--------- ws/room.go | 6 ++++++ ws/rooms.go | 33 ++++++++++++++------------------- 7 files changed, 50 insertions(+), 33 deletions(-) diff --git a/ws/event_create.go b/ws/event_create.go index bec606db..7e04da76 100644 --- a/ws/event_create.go +++ b/ws/event_create.go @@ -72,5 +72,7 @@ func (e *Create) Execute(rooms *Rooms, current ClientInfo) error { } rooms.Rooms[e.ID] = room room.notifyInfoChanged() + usersJoinedTotal.Inc() + roomsCreatedTotal.Inc() return nil } diff --git a/ws/event_disconnected.go b/ws/event_disconnected.go index 84e7c81d..6f6acc99 100644 --- a/ws/event_disconnected.go +++ b/ws/event_disconnected.go @@ -28,6 +28,7 @@ func (e *Disconnected) Execute(rooms *Rooms, current ClientInfo) error { current.Close <- CloseDone delete(room.Users, current.ID) + usersLeftTotal.Inc() for id, session := range room.Sessions { if bytes.Equal(session.Client.Bytes(), current.ID.Bytes()) { @@ -35,14 +36,14 @@ func (e *Disconnected) Execute(rooms *Rooms, current ClientInfo) error { if ok { host.Write <- outgoing.EndShare(id) } - delete(room.Sessions, id) + room.closeSession(id) } if bytes.Equal(session.Host.Bytes(), current.ID.Bytes()) { client, ok := room.Users[session.Client] if ok { client.Write <- outgoing.EndShare(id) } - delete(room.Sessions, id) + room.closeSession(id) } } @@ -50,12 +51,12 @@ func (e *Disconnected) Execute(rooms *Rooms, current ClientInfo) error { for _, member := range room.Users { member.Close <- CloseOwnerLeft } - delete(rooms.Rooms, current.RoomID) + rooms.closeRoom(current.RoomID) return nil } if len(room.Users) == 0 { - delete(rooms.Rooms, current.RoomID) + rooms.closeRoom(current.RoomID) return nil } diff --git a/ws/event_join.go b/ws/event_join.go index d99d517c..ac1b9162 100644 --- a/ws/event_join.go +++ b/ws/event_join.go @@ -45,6 +45,7 @@ func (e *Join) Execute(rooms *Rooms, current ClientInfo) error { Close: current.Close, } room.notifyInfoChanged() + usersJoinedTotal.Inc() for _, user := range room.Users { if current.ID == user.ID || !user.Streaming { diff --git a/ws/event_stop_share.go b/ws/event_stop_share.go index 0319b8fa..ac081211 100644 --- a/ws/event_stop_share.go +++ b/ws/event_stop_share.go @@ -33,7 +33,7 @@ func (e *StopShare) Execute(rooms *Rooms, current ClientInfo) error { if ok { client.Write <- outgoing.EndShare(id) } - delete(room.Sessions, id) + room.closeSession(id) } } diff --git a/ws/prometheus.go b/ws/prometheus.go index 1c6f5f99..f01a4e21 100644 --- a/ws/prometheus.go +++ b/ws/prometheus.go @@ -6,16 +6,28 @@ import ( ) var ( - roomGauge = promauto.NewGauge(prometheus.GaugeOpts{ - Name: "screego_room_total", - Help: "The total number of rooms", + roomsCreatedTotal = promauto.NewCounter(prometheus.CounterOpts{ + Name: "screego_room_created_total", + Help: "The total number of rooms created", }) - userGauge = promauto.NewGauge(prometheus.GaugeOpts{ - Name: "screego_user_total", - Help: "The total number of users", + roomsClosedTotal = promauto.NewCounter(prometheus.CounterOpts{ + Name: "screego_room_closed_total", + Help: "The total number of rooms closed", }) - sessionGauge = promauto.NewGauge(prometheus.GaugeOpts{ - Name: "screego_session_total", - Help: "The total number of sessions", + usersJoinedTotal = promauto.NewCounter(prometheus.CounterOpts{ + Name: "screego_user_joined_total", + Help: "The total number of users joined", + }) + usersLeftTotal = promauto.NewCounter(prometheus.CounterOpts{ + Name: "screego_user_left_total", + Help: "The total number of users left", + }) + sessionCreatedTotal = promauto.NewCounter(prometheus.CounterOpts{ + Name: "screego_session_created_total", + Help: "The total number of sessions created", + }) + sessionClosedTotal = promauto.NewCounter(prometheus.CounterOpts{ + Name: "screego_session_closed_total", + Help: "The total number of sessions closed", }) ) diff --git a/ws/room.go b/ws/room.go index 097d783e..cf846b22 100644 --- a/ws/room.go +++ b/ws/room.go @@ -37,6 +37,7 @@ func (r *Room) newSession(host, client xid.ID, rooms *Rooms) { Host: host, Client: client, } + sessionCreatedTotal.Inc() iceHost := []outgoing.ICEServer{} iceClient := []outgoing.ICEServer{} @@ -74,6 +75,11 @@ func (r *Room) newSession(host, client xid.ID, rooms *Rooms) { r.Users[client].Write <- outgoing.ClientSession{Peer: host, ID: id, ICEServers: iceClient} } +func (r *Room) closeSession(id xid.ID) { + delete(r.Sessions, id) + sessionClosedTotal.Inc() +} + type RoomSession struct { Host xid.ID Client xid.ID diff --git a/ws/rooms.go b/ws/rooms.go index 4d9dcd45..f709669c 100644 --- a/ws/rooms.go +++ b/ws/rooms.go @@ -65,26 +65,21 @@ func (r *Rooms) Upgrade(w http.ResponseWriter, req *http.Request) { } func (r *Rooms) Start() { - prometheusTicker := time.NewTicker(time.Second * 5) - if !r.config.Prometheus { - prometheusTicker.Stop() - } for { - select { - case msg := <-r.Incoming: - if err := msg.Incoming.Execute(r, msg.Info); err != nil { - msg.Info.Close <- err.Error() - } - case <-prometheusTicker.C: - roomGauge.Set(float64(len(r.Rooms))) - users := 0 - sessions := 0 - for _, room := range r.Rooms { - users += len(room.Users) - sessions += len(room.Sessions) - } - userGauge.Set(float64(users)) - sessionGauge.Set(float64(sessions)) + msg := <-r.Incoming + if err := msg.Incoming.Execute(r, msg.Info); err != nil { + msg.Info.Close <- err.Error() } } } + +func (r *Rooms) closeRoom(roomId string) { + room, ok := r.Rooms[roomId] + if !ok { + return + } + usersLeftTotal.Add(float64(len(room.Users))) + sessionClosedTotal.Add(float64(len(room.Sessions))) + delete(r.Rooms, roomId) + roomsClosedTotal.Inc() +}