feat(go): expose gateway frame duration histogram
This commit is contained in:
@@ -266,10 +266,16 @@ func (c *MQTTClient) recordFrameDuration(status envelope.ParseStatus, elapsed ti
|
||||
if c.cfg.Metrics == nil {
|
||||
return
|
||||
}
|
||||
elapsedMS := float64(elapsed.Milliseconds())
|
||||
labels := metrics.Labels{
|
||||
"protocol": string(envelope.ProtocolYutongMQTT),
|
||||
"status": string(status),
|
||||
}
|
||||
c.cfg.Metrics.SetGauge("vehicle_gateway_frame_duration_ms", metrics.Labels{
|
||||
"protocol": string(envelope.ProtocolYutongMQTT),
|
||||
"status": string(status),
|
||||
}, float64(elapsed.Milliseconds()))
|
||||
}, elapsedMS)
|
||||
c.cfg.Metrics.ObserveHistogram("vehicle_gateway_frame_duration_ms_histogram", labels, gatewayFrameDurationBucketsMS, elapsedMS)
|
||||
}
|
||||
|
||||
func (c *MQTTClient) recordPublishMetric(kind string, status string) {
|
||||
|
||||
@@ -80,6 +80,9 @@ func TestMQTTClientRecordsMessageMetrics(t *testing.T) {
|
||||
`vehicle_gateway_identity_total{protocol="YUTONG_MQTT",status="resolved"} 1`,
|
||||
`vehicle_gateway_publish_total{kind="raw",protocol="YUTONG_MQTT",status="ok"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms{protocol="YUTONG_MQTT",status="OK"}`,
|
||||
`vehicle_gateway_frame_duration_ms_histogram_bucket{le="+Inf",protocol="YUTONG_MQTT",status="OK"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms_histogram_count{protocol="YUTONG_MQTT",status="OK"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms_histogram_sum{protocol="YUTONG_MQTT",status="OK"}`,
|
||||
} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("metrics missing %s:\n%s", want, text)
|
||||
|
||||
@@ -63,6 +63,8 @@ type TCPServerConfig struct {
|
||||
|
||||
const frameOperationTimeout = 30 * time.Second
|
||||
|
||||
var gatewayFrameDurationBucketsMS = []float64{1, 5, 10, 25, 50, 100, 250, 500, 1000, 5000}
|
||||
|
||||
func NewTCPServer(cfg TCPServerConfig) (*TCPServer, error) {
|
||||
if cfg.Protocol.Protocol == "" {
|
||||
return nil, errors.New("protocol is required")
|
||||
@@ -322,10 +324,16 @@ func (s *TCPServer) recordFrameDuration(status envelope.ParseStatus, elapsed tim
|
||||
if s.metrics == nil {
|
||||
return
|
||||
}
|
||||
elapsedMS := float64(elapsed.Milliseconds())
|
||||
labels := metrics.Labels{
|
||||
"protocol": string(s.protocol.Protocol),
|
||||
"status": string(status),
|
||||
}
|
||||
s.metrics.SetGauge("vehicle_gateway_frame_duration_ms", metrics.Labels{
|
||||
"protocol": string(s.protocol.Protocol),
|
||||
"status": string(status),
|
||||
}, float64(elapsed.Milliseconds()))
|
||||
}, elapsedMS)
|
||||
s.metrics.ObserveHistogram("vehicle_gateway_frame_duration_ms_histogram", labels, gatewayFrameDurationBucketsMS, elapsedMS)
|
||||
}
|
||||
|
||||
func (s *TCPServer) recordPublishMetric(kind string, status string) {
|
||||
|
||||
@@ -82,6 +82,9 @@ func TestTCPServerRecordsFrameMetrics(t *testing.T) {
|
||||
`vehicle_gateway_identity_total{protocol="JT808",status="unresolved"} 1`,
|
||||
`vehicle_gateway_publish_total{kind="raw",protocol="JT808",status="ok"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms{protocol="JT808",status="OK"}`,
|
||||
`vehicle_gateway_frame_duration_ms_histogram_bucket{le="+Inf",protocol="JT808",status="OK"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms_histogram_count{protocol="JT808",status="OK"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms_histogram_sum{protocol="JT808",status="OK"}`,
|
||||
} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("metrics missing %s:\n%s", want, text)
|
||||
|
||||
@@ -2,6 +2,7 @@ package metrics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -12,15 +13,23 @@ import (
|
||||
type Labels map[string]string
|
||||
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
counters map[string]map[string]float64
|
||||
gauges map[string]map[string]float64
|
||||
mu sync.RWMutex
|
||||
counters map[string]map[string]float64
|
||||
gauges map[string]map[string]float64
|
||||
histograms map[string]*histogramFamily
|
||||
}
|
||||
|
||||
type histogramFamily struct {
|
||||
buckets map[string]map[float64]float64
|
||||
counts map[string]float64
|
||||
sums map[string]float64
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{
|
||||
counters: map[string]map[string]float64{},
|
||||
gauges: map[string]map[string]float64{},
|
||||
counters: map[string]map[string]float64{},
|
||||
gauges: map[string]map[string]float64{},
|
||||
histograms: map[string]*histogramFamily{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,6 +90,40 @@ func (r *Registry) SetKafkaLag(name string, topic string, partition int, offset
|
||||
}, float64(lag))
|
||||
}
|
||||
|
||||
func (r *Registry) ObserveHistogram(name string, labels Labels, buckets []float64, value float64) {
|
||||
name = strings.TrimSpace(name)
|
||||
if r == nil || name == "" {
|
||||
return
|
||||
}
|
||||
key := labelsKey(labels)
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
family := r.histograms[name]
|
||||
if family == nil {
|
||||
family = &histogramFamily{
|
||||
buckets: map[string]map[float64]float64{},
|
||||
counts: map[string]float64{},
|
||||
sums: map[string]float64{},
|
||||
}
|
||||
r.histograms[name] = family
|
||||
}
|
||||
if family.buckets[key] == nil {
|
||||
family.buckets[key] = map[float64]float64{}
|
||||
}
|
||||
normalizedBuckets := append([]float64(nil), buckets...)
|
||||
sort.Float64s(normalizedBuckets)
|
||||
for _, bucket := range normalizedBuckets {
|
||||
if value <= bucket {
|
||||
family.buckets[key][bucket]++
|
||||
} else if _, ok := family.buckets[key][bucket]; !ok {
|
||||
family.buckets[key][bucket] = 0
|
||||
}
|
||||
}
|
||||
family.buckets[key][math.Inf(1)]++
|
||||
family.counts[key]++
|
||||
family.sums[key] += value
|
||||
}
|
||||
|
||||
func (r *Registry) Render() string {
|
||||
if r == nil {
|
||||
return ""
|
||||
@@ -107,6 +150,15 @@ func (r *Registry) Render() string {
|
||||
for _, name := range names {
|
||||
renderMetricFamily(&b, name, "gauge", r.gauges[name])
|
||||
}
|
||||
|
||||
names = names[:0]
|
||||
for name := range r.histograms {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
renderHistogramFamily(&b, name, r.histograms[name])
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
@@ -134,6 +186,72 @@ func renderMetricFamily(b *strings.Builder, name string, metricType string, valu
|
||||
}
|
||||
}
|
||||
|
||||
func renderHistogramFamily(b *strings.Builder, name string, family *histogramFamily) {
|
||||
if family == nil {
|
||||
return
|
||||
}
|
||||
b.WriteString("# TYPE ")
|
||||
b.WriteString(name)
|
||||
b.WriteString(" histogram\n")
|
||||
var keys []string
|
||||
for key := range family.counts {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
var buckets []float64
|
||||
for bucket := range family.buckets[key] {
|
||||
buckets = append(buckets, bucket)
|
||||
}
|
||||
sort.Float64s(buckets)
|
||||
for _, bucket := range buckets {
|
||||
renderHistogramMetric(b, name+"_bucket", key, "le", bucketLabel(bucket), family.buckets[key][bucket])
|
||||
}
|
||||
renderMetricSample(b, name+"_count", key, family.counts[key])
|
||||
renderMetricSample(b, name+"_sum", key, family.sums[key])
|
||||
}
|
||||
}
|
||||
|
||||
func renderHistogramMetric(b *strings.Builder, name string, key string, extraLabel string, extraValue string, value float64) {
|
||||
b.WriteString(name)
|
||||
labels := mergeLabelKey(key, extraLabel, extraValue)
|
||||
if labels != "" {
|
||||
b.WriteString("{")
|
||||
b.WriteString(labels)
|
||||
b.WriteString("}")
|
||||
}
|
||||
b.WriteString(" ")
|
||||
b.WriteString(formatNumber(value))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
|
||||
func renderMetricSample(b *strings.Builder, name string, key string, value float64) {
|
||||
b.WriteString(name)
|
||||
if key != "" {
|
||||
b.WriteString("{")
|
||||
b.WriteString(key)
|
||||
b.WriteString("}")
|
||||
}
|
||||
b.WriteString(" ")
|
||||
b.WriteString(formatNumber(value))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
|
||||
func mergeLabelKey(key string, extraLabel string, extraValue string) string {
|
||||
extra := fmt.Sprintf(`%s="%s"`, sanitizeLabelName(extraLabel), escapeLabelValue(extraValue))
|
||||
if key == "" {
|
||||
return extra
|
||||
}
|
||||
return extra + "," + key
|
||||
}
|
||||
|
||||
func bucketLabel(value float64) string {
|
||||
if math.IsInf(value, 1) {
|
||||
return "+Inf"
|
||||
}
|
||||
return formatNumber(value)
|
||||
}
|
||||
|
||||
func NewHandler(registry *Registry) http.Handler {
|
||||
if registry == nil {
|
||||
registry = NewRegistry()
|
||||
|
||||
@@ -46,6 +46,27 @@ func TestRegistryRendersGaugesWithLabels(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryObservesHistogramBuckets(t *testing.T) {
|
||||
registry := NewRegistry()
|
||||
|
||||
registry.ObserveHistogram("vehicle_gateway_frame_duration_ms", Labels{"protocol": "JT808"}, []float64{1, 5, 10}, 7)
|
||||
|
||||
text := registry.Render()
|
||||
for _, want := range []string{
|
||||
`# TYPE vehicle_gateway_frame_duration_ms histogram`,
|
||||
`vehicle_gateway_frame_duration_ms_bucket{le="1",protocol="JT808"} 0`,
|
||||
`vehicle_gateway_frame_duration_ms_bucket{le="5",protocol="JT808"} 0`,
|
||||
`vehicle_gateway_frame_duration_ms_bucket{le="10",protocol="JT808"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms_bucket{le="+Inf",protocol="JT808"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms_count{protocol="JT808"} 1`,
|
||||
`vehicle_gateway_frame_duration_ms_sum{protocol="JT808"} 7`,
|
||||
} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("histogram missing %s:\n%s", want, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRecordsKafkaLagGauge(t *testing.T) {
|
||||
registry := NewRegistry()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user