diff --git a/handler/heartbeat.go b/handler/heartbeat.go index f13df3ef..010e5d43 100644 --- a/handler/heartbeat.go +++ b/handler/heartbeat.go @@ -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) } @@ -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 { @@ -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) } diff --git a/metrics/metrics.go b/metrics/metrics.go index 6599f158..5b81f136 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -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 diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 9903e849..19b9e430 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -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")