Add basic active users and downstreams metrics

This commit is contained in:
Simon Ser 2021-11-15 20:36:14 +01:00
parent 8f8d7aab0e
commit df6b161f55

View file

@ -15,6 +15,7 @@ import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"gopkg.in/irc.v3"
"nhooyr.io/websocket"
@ -111,6 +112,8 @@ func (s *Server) SetConfig(cfg *Config) {
}
func (s *Server) Start() error {
s.registerMetrics()
users, err := s.db.ListUsers(context.TODO())
if err != nil {
return err
@ -125,6 +128,27 @@ func (s *Server) Start() error {
return nil
}
func (s *Server) registerMetrics() {
factory := promauto.With(s.MetricsRegistry)
factory.NewGaugeFunc(prometheus.GaugeOpts{
Name: "soju_users_active",
Help: "Current number of active users",
}, func() float64 {
s.lock.Lock()
n := len(s.users)
s.lock.Unlock()
return float64(n)
})
factory.NewGaugeFunc(prometheus.GaugeOpts{
Name: "soju_downstreams_active",
Help: "Current number of downstream connections",
}, func() float64 {
return float64(atomic.LoadInt64(&s.connCount))
})
}
func (s *Server) Shutdown() {
s.lock.Lock()
for ln := range s.listeners {