Add websocket metrics

This commit is contained in:
rxdn 2023-09-13 17:23:37 +01:00
parent b7afa2373a
commit f01ef763a3

View File

@ -3,6 +3,25 @@ package livechat
import (
"encoding/json"
"github.com/TicketsBot/common/chatrelay"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"strconv"
)
var (
activeWebsockets = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "tickets",
Subsystem: "api",
Name: "active_livechat_websockets",
Help: "The number of open live-chat websockets",
})
websocketMessages = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "tickets",
Subsystem: "api",
Name: "livechat_websocket_messages",
Help: "The number of messages relayed over live-chat websockets",
}, []string{"guild_id", "message_id"})
)
type (
@ -30,6 +49,8 @@ func (sm *SocketManager) Run() {
guildClients := sm.clients[client.GuildId]
guildClients = append(guildClients, client)
sm.clients[client.GuildId] = guildClients
activeWebsockets.Inc()
case client := <-sm.unregister:
guildClients := sm.clients[client.GuildId]
if len(guildClients) == 0 {
@ -49,6 +70,8 @@ func (sm *SocketManager) Run() {
}
sm.clients[client.GuildId] = guildClients
activeWebsockets.Dec()
case msg := <-sm.messages:
guildClients, ok := sm.clients[msg.Ticket.GuildId]
if !ok || len(guildClients) == 0 { // No clients connected to this API server for this guild
@ -70,6 +93,11 @@ func (sm *SocketManager) Run() {
continue
}
websocketMessages.WithLabelValues(
strconv.FormatUint(client.GuildId, 10),
strconv.FormatUint(msg.Message.Id, 10),
).Inc()
client.Write(Event{
Type: EventTypeMessage,
Data: encoded,