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
26 changes: 26 additions & 0 deletions cmd/fabric-api/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

"go.datum.net/galactic/internal/fabric/node"
)

// TestMetricsScrape scrapes both roles' /metrics: the gateway's manager
// serves controller-runtime's registry, which metrics.Handler serves
// unchanged, and the node sidecar serves its own registry.
func TestMetricsScrape(t *testing.T) {
t.Run("gateway", func(t *testing.T) {
instrumenttest.AssertScrapes(t, metrics.Handler())
})
t.Run("node", func(t *testing.T) {
instrumenttest.AssertScrapes(t, node.NewMetrics().Handler())
})
}
10 changes: 8 additions & 2 deletions cmd/fabric-config-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,20 @@ func writerLock(opts *options, metrics *fabricconfig.AgentMetrics, writer string
return fabricconfig.TimedLock{Locker: lock, Metrics: metrics, Writer: writer}
}

// newMetrics builds the registry served at /metrics, holding the next-hop
// guard's and the activation path's metrics.
func newMetrics() (*fabricconfig.NextHopMetrics, *fabricconfig.AgentMetrics) {
m := fabricconfig.NewNextHopMetrics()
return m, fabricconfig.NewAgentMetrics(m.Registry)
}

// watchWithGuard runs a.Watch alongside a NextHopGuard over the same FRR
// instance, and serves the guard's and the activation path's metrics, and
// the boot source init recorded, when opts asks for them. With the
// ConfigMap path retired it runs only the deployment agent and the guard
// until ctx ends.
func watchWithGuard(ctx context.Context, a *fabricconfig.Agent, opts *options) error {
metrics := fabricconfig.NewNextHopMetrics()
agentMetrics := fabricconfig.NewAgentMetrics(metrics.Registry)
metrics, agentMetrics := newMetrics()
if rec, err := fabricconfig.ReadBootRecord(opts.bootRecord); err == nil {
agentMetrics.SetBoot(rec.Source)
} else {
Expand Down
17 changes: 17 additions & 0 deletions cmd/fabric-config-agent/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

// TestMetricsScrape scrapes the registry the agent serves at /metrics.
func TestMetricsScrape(t *testing.T) {
m, _ := newMetrics()
instrumenttest.AssertScrapes(t, m.Handler())
}
19 changes: 19 additions & 0 deletions cmd/fabric-router-controller/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

// TestMetricsScrape scrapes what the manager serves at /metrics:
// controller-runtime's registry, which metrics.Handler serves unchanged,
// holding every metric this command's packages register when linked.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, metrics.Handler())
}
18 changes: 18 additions & 0 deletions cmd/galactic-cni/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

"go.miloapis.com/instrumentation/instrumenttest"

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

// TestMetricsScrape scrapes the registry the run daemon serves at /metrics.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, installer.NewMetrics().Handler())
}
19 changes: 19 additions & 0 deletions cmd/galactic-controller/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

// TestMetricsScrape scrapes what the manager serves at /metrics:
// controller-runtime's registry, which metrics.Handler serves unchanged,
// holding every metric this command's packages register when linked.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, metrics.Handler())
}
19 changes: 19 additions & 0 deletions cmd/galactic-edge/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

// TestMetricsScrape scrapes what the manager serves at /metrics:
// controller-runtime's registry, which metrics.Handler serves unchanged,
// holding every metric this command's packages register when linked.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, metrics.Handler())
}
19 changes: 19 additions & 0 deletions cmd/galactic-gateway/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

// TestMetricsScrape scrapes what the manager serves at /metrics:
// controller-runtime's registry, which metrics.Handler serves unchanged,
// holding every metric this command's packages register when linked.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, metrics.Handler())
}
19 changes: 19 additions & 0 deletions cmd/galactic-nat/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

// TestMetricsScrape scrapes what the manager serves at /metrics:
// controller-runtime's registry, which metrics.Handler serves unchanged,
// holding every metric this command's packages register when linked.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, metrics.Handler())
}
19 changes: 19 additions & 0 deletions cmd/galactic-router/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

// TestMetricsScrape scrapes what the manager serves at /metrics:
// controller-runtime's registry, which metrics.Handler serves unchanged,
// holding every metric this command's packages register when linked.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, metrics.Handler())
}
19 changes: 19 additions & 0 deletions cmd/galactic-vrf/scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"testing"

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

// TestMetricsScrape scrapes what the manager serves at /metrics:
// controller-runtime's registry, which metrics.Handler serves unchanged,
// holding every metric this command's packages register when linked.
func TestMetricsScrape(t *testing.T) {
instrumenttest.AssertScrapes(t, metrics.Handler())
}
30 changes: 30 additions & 0 deletions docs/observability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ Metrics, scrape configuration, shipped alert rules and log format. Use the subsy
- [Shipped alerts](alerts.md)
- [Logging](logging.md)

## Adding a metric

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:

- `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.
- `TestNoMetricsOutsideLibrary` fails on any metric constructor or registry
from `client_golang`'s `prometheus` package, including one passed in as a
`prometheus.Registerer`.
- `TestMetricsLint` checks every library metric's name against Prometheus
conventions. It imports each package that defines one, and
`TestLintLinksEveryMetric` fails when a package is missing from that list
or defines its metrics in a command's `main` package.
- 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.

Both source checks skip the directories in `pending` in
`internal/instrumentation/adoption_test.go`, which haven't moved to the
library yet.

## Where the metrics come from

The dataplane and fabric-router DaemonSets use `hostNetwork: true`, so
Expand Down
6 changes: 3 additions & 3 deletions docs/observability/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ text.

For local runs, `--zap-devel --log-level=debug` gives console output with the
verbose lines. No binary turns on development mode itself:
`internal/logging`'s `TestLoggerAdopted` runs the instrumentation library's
logger check over the whole module, which fails on any logger not built by the
library and on development mode turned on in source.
`internal/instrumentation`'s `TestAdopted` runs the instrumentation library's
adoption check over the whole module, which fails on any logger not built by
the library and on development mode turned on in source.
`TestBinariesUseThisPackage` fails when a command under `cmd/` imports
controller-runtime's zap package or calls `SetLogger` itself, and
`TestLongRunningBinariesInstallTheHandler` fails when a command under `cmd/`
Expand Down
11 changes: 9 additions & 2 deletions internal/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,14 @@ 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.
func NewMetrics() *metrics.Metrics {
m := metrics.New()
nldump.MustRegister(m.Registry)
return m
}

// Run executes the CNI installer main container tasks:
// 1. Loads, pins, and attaches the eBPF uSID datapath and keeps its attachment
// set re-evaluated against netlink events for the life of ctx. This is the
Expand Down Expand Up @@ -1109,8 +1117,7 @@ func radvActorFailed(actors *radvActorSet, failure radvActorFailure) {
func Run(ctx context.Context, grpcHealthPort, metricsPort int) error {
slog.Info("Starting CNI installer run daemon", "grpcHealthPort", grpcHealthPort, "metricsPort", metricsPort)

m := metrics.New()
nldump.MustRegister(m.Registry)
m := NewMetrics()

ebpfState, datapath, err := startEBPFDatapath(ctx, m)
if err != nil {
Expand Down
Loading
Loading