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
8 changes: 3 additions & 5 deletions cmd/galactic-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/spf13/cobra"
"go.miloapis.com/instrumentation/metrics"
_ "golang.org/x/crypto/x509roots/fallback"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -28,7 +29,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook"

Expand Down Expand Up @@ -177,11 +177,9 @@ func run(parent context.Context, o *options) error {
if o.edge {
rec := &edgediag.Recorder{
Events: mgr.GetEventRecorder("galactic-controller"),
Metrics: edgediag.NewMetrics(ctrlmetrics.Registry, "core"),
}
if err := ctrlmetrics.Registry.Register(&edgediag.StateCollector{Reader: mgr.GetClient()}); err != nil {
return fmt.Errorf("register edge state metrics: %w", err)
Metrics: edgediag.NewMetrics(edgediag.RoleCore),
}
metrics.MustRegister(&edgediag.StateCollector{Reader: mgr.GetClient()})
if err := edgeattachment.Setup(mgr, &edgeattachment.Reconciler{
Client: mgr.GetClient(), APIReader: mgr.GetAPIReader(),
Authority: admission.NewAuthority(o.edgeMaxSkew), MaxSkew: o.edgeMaxSkew, Diag: rec,
Expand Down
25 changes: 22 additions & 3 deletions cmd/galactic-controller/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,34 @@
package main

import (
"sync"
"testing"

"go.miloapis.com/instrumentation/instrumenttest"
"go.miloapis.com/instrumentation/metrics"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

galacticv1alpha1 "go.datum.net/galactic/api/v1alpha1"
edgediag "go.datum.net/galactic/internal/edge/diag"
)

// 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 registers the series once per test process, as run 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 it holds what run adds with
// --edge: the failure counters and the state collector, here reading an
// empty cluster. It lints them too.
func TestMetricsScrape(t *testing.T) {
startup.Do(func() {
scheme := runtime.NewScheme()
utilruntime.Must(galacticv1alpha1.AddToScheme(scheme))
edgediag.NewMetrics(edgediag.RoleCore)
metrics.MustRegister(&edgediag.StateCollector{Reader: fake.NewClientBuilder().WithScheme(scheme).Build()})
})
instrumenttest.AssertScrapes(t, metrics.Handler())
instrumenttest.Lint(t)
}
8 changes: 3 additions & 5 deletions cmd/galactic-edge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (
"time"

"github.com/spf13/cobra"
"go.miloapis.com/instrumentation/metrics"
_ "golang.org/x/crypto/x509roots/fallback"
coordinationv1 "k8s.io/api/coordination/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

galacticv1alpha1 "go.datum.net/galactic/api/v1alpha1"
Expand Down Expand Up @@ -174,12 +174,10 @@ func run(parent context.Context, o *options) error {
BootID: strings.TrimSpace(string(bootID)),
Diag: &diag.Recorder{
Events: mgr.GetEventRecorder("galactic-edge"),
Metrics: diag.NewMetrics(ctrlmetrics.Registry, "node"),
Metrics: diag.NewMetrics(diag.RoleNode),
},
}
if err := ctrlmetrics.Registry.Register(agent.Collector{Agent: a}); err != nil {
return fmt.Errorf("register edge node metrics: %w", err)
}
metrics.MustRegister(agent.Collector{Agent: a})
if err := a.SetupWithManager(mgr); err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/galactic-edge/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import (

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

"go.datum.net/galactic/internal/edge/diag"
)

// 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.
// TestMetricsScrape scrapes what the manager serves at /metrics, the
// instrumentation library's registry, once it holds the failure counters run
// creates, and lints them. The node collector reads the agent's pinned gate
// maps, so it is left out.
func TestMetricsScrape(t *testing.T) {
diag.NewMetrics(diag.RoleNode)
instrumenttest.AssertScrapes(t, metrics.Handler())
instrumenttest.Lint(t)
}
8 changes: 5 additions & 3 deletions docs/observability/edge.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ objects behind a series with the Events and `kubectl` queries in
| `galactic_edge_foundation_routes` | gauge | core | `provider`, `state`, `reason` | Routes by claim state, and the condition reason holding them |
| `galactic_edge_foundation_services` | gauge | core | `provider`, `policy`, `reason` | Services whose traffic policy is `Resolved` or `Unresolved`, with the reason when unresolved |
| `galactic_edge_foundation_state_collect_errors` | gauge | core | none | Kinds the last scrape could not list and left out |
| `galactic_edge_foundation_failures_total` | counter | both | `component`, `stage`, `reason` | Transitions into a failed stage, counted once per transition, not per retry. Each also records an Event |
| `galactic_edge_foundation_gate_closures_total` | counter | node | `component`, `cause` | Open forwarding gates the agent closed, by cause. Each also records an Event on the route |
| `galactic_edge_foundation_cleanup_failures_total` | counter | node | `component` | Host state releases and gate sweeps that failed and are retried |
| `galactic_edge_foundation_failures_total` | counter | both | `edge_role`, `stage`, `reason` | Transitions into a failed stage, counted once per transition, not per retry. Each also records an Event |
| `galactic_edge_foundation_gate_closures_total` | counter | node | `edge_role`, `cause` | Open forwarding gates the agent closed, by cause. Each also records an Event on the route |
| `galactic_edge_foundation_cleanup_failures_total` | counter | node | `edge_role` | Host state releases and gate sweeps that failed and are retried |
| `galactic_edge_foundation_links` | gauge | node | none | Host links registered with the node's forwarding gate |
| `galactic_edge_foundation_gates` | gauge | node | `state` (`open`, `expired`, `closed`) | Gates on the node. `expired` passed its deadline before the agent closed it |
| `galactic_edge_foundation_gate_drops_total` | counter | node | `check` (`ingress`, `egress`, `deliver`) | Packets dropped on an edge link because no open gate covered the address |
Expand All @@ -34,6 +34,8 @@ objects behind a series with the Events and `kubectl` queries in
`NotReady` for `Ready`, whose own reason the provider writes.
- Route `state`: `Unclaimed`, `Pending`, `Accepted` (with the `Published`
condition's reason), `Published`, `Revoked` or `Released`.
- `edge_role`: `core` from `galactic-controller`, `node` from
`galactic-edge`.
- `stage`: `Admission`, `Allocation`, `Programming`, `Policy`, `Route`.
- `cause`: `Expired` (no renewal extended the gate), `Released`,
`Revoked`, `InstanceGone`, `AuthorityEnded`, `GrantCeiling`,
Expand Down
2 changes: 1 addition & 1 deletion internal/edge/agent/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ type fixture struct {
// metrics.
diag *diag.Recorder
events *diagtest.Recorder
metrics *prometheus.Registry
metrics prometheus.Gatherer
}

func newFixture(t *testing.T) *fixture {
Expand Down
4 changes: 2 additions & 2 deletions internal/edge/attachment/reconciler_envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type fixture struct {
vpc *cloudv1alpha1.VPC
// events and metrics are what the controller's diagnostics recorded.
events *diagtest.Recorder
metrics *prometheus.Registry
metrics prometheus.Gatherer
}

func startFixture(t *testing.T) *fixture {
Expand Down Expand Up @@ -675,7 +675,7 @@ func expectCorrelation(t *testing.T, e diagtest.Event, want map[string]string) {
}
}

func expectFailures(t *testing.T, reg *prometheus.Registry, stage, reason string, want float64) {
func expectFailures(t *testing.T, reg prometheus.Gatherer, stage, reason string, want float64) {
t.Helper()
families, err := reg.Gather()
if err != nil {
Expand Down
46 changes: 32 additions & 14 deletions internal/edge/diag/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/prometheus/client_golang/prometheus"
"go.miloapis.com/instrumentation/metrics"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -179,37 +180,54 @@ func Note(message string, c Correlation) string {
return message + correlationSeparator + corr
}

// Metrics are the foundation's failure counters. Each process registers its
// own.
// LabelRole is the constant label naming the role of the process emitting
// the failure counters: RoleCore or RoleNode.
const LabelRole = "edge_role"

// The process roles LabelRole names.
const (
// RoleCore is galactic-controller's Edge controllers.
RoleCore = "core"
// RoleNode is the galactic-edge node agent.
RoleNode = "node"
)

// Metrics are the foundation's failure counters.
type Metrics struct {
failures *prometheus.CounterVec
gateClosures *prometheus.CounterVec
cleanup prometheus.Counter
}

// NewMetrics registers the counters with reg. component is the emitting
// process's role, core or node, and becomes a constant label.
func NewMetrics(reg prometheus.Registerer, component string) *Metrics {
labels := prometheus.Labels{"component": component}
// NewMetrics creates the counters on the instrumentation library's registry.
// role is the emitting process's role, RoleCore or RoleNode, and becomes the
// LabelRole constant label. The registry keeps role's series across calls, so
// each call starts them over.
func NewMetrics(role string) *Metrics {
labels := prometheus.Labels{LabelRole: role}
// A vector with no labels, so it can be reset like the others.
cleanup := metrics.NewCounterVec(prometheus.CounterOpts{
Name: "galactic_edge_foundation_cleanup_failures_total",
Help: "Host state releases or gate sweeps that failed and are retried on the next pass.",
ConstLabels: labels,
}, nil)
m := &Metrics{
failures: prometheus.NewCounterVec(prometheus.CounterOpts{
failures: metrics.NewCounterVec(prometheus.CounterOpts{
Name: "galactic_edge_foundation_failures_total",
Help: "Transitions of an attachment, route or service into a failed lifecycle stage, by stage and " +
"condition reason. Counted once per transition, not per retry.",
ConstLabels: labels,
}, []string{"stage", labelReason}),
gateClosures: prometheus.NewCounterVec(prometheus.CounterOpts{
gateClosures: metrics.NewCounterVec(prometheus.CounterOpts{
Name: "galactic_edge_foundation_gate_closures_total",
Help: "Forwarding gates the node agent closed while they were open, by cause.",
ConstLabels: labels,
}, []string{"cause"}),
cleanup: prometheus.NewCounter(prometheus.CounterOpts{
Name: "galactic_edge_foundation_cleanup_failures_total",
Help: "Host state releases or gate sweeps that failed and are retried on the next pass.",
ConstLabels: labels,
}),
}
reg.MustRegister(m.failures, m.gateClosures, m.cleanup)
m.failures.Reset()
m.gateClosures.Reset()
cleanup.Reset()
m.cleanup = cleanup.WithLabelValues()
return m
}

Expand Down
41 changes: 36 additions & 5 deletions internal/edge/diag/diagtest/diagtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ package diagtest

import (
"fmt"
"slices"
"strings"
"sync"

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

"go.datum.net/galactic/internal/edge/diag"
)
Expand Down Expand Up @@ -78,10 +81,38 @@ func (r *Recorder) Find(action diag.Stage, reason string) []Event {
return out
}

// New returns a diag.Recorder recording into a new Recorder and counting
// into a new registry.
func New(component string) (*diag.Recorder, *Recorder, *prometheus.Registry) {
// New returns a diag.Recorder for role recording into a new Recorder and
// counting from zero, and a Gatherer reading back only those counts.
func New(role string) (*diag.Recorder, *Recorder, prometheus.Gatherer) {
events := &Recorder{}
reg := prometheus.NewRegistry()
return &diag.Recorder{Events: events, Metrics: diag.NewMetrics(reg, component)}, events, reg
rec := &diag.Recorder{Events: events, Metrics: diag.NewMetrics(role)}
return rec, events, prometheus.GathererFunc(func() ([]*dto.MetricFamily, error) {
families, err := ctrlmetrics.Registry.Gather()
var out []*dto.MetricFamily
for _, f := range families {
if !slices.Contains(foundationCounters, f.GetName()) {
continue
}
var mine []*dto.Metric
for _, m := range f.GetMetric() {
if slices.ContainsFunc(m.GetLabel(), func(l *dto.LabelPair) bool {
return l.GetName() == diag.LabelRole && l.GetValue() == role
}) {
mine = append(mine, m)
}
}
if len(mine) > 0 {
f.Metric = mine
out = append(out, f)
}
}
return out, err
})
}

// foundationCounters are the series diag.Metrics exports.
var foundationCounters = []string{
"galactic_edge_foundation_failures_total",
"galactic_edge_foundation_gate_closures_total",
"galactic_edge_foundation_cleanup_failures_total",
}
6 changes: 3 additions & 3 deletions internal/edge/diag/testdata/recorder.prom
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# HELP galactic_edge_foundation_failures_total Transitions of an attachment, route or service into a failed lifecycle stage, by stage and condition reason. Counted once per transition, not per retry.
# TYPE galactic_edge_foundation_failures_total counter
galactic_edge_foundation_failures_total{component="core",reason="UnknownProvider",stage="Admission"} 1
galactic_edge_foundation_failures_total{edge_role="core",reason="UnknownProvider",stage="Admission"} 1
# HELP galactic_edge_foundation_gate_closures_total Forwarding gates the node agent closed while they were open, by cause.
# TYPE galactic_edge_foundation_gate_closures_total counter
galactic_edge_foundation_gate_closures_total{cause="Expired",component="core"} 1
galactic_edge_foundation_gate_closures_total{cause="Expired",edge_role="core"} 1
# HELP galactic_edge_foundation_cleanup_failures_total Host state releases or gate sweeps that failed and are retried on the next pass.
# TYPE galactic_edge_foundation_cleanup_failures_total counter
galactic_edge_foundation_cleanup_failures_total{component="core"} 1
galactic_edge_foundation_cleanup_failures_total{edge_role="core"} 1
3 changes: 0 additions & 3 deletions internal/instrumentation/adoption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ var pending = []string{
// #1149: serve controller-runtime's registry through library metrics.
"cmd/fabric-api",
"cmd/fabric-router-controller",
"cmd/galactic-controller",
"cmd/galactic-edge",
// #1149: create metrics with the library's constructors instead of a
// registry parameter or a private registry.
"internal/edge/diag",
"internal/fabric/gateway",
"internal/fabric/node",
"internal/fabricconfig",
Expand Down
1 change: 1 addition & 0 deletions internal/instrumentation/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

// Every package that creates a metric through the library, so Lint sees
// it. TestLintLinksEveryMetric keeps this list complete.
_ "go.datum.net/galactic/internal/edge/diag"
_ "go.datum.net/galactic/internal/gateway"
_ "go.datum.net/galactic/internal/ingresssidecar"
_ "go.datum.net/galactic/internal/plumbing/ebpf/metrics"
Expand Down
Loading