From 3a4cc0c7197c40929d3524558b0a424f7c0391b9 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Thu, 17 Sep 2026 15:27:39 +0200 Subject: [PATCH] Register the fleet collector before the registry is built MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 42fd0aa deployed and published nothing. RegisterMetrics appends to a slice that AdminHandler reads when it constructs the registry, and I called it after — a comment on the function says "called before AdminHandler, because the registry is built there", written by me, immediately above the code that did not. Nothing failed. The build was clean, the tests passed, the endpoint returned 200 with fourteen kilobytes of metrics, and not one of them was a node. Only grepping the live endpoint for what was supposed to be there showed it. So there is now a test that a registered collector reaches /metrics, which is the assertion that was missing: everything around it was tested and the wiring between them was not. Co-Authored-By: Claude Opus 5 (1M context) --- internal/cli/server.go | 18 ++++++++------ internal/router/admin_test.go | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 internal/router/admin_test.go diff --git a/internal/cli/server.go b/internal/cli/server.go index e076da7..6f6bfcb 100644 --- a/internal/cli/server.go +++ b/internal/cli/server.go @@ -112,6 +112,17 @@ func newServerCmd() *cobra.Command { admin = ctl agentPath, agentHandler = svc.Handler() + // Before AdminHandler, which is where the registry is built — + // registering after it silently publishes nothing, which is + // exactly what the first version of this did. + // + // On the same /metrics as the traffic figures: correlating + // "the cluster was full" with "requests were queueing" should + // not need two systems. + if err := rt.RegisterMetrics(fleetmetrics.New(ctl)); err != nil { + return err + } + log.Info("control plane running", "models", len(cfg.Models), "agent_listen", cfg.AgentListen, "agent_mtls", cfg.AgentTLS.Enabled()) @@ -122,13 +133,6 @@ func newServerCmd() *cobra.Command { return err } if admin != nil { - // What the cluster is made of, on the same /metrics as the - // traffic figures: correlating "the cluster was full" with - // "requests were queueing" should not need two systems. - if err := rt.RegisterMetrics(fleetmetrics.New(admin)); err != nil { - return err - } - api := adminapi.New(admin) // Catalog changes need somewhere to persist, so this is wired // only when there is a store. Without one, the endpoints say so diff --git a/internal/router/admin_test.go b/internal/router/admin_test.go new file mode 100644 index 0000000..99cc6e1 --- /dev/null +++ b/internal/router/admin_test.go @@ -0,0 +1,46 @@ +package router + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/prometheus/client_golang/prometheus" + + "gitea.mynodes.xyz/ilker/llmgw/internal/config" +) + +type constantCollector struct{ desc *prometheus.Desc } + +func (c constantCollector) Describe(ch chan<- *prometheus.Desc) { ch <- c.desc } +func (c constantCollector) Collect(ch chan<- prometheus.Metric) { + ch <- prometheus.MustNewConstMetric(c.desc, prometheus.GaugeValue, 1) +} + +func TestARegisteredCollectorReachesTheMetricsEndpoint(t *testing.T) { + // Registering after AdminHandler silently published nothing, because that + // is where the registry is built. The fleet metrics were deployed and + // absent, and only looking at the live endpoint showed it — a wiring + // mistake that no type checks and nothing fails on. + e := newFakeEngine(t, "m") + rt := newTestRouter(t, baseConfig(config.Endpoint{ID: "a", URL: e.srv.URL, Models: []string{"m"}})) + + if err := rt.RegisterMetrics(constantCollector{ + desc: prometheus.NewDesc("llmgw_test_collector", "proof it was registered", nil, nil), + }); err != nil { + t.Fatal(err) + } + + mux, err := rt.AdminHandler() + if err != nil { + t.Fatal(err) + } + req := httptest.NewRequest(http.MethodGet, "/metrics", nil) + rec := httptest.NewRecorder() + mux.ServeHTTP(rec, req) + + if !strings.Contains(rec.Body.String(), "llmgw_test_collector") { + t.Error("a registered collector did not reach /metrics") + } +}