feat(go): expose kafka consumer lag metrics
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
@@ -69,6 +70,17 @@ func (r *Registry) AddGauge(name string, labels Labels, value float64) {
|
||||
r.gauges[name][key] += value
|
||||
}
|
||||
|
||||
func (r *Registry) SetKafkaLag(name string, topic string, partition int, offset int64, highWaterMark int64) {
|
||||
lag := highWaterMark - offset - 1
|
||||
if lag < 0 {
|
||||
lag = 0
|
||||
}
|
||||
r.SetGauge(name, Labels{
|
||||
"topic": topic,
|
||||
"partition": strconv.Itoa(partition),
|
||||
}, float64(lag))
|
||||
}
|
||||
|
||||
func (r *Registry) Render() string {
|
||||
if r == nil {
|
||||
return ""
|
||||
|
||||
@@ -46,6 +46,24 @@ func TestRegistryRendersGaugesWithLabels(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRecordsKafkaLagGauge(t *testing.T) {
|
||||
registry := NewRegistry()
|
||||
|
||||
registry.SetKafkaLag("vehicle_history_kafka_lag", "vehicle.raw.go.jt808.v1", 3, 101, 130)
|
||||
registry.SetKafkaLag("vehicle_history_kafka_lag", "vehicle.raw.go.gb32960.v1", 0, 9, 9)
|
||||
|
||||
text := registry.Render()
|
||||
for _, want := range []string{
|
||||
`# TYPE vehicle_history_kafka_lag gauge`,
|
||||
`vehicle_history_kafka_lag{partition="0",topic="vehicle.raw.go.gb32960.v1"} 0`,
|
||||
`vehicle_history_kafka_lag{partition="3",topic="vehicle.raw.go.jt808.v1"} 28`,
|
||||
} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("metrics missing %s:\n%s", want, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerServesPrometheusText(t *testing.T) {
|
||||
registry := NewRegistry()
|
||||
registry.IncCounter("vehicle_kafka_commits_total", Labels{"service": "history"})
|
||||
|
||||
Reference in New Issue
Block a user