Skip to content

Commit a2e823e

Browse files
Add histogram for health transmission duration (#147)
* Add histogram for health transmission duration * Add buckets * One decimal place
1 parent 362c3c7 commit a2e823e

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

cmd/heartbeat/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"flag"
6+
"fmt"
67
"log"
78
"net/http"
89
"os"
@@ -19,6 +20,7 @@ import (
1920
"github.com/m-lab/locate/cmd/heartbeat/health"
2021
"github.com/m-lab/locate/cmd/heartbeat/registration"
2122
"github.com/m-lab/locate/connection"
23+
"github.com/m-lab/locate/metrics"
2224
"github.com/m-lab/locate/static"
2325
)
2426

@@ -124,10 +126,15 @@ func write(ws *connection.Conn, hc *health.Checker, ldr *registration.Loader) {
124126
log.Printf("updated registration to %v", reg)
125127
}
126128
case <-hbTicker.C:
129+
t := time.Now()
127130
score := getHealth(hc)
128131
healthMsg := v2.Health{Score: score}
129132
hbm := v2.HeartbeatMessage{Health: &healthMsg}
130133
sendMessage(ws, hbm, "health")
134+
135+
// Record duration metric.
136+
fmtScore := fmt.Sprintf("%.1f", score)
137+
metrics.HealthTransmissionDuration.WithLabelValues(fmtScore).Observe(time.Since(t).Seconds())
131138
}
132139
}
133140
}

metrics/metrics.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,15 @@ var (
162162
Help: "Time of new registration retrieval from siteinfo.",
163163
},
164164
)
165+
166+
// HealthTransmissionDuration is a histogram for the latency of the heartbeat
167+
// to assess local health and send it to the Locate.
168+
HealthTransmissionDuration = promauto.NewHistogramVec(
169+
prometheus.HistogramOpts{
170+
Name: "heartbeat_health_transmission_duration",
171+
Help: "Latency for the heartbeat to assess local health and send it.",
172+
Buckets: prometheus.LinearBuckets(0, 2, 30),
173+
},
174+
[]string{"score"},
175+
)
165176
)

metrics/metrics_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ func TestLintMetrics(t *testing.T) {
2020
KubernetesRequestsTotal.WithLabelValues("type", "status")
2121
KubernetesRequestTimeHistogram.WithLabelValues("healthy")
2222
RegistrationUpdateTime.Set(0)
23+
HealthTransmissionDuration.WithLabelValues("score")
2324
promtest.LintMetrics(nil)
2425
}

0 commit comments

Comments
 (0)