osbase/supervisor: use MustRegister for metrics registration
Change-Id: I4321c626f210bea025ab27bfecf783425f1482b5
Reviewed-on: https://review.monogon.dev/c/monogon/+/3703
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/metropolis/node/core/main.go b/metropolis/node/core/main.go
index 2a667d6..5faed17 100644
--- a/metropolis/node/core/main.go
+++ b/metropolis/node/core/main.go
@@ -245,12 +245,6 @@
return m.Run(ctx)
}
- pm, err := supervisor.NewMetricsPrometheus(metrics.CoreRegistry)
- if err != nil {
- // Fatal, because this generally shouldn't happen.
- logger.Fatalf("Failed to register supervisor metrics: %v", err)
- }
-
// Start the init function in a one-shot runnable. Smuggle out any errors from
// the init function and stuff them into the fatal channel. This is where the
// system supervisor takes over as the main process management system.
@@ -262,11 +256,11 @@
select {}
}
return nil
- }, supervisor.WithExistingLogtree(lt), supervisor.WithMetrics(pm))
+ }, supervisor.WithExistingLogtree(lt), supervisor.WithMetrics(supervisor.NewMetricsPrometheus(metrics.CoreRegistry)))
// Meanwhile, wait for any fatal error from the init process, and handle it
// accordingly.
- err = <-fatal
+ err := <-fatal
// Log error with primary logging mechanism still active.
logger.Infof("Node startup failed: %v", err)
// Start shutting down the supervision tree...
diff --git a/osbase/supervisor/BUILD.bazel b/osbase/supervisor/BUILD.bazel
index 9dfda5a..2fc6634 100644
--- a/osbase/supervisor/BUILD.bazel
+++ b/osbase/supervisor/BUILD.bazel
@@ -19,6 +19,7 @@
"//osbase/logtree",
"@com_github_cenkalti_backoff_v4//:backoff",
"@com_github_prometheus_client_golang//prometheus",
+ "@com_github_prometheus_client_golang//prometheus/promauto",
"@org_golang_google_grpc//:grpc",
],
)
diff --git a/osbase/supervisor/supervisor_metrics_prometheus.go b/osbase/supervisor/supervisor_metrics_prometheus.go
index 49ee973..9ae18a8 100644
--- a/osbase/supervisor/supervisor_metrics_prometheus.go
+++ b/osbase/supervisor/supervisor_metrics_prometheus.go
@@ -1,9 +1,8 @@
package supervisor
import (
- "fmt"
-
"github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promauto"
)
// MetricsPrometheus is a Metrics implementation which exports the supervisor
@@ -24,15 +23,16 @@
// and return a Metrics instance to be used with WithMetrics.
//
// This should only be called once for a given registry.
-func NewMetricsPrometheus(registry *prometheus.Registry) (*MetricsPrometheus, error) {
+func NewMetricsPrometheus(registry *prometheus.Registry) *MetricsPrometheus {
+ factory := promauto.With(registry)
res := &MetricsPrometheus{
- exportedState: prometheus.NewGaugeVec(prometheus.GaugeOpts{
+ exportedState: factory.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "monogon",
Subsystem: "supervisor",
Name: "dn_state_total",
Help: "Total count of supervisor runnables, broken up by DN and state",
}, []string{"dn", "state"}),
- exportedEdge: prometheus.NewCounterVec(prometheus.CounterOpts{
+ exportedEdge: factory.NewCounterVec(prometheus.CounterOpts{
Namespace: "monogon",
Subsystem: "supervisor",
Name: "dn_state_transition_count",
@@ -41,13 +41,7 @@
}, []string{"dn", "old_state", "new_state"}),
cachedState: make(map[string]*NodeState),
}
- if err := registry.Register(res.exportedState); err != nil {
- return nil, fmt.Errorf("when registering dn_state_total: %w", err)
- }
- if err := registry.Register(res.exportedEdge); err != nil {
- return nil, fmt.Errorf("when registering dn_state_transition_count: %w", err)
- }
- return res, nil
+ return res
}
func (m *MetricsPrometheus) exportState(dn string, state NodeState, value float64) {