mirror of
https://github.com/multica-ai/multica.git
synced 2026-06-17 11:48:42 +02:00
Add Prometheus metrics endpoint with local-bind listener support and baseline metrics collectors.
47 lines
1.1 KiB
Go
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)
|
|
}
|
|
}
|