Repository: [IBM/Portieris]
Issue Description:
In the NewMetrics function of metrics.go, the call to prometheus.MustRegister(p.allMetrics...) happens before any metrics are added to allMetrics. Since allMetrics is initially empty, this can cause a panic or unexpected behavior.
Steps to Reproduce:
- Instantiate
PortierisMetrics using NewMetrics().
- Observe a runtime error if
allMetrics is empty at registration time.
Proposed Fix:
Register each metric immediately upon creation in the counter method instead of registering them in NewMetrics().
func (p *PortierisMetrics) counter(name, help string) prometheus.Counter {
result := prometheus.NewCounter(prometheus.CounterOpts{
Name: metricName(name),
Help: metricHelp(help),
})
prometheus.MustRegister(result) // Register the metric immediately
p.allMetrics = append(p.allMetrics, result)
return result
}
Expected Behavior After Fix:
- Each metric gets registered as soon as it is created, avoiding any issues with an empty
allMetrics slice.
NewMetrics() initializes correctly without errors.
Additional Context:
This issue aligns with Prometheus best practices, ensuring that metrics are registered as they are defined rather than in bulk.
Repository: [IBM/Portieris]
Issue Description:
In the
NewMetricsfunction ofmetrics.go, the call toprometheus.MustRegister(p.allMetrics...)happens before any metrics are added toallMetrics. SinceallMetricsis initially empty, this can cause a panic or unexpected behavior.Steps to Reproduce:
PortierisMetricsusingNewMetrics().allMetricsis empty at registration time.Proposed Fix:
Register each metric immediately upon creation in the
countermethod instead of registering them inNewMetrics().Expected Behavior After Fix:
allMetricsslice.NewMetrics()initializes correctly without errors.Additional Context:
This issue aligns with Prometheus best practices, ensuring that metrics are registered as they are defined rather than in bulk.