Skip to content

Commit

Permalink
Add a metric for number of reconnections
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 21, 2023
1 parent 063961e commit 547ab93
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Go-related metrics are available. We also have custom metrics:
- `received_events_counter`
- `relay_connection_subscriptions_gauge`
- `relay_connection_received_messages_counter`
- `relay_connection_reconnections_counter`

See `service/adapters/prometheus`.

Expand Down
14 changes: 14 additions & 0 deletions service/adapters/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Prometheus struct {
receivedEventsCounter *prometheus.CounterVec
relayConnectionSubscriptionsGauge *prometheus.GaugeVec
relayConnectionReceivedMessagesCounter *prometheus.CounterVec
relayConnectionReconnectionsCounter *prometheus.CounterVec

registry *prometheus.Registry

Expand Down Expand Up @@ -109,6 +110,13 @@ func NewPrometheus(logger logging.Logger) (*Prometheus, error) {
},
[]string{labelAddress, labelMessageType, labelResult},
)
relayConnectionReconnectionsCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "relay_connection_reconnections_counter",
Help: "Number of reconnections to a relay.",
},
[]string{labelAddress},
)

reg := prometheus.NewRegistry()
for _, v := range []prometheus.Collector{
Expand All @@ -121,6 +129,7 @@ func NewPrometheus(logger logging.Logger) (*Prometheus, error) {
receivedEventsCounter,
relayConnectionSubscriptionsGauge,
relayConnectionReceivedMessagesCounter,
relayConnectionReconnectionsCounter,
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
collectors.NewGoCollector(),
} {
Expand Down Expand Up @@ -152,6 +161,7 @@ func NewPrometheus(logger logging.Logger) (*Prometheus, error) {
receivedEventsCounter: receivedEventsCounter,
relayConnectionSubscriptionsGauge: relayConnectionSubscriptionsGauge,
relayConnectionReceivedMessagesCounter: relayConnectionReceivedMessagesCounter,
relayConnectionReconnectionsCounter: relayConnectionReconnectionsCounter,

registry: reg,

Expand Down Expand Up @@ -202,6 +212,10 @@ func (p *Prometheus) ReportMessageReceived(address domain.RelayAddress, messageT
p.relayConnectionReceivedMessagesCounter.With(labels).Inc()
}

func (p *Prometheus) ReportRelayReconnection(address domain.RelayAddress) {
p.relayConnectionReconnectionsCounter.With(prometheus.Labels{labelAddress: address.String()}).Inc()
}

func (p *Prometheus) Registry() *prometheus.Registry {
return p.registry
}
Expand Down
1 change: 1 addition & 0 deletions service/domain/relays/relay_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (r *RelayConnection) run(ctx context.Context) error {
defer r.setState(RelayConnectionStateDisconnected)

r.logger.Trace().Message("connecting")
r.metrics.ReportRelayReconnection(r.address)

conn, _, err := websocket.DefaultDialer.DialContext(ctx, r.address.String(), nil)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions service/domain/relays/relay_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Metrics interface {
ReportRelayConnectionsState(m map[domain.RelayAddress]RelayConnectionState)
ReportNumberOfSubscriptions(address domain.RelayAddress, n int)
ReportMessageReceived(address domain.RelayAddress, messageType MessageType, err *error)
ReportRelayReconnection(address domain.RelayAddress)
}

type MessageType struct {
Expand Down

0 comments on commit 547ab93

Please sign in to comment.