feat(go): expose runtime ingest metrics
This commit is contained in:
141
go/vehicle-gateway/internal/metrics/metrics.go
Normal file
141
go/vehicle-gateway/internal/metrics/metrics.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Labels map[string]string
|
||||
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
counters map[string]map[string]float64
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{counters: map[string]map[string]float64{}}
|
||||
}
|
||||
|
||||
func (r *Registry) IncCounter(name string, labels Labels) {
|
||||
r.AddCounter(name, labels, 1)
|
||||
}
|
||||
|
||||
func (r *Registry) AddCounter(name string, labels Labels, value float64) {
|
||||
name = strings.TrimSpace(name)
|
||||
if r == nil || name == "" || value == 0 {
|
||||
return
|
||||
}
|
||||
key := labelsKey(labels)
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.counters[name] == nil {
|
||||
r.counters[name] = map[string]float64{}
|
||||
}
|
||||
r.counters[name][key] += value
|
||||
}
|
||||
|
||||
func (r *Registry) Render() string {
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
var names []string
|
||||
for name := range r.counters {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
|
||||
var b strings.Builder
|
||||
for _, name := range names {
|
||||
b.WriteString("# TYPE ")
|
||||
b.WriteString(name)
|
||||
b.WriteString(" counter\n")
|
||||
var keys []string
|
||||
for key := range r.counters[name] {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
b.WriteString(name)
|
||||
if key != "" {
|
||||
b.WriteString("{")
|
||||
b.WriteString(key)
|
||||
b.WriteString("}")
|
||||
}
|
||||
b.WriteString(" ")
|
||||
b.WriteString(formatNumber(r.counters[name][key]))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func NewHandler(registry *Registry) http.Handler {
|
||||
if registry == nil {
|
||||
registry = NewRegistry()
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
if strings.Trim(r.URL.Path, "/") != "metrics" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
|
||||
_, _ = w.Write([]byte(registry.Render()))
|
||||
})
|
||||
}
|
||||
|
||||
func labelsKey(labels Labels) string {
|
||||
if len(labels) == 0 {
|
||||
return ""
|
||||
}
|
||||
keys := make([]string, 0, len(labels))
|
||||
for key := range labels {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
parts := make([]string, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
value := labels[key]
|
||||
parts = append(parts, fmt.Sprintf(`%s="%s"`, sanitizeLabelName(key), escapeLabelValue(value)))
|
||||
}
|
||||
return strings.Join(parts, ",")
|
||||
}
|
||||
|
||||
func sanitizeLabelName(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return "label"
|
||||
}
|
||||
var b strings.Builder
|
||||
for i, r := range value {
|
||||
ok := r == '_' || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (i > 0 && r >= '0' && r <= '9')
|
||||
if ok {
|
||||
b.WriteRune(r)
|
||||
} else {
|
||||
b.WriteByte('_')
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func escapeLabelValue(value string) string {
|
||||
value = strings.ReplaceAll(value, `\`, `\\`)
|
||||
value = strings.ReplaceAll(value, "\n", `\n`)
|
||||
return strings.ReplaceAll(value, `"`, `\"`)
|
||||
}
|
||||
|
||||
func formatNumber(value float64) string {
|
||||
if value == float64(int64(value)) {
|
||||
return fmt.Sprintf("%d", int64(value))
|
||||
}
|
||||
return fmt.Sprintf("%g", value)
|
||||
}
|
||||
60
go/vehicle-gateway/internal/metrics/metrics_test.go
Normal file
60
go/vehicle-gateway/internal/metrics/metrics_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegistryRendersCountersWithLabels(t *testing.T) {
|
||||
registry := NewRegistry()
|
||||
registry.AddCounter("vehicle_frames_total", Labels{"protocol": "JT808", "status": "ok"}, 2)
|
||||
registry.IncCounter("vehicle_frames_total", Labels{"status": "ok", "protocol": "JT808"})
|
||||
registry.IncCounter("vehicle_publish_errors_total", Labels{"target": "kafka"})
|
||||
|
||||
text := registry.Render()
|
||||
|
||||
for _, want := range []string{
|
||||
`# TYPE vehicle_frames_total counter`,
|
||||
`vehicle_frames_total{protocol="JT808",status="ok"} 3`,
|
||||
`# TYPE vehicle_publish_errors_total counter`,
|
||||
`vehicle_publish_errors_total{target="kafka"} 1`,
|
||||
} {
|
||||
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"})
|
||||
handler := NewHandler(registry)
|
||||
request := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
if got := response.Header().Get("Content-Type"); !strings.Contains(got, "text/plain") {
|
||||
t.Fatalf("content-type = %q", got)
|
||||
}
|
||||
if !strings.Contains(response.Body.String(), `vehicle_kafka_commits_total{service="history"} 1`) {
|
||||
t.Fatalf("body = %s", response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRejectsNonMetricsPath(t *testing.T) {
|
||||
handler := NewHandler(NewRegistry())
|
||||
request := httptest.NewRequest(http.MethodGet, "/readyz", nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d", response.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user