Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/galactic-cni/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import (
"testing"

"go.miloapis.com/instrumentation/instrumenttest"
"go.miloapis.com/instrumentation/metrics"

"go.datum.net/galactic/internal/installer"
)

// TestMetricsScrape scrapes the registry the run daemon serves at /metrics.
// TestMetricsScrape scrapes what the run daemon serves at /metrics, the
// instrumentation library's registry, once installer.NewMetrics has created
// its series, and lints them. The datapath collectors that read loaded eBPF
// maps need a kernel, so they are left out.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, installer.NewMetrics().Handler())
installer.NewMetrics()
instrumenttest.AssertScrapes(t, metrics.Handler())
instrumenttest.Lint(t)
}
2 changes: 0 additions & 2 deletions cmd/galactic-edge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"go.datum.net/galactic/internal/logging"
"go.datum.net/galactic/internal/metadata"
"go.datum.net/galactic/internal/plumbing/ebpf/attach"
"go.datum.net/galactic/internal/plumbing/nldump"
)

type options struct {
Expand Down Expand Up @@ -178,7 +177,6 @@ func run(parent context.Context, o *options) error {
Metrics: diag.NewMetrics(ctrlmetrics.Registry, "node"),
},
}
nldump.MustRegister(ctrlmetrics.Registry)
if err := ctrlmetrics.Registry.Register(agent.Collector{Agent: a}); err != nil {
return fmt.Errorf("register edge node metrics: %w", err)
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/galactic-gateway/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"go.datum.net/galactic/internal/logging"
"go.datum.net/galactic/internal/metadata"
"go.datum.net/galactic/internal/plumbing/ebpf/xdpdispatch"
"go.datum.net/galactic/internal/plumbing/nldump"
"go.datum.net/galactic/internal/plumbing/sysctl"
)

Expand Down Expand Up @@ -207,7 +206,6 @@ func runCmd(cfg *config.GatewayConfig) error {
gwQuota := gateway.NewNodeQuotaEnforcer(gateway.DefaultMaxRulesPerTenant, gateway.DefaultMaxRuleTableEntries)
gwTelemetry := gateway.NewPrometheusTelemetryEmitter()
gwTelemetry.MustRegister(ctrlmetrics.Registry)
nldump.MustRegister(ctrlmetrics.Registry)
gwEngine := gateway.NewEngine(gwDatapath, gwQuota, gwTelemetry)

// The gateway engine reconciler. No SRv6 address is passed: this datapath
Expand Down
2 changes: 0 additions & 2 deletions cmd/galactic-nat/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"go.datum.net/galactic/internal/logging"
"go.datum.net/galactic/internal/metadata"
"go.datum.net/galactic/internal/plumbing/ebpf/xdpdispatch"
"go.datum.net/galactic/internal/plumbing/nldump"
"go.datum.net/galactic/internal/plumbing/sysctl"
)

Expand Down Expand Up @@ -142,7 +141,6 @@ func runCmd(cfg *config.NATConfig) error {
if err != nil {
return fmt.Errorf("setup egress translation eBPF datapath: %w", err)
}
nldump.MustRegister(ctrlmetrics.Registry)
if err := ctrlmetrics.Registry.Register(
newShardStateCollector(datapath, cfg.DatapathEnabled, cfg.XDPAttach)); err != nil {
return fmt.Errorf("register egress shard state metrics: %w", err)
Expand Down
2 changes: 0 additions & 2 deletions cmd/galactic-router/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"go.datum.net/galactic/internal/plumbing/ebpf/attach"
"go.datum.net/galactic/internal/plumbing/ebpf/vipxlatmap"
"go.datum.net/galactic/internal/plumbing/loaddr"
"go.datum.net/galactic/internal/plumbing/nldump"
"go.datum.net/galactic/internal/reconcile"
galacticruntime "go.datum.net/galactic/internal/runtime"
"go.datum.net/galactic/internal/runtime/gobgp"
Expand Down Expand Up @@ -122,7 +121,6 @@ func runCmd(cfg *config.RouterConfig) error {
}
serviceRouteMetrics := serviceroute.NewMetrics()
serviceRouteMetrics.MustRegister(ctrlmetrics.Registry)
nldump.MustRegister(ctrlmetrics.Registry)

// ctx carries a cause so an ordinary signal-triggered shutdown can be told
// apart from the health server's own Serve failure below. See the cause
Expand Down
2 changes: 0 additions & 2 deletions cmd/galactic-vrf/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go.datum.net/galactic/internal/ingresssidecar"
"go.datum.net/galactic/internal/logging"
"go.datum.net/galactic/internal/metadata"
"go.datum.net/galactic/internal/plumbing/nldump"
)

const (
Expand Down Expand Up @@ -66,7 +65,6 @@ func runCmd(cfg *config.VRFConfig) error {

metrics := ingresssidecar.NewMetrics()
metrics.MustRegister(ctrlmetrics.Registry)
nldump.MustRegister(ctrlmetrics.Registry)
lifecycle := ingresssidecar.NewLifecycleMetrics(cfg.SweepInterval, cfg.TeardownGracePeriod)
lifecycle.MustRegister(ctrlmetrics.Registry)

Expand Down
15 changes: 9 additions & 6 deletions docs/observability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ Create every metric with the constructors in
[`go.miloapis.com/instrumentation/metrics`](https://github.com/milo-os/instrumentation),
and register a collector that reads state at scrape time with its
`metrics.MustRegister`. Both put the metric on controller-runtime's registry,
which every controller-runtime manager serves. `galactic-cni`,
`fabric-config-agent` and the fabric-api node sidecar serve only their own
registries, so a library metric linked into them is not exported until they
serve the library's handler. The tests in `internal/instrumentation` enforce
the rule in the unit suite:
which every controller-runtime manager serves and `galactic-cni` serves with
the library's `metrics.Handler`. `fabric-config-agent` and the fabric-api node
sidecar serve only their own registries, so a library metric linked into them
is not exported until they serve the library's handler. The tests in
`internal/instrumentation` enforce the rule in the unit suite:

- `TestAdopted` runs the library's adoption check, which fails on a metric
registered outside the library and on a logger the library didn't build.
Expand All @@ -39,7 +39,10 @@ the rule in the unit suite:
- Each command that serves `/metrics` has a `TestMetricsScrape`, which fails
when two registries export the same series. `TestEveryBinaryHasScrapeTest`
requires one for every new command, but can't tell whether the test scrapes
the handler the command serves; reviewers check that.
the handler the command serves; reviewers check that. Where a command
creates its metrics at startup rather than when a package loads, its test
first creates them through the same function the command calls, and also
runs the library's lint, which `TestMetricsLint` can't reach.

Both source checks skip the directories in `pending` in
`internal/instrumentation/adoption_test.go`, which haven't moved to the
Expand Down
17 changes: 6 additions & 11 deletions internal/installer/egressgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"testing"

"github.com/cilium/ebpf"
"github.com/prometheus/client_golang/prometheus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

bgpv1alpha1 "go.datum.net/galactic/api/network/v1alpha1"
Expand Down Expand Up @@ -990,8 +989,7 @@ func TestSweepHashed_EmptyMembershipKeepsRoutesOnGroups(t *testing.T) {
t.Run(name, func(t *testing.T) {
static := shardSIDFor(0x10)
f := newFixture(t, testHashed, []string{static}, []netip.Prefix{testNSP})
reg := prometheus.NewRegistry()
f.g.ctl = metrics.NewEgressControl(reg)
f.g.ctl = metrics.NewEgressControl()
lister := &fakeLister{shards: []bgpv1alpha1.EgressShard{dual("dual", 0x10)}, synced: true}
f.g.shards = lister

Expand Down Expand Up @@ -1029,22 +1027,19 @@ func TestSweepHashed_EmptyMembershipKeepsRoutesOnGroups(t *testing.T) {
}
}
want := map[string]float64{egressroutemap.NAT66Class.String(): 3, nat64NSP.String(): 1}
if got := groupRoutes(t, reg); !maps.Equal(got, want) {
if got := groupRoutes(t); !maps.Equal(got, want) {
t.Errorf("group_routes = %v, want %v", got, want)
}
})
}
}

// groupRoutes reads galactic_cni_egress_group_routes from reg, by class.
func groupRoutes(t *testing.T, reg *prometheus.Registry) map[string]float64 {
// groupRoutes reads galactic_cni_egress_group_routes from the instrumentation
// library's registry, by class.
func groupRoutes(t *testing.T) map[string]float64 {
t.Helper()
families, err := reg.Gather()
if err != nil {
t.Fatal(err)
}
got := map[string]float64{}
for _, mf := range families {
for _, mf := range gatherPrefixed(t, "galactic_cni_egress_group_routes") {
if mf.GetName() != "galactic_cni_egress_group_routes" {
continue
}
Expand Down
41 changes: 41 additions & 0 deletions internal/installer/gather_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package installer

import (
"errors"
"strings"
"testing"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)

// gatherPrefixed returns the families on the instrumentation library's
// registry whose names start with prefix. The registry is shared by the whole
// test process, and a test that ran Run against a real datapath leaves map
// collectors behind whose maps are closed, so collection errors from other
// families are ignored. An error from a family under prefix fails the test.
func gatherPrefixed(t *testing.T, prefix string) []*dto.MetricFamily {
t.Helper()
families, err := ctrlmetrics.Registry.Gather()
var errs prometheus.MultiError
if !errors.As(err, &errs) && err != nil {
errs = prometheus.MultiError{err}
}
for _, e := range errs {
if strings.Contains(e.Error(), `fqName: "`+prefix) {
t.Fatal(e)
}
}
var out []*dto.MetricFamily
for _, f := range families {
if strings.HasPrefix(f.GetName(), prefix) {
out = append(out, f)
}
}
return out
}
20 changes: 8 additions & 12 deletions internal/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/vishvananda/netlink"
instmetrics "go.miloapis.com/instrumentation/metrics"
"golang.org/x/sys/unix"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
Expand Down Expand Up @@ -577,12 +578,8 @@ func startEBPFDatapath(ctx context.Context, m *metrics.Metrics) (ebpfDatapathSta
if err := usidmap.NewRegistryFromObjects(objs).VRF.SyncTraffic(); err != nil {
slog.Warn("Failed to create per-VRF traffic counter rows; the next GC sweep retries", "err", err)
}
if err := m.RegisterDatapathCollector(objs); err != nil {
slog.Warn("Failed to register eBPF datapath metrics collector", "err", err)
}
if err := m.RegisterEgressShardCollector(objs, egressCfg.MinActive); err != nil {
slog.Warn("Failed to register egress shard metrics collector", "err", err)
}
m.RegisterDatapathCollector(objs)
m.RegisterEgressShardCollector(objs, egressCfg.MinActive)
}

// Best-effort setup for the eBPF map GC sweeps. Unlike the datapath start
Expand Down Expand Up @@ -1075,12 +1072,11 @@ func radvActorFailed(actors *radvActorSet, failure radvActorFailure) {
"err", failure.err, "hostInterface", failure.iface, "failures", p.failures, "retryIn", backoff)
}

// NewMetrics builds the registry the run daemon serves at /metrics, before
// the datapath collectors that need loaded eBPF objects are added.
// NewMetrics creates the series the run daemon serves at /metrics, before the
// datapath collectors that need loaded eBPF objects are added. Run calls it
// once.
func NewMetrics() *metrics.Metrics {
m := metrics.New()
nldump.MustRegister(m.Registry)
return m
return metrics.New()
}

// Run executes the CNI installer main container tasks:
Expand Down Expand Up @@ -1149,7 +1145,7 @@ func Run(ctx context.Context, grpcHealthPort, metricsPort int) error {

// Serve Prometheus metrics at the conventional /metrics scrape path.
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", m.Handler())
metricsMux.Handle("/metrics", instmetrics.Handler())
metricsSrv := &http.Server{
Addr: fmt.Sprintf(":%d", metricsPort),
Handler: metricsMux,
Expand Down
Loading
Loading