Files
multica/server/internal/metrics/realtime_test.go
devv-eve f864a07bd5 feat: add server Prometheus metrics endpoint
Add Prometheus metrics endpoint with local-bind listener support and baseline metrics collectors.
2026-04-28 14:29:01 +08:00

37 lines
1013 B
Go

package metrics
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/multica-ai/multica/server/internal/realtime"
)
func TestRealtimeCollectorExposesCounters(t *testing.T) {
m := &realtime.Metrics{}
m.ActiveConnections.Store(3)
m.MessagesSentTotal.Store(11)
m.RedisConnected.Store(true)
m.RedisMirrorPrimaryErrors.Store(2)
m.RedisMirrorSecondaryErrors.Store(5)
registry := NewRegistry(RegistryOptions{Realtime: m})
rec := httptest.NewRecorder()
NewHandler(registry.Gatherer).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/metrics", nil))
body := rec.Body.String()
for _, want := range []string{
"multica_realtime_active_connections 3",
"multica_realtime_messages_sent_total 11",
"multica_realtime_redis_connected 1",
`multica_realtime_redis_mirror_errors_total{target="primary"} 2`,
`multica_realtime_redis_mirror_errors_total{target="secondary"} 5`,
} {
if !strings.Contains(body, want) {
t.Fatalf("metrics body missing %q\n%s", want, body)
}
}
}