Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Merge branch 'af/use-metrics'
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Feb 22, 2024
2 parents aa09f47 + 726e6ef commit b475933
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 49 deletions.
58 changes: 9 additions & 49 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ import (
"context"
"net/http"
"runtime/debug"
"strconv"
"sync"
"time"

"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"

"github.com/element-hq/mautrix-go/event"
"github.com/element-hq/mautrix-go/id"

"github.com/element-hq/mautrix-signal/database"
)
Expand All @@ -49,9 +48,6 @@ type MetricsHandler struct {
signalMessageAge prometheus.Histogram
signalMessageHandling *prometheus.HistogramVec
countCollection prometheus.Histogram
disconnections *prometheus.CounterVec
incomingRetryReceipts *prometheus.CounterVec
connectionFailures *prometheus.CounterVec
puppetCount prometheus.Gauge
activePuppetCount prometheus.Gauge
bridgeBlocked prometheus.Gauge
Expand All @@ -65,10 +61,10 @@ type MetricsHandler struct {
unencryptedPrivateCount prometheus.Gauge

connected prometheus.Gauge
connectedState map[string]bool
connectedState map[uuid.UUID]bool
connectedStateLock sync.Mutex
loggedIn prometheus.Gauge
loggedInState map[string]bool
loggedInState map[uuid.UUID]bool
loggedInStateLock sync.Mutex
}

Expand Down Expand Up @@ -101,18 +97,6 @@ func NewMetricsHandler(address string, log zerolog.Logger, db *database.Database
Name: "bridge_count_collection",
Help: "Time spent collecting the bridge_*_total metrics",
}),
disconnections: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "bridge_disconnections",
Help: "Number of times a Matrix user has been disconnected from Signal",
}, []string{"user_id"}),
connectionFailures: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "bridge_connection_failures",
Help: "Number of times a connection has failed to Signal",
}, []string{"reason"}),
incomingRetryReceipts: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "bridge_incoming_retry_receipts",
Help: "Number of times a remote Signal user has requested a retry from the bridge. retry_count = 5 is usually the last attempt (and very likely means a failed message)",
}, []string{"retry_count", "message_found"}),
puppetCount: promauto.NewGauge(prometheus.GaugeOpts{
Name: "bridge_puppets_total",
Help: "Number of Signal users bridged into Matrix",
Expand Down Expand Up @@ -147,12 +131,12 @@ func NewMetricsHandler(address string, log zerolog.Logger, db *database.Database
Name: "bridge_logged_in",
Help: "Bridge users logged into Signal",
}),
loggedInState: make(map[string]bool),
loggedInState: make(map[uuid.UUID]bool),
connected: promauto.NewGauge(prometheus.GaugeOpts{
Name: "bridge_connected",
Help: "Bridge users connected to Signal",
}),
connectedState: make(map[string]bool),
connectedState: make(map[uuid.UUID]bool),
}
}

Expand Down Expand Up @@ -186,31 +170,7 @@ func (mh *MetricsHandler) TrackSignalMessage(timestamp time.Time, messageType st
}
}

func (mh *MetricsHandler) TrackDisconnection(userID id.UserID) {
if !mh.running {
return
}
mh.disconnections.With(prometheus.Labels{"user_id": string(userID)}).Inc()
}

func (mh *MetricsHandler) TrackConnectionFailure(reason string) {
if !mh.running {
return
}
mh.connectionFailures.With(prometheus.Labels{"reason": reason}).Inc()
}

func (mh *MetricsHandler) TrackRetryReceipt(count int, found bool) {
if !mh.running {
return
}
mh.incomingRetryReceipts.With(prometheus.Labels{
"retry_count": strconv.Itoa(count),
"message_found": strconv.FormatBool(found),
}).Inc()
}

func (mh *MetricsHandler) TrackLoginState(signalID string, loggedIn bool) {
func (mh *MetricsHandler) TrackLoginState(signalID uuid.UUID, loggedIn bool) {
if !mh.running {
return
}
Expand All @@ -221,13 +181,13 @@ func (mh *MetricsHandler) TrackLoginState(signalID string, loggedIn bool) {
mh.loggedInState[signalID] = loggedIn
if loggedIn {
mh.loggedIn.Inc()
} else {
} else if ok {
mh.loggedIn.Dec()
}
}
}

func (mh *MetricsHandler) TrackConnectionState(signalID string, connected bool) {
func (mh *MetricsHandler) TrackConnectionState(signalID uuid.UUID, connected bool) {
if !mh.running {
return
}
Expand All @@ -238,7 +198,7 @@ func (mh *MetricsHandler) TrackConnectionState(signalID string, connected bool)
mh.connectedState[signalID] = connected
if connected {
mh.connected.Inc()
} else {
} else if ok {
mh.connected.Dec()
}
}
Expand Down
7 changes: 7 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ func (user *User) startupTryConnect(retryCount int) {
case signalmeow.SignalConnectionEventConnected:
user.log.Debug().Msg("Sending Connected BridgeState")
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
user.bridge.Metrics.TrackConnectionState(user.SignalID, true)
user.bridge.Metrics.TrackLoginState(user.SignalID, true)

case signalmeow.SignalConnectionEventDisconnected:
user.log.Debug().Msg("Received SignalConnectionEventDisconnected")
Expand Down Expand Up @@ -473,6 +475,7 @@ func (user *User) startupTryConnect(retryCount int) {
} else {
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: "unknown-websocket-error", Message: err.Error()})
}
user.bridge.Metrics.TrackConnectionState(user.SignalID, false)
}
}

Expand All @@ -483,6 +486,7 @@ func (user *User) startupTryConnect(retryCount int) {
} else {
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Message: err.Error()})
}
user.bridge.Metrics.TrackConnectionState(user.SignalID, false)
err = user.Logout(ctx)
if err != nil {
user.log.Err(err).Msg("Error logging out user in response to remote logout")
Expand All @@ -497,6 +501,7 @@ func (user *User) startupTryConnect(retryCount int) {
case signalmeow.SignalConnectionEventError:
user.log.Debug().Msg("Sending UnknownError BridgeState")
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Error: "unknown-websocket-error", Message: err.Error()})
user.bridge.Metrics.TrackConnectionState(user.SignalID, false)

case signalmeow.SignalConnectionCleanShutdown:
if user.Client == nil {
Expand All @@ -507,6 +512,7 @@ func (user *User) startupTryConnect(retryCount int) {
user.log.Debug().Msg("Clean Shutdown, but logged out - Sending BadCredentials BridgeState")
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Message: "You have been logged out of Signal, please reconnect"})
}
user.bridge.Metrics.TrackConnectionState(user.SignalID, false)
}
}
}()
Expand Down Expand Up @@ -865,6 +871,7 @@ func (user *User) handleLoggedOut(ctx context.Context) {
}

func (user *User) handleLoggedOutNoLock(ctx context.Context) {
user.bridge.Metrics.TrackLoginState(user.SignalID, false)
id := user.SignalID
user.SignalID = uuid.Nil
user.SignalUsername = ""
Expand Down

0 comments on commit b475933

Please sign in to comment.