Skip to content

Commit 5fe4aab

Browse files
committed
Inject prometheus.Registerer into metrics factory for test isolation
1 parent 319552c commit 5fe4aab

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

node-core/components/metrics_factory.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@ import (
3434

3535
type MetricsFactoryInput struct {
3636
depinject.In
37-
Config *config.Config
38-
Logger *phuslu.Logger
37+
38+
Registerer promlib.Registerer
39+
Config *config.Config
40+
Logger *phuslu.Logger
41+
}
42+
43+
// ProvidePrometheusRegisterer provides a prometheus.Registerer for metrics registration.
44+
// Uses the default registerer which is also used by the prometheus HTTP handler.
45+
func ProvidePrometheusRegisterer() promlib.Registerer {
46+
return promlib.DefaultRegisterer
3947
}
4048

4149
// ProvideMetricsFactory provides a metrics factory based on configuration.
@@ -53,12 +61,13 @@ func ProvideMetricsFactory(in MetricsFactoryInput) metrics.Factory {
5361
// If we have any constant labels, create factory with labels
5462
if len(constLabels) > 0 {
5563
return prometheus.NewFactoryWithLabels(
64+
in.Registerer,
5665
in.Config.Telemetry.ServiceName,
5766
constLabels,
5867
)
5968
}
6069

61-
return prometheus.NewFactory(in.Config.Telemetry.ServiceName)
70+
return prometheus.NewFactory(in.Registerer, in.Config.Telemetry.ServiceName)
6271
}
6372

6473
// buildConstLabels builds constant labels from telemetry config.

node-core/components/metrics_providers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ func ProvideExecutionEngineMetrics(in ExecutionEngineMetricsInput) *engine.Metri
9797
// in component lists (defaults.go, components.go, etc.).
9898
func AllMetricsProviders() []any {
9999
return []any{
100-
ProvideMetricsFactory, // Must be first - creates factory used by all others
100+
ProvidePrometheusRegisterer, // Must be first - creates registerer used by factory
101+
ProvideMetricsFactory, // Must be second - creates factory used by all others
101102
ProvideStateDBMetrics,
102103
ProvideValidatorMetrics,
103104
ProvideExecutionClientMetrics,

observability/metrics/prometheus/prometheus.go

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,69 +26,80 @@ import (
2626
"github.com/prometheus/client_golang/prometheus"
2727
)
2828

29-
// Factory creates Prometheus metrics and registers them with prometheus.DefaultRegisterer.
29+
// Factory creates Prometheus metrics and registers them with the provided registerer.
3030
type Factory struct {
3131
namespace string
3232
constLabels prometheus.Labels
33+
registerer prometheus.Registerer
3334
}
3435

35-
// NewFactory creates a new Prometheus metrics factory with the given namespace.
36-
func NewFactory(namespace string) metrics.Factory {
36+
// NewFactory creates a new Prometheus metrics factory with the given registerer and namespace.
37+
func NewFactory(registerer prometheus.Registerer, namespace string) metrics.Factory {
3738
return &Factory{
3839
namespace: namespace,
3940
constLabels: nil,
41+
registerer: registerer,
4042
}
4143
}
4244

43-
// NewFactoryWithLabels creates a new Prometheus metrics factory with constant labels.
45+
// NewFactoryWithLabels creates a new Prometheus metrics factory with registerer, namespace, and constant labels.
4446
// Constant labels are applied to all metrics created by this factory.
45-
func NewFactoryWithLabels(namespace string, constLabels prometheus.Labels) metrics.Factory {
47+
func NewFactoryWithLabels(registerer prometheus.Registerer, namespace string, constLabels prometheus.Labels) metrics.Factory {
4648
return &Factory{
4749
namespace: namespace,
4850
constLabels: constLabels,
51+
registerer: registerer,
4952
}
5053
}
5154

52-
// NewCounter creates a new Counter that registers with prometheus.DefaultRegisterer.
55+
// NewCounter creates a new Counter that registers with the factory's registerer.
5356
func (f *Factory) NewCounter(opts metrics.CounterOpts, labelNames []string) metrics.Counter {
54-
return NewCounter(prometheus.CounterOpts{
57+
cv := prometheus.NewCounterVec(prometheus.CounterOpts{
5558
Namespace: f.namespace,
5659
Name: opts.Name,
5760
Help: opts.Help,
5861
ConstLabels: f.constLabels,
5962
}, labelNames)
63+
f.registerer.MustRegister(cv)
64+
return &counter{cv: cv}
6065
}
6166

62-
// NewGauge creates a new Gauge that registers with prometheus.DefaultRegisterer.
67+
// NewGauge creates a new Gauge that registers with the factory's registerer.
6368
func (f *Factory) NewGauge(opts metrics.GaugeOpts, labelNames []string) metrics.Gauge {
64-
return NewGauge(prometheus.GaugeOpts{
69+
gv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
6570
Namespace: f.namespace,
6671
Name: opts.Name,
6772
Help: opts.Help,
6873
ConstLabels: f.constLabels,
6974
}, labelNames)
75+
f.registerer.MustRegister(gv)
76+
return &gauge{gv: gv}
7077
}
7178

72-
// NewHistogram creates a new Histogram that registers with prometheus.DefaultRegisterer.
79+
// NewHistogram creates a new Histogram that registers with the factory's registerer.
7380
func (f *Factory) NewHistogram(opts metrics.HistogramOpts, labelNames []string) metrics.Histogram {
74-
return NewHistogram(prometheus.HistogramOpts{
81+
hv := prometheus.NewHistogramVec(prometheus.HistogramOpts{
7582
Namespace: f.namespace,
7683
Name: opts.Name,
7784
Help: opts.Help,
7885
Buckets: opts.Buckets,
7986
ConstLabels: f.constLabels,
8087
}, labelNames)
88+
f.registerer.MustRegister(hv)
89+
return &histogram{hv: hv}
8190
}
8291

83-
// NewSummary creates a new Summary that registers with prometheus.DefaultRegisterer.
92+
// NewSummary creates a new Summary that registers with the factory's registerer.
8493
func (f *Factory) NewSummary(opts metrics.SummaryOpts, labelNames []string) metrics.Summary {
85-
return NewSummary(prometheus.SummaryOpts{
94+
sv := prometheus.NewSummaryVec(prometheus.SummaryOpts{
8695
Namespace: f.namespace,
8796
Name: opts.Name,
8897
Help: opts.Help,
8998
Objectives: opts.Objectives,
9099
ConstLabels: f.constLabels,
91100
}, labelNames)
101+
f.registerer.MustRegister(sv)
102+
return &summary{sv: sv}
92103
}
93104

94105
// counter wraps a prometheus.CounterVec and implements the metrics.Counter interface.
@@ -97,10 +108,10 @@ type counter struct {
97108
lvs lv.LabelValues
98109
}
99110

100-
// NewCounter creates a new Counter that registers with prometheus.DefaultRegisterer.
101-
func NewCounter(opts prometheus.CounterOpts, labelNames []string) metrics.Counter {
111+
// NewCounter creates a new Counter that registers with the provided registerer.
112+
func NewCounter(registerer prometheus.Registerer, opts prometheus.CounterOpts, labelNames []string) metrics.Counter {
102113
cv := prometheus.NewCounterVec(opts, labelNames)
103-
prometheus.MustRegister(cv)
114+
registerer.MustRegister(cv)
104115
return &counter{cv: cv}
105116
}
106117

@@ -132,10 +143,10 @@ type gauge struct {
132143
lvs lv.LabelValues
133144
}
134145

135-
// NewGauge creates a new Gauge that registers with prometheus.DefaultRegisterer.
136-
func NewGauge(opts prometheus.GaugeOpts, labelNames []string) metrics.Gauge {
146+
// NewGauge creates a new Gauge that registers with the provided registerer.
147+
func NewGauge(registerer prometheus.Registerer, opts prometheus.GaugeOpts, labelNames []string) metrics.Gauge {
137148
gv := prometheus.NewGaugeVec(opts, labelNames)
138-
prometheus.MustRegister(gv)
149+
registerer.MustRegister(gv)
139150
return &gauge{gv: gv}
140151
}
141152

@@ -172,10 +183,10 @@ type histogram struct {
172183
lvs lv.LabelValues
173184
}
174185

175-
// NewHistogram creates a new Histogram that registers with prometheus.DefaultRegisterer.
176-
func NewHistogram(opts prometheus.HistogramOpts, labelNames []string) metrics.Histogram {
186+
// NewHistogram creates a new Histogram that registers with the provided registerer.
187+
func NewHistogram(registerer prometheus.Registerer, opts prometheus.HistogramOpts, labelNames []string) metrics.Histogram {
177188
hv := prometheus.NewHistogramVec(opts, labelNames)
178-
prometheus.MustRegister(hv)
189+
registerer.MustRegister(hv)
179190
return &histogram{hv: hv}
180191
}
181192

@@ -207,10 +218,10 @@ type summary struct {
207218
lvs lv.LabelValues
208219
}
209220

210-
// NewSummary creates a new Summary that registers with prometheus.DefaultRegisterer.
211-
func NewSummary(opts prometheus.SummaryOpts, labelNames []string) metrics.Summary {
221+
// NewSummary creates a new Summary that registers with the provided registerer.
222+
func NewSummary(registerer prometheus.Registerer, opts prometheus.SummaryOpts, labelNames []string) metrics.Summary {
212223
sv := prometheus.NewSummaryVec(opts, labelNames)
213-
prometheus.MustRegister(sv)
224+
registerer.MustRegister(sv)
214225
return &summary{sv: sv}
215226
}
216227

0 commit comments

Comments
 (0)