feat(metrics): Prometheus registry (checks/drift/notifications) + /metrics handler

This commit is contained in:
2026-07-04 13:18:58 +07:00
parent 6fd847a909
commit 98d8dee413
4 changed files with 139 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package metrics
import (
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus/testutil"
)
func TestMetricsRecord(t *testing.T) {
m := New()
m.ObserveCheck("drift", 100*time.Millisecond)
m.ObserveCheck("in_sync", 50*time.Millisecond)
m.IncNotification("telegram", "ok")
m.SetDrift(3)
if got := testutil.ToFloat64(m.ChecksTotal.WithLabelValues("drift")); got != 1 {
t.Fatalf("checks drift = %v", got)
}
if got := testutil.ToFloat64(m.DriftDomains); got != 3 {
t.Fatalf("drift gauge = %v", got)
}
if got := testutil.ToFloat64(m.NotificationsTotal.WithLabelValues("telegram", "ok")); got != 1 {
t.Fatalf("notif = %v", got)
}
}
func TestHandlerExposesMetrics(t *testing.T) {
m := New()
m.ObserveCheck("in_sync", time.Millisecond)
rec := httptest.NewRecorder()
m.Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/metrics", nil))
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "dns_ar_checks_total") {
t.Fatalf("metrics not exposed: %d", rec.Code)
}
}