diff --git a/service/bot/ws/metrics.go b/service/bot/ws/metrics.go new file mode 100644 index 000000000..e7418f083 --- /dev/null +++ b/service/bot/ws/metrics.go @@ -0,0 +1,27 @@ +package ws + +import ( + "github.com/gofrs/uuid" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) + +var ( + webSocketReadBytesTotal = promauto.NewCounterVec(prometheus.CounterOpts{ + Namespace: "traq", + Name: "bot_ws_read_bytes_total", + }, []string{"user_id"}) + + webSocketWriteBytesTotal = promauto.NewCounterVec(prometheus.CounterOpts{ + Namespace: "traq", + Name: "bot_ws_write_bytes_total", + }, []string{"user_id"}) +) + +func incWebSocketReadBytesTotal(userID uuid.UUID, bytes int) { + webSocketReadBytesTotal.WithLabelValues(userID.String()).Add(float64(bytes)) +} + +func incWebSocketWriteBytesTotal(userID uuid.UUID, bytes int) { + webSocketWriteBytesTotal.WithLabelValues(userID.String()).Add(float64(bytes)) +} diff --git a/service/bot/ws/session.go b/service/bot/ws/session.go index c6e48d45b..730f59eb7 100644 --- a/service/bot/ws/session.go +++ b/service/bot/ws/session.go @@ -52,6 +52,7 @@ func (s *session) ReadLoop() { if err != nil { return } + incWebSocketReadBytesTotal(s.userID, len(m)) if t == websocket.TextMessage { s.commandHandler(string(m)) @@ -80,6 +81,7 @@ func (s *session) WriteLoop() { if err := s.write(msg.t, msg.data); err != nil { return } + incWebSocketWriteBytesTotal(s.userID, len(msg.data)) if msg.t == websocket.CloseMessage { return