The web UI serves the router's own state at /metrics (token needed); router.metrics.nodeExporter runs node_exporter for the machine's and opens its port only to the zones named. The Prometheus page edits those settings and shows the scrape configuration. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
96 lines
3.1 KiB
Go
96 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPromWriter(t *testing.T) {
|
|
var p promWriter
|
|
p.sample("x_up", "gauge", "Is it up.", 1, "device", `a"b\c`+"\n")
|
|
p.sample("x_up", "gauge", "Is it up.", 0, "device", "other")
|
|
p.sample("x_plain", "counter", "No labels.", 2.5)
|
|
p.sample("x_nan", "gauge", "Left out.", nanValue())
|
|
want := "# HELP x_up Is it up.\n# TYPE x_up gauge\n" +
|
|
`x_up{device="a\"b\\c\n"} 1` + "\n" +
|
|
`x_up{device="other"} 0` + "\n" +
|
|
"# HELP x_plain No labels.\n# TYPE x_plain counter\nx_plain 2.5\n"
|
|
if got := p.b.String(); got != want {
|
|
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
|
|
}
|
|
}
|
|
|
|
func nanValue() float64 { var z float64; return z / z }
|
|
|
|
func TestMetricsEndpoint(t *testing.T) {
|
|
a, _ := newTestApp(t)
|
|
h := a.routes()
|
|
if rec := rest(h, "", "GET", "/metrics", "", nil); rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("no token: %d", rec.Code)
|
|
}
|
|
_, read, _ := a.tokens.Create("prometheus", "read")
|
|
rec := rest(h, read, "GET", "/metrics", "", nil)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/plain; version=0.0.4") {
|
|
t.Fatalf("content type %q", ct)
|
|
}
|
|
body := rec.Body.String()
|
|
for _, want := range []string{
|
|
"# TYPE router_info gauge\nrouter_info{version=",
|
|
"router_settings_unapplied 0\n",
|
|
"router_apply_running 0\n",
|
|
"router_confirmation_due_seconds 0\n",
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("no %q in:\n%s", want, body)
|
|
}
|
|
}
|
|
// Every sample line is `name{labels} number` or `name number`.
|
|
for _, line := range strings.Split(strings.TrimSpace(body), "\n") {
|
|
if strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
if i := strings.LastIndexByte(line, ' '); i < 1 {
|
|
t.Errorf("not a sample: %q", line)
|
|
}
|
|
}
|
|
if rec := rest(h, read, "POST", "/metrics", "", nil); rec.Code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("POST: %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestMetricsFromASnapshot(t *testing.T) {
|
|
a, _ := newTestApp(t)
|
|
now := time.Unix(1_800_000_000, 0)
|
|
a.metrics.mu.Lock()
|
|
a.metrics.snap = Snapshot{
|
|
Time: now,
|
|
Uplink: UplinkStatus{Device: "pppoe-wan", Type: "pppoe", Connected: true, Checked: true, Reachable: true, Since: now.Add(-time.Hour)},
|
|
System: SystemStatus{Kernel: "7.3.4", KernelNext: "7.3.5"},
|
|
Pings: []PingResult{{Target: "gateway", Address: "10.0.0.1", RTT: 8 * time.Millisecond, OK: true}, {Target: "1.1.1.1", Address: "1.1.1.1"}},
|
|
}
|
|
a.metrics.mu.Unlock()
|
|
body := a.promText(now)
|
|
for _, want := range []string{
|
|
`router_uplink_up{device="pppoe-wan",type="pppoe"} 1`,
|
|
"router_uplink_reachable 1\n",
|
|
"router_uplink_since_timestamp_seconds 1.7999964e+09\n",
|
|
`router_ping_up{target="gateway",address="10.0.0.1"} 1`,
|
|
`router_ping_rtt_seconds{target="gateway",address="10.0.0.1"} 0.008`,
|
|
`router_ping_up{target="1.1.1.1",address="1.1.1.1"} 0`,
|
|
`router_reboot_required{reason="kernel"} 1`,
|
|
`router_reboot_required{reason="configuration"} 0`,
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("no %q in:\n%s", want, body)
|
|
}
|
|
}
|
|
if strings.Contains(body, `router_ping_rtt_seconds{target="1.1.1.1"`) {
|
|
t.Error("an RTT for a ping that failed")
|
|
}
|
|
}
|