72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
package observability
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHealthHandlerReadinessAndMetrics_HP_OPS_01(t *testing.T) {
|
|
t.Parallel()
|
|
health := New()
|
|
health.Inc("dispatch_attempt")
|
|
handler := health.Handler(Paths{Liveness: "/live", Readiness: "/ready", Metrics: "/metrics"})
|
|
ready := httptest.NewRecorder()
|
|
handler.ServeHTTP(ready, httptest.NewRequest(http.MethodGet, "/ready", nil))
|
|
if ready.Code != http.StatusServiceUnavailable || !strings.Contains(ready.Body.String(), "ready=false") {
|
|
t.Fatalf("initial readiness = %d %q", ready.Code, ready.Body.String())
|
|
}
|
|
health.SetReady(true)
|
|
health.SetDirty(true)
|
|
ready = httptest.NewRecorder()
|
|
handler.ServeHTTP(ready, httptest.NewRequest(http.MethodGet, "/ready", nil))
|
|
if ready.Code != http.StatusOK || !strings.Contains(ready.Body.String(), "dirty=true") {
|
|
t.Fatalf("ready health = %d %q", ready.Code, ready.Body.String())
|
|
}
|
|
metrics := httptest.NewRecorder()
|
|
handler.ServeHTTP(metrics, httptest.NewRequest(http.MethodGet, "/metrics", nil))
|
|
if metrics.Code != http.StatusOK || !strings.Contains(metrics.Body.String(), "rvbox_dispatch_attempt_total 1") {
|
|
t.Fatalf("metrics = %d %q", metrics.Code, metrics.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHealthMetricNamesAreBounded_BH_OPS_01(t *testing.T) {
|
|
t.Parallel()
|
|
health := New()
|
|
health.Inc("bad name")
|
|
health.Inc("1bad_name")
|
|
_, _, counters := health.Snapshot()
|
|
if len(counters) != 0 {
|
|
t.Fatalf("invalid metric name recorded: %#v", counters)
|
|
}
|
|
}
|
|
|
|
func TestHealthRendersBoundedGaugesAndDurationHistograms_HP_OPS_02(t *testing.T) {
|
|
t.Parallel()
|
|
health := New()
|
|
health.SetGauge("client_spool_bytes", 4096)
|
|
health.SetGauge("bad gauge", 1)
|
|
health.ObserveDuration("dispatch_latency", 12*time.Millisecond)
|
|
health.ObserveDuration("bad latency", time.Second)
|
|
|
|
metrics := httptest.NewRecorder()
|
|
health.Handler(Paths{}).ServeHTTP(metrics, httptest.NewRequest(http.MethodGet, "/metrics", nil))
|
|
body := metrics.Body.String()
|
|
for _, want := range []string{
|
|
"rvbox_client_spool_bytes 4096",
|
|
"rvbox_dispatch_latency_seconds_bucket{le=\"0.05\"} 1",
|
|
"rvbox_dispatch_latency_seconds_bucket{le=\"+Inf\"} 1",
|
|
"rvbox_dispatch_latency_seconds_sum 0.012",
|
|
"rvbox_dispatch_latency_seconds_count 1",
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Fatalf("metrics missing %q:\n%s", want, body)
|
|
}
|
|
}
|
|
if strings.Contains(body, "bad gauge") || strings.Contains(body, "bad latency") {
|
|
t.Fatalf("unbounded metric name rendered: %s", body)
|
|
}
|
|
}
|