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:
2026-11-19 10:56:43 +01:00
co-authored by Claude Sonnet 5.5
parent b922808a51
commit c653ae4206
13 changed files with 306 additions and 3 deletions
+24
View File
@@ -53,6 +53,8 @@ func cmdRun(args []string) error {
"address others should dial, when this node sits behind a router or in a container, e.g. /dns4/node.example.org/tcp/4001; repeatable")
fs.Var(&listen, "listen", "libp2p listen multiaddr; repeatable (default: all interfaces, random ports)")
httpAddr := fs.String("http", "127.0.0.1:8080", "address for the HTTP API")
metricsAddr := fs.String("metrics", "",
"serve metrics for Prometheus on this address, e.g. 127.0.0.1:9090 (off by default; it shows the node's internals and has no login, so keep it off the public network)")
noLoginLink := fs.Bool("no-login-link", false,
"do not print the owner sign-in link (it holds the admin token); use where the terminal is a log, e.g. in a container")
noMDNS := fs.Bool("no-mdns", false, "do not look for peers on the local network")
@@ -223,6 +225,28 @@ func cmdRun(args []string) error {
}
}()
if *metricsAddr != "" {
mln, err := net.Listen("tcp", *metricsAddr)
if err != nil {
return fmt.Errorf("--metrics: %w", err)
}
msrv := &http.Server{
Handler: api.MetricsHandler(node, st, version, time.Now()),
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
go func() {
if err := msrv.Serve(mln); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Error("metrics server", "err", err)
}
}()
defer msrv.Close()
log.Info("metrics", "addr", mln.Addr().String())
if host, _, _ := net.SplitHostPort(mln.Addr().String()); host != "127.0.0.1" && host != "::1" {
log.Warn("the metrics are reachable from other machines and have no login; keep this port off the public network")
}
}
log.Info("node running", "version", version,
"peer_id", node.ID().String(), "identity", dev.Cert.Author[:12]+"…",
"regions", []string(regions), "http", ln.Addr().String())