|
@@ -30,6 +30,7 @@ import (
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
|
|
+ "maunium.net/go/mautrix-whatsapp/types"
|
|
|
)
|
|
|
|
|
|
type MetricsHandler struct {
|
|
@@ -52,6 +53,9 @@ type MetricsHandler struct {
|
|
|
encryptedPrivateCount prometheus.Gauge
|
|
|
unencryptedGroupCount prometheus.Gauge
|
|
|
unencryptedPrivateCount prometheus.Gauge
|
|
|
+
|
|
|
+ connected *prometheus.GaugeVec
|
|
|
+ loggedIn *prometheus.GaugeVec
|
|
|
}
|
|
|
|
|
|
func NewMetricsHandler(address string, log log.Logger, db *database.Database) *MetricsHandler {
|
|
@@ -94,6 +98,15 @@ func NewMetricsHandler(address string, log log.Logger, db *database.Database) *M
|
|
|
encryptedPrivateCount: portalCount.With(prometheus.Labels{"type": "private", "encrypted": "true"}),
|
|
|
unencryptedGroupCount: portalCount.With(prometheus.Labels{"type": "group", "encrypted": "false"}),
|
|
|
unencryptedPrivateCount: portalCount.With(prometheus.Labels{"type": "private", "encrypted": "false"}),
|
|
|
+
|
|
|
+ loggedIn: promauto.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
+ Name: "bridge_logged_in",
|
|
|
+ Help: "Users logged into the bridge",
|
|
|
+ }, []string{"jid", "bridge_logged_in"}),
|
|
|
+ connected: promauto.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
+ Name: "bridge_connected",
|
|
|
+ Help: "Users connected to WhatsApp",
|
|
|
+ }, []string{"jid", "bridge_connected"}),
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -119,6 +132,32 @@ func (mh *MetricsHandler) TrackDisconnection(userID id.UserID) {
|
|
|
mh.disconnections.With(prometheus.Labels{"user_id": string(userID)}).Inc()
|
|
|
}
|
|
|
|
|
|
+func (mh *MetricsHandler) TrackLoginState(jid types.WhatsAppID, loggedIn bool) {
|
|
|
+ if !mh.running {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var loggedInVal float64 = 0
|
|
|
+ if loggedIn {
|
|
|
+ loggedInVal = 1
|
|
|
+ }
|
|
|
+ metric := mh.loggedIn.MustCurryWith(prometheus.Labels{"jid": jid})
|
|
|
+ metric.With(prometheus.Labels{"bridge_logged_in": "true"}).Set(loggedInVal)
|
|
|
+ metric.With(prometheus.Labels{"bridge_logged_in": "false"}).Set(1 - loggedInVal)
|
|
|
+}
|
|
|
+
|
|
|
+func (mh *MetricsHandler) TrackConnectionState(jid types.WhatsAppID, connected bool) {
|
|
|
+ if !mh.running {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var connectedVal float64 = 0
|
|
|
+ if connected {
|
|
|
+ connectedVal = 1
|
|
|
+ }
|
|
|
+ metric := mh.connected.MustCurryWith(prometheus.Labels{"jid": jid})
|
|
|
+ metric.With(prometheus.Labels{"bridge_connected": "true"}).Set(connectedVal)
|
|
|
+ metric.With(prometheus.Labels{"bridge_connected": "false"}).Set(1 - connectedVal)
|
|
|
+}
|
|
|
+
|
|
|
func (mh *MetricsHandler) updateStats() {
|
|
|
start := time.Now()
|
|
|
var puppetCount int
|