Skip to content

Commit

Permalink
Add 'experiment' label to heartbeat connections gauge (#90)
Browse files Browse the repository at this point in the history
* Add 'experiment' label to heartbeat connections gauge

* Remove metric

* Add tests

* Comments

* Undo unnecessary changes

* Make decrease conditional
  • Loading branch information
cristinaleonr authored Nov 16, 2022
1 parent a92b36b commit 44fb82b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
8 changes: 6 additions & 2 deletions handler/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (c *Client) Heartbeat(rw http.ResponseWriter, req *http.Request) {
metrics.RequestsTotal.WithLabelValues("heartbeat", err.Error()).Inc()
return
}
metrics.CurrentHeartbeatConnections.Inc()
metrics.RequestsTotal.WithLabelValues("heartbeat", "OK").Inc()
go c.handleHeartbeats(ws)
}
Expand All @@ -39,11 +38,14 @@ func (c *Client) handleHeartbeats(ws *websocket.Conn) {
setReadDeadline(ws)

var hostname string
var experiment string
for {
_, message, err := ws.ReadMessage()
if err != nil {
log.Errorf("read error: %v", err)
metrics.CurrentHeartbeatConnections.Dec()
if experiment != "" {
metrics.CurrentHeartbeatConnections.WithLabelValues(experiment).Dec()
}
return
}
if message != nil {
Expand All @@ -59,6 +61,8 @@ func (c *Client) handleHeartbeats(ws *websocket.Conn) {
case hbm.Registration != nil:
hostname = hbm.Registration.Hostname
c.RegisterInstance(*hbm.Registration)
experiment = hbm.Registration.Experiment
metrics.CurrentHeartbeatConnections.WithLabelValues(experiment).Inc()
case hbm.Health != nil:
c.UpdateHealth(hostname, *hbm.Health)
}
Expand Down
3 changes: 2 additions & 1 deletion metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ var (
//
// Example usage:
// metrics.CurrentHeartbeatConnections.Inc()
CurrentHeartbeatConnections = promauto.NewGauge(
CurrentHeartbeatConnections = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "locate_current_heartbeat_connections",
Help: "Number of currently active Heartbeat connections.",
},
[]string{"experiment"},
)

// PrometheusHealthCollectionDuration is a histogram that tracks the latency of the
Expand Down
2 changes: 1 addition & 1 deletion metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestLintMetrics(t *testing.T) {
RequestsTotal.WithLabelValues("type", "status")
AppEngineTotal.WithLabelValues("country")
CurrentHeartbeatConnections.Set(0)
CurrentHeartbeatConnections.WithLabelValues("experiment").Set(0)
PrometheusHealthCollectionDuration.WithLabelValues("code")
PortChecksTotal.WithLabelValues("status")
KubernetesRequestsTotal.WithLabelValues("status")
Expand Down

0 comments on commit 44fb82b

Please sign in to comment.