Add simple disconnection count to metrics

This commit is contained in:
Tulir Asokan 2020-06-17 17:57:14 +03:00
parent 689202f43d
commit 5a04f6c871
2 changed files with 20 additions and 0 deletions

View file

@ -27,6 +27,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
log "maunium.net/go/maulogger/v2" log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"maunium.net/go/mautrix-whatsapp/database" "maunium.net/go/mautrix-whatsapp/database"
) )
@ -42,6 +43,7 @@ type MetricsHandler struct {
messageHandling *prometheus.HistogramVec messageHandling *prometheus.HistogramVec
countCollection prometheus.Histogram countCollection prometheus.Histogram
disconnections *prometheus.CounterVec
puppetCount prometheus.Gauge puppetCount prometheus.Gauge
userCount prometheus.Gauge userCount prometheus.Gauge
messageCount prometheus.Gauge messageCount prometheus.Gauge
@ -71,6 +73,10 @@ func NewMetricsHandler(address string, log log.Logger, db *database.Database) *M
Name: "whatsapp_count_collection", Name: "whatsapp_count_collection",
Help: "Time spent collecting the whatsapp_*_total metrics", Help: "Time spent collecting the whatsapp_*_total metrics",
}), }),
disconnections: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "whatsapp_disconnections",
Help: "Number of times a Matrix user has been disconnected from WhatsApp",
}, []string{"user_id"}),
puppetCount: promauto.NewGauge(prometheus.GaugeOpts{ puppetCount: promauto.NewGauge(prometheus.GaugeOpts{
Name: "whatsapp_puppets_total", Name: "whatsapp_puppets_total",
Help: "Number of WhatsApp users bridged into Matrix", Help: "Number of WhatsApp users bridged into Matrix",
@ -91,7 +97,12 @@ func NewMetricsHandler(address string, log log.Logger, db *database.Database) *M
} }
} }
func noop() {}
func (mh *MetricsHandler) TrackEvent(eventType event.Type) func() { func (mh *MetricsHandler) TrackEvent(eventType event.Type) func() {
if !mh.running {
return noop
}
start := time.Now() start := time.Now()
return func() { return func() {
duration := time.Now().Sub(start) duration := time.Now().Sub(start)
@ -101,6 +112,13 @@ func (mh *MetricsHandler) TrackEvent(eventType event.Type) func() {
} }
} }
func (mh *MetricsHandler) TrackDisconnection(userID id.UserID) {
if !mh.running {
return
}
mh.disconnections.With(prometheus.Labels{"user_id": string(userID)}).Inc()
}
func (mh *MetricsHandler) updateStats() { func (mh *MetricsHandler) updateStats() {
start := time.Now() start := time.Now()
var puppetCount int var puppetCount int

View file

@ -561,6 +561,7 @@ func (user *User) HandleError(err error) {
user.log.Errorfln("WhatsApp error: %v", err) user.log.Errorfln("WhatsApp error: %v", err)
} }
if closed, ok := err.(*whatsapp.ErrConnectionClosed); ok { if closed, ok := err.(*whatsapp.ErrConnectionClosed); ok {
user.bridge.Metrics.TrackDisconnection(user.MXID)
if closed.Code == 1000 && user.cleanDisconnection { if closed.Code == 1000 && user.cleanDisconnection {
user.cleanDisconnection = false user.cleanDisconnection = false
user.log.Infoln("Clean disconnection by server") user.log.Infoln("Clean disconnection by server")
@ -568,6 +569,7 @@ func (user *User) HandleError(err error) {
} }
go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection was closed with websocket status code %d", closed.Code)) go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection was closed with websocket status code %d", closed.Code))
} else if failed, ok := err.(*whatsapp.ErrConnectionFailed); ok { } else if failed, ok := err.(*whatsapp.ErrConnectionFailed); ok {
user.bridge.Metrics.TrackDisconnection(user.MXID)
user.ConnectionErrors++ user.ConnectionErrors++
go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection failed: %v", failed.Err)) go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection failed: %v", failed.Err))
} }