feat(go): expose runtime ingest metrics

This commit is contained in:
lingniu
2026-07-02 19:06:24 +08:00
parent 1dfd540700
commit 896e841902
18 changed files with 602 additions and 28 deletions

View File

@@ -3,11 +3,13 @@ package main
import (
"context"
"encoding/json"
"strings"
"testing"
"github.com/segmentio/kafka-go"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
)
func TestProcessHistoryMessageUsesUncancelledContextForAppendAndCommit(t *testing.T) {
@@ -29,7 +31,7 @@ func TestProcessHistoryMessageUsesUncancelledContextForAppendAndCommit(t *testin
appender := &contextCheckingHistoryAppender{}
committer := &contextCheckingHistoryCommitter{}
processHistoryMessage(parent, discardHistoryLogger{}, appender, committer, kafka.Message{Value: payload})
processHistoryMessage(parent, discardHistoryLogger{}, nil, appender, committer, kafka.Message{Value: payload})
if appender.ctxErr != nil {
t.Fatalf("appender saw cancelled context: %v", appender.ctxErr)
@@ -42,6 +44,35 @@ func TestProcessHistoryMessageUsesUncancelledContextForAppendAndCommit(t *testin
}
}
func TestProcessHistoryMessageRecordsMetrics(t *testing.T) {
env := envelope.FrameEnvelope{Protocol: envelope.ProtocolGB32960, VIN: "VIN001", MessageID: "0x01"}
payload, err := json.Marshal(env)
if err != nil {
t.Fatal(err)
}
registry := metrics.NewRegistry()
processHistoryMessage(
context.Background(),
discardHistoryLogger{},
registry,
&contextCheckingHistoryAppender{},
&contextCheckingHistoryCommitter{},
kafka.Message{Topic: "vehicle.raw.gb32960.v1", Value: payload},
)
text := registry.Render()
for _, want := range []string{
`vehicle_history_kafka_messages_total{status="received",topic="vehicle.raw.gb32960.v1"} 1`,
`vehicle_history_writes_total{status="ok",topic="vehicle.raw.gb32960.v1"} 1`,
`vehicle_history_kafka_commits_total{status="ok",topic="vehicle.raw.gb32960.v1"} 1`,
} {
if !strings.Contains(text, want) {
t.Fatalf("metrics missing %s:\n%s", want, text)
}
}
}
type contextCheckingHistoryAppender struct {
ctxErr error
count int