Skip to content

Commit 44fb82b

Browse files
Add 'experiment' label to heartbeat connections gauge (#90)
* Add 'experiment' label to heartbeat connections gauge * Remove metric * Add tests * Comments * Undo unnecessary changes * Make decrease conditional
1 parent a92b36b commit 44fb82b

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

handler/heartbeat.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func (c *Client) Heartbeat(rw http.ResponseWriter, req *http.Request) {
2828
metrics.RequestsTotal.WithLabelValues("heartbeat", err.Error()).Inc()
2929
return
3030
}
31-
metrics.CurrentHeartbeatConnections.Inc()
3231
metrics.RequestsTotal.WithLabelValues("heartbeat", "OK").Inc()
3332
go c.handleHeartbeats(ws)
3433
}
@@ -39,11 +38,14 @@ func (c *Client) handleHeartbeats(ws *websocket.Conn) {
3938
setReadDeadline(ws)
4039

4140
var hostname string
41+
var experiment string
4242
for {
4343
_, message, err := ws.ReadMessage()
4444
if err != nil {
4545
log.Errorf("read error: %v", err)
46-
metrics.CurrentHeartbeatConnections.Dec()
46+
if experiment != "" {
47+
metrics.CurrentHeartbeatConnections.WithLabelValues(experiment).Dec()
48+
}
4749
return
4850
}
4951
if message != nil {
@@ -59,6 +61,8 @@ func (c *Client) handleHeartbeats(ws *websocket.Conn) {
5961
case hbm.Registration != nil:
6062
hostname = hbm.Registration.Hostname
6163
c.RegisterInstance(*hbm.Registration)
64+
experiment = hbm.Registration.Experiment
65+
metrics.CurrentHeartbeatConnections.WithLabelValues(experiment).Inc()
6266
case hbm.Health != nil:
6367
c.UpdateHealth(hostname, *hbm.Health)
6468
}

metrics/metrics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ var (
3737
//
3838
// Example usage:
3939
// metrics.CurrentHeartbeatConnections.Inc()
40-
CurrentHeartbeatConnections = promauto.NewGauge(
40+
CurrentHeartbeatConnections = promauto.NewGaugeVec(
4141
prometheus.GaugeOpts{
4242
Name: "locate_current_heartbeat_connections",
4343
Help: "Number of currently active Heartbeat connections.",
4444
},
45+
[]string{"experiment"},
4546
)
4647

4748
// PrometheusHealthCollectionDuration is a histogram that tracks the latency of the

metrics/metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestLintMetrics(t *testing.T) {
1010
RequestsTotal.WithLabelValues("type", "status")
1111
AppEngineTotal.WithLabelValues("country")
12-
CurrentHeartbeatConnections.Set(0)
12+
CurrentHeartbeatConnections.WithLabelValues("experiment").Set(0)
1313
PrometheusHealthCollectionDuration.WithLabelValues("code")
1414
PortChecksTotal.WithLabelValues("status")
1515
KubernetesRequestsTotal.WithLabelValues("status")

0 commit comments

Comments
 (0)