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
61 changes: 39 additions & 22 deletions cmd/galactic-vrf/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

bgpv1alpha1 "go.datum.net/galactic/api/network/v1alpha1"
Expand All @@ -34,6 +33,37 @@ const (
Find more information at: https://www.datum.net/docs`
)

// sidecarMetrics are the sidecar's metrics, all on the instrumentation
// library's registry.
type sidecarMetrics struct {
store *ingresssidecar.Metrics
lifecycle *ingresssidecar.LifecycleMetrics
convergence *ingresssidecar.ConvergenceMetrics
inputs *ingresssidecar.InputMetrics
datapath *ingresssidecar.DatapathMetrics
nodeSource *ingresssidecar.NodeSourceMetrics
gateway *ingresssidecar.GatewayMetrics
}

// newSidecarMetrics creates every metric the sidecar serves, configured from
// cfg, and registers the collectors among them. runCmd calls it once.
func newSidecarMetrics(cfg *config.VRFConfig) sidecarMetrics {
m := sidecarMetrics{
store: ingresssidecar.NewMetrics(),
lifecycle: ingresssidecar.NewLifecycleMetrics(cfg.SweepInterval, cfg.TeardownGracePeriod),
convergence: ingresssidecar.NewConvergenceMetrics(),
inputs: ingresssidecar.NewInputMetrics(),
datapath: ingresssidecar.NewDatapathMetrics(),
nodeSource: ingresssidecar.NewNodeSourceMetrics(),
gateway: ingresssidecar.NewGatewayMetrics(cfg.NodeName != "",
cfg.NodeName != "" && cfg.GatewayPrefix != ""),
}
m.lifecycle.MustRegister()
m.datapath.MustRegister()
m.nodeSource.MustRegister()
return m
}

// runCmd is the application startup: it registers the ingress sidecar's
// reconciler against a cluster-scoped EndpointSlice watch, then seeds the store
// from live API state and runs its startup inventory and periodic sweep.
Expand Down Expand Up @@ -63,25 +93,14 @@ func runCmd(cfg *config.VRFConfig) error {
// Pre-flight RBAC check.
checkWatchPermissions(mgr)

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

backend := ingresssidecar.NewKernelBackend()
store := ingresssidecar.NewStore(backend, cfg.TeardownGracePeriod, metrics)
convergence := ingresssidecar.NewConvergenceMetrics()
convergence.MustRegister(ctrlmetrics.Registry)
store.SetConvergenceMetrics(convergence)
inputs := ingresssidecar.NewInputMetrics()
inputs.MustRegister(ctrlmetrics.Registry)
store.SetInputMetrics(inputs)
datapath := ingresssidecar.NewDatapathMetrics()
datapath.MustRegister(ctrlmetrics.Registry)
store.SetDatapathMetrics(datapath)
nodeSourceMetrics := ingresssidecar.NewNodeSourceMetrics()
nodeSourceMetrics.MustRegister(ctrlmetrics.Registry)
store.SetNodeSourceMetrics(nodeSourceMetrics)
store := ingresssidecar.NewStore(backend, cfg.TeardownGracePeriod, m.store)
store.SetConvergenceMetrics(m.convergence)
store.SetInputMetrics(m.inputs)
store.SetDatapathMetrics(m.datapath)
store.SetNodeSourceMetrics(m.nodeSource)

// Return-path gateway advertisement is opt-in on the node name alone,
// and leaving it unset is fully inert. The cached client is fine here:
Expand Down Expand Up @@ -127,9 +146,7 @@ func runCmd(cfg *config.VRFConfig) error {
"return-path gateway advertisement publishing is disabled", config.EnvVRFNodeName)
}

gateway := ingresssidecar.NewGatewayMetrics(cfg.NodeName != "", cfg.NodeName != "" && cfg.GatewayPrefix != "")
gateway.MustRegister(ctrlmetrics.Registry)
store.SetGatewayMetrics(gateway)
store.SetGatewayMetrics(m.gateway)

if err := (&ingresssidecar.Reconciler{Store: store}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("setup EndpointSlice controller: %w", err)
Expand All @@ -145,7 +162,7 @@ func runCmd(cfg *config.VRFConfig) error {
// controller's own reconciles have drained the workqueue that list
// fed, so on a busy node at boot the two race. Seeding uses the
// manager's uncached reader, which depends on neither.
go ingresssidecar.RunStartup(ctx, mgr.GetAPIReader(), store, cfg.SweepInterval, lifecycle)
go ingresssidecar.RunStartup(ctx, mgr.GetAPIReader(), store, cfg.SweepInterval, m.lifecycle)

if err := mgr.Start(ctx); err != nil {
return fmt.Errorf("manager exited: %w", err)
Expand Down
19 changes: 16 additions & 3 deletions cmd/galactic-vrf/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@
package main

import (
"sync"
"testing"

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

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

// 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.
// startup creates the series once per test process, as runCmd does once per
// process, so repeated runs of TestMetricsScrape share them.
var startup sync.Once

// TestMetricsScrape scrapes what the manager serves at /metrics, the
// instrumentation library's registry, once newSidecarMetrics has created
// every series the sidecar serves, and lints them.
func TestMetricsScrape(t *testing.T) {
startup.Do(func() {
cfg := config.NewVRFConfig()
cfg.NodeName = "node-a"
newSidecarMetrics(cfg)
})
instrumenttest.AssertScrapes(t, metrics.Handler())
instrumenttest.Lint(t)
}
27 changes: 14 additions & 13 deletions internal/ingresssidecar/convergence_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"go.miloapis.com/instrumentation/metrics"
)

// Backend operations, as operations_total labels them.
Expand Down Expand Up @@ -52,41 +53,47 @@ type ConvergenceMetrics struct {
operations *prometheus.CounterVec
}

// NewConvergenceMetrics builds unregistered convergence metrics, every series
// at zero.
// NewConvergenceMetrics creates convergence metrics on the instrumentation
// library's registry, every series at zero. The registry keeps the series
// across calls, so each call sets them back to zero.
func NewConvergenceMetrics() *ConvergenceMetrics {
m := &ConvergenceMetrics{
desired: prometheus.NewGauge(prometheus.GaugeOpts{
desired: metrics.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "route_claims_desired",
Help: "Route claims, one per selected and ready EndpointSlice, that this sidecar wants installed, " +
"including any whose install failed.",
}),
applyPending: prometheus.NewGauge(prometheus.GaugeOpts{
applyPending: metrics.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "route_claims_apply_pending",
Help: "Desired route claims whose latest update is not applied yet, whether or not an earlier " +
"version of the route is installed.",
}),
oldest: prometheus.NewGauge(prometheus.GaugeOpts{
oldest: metrics.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "route_apply_oldest_pending_timestamp_seconds",
Help: "Unix time the longest-pending desired claim stopped being applied; 0 when none is pending. " +
"Retries and newer updates of a pending claim do not reset it.",
}),
overdue: prometheus.NewGaugeVec(prometheus.GaugeOpts{
overdue: metrics.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "teardown_overdue",
Help: "Routes or VRFs, by resource, whose own grace period has run out but whose removal has not " +
"completed, as of the last sweep.",
}, []string{"resource"}),
operations: prometheus.NewCounterVec(prometheus.CounterOpts{
operations: metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "operations_total",
Help: "Completed backend operations, by operation (ensure_vrf, ensure_route, remove_vrf, " +
"remove_route, reapply_vrf, reapply_route) and result (success, error).",
}, []string{"operation", resultLabel}),
}
for _, g := range []prometheus.Gauge{m.desired, m.applyPending, m.oldest} {
g.Set(0)
}
m.overdue.Reset()
m.operations.Reset()
for _, r := range teardownResources {
m.overdue.WithLabelValues(r)
}
Expand All @@ -98,12 +105,6 @@ func NewConvergenceMetrics() *ConvergenceMetrics {
return m
}

// MustRegister registers every convergence metric against reg, panicking on a
// duplicate. Call it once per process.
func (m *ConvergenceMetrics) MustRegister(reg prometheus.Registerer) {
reg.MustRegister(m.desired, m.applyPending, m.oldest, m.overdue, m.operations)
}

// operation records one completed backend operation.
func (m *ConvergenceMetrics) operation(op string, err error) {
if m == nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/ingresssidecar/convergence_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func newConvergenceStore(t *testing.T, grace time.Duration) *convergenceStore {
t.Helper()
reg := prometheus.NewPedanticRegistry()
m := NewConvergenceMetrics()
m.MustRegister(reg)
reg.MustRegister(m.collectors()...)
legacy := NewMetrics()
legacy.MustRegister(reg)
reg.MustRegister(legacy.collectors()...)
backend := newFakeBackend()
store := NewStore(backend, grace, legacy)
clock := &stepClock{t: t0}
Expand Down Expand Up @@ -134,7 +134,7 @@ func route(t *testing.T, prefix, sid string) *DesiredRoute {

func TestConvergenceMetricsInitialState(t *testing.T) {
reg := prometheus.NewPedanticRegistry()
NewConvergenceMetrics().MustRegister(reg)
reg.MustRegister(NewConvergenceMetrics().collectors()...)
families, err := reg.Gather()
if err != nil {
t.Fatal(err)
Expand Down
27 changes: 17 additions & 10 deletions internal/ingresssidecar/datapath_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"go.miloapis.com/instrumentation/metrics"
)

// Datapath checks each sweep makes: reading the shared eBPF datapath's
Expand Down Expand Up @@ -72,35 +73,41 @@ type DatapathMetrics struct {
now func() time.Time
}

// NewDatapathMetrics builds unregistered datapath metrics, every check's
// status unknown.
// NewDatapathMetrics creates datapath metrics on the instrumentation
// library's registry, every check's status unknown. The registry keeps the
// series across calls, so each call starts them over. MustRegister exports
// the check statuses.
func NewDatapathMetrics() *DatapathMetrics {
m := &DatapathMetrics{
checks: prometheus.NewCounterVec(prometheus.CounterOpts{
checks: metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "datapath_checks_total",
Help: "Completed datapath checks, by check (generation, registration_rows) and result (ok, " +
"missing for registration_rows, error when the read failed).",
}, []string{checkLabel, resultLabel}),
status: newCheckStatusCollector(),
lastCompleted: prometheus.NewGaugeVec(prometheus.GaugeOpts{
lastCompleted: metrics.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "datapath_check_last_completed_timestamp_seconds",
Help: "Unix time each check last completed, whatever its result; 0 before the first.",
}, []string{checkLabel}),
reapplies: prometheus.NewCounterVec(prometheus.CounterOpts{
reapplies: metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "datapath_reapply_total",
Help: "Reapply passes over every tracked VRF and route, by reason (generation_changed, " +
"rows_missing, retry of a failed pass) and result (success when every reapply succeeded).",
}, []string{"reason", resultLabel}),
pending: prometheus.NewGauge(prometheus.GaugeOpts{
pending: metrics.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "datapath_reapply_pending",
Help: "1 while a reapply pass left something unapplied, until a later pass succeeds.",
}),
now: time.Now,
}
m.checks.Reset()
m.lastCompleted.Reset()
m.reapplies.Reset()
m.pending.Set(0)
for _, check := range datapathChecks {
m.lastCompleted.WithLabelValues(check)
for _, result := range checkResults[check] {
Expand All @@ -115,10 +122,10 @@ func NewDatapathMetrics() *DatapathMetrics {
return m
}

// MustRegister registers every datapath metric against reg, panicking on a
// duplicate. Call it once per process.
func (m *DatapathMetrics) MustRegister(reg prometheus.Registerer) {
reg.MustRegister(m.checks, m.status, m.lastCompleted, m.reapplies, m.pending)
// MustRegister registers m's check status collector on the instrumentation
// library's registry. Call it once per process.
func (m *DatapathMetrics) MustRegister() {
metrics.MustRegister(m.status)
}

// checked records a completed check of check with result. The counter,
Expand Down
2 changes: 1 addition & 1 deletion internal/ingresssidecar/datapath_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newDatapathStore(t *testing.T) *datapathStore {
m := NewDatapathMetrics()
m.now = func() time.Time { return time.Unix(testCheckTime, 0) }
reg := prometheus.NewPedanticRegistry()
m.MustRegister(reg)
reg.MustRegister(m.collectors()...)
backend := newFakeBackend()
backend.generation = testGenLoaded
store := NewStore(backend, time.Hour, nil)
Expand Down
26 changes: 13 additions & 13 deletions internal/ingresssidecar/gateway_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"go.miloapis.com/instrumentation/metrics"
)

// A desired VRF's return-path gateway state, as gateway_vrfs counts them.
Expand Down Expand Up @@ -55,41 +56,46 @@ type GatewayMetrics struct {
now func() time.Time
}

// NewGatewayMetrics builds unregistered gateway metrics for a sidecar whose
// gateway publication and gateway address assignment are enabled as given.
// NewGatewayMetrics creates gateway metrics on the instrumentation library's
// registry for a sidecar whose gateway publication and gateway address
// assignment are enabled as given. The registry keeps the series across
// calls, so each call starts them over.
func NewGatewayMetrics(publicationEnabled, addressAssignmentEnabled bool) *GatewayMetrics {
m := &GatewayMetrics{
publicationEnabled: prometheus.NewGauge(prometheus.GaugeOpts{
publicationEnabled: metrics.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "gateway_publication_enabled",
Help: "1 when this sidecar publishes a return-path gateway advertisement per VPC.",
}),
addressAssignmentEnabled: prometheus.NewGauge(prometheus.GaugeOpts{
addressAssignmentEnabled: metrics.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "gateway_address_assignment_enabled",
Help: "1 when this sidecar assigns each VPC's gateway address itself, from its gateway prefix.",
}),
vrfs: prometheus.NewGaugeVec(prometheus.GaugeOpts{
vrfs: metrics.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "gateway_vrfs",
Help: "Installed, non-draining VRFs by return-path gateway state: awaiting_address, " +
"awaiting_publication, or published (locally, not proof any peer has the route). " +
"All zero while publication is disabled.",
}, []string{"state"}),
oldest: prometheus.NewGauge(prometheus.GaugeOpts{
oldest: metrics.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "gateway_oldest_pending_timestamp_seconds",
Help: "Unix time the longest-unpublished desired VRF started waiting; 0 when none is waiting. " +
"Retries do not reset it.",
}),
operations: prometheus.NewCounterVec(prometheus.CounterOpts{
operations: metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "gateway_operations_total",
Help: "Gateway operations by operation and result: resolve (success, error, not_provisioned), " +
"publish and withdraw (success, error).",
}, []string{"operation", resultLabel}),
now: time.Now,
}
m.oldest.Set(0)
m.vrfs.Reset()
m.operations.Reset()
m.publicationEnabled.Set(boolGauge(publicationEnabled))
m.addressAssignmentEnabled.Set(boolGauge(addressAssignmentEnabled))
for _, s := range gatewayStates {
Expand All @@ -103,12 +109,6 @@ func NewGatewayMetrics(publicationEnabled, addressAssignmentEnabled bool) *Gatew
return m
}

// MustRegister registers every gateway metric against reg, panicking on a
// duplicate. Call it once per process.
func (m *GatewayMetrics) MustRegister(reg prometheus.Registerer) {
reg.MustRegister(m.publicationEnabled, m.addressAssignmentEnabled, m.vrfs, m.oldest, m.operations)
}

func (m *GatewayMetrics) operation(op, result string) {
if m != nil {
m.operations.WithLabelValues(op, result).Inc()
Expand Down
4 changes: 2 additions & 2 deletions internal/ingresssidecar/gateway_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newGatewayStore(t *testing.T, enabled bool) *gatewayStore {
defer g.mu.Unlock()
return g.now
}
m.MustRegister(g.reg)
g.reg.MustRegister(m.collectors()...)
g.Store = NewStore(g.backend, time.Minute, nil)
if enabled {
g.SetGatewayPublisher(g.pub, g.resolver)
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestSetGatewayMetricsCountsExistingVRFs(t *testing.T) {
}
m := NewGatewayMetrics(true, true)
reg := prometheus.NewPedanticRegistry()
m.MustRegister(reg)
reg.MustRegister(m.collectors()...)
store.SetGatewayMetrics(m)
if v := testutil.ToFloat64(m.vrfs.WithLabelValues(GatewayAwaitingAddress)); v != 1 {
t.Errorf("gateway_vrfs{state=awaiting_address} = %v, want the existing VRF counted", v)
Expand Down
Loading
Loading