feat(go): expose runtime ingest metrics
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
||||
)
|
||||
|
||||
type Check struct {
|
||||
@@ -32,22 +34,25 @@ func NewHandler(service string, checks []Check) *Handler {
|
||||
return &Handler{service: service, checks: checks}
|
||||
}
|
||||
|
||||
func NewMux(service string, checks []Check) *http.ServeMux {
|
||||
func NewMux(service string, checks []Check, registry *metrics.Registry) *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
handler := NewHandler(service, checks)
|
||||
mux.Handle("/healthz", handler)
|
||||
mux.Handle("/readyz", handler)
|
||||
if registry != nil {
|
||||
mux.Handle("/metrics", metrics.NewHandler(registry))
|
||||
}
|
||||
return mux
|
||||
}
|
||||
|
||||
func NewServer(addr string, service string, checks []Check) *http.Server {
|
||||
func NewServer(addr string, service string, checks []Check, registry *metrics.Registry) *http.Server {
|
||||
addr = strings.TrimSpace(addr)
|
||||
if addr == "" {
|
||||
return nil
|
||||
}
|
||||
return &http.Server{
|
||||
Addr: addr,
|
||||
Handler: NewMux(service, checks),
|
||||
Handler: NewMux(service, checks, registry),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
||||
)
|
||||
|
||||
func TestHandlerReturnsHealthyWhenAllChecksPass(t *testing.T) {
|
||||
@@ -63,10 +65,12 @@ func TestHandlerRejectsUnknownPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewMuxRegistersHealthAndReadinessRoutes(t *testing.T) {
|
||||
registry := metrics.NewRegistry()
|
||||
registry.IncCounter("vehicle_test_total", metrics.Labels{"service": "stat"})
|
||||
mux := NewMux("vehicle-stat-writer", []Check{
|
||||
{Name: "mysql", Check: func(context.Context) error { return nil }},
|
||||
})
|
||||
for _, path := range []string{"/healthz", "/readyz"} {
|
||||
}, registry)
|
||||
for _, path := range []string{"/healthz", "/readyz", "/metrics"} {
|
||||
request := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
@@ -79,13 +83,13 @@ func TestNewMuxRegistersHealthAndReadinessRoutes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewServerReturnsNilWhenAddressIsEmpty(t *testing.T) {
|
||||
if server := NewServer("", "vehicle-gateway", nil); server != nil {
|
||||
if server := NewServer("", "vehicle-gateway", nil, nil); server != nil {
|
||||
t.Fatalf("server = %#v, want nil", server)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewServerBuildsConfiguredHTTPServer(t *testing.T) {
|
||||
server := NewServer(":20290", "vehicle-gateway", nil)
|
||||
server := NewServer(":20290", "vehicle-gateway", nil, nil)
|
||||
if server == nil {
|
||||
t.Fatal("server is nil")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user