metrics: Prometheus endpoint on its own --metrics listener; the pod exposes it inside the cluster only
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/p2p"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
// MetricsHandler serves the node's counters in the Prometheus text format. It
|
||||
// has no authentication and shows the node's internals, so it belongs on a
|
||||
// listener of its own that only the operator can reach (`--metrics`), never on
|
||||
// the public HTTP API.
|
||||
func MetricsHandler(node *p2p.Node, st *store.Store, version string, started time.Time) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
var b strings.Builder
|
||||
write := func(name, typ, help string, samples ...sample) {
|
||||
fmt.Fprintf(&b, "# HELP %s %s\n# TYPE %s %s\n", name, help, name, typ)
|
||||
for _, s := range samples {
|
||||
if s.labels == "" {
|
||||
fmt.Fprintf(&b, "%s %v\n", name, s.value)
|
||||
} else {
|
||||
fmt.Fprintf(&b, "%s{%s} %v\n", name, s.labels, s.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s := node.Stats()
|
||||
write("allmende_build_info", "gauge", "The version of this node.",
|
||||
sample{label("version", version), 1})
|
||||
write("allmende_start_time_seconds", "gauge", "When the node started, in unix seconds.",
|
||||
sample{"", started.Unix()})
|
||||
write("allmende_peers", "gauge", "Connected peers.", sample{"", s.Peers})
|
||||
|
||||
regions := make([]string, 0, len(s.RegionPeers))
|
||||
for r := range s.RegionPeers {
|
||||
regions = append(regions, r)
|
||||
}
|
||||
sort.Strings(regions)
|
||||
var rp []sample
|
||||
for _, r := range regions {
|
||||
rp = append(rp, sample{label("region", r), s.RegionPeers[r]})
|
||||
}
|
||||
write("allmende_region_peers", "gauge", "Peers known to follow each region this node follows.", rp...)
|
||||
|
||||
if n, err := st.Count(r.Context()); err == nil {
|
||||
write("allmende_listings", "gauge", "Live listings held.", sample{"", n})
|
||||
}
|
||||
|
||||
write("allmende_guard_messages_dropped_total", "counter",
|
||||
"Gossip messages dropped because a peer sent more than its rate.", sample{"", s.MessagesDropped})
|
||||
write("allmende_guard_messages_invalid_total", "counter",
|
||||
"Gossip messages rejected as invalid.", sample{"", s.MessagesInvalid})
|
||||
write("allmende_guard_peers_banned_total", "counter",
|
||||
"Peers banned for sending too many invalid messages.", sample{"", s.PeersBanned})
|
||||
write("allmende_guard_sync_refused_total", "counter",
|
||||
"Sync requests refused (rate, parallel streams, banned peer).", sample{"", s.SyncRefused})
|
||||
write("allmende_connections_refused_total", "counter",
|
||||
"Inbound connections refused.",
|
||||
sample{label("reason", "address_full"), s.ConnectionsFull},
|
||||
sample{label("reason", "banned"), s.ConnectionsBanned})
|
||||
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
write("go_goroutines", "gauge", "Goroutines.", sample{"", runtime.NumGoroutine()})
|
||||
write("go_memstats_heap_alloc_bytes", "gauge", "Bytes of live heap objects.", sample{"", m.HeapAlloc})
|
||||
write("go_memstats_sys_bytes", "gauge", "Bytes the Go runtime obtained from the system.", sample{"", m.Sys})
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
_, _ = w.Write([]byte(b.String()))
|
||||
})
|
||||
}
|
||||
|
||||
type sample struct {
|
||||
labels string
|
||||
value any
|
||||
}
|
||||
|
||||
// label formats one label, escaping what the text format requires.
|
||||
func label(name, value string) string {
|
||||
r := strings.NewReplacer(`\`, `\\`, `"`, `\"`, "\n", `\n`)
|
||||
return name + `="` + r.Replace(value) + `"`
|
||||
}
|
||||
Reference in New Issue
Block a user