Add websocket metrics
This commit is contained in:
parent
b7afa2373a
commit
f01ef763a3
@ -3,6 +3,25 @@ package livechat
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TicketsBot/common/chatrelay"
|
"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 (
|
type (
|
||||||
@ -30,6 +49,8 @@ func (sm *SocketManager) Run() {
|
|||||||
guildClients := sm.clients[client.GuildId]
|
guildClients := sm.clients[client.GuildId]
|
||||||
guildClients = append(guildClients, client)
|
guildClients = append(guildClients, client)
|
||||||
sm.clients[client.GuildId] = guildClients
|
sm.clients[client.GuildId] = guildClients
|
||||||
|
|
||||||
|
activeWebsockets.Inc()
|
||||||
case client := <-sm.unregister:
|
case client := <-sm.unregister:
|
||||||
guildClients := sm.clients[client.GuildId]
|
guildClients := sm.clients[client.GuildId]
|
||||||
if len(guildClients) == 0 {
|
if len(guildClients) == 0 {
|
||||||
@ -49,6 +70,8 @@ func (sm *SocketManager) Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sm.clients[client.GuildId] = guildClients
|
sm.clients[client.GuildId] = guildClients
|
||||||
|
|
||||||
|
activeWebsockets.Dec()
|
||||||
case msg := <-sm.messages:
|
case msg := <-sm.messages:
|
||||||
guildClients, ok := sm.clients[msg.Ticket.GuildId]
|
guildClients, ok := sm.clients[msg.Ticket.GuildId]
|
||||||
if !ok || len(guildClients) == 0 { // No clients connected to this API server for this guild
|
if !ok || len(guildClients) == 0 { // No clients connected to this API server for this guild
|
||||||
@ -70,6 +93,11 @@ func (sm *SocketManager) Run() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
websocketMessages.WithLabelValues(
|
||||||
|
strconv.FormatUint(client.GuildId, 10),
|
||||||
|
strconv.FormatUint(msg.Message.Id, 10),
|
||||||
|
).Inc()
|
||||||
|
|
||||||
client.Write(Event{
|
client.Write(Event{
|
||||||
Type: EventTypeMessage,
|
Type: EventTypeMessage,
|
||||||
Data: encoded,
|
Data: encoded,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user