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
30 changes: 1 addition & 29 deletions cmd/galactic-router/bmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,10 @@ package main
import (
"fmt"

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

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

// bmpStationUp reports, per BGPRouter and BMP station, whether the router's
// session to that collector is established. A station that is configured but
// down, or whose host does not resolve, reads 0, so an alert can tell a
// collector this router has lost apart from one it was never told about.
var bmpStationUp = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "galactic_router_bmp_station_up",
Help: "Whether the router's BMP session to a collector is established (1) or not (0).",
}, []string{"router", "station"})

func init() {
ctrlmetrics.Registry.MustRegister(bmpStationUp)
}

// bmpGaugeObserver records each BMP station's state in bmpStationUp.
type bmpGaugeObserver struct{}

// BMPStationState sets the station's gauge to 1 when up and 0 otherwise.
func (bmpGaugeObserver) BMPStationState(router, station string, up bool) {
v := 0.0
if up {
v = 1
}
bmpStationUp.WithLabelValues(router, station).Set(v)
}

// bmpConfigFromRouter builds the runtime's BMP configuration from cfg, naming
// the router nodeName in each collector's Initiation message. It returns the
// zero BMPConfig, which disables BMP, when no stations are configured, and an
Expand All @@ -64,6 +36,6 @@ func bmpConfigFromRouter(cfg *config.RouterConfig, nodeName string) (gobgp.BMPCo
Policy: policy,
StatisticsInterval: cfg.BMPStatisticsInterval,
SysName: nodeName,
Observer: bmpGaugeObserver{},
Observer: gobgp.BMPStationGauge{},
}, nil
}
7 changes: 2 additions & 5 deletions cmd/galactic-router/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/spf13/cobra"
"go.miloapis.com/instrumentation/metrics"
"google.golang.org/grpc"
grpchealth "google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
Expand All @@ -24,7 +25,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"

cloudv1alpha1 "go.datum.net/galactic/api/cloud/v1alpha1"
Expand Down Expand Up @@ -120,7 +120,6 @@ func runCmd(cfg *config.RouterConfig) error {
return fmt.Errorf("create manager: %w", err)
}
serviceRouteMetrics := serviceroute.NewMetrics()
serviceRouteMetrics.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 Expand Up @@ -459,9 +458,7 @@ func setupTrafficPolicyController(mgr ctrl.Manager, nodeName string) error {
if err := rec.SetupWithManager(mgr); err != nil {
return fmt.Errorf("setup traffic policy controller: %w", err)
}
if err := ctrlmetrics.Registry.Register(rec); err != nil {
return fmt.Errorf("register traffic policy metrics: %w", err)
}
metrics.MustRegister(rec)
return nil
}

Expand Down
11 changes: 8 additions & 3 deletions cmd/galactic-router/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/serviceroute"
)

// 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 the service route metrics run
// creates at startup exist, and lints them. The traffic policy collector
// reads pinned eBPF maps, so it is left out.
func TestMetricsScrape(t *testing.T) {
serviceroute.NewMetrics()
instrumenttest.AssertScrapes(t, metrics.Handler())
instrumenttest.Lint(t)
}
2 changes: 0 additions & 2 deletions internal/instrumentation/adoption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var pending = []string{
"cmd/galactic-edge",
"cmd/galactic-gateway",
"cmd/galactic-nat",
"cmd/galactic-router",
"cmd/galactic-vrf",
// #1149: create metrics with the library's constructors instead of a
// registry parameter or a private registry.
Expand All @@ -46,7 +45,6 @@ var pending = []string{
"internal/gateway",
"internal/ingresssidecar",
"internal/plumbing/ebpf/xdpdispatch",
"internal/serviceroute",
}

// TestAdopted runs the instrumentation library's adoption check over the
Expand Down
5 changes: 4 additions & 1 deletion internal/instrumentation/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "go.datum.net/galactic/internal/plumbing/ebpf/metrics"
_ "go.datum.net/galactic/internal/plumbing/nldump"
_ "go.datum.net/galactic/internal/runtime/gobgp"
_ "go.datum.net/galactic/internal/serviceroute"
)

const (
Expand All @@ -44,8 +45,10 @@ func TestLintLinksEveryMetric(t *testing.T) {
linked := blankImports(t, "lint_test.go")
fset := token.NewFileSet()
walkSource(t, func(rel string, f *ast.File) {
// metrics.MustRegister doesn't count: Lint checks only the metrics
// the library's constructors create, never a custom collector.
creates := slices.ContainsFunc(selectors(f, metricsPath), func(sel *ast.SelectorExpr) bool {
return strings.HasPrefix(sel.Sel.Name, "New") || sel.Sel.Name == "MustRegister"
return strings.HasPrefix(sel.Sel.Name, "New")
})
if !creates {
return
Expand Down
24 changes: 24 additions & 0 deletions internal/runtime/gobgp/bmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

api "github.com/osrg/gobgp/v4/api"
gobgpserver "github.com/osrg/gobgp/v4/pkg/server"
"github.com/prometheus/client_golang/prometheus"
"go.miloapis.com/instrumentation/metrics"
)

const (
Expand Down Expand Up @@ -69,6 +71,28 @@ func (s BMPStation) String() string {
return net.JoinHostPort(s.Host, strconv.Itoa(int(s.Port)))
}

// bmpStationUp reports, per BGPRouter and BMP station, whether the router's
// session to that collector is established. A station that is configured but
// down, or whose host does not resolve, reads 0, so an alert can tell a
// collector this router has lost apart from one it was never told about.
var bmpStationUp = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "galactic_router_bmp_station_up",
Help: "Whether the router's BMP session to a collector is established (1) or not (0).",
}, []string{labelRouter, "station"})

// BMPStationGauge is a BMPStateObserver recording each station's state in
// galactic_router_bmp_station_up.
type BMPStationGauge struct{}

// BMPStationState sets the station's gauge to 1 when up and 0 otherwise.
func (BMPStationGauge) BMPStationState(router, station string, up bool) {
v := 0.0
if up {
v = 1
}
bmpStationUp.WithLabelValues(router, station).Set(v)
}

// BMPStateObserver is notified of each BMP station's session state every time
// the keeper checks it. router is the BGPRouter key of the runtime streaming to
// station; up reports whether its TCP session to the collector is established.
Expand Down
20 changes: 8 additions & 12 deletions internal/serviceroute/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"
)

const metricsNamespace = "galactic_service_route"
Expand All @@ -23,33 +24,33 @@ type Metrics struct {
producers *prometheus.GaugeVec
}

// NewMetrics creates the service-route metrics. Call MustRegister once during
// process startup.
// NewMetrics creates the service-route metrics on the instrumentation
// library's registry.
func NewMetrics() *Metrics {
reconcileTime := prometheus.NewHistogram(prometheus.HistogramOpts{
reconcileTime := metrics.NewHistogram(prometheus.HistogramOpts{
Namespace: metricsNamespace,
Name: "reconcile_duration_seconds",
Help: "Time spent reconciling a ServiceRoutePolicy.",
Buckets: prometheus.DefBuckets,
})
return &Metrics{
reconciliations: prometheus.NewCounterVec(prometheus.CounterOpts{
reconciliations: metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "reconciliations_total",
Help: "ServiceRoutePolicy reconciliations by outcome.",
}, []string{"result"}),
reconcileTime: reconcileTime,
operations: prometheus.NewCounterVec(prometheus.CounterOpts{
operations: metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "route_operations_total",
Help: "Service route dataplane operations by operation and outcome.",
}, []string{"operation", "result"}),
programmed: prometheus.NewGauge(prometheus.GaugeOpts{
programmed: metrics.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "programmed_routes",
Help: "Number of attachment-scoped eBPF service paths currently tracked by the controller.",
}),
producers: prometheus.NewGaugeVec(prometheus.GaugeOpts{
producers: metrics.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "producers",
Help: "Producers of Spread ServiceEndpoints on this node, by health state. " +
Expand All @@ -58,11 +59,6 @@ func NewMetrics() *Metrics {
}
}

// MustRegister registers all metrics owned by this package.
func (m *Metrics) MustRegister(reg prometheus.Registerer) {
reg.MustRegister(m.reconciliations, m.reconcileTime, m.operations, m.programmed, m.producers)
}

func (m *Metrics) ObserveReconcile(result string, duration time.Duration) {
if m == nil {
return
Expand Down
Loading