Skip to content

Commit 0978ff2

Browse files
Add reconnection metrics (#119)
* Add reconnection metrics * Add status * Remove code * Add return statement
1 parent c4918c9 commit 0978ff2

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

connection/connection.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/cenkalti/backoff/v4"
1414
"github.com/gorilla/websocket"
15+
"github.com/m-lab/locate/metrics"
1516
"github.com/m-lab/locate/static"
1617
)
1718

@@ -126,12 +127,12 @@ func (c *Conn) Dial(address string, header http.Header, dialMsg interface{}) err
126127
// message.
127128
//
128129
// The write will fail under the following conditions:
129-
// 1. The client has not called Dial (ErrNotDialed).
130-
// 2. The connection is disconnected and it was not able to
131-
// reconnect (ErrTooManyReconnects or an internal connection
132-
// error).
133-
// 3. The write call in the websocket package failed
134-
// (gorilla/websocket error).
130+
// 1. The client has not called Dial (ErrNotDialed).
131+
// 2. The connection is disconnected and it was not able to
132+
// reconnect (ErrTooManyReconnects or an internal connection
133+
// error).
134+
// 3. The write call in the websocket package failed
135+
// (gorilla/websocket error).
135136
func (c *Conn) WriteMessage(messageType int, data interface{}) error {
136137
if !c.isDialed {
137138
return ErrNotDailed
@@ -235,16 +236,20 @@ func (c *Conn) connect() error {
235236
if resp != nil && !retryErrors[resp.StatusCode] {
236237
log.Printf("error trying to establish a connection with %s, err: %v, status: %d",
237238
c.url.String(), err, resp.StatusCode)
239+
metrics.ConnectionRequestsTotal.WithLabelValues("error").Inc()
238240
ticker.Stop()
241+
return err
239242
}
240243
log.Printf("could not establish a connection with %s (will retry), err: %v",
241244
c.url.String(), err)
245+
metrics.ConnectionRequestsTotal.WithLabelValues("retry").Inc()
242246
continue
243247
}
244248

245249
c.ws = ws
246250
c.isConnected = true
247251
log.Printf("successfully established a connection with %s", c.url.String())
252+
metrics.ConnectionRequestsTotal.WithLabelValues("OK").Inc()
248253
ticker.Stop()
249254
}
250255

metrics/metrics.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ var (
7474
[]string{"code"},
7575
)
7676

77+
// ConnectionRequestsTotal counts the number of (re)connection requests the Heartbeat Service
78+
// makes to the Locate Service.
79+
ConnectionRequestsTotal = promauto.NewCounterVec(
80+
prometheus.CounterOpts{
81+
Name: "connection_requests_total",
82+
Help: "Number of connection requests from the HBS to the Locate Service.",
83+
},
84+
[]string{"status"},
85+
)
86+
7787
// PortChecksTotal counts the number of port checks performed by the Heartbeat
7888
// Service.
7989
PortChecksTotal = promauto.NewCounterVec(

metrics/metrics_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func TestLintMetrics(t *testing.T) {
1313
LocateHealthStatus.WithLabelValues("experiment").Set(0)
1414
ImportMemorystoreTotal.WithLabelValues("status")
1515
PrometheusHealthCollectionDuration.WithLabelValues("code")
16+
ConnectionRequestsTotal.WithLabelValues("status")
1617
PortChecksTotal.WithLabelValues("status")
1718
KubernetesRequestsTotal.WithLabelValues("status")
1819
KubernetesRequestTimeHistogram.WithLabelValues("healthy")

0 commit comments

Comments
 (0)