Files
multica/server/internal/metrics/server_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

47 lines
1.1 KiB
Go

package metrics
import (
"context"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
)
func TestMetricsServerCanBindLoopback(t *testing.T) {
registry := NewRegistry(RegistryOptions{})
server := NewServer("127.0.0.1:0", registry.Gatherer)
ln, err := net.Listen("tcp", server.Addr)
if err != nil {
t.Fatalf("listen: %v", err)
}
errCh := make(chan error, 1)
go func() {
errCh <- server.Serve(ln)
}()
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_ = server.Shutdown(ctx)
if err := <-errCh; err != nil && err != http.ErrServerClosed {
t.Fatalf("serve: %v", err)
}
})
resp, err := http.Get("http://" + ln.Addr().String() + "/metrics")
if err != nil {
t.Fatalf("get /metrics: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("/metrics status = %d, want %d", resp.StatusCode, http.StatusOK)
}
body, _ := io.ReadAll(resp.Body)
if !strings.Contains(string(body), "multica_build_info") {
t.Fatalf("/metrics body missing build info: %s", body)
}
}