Skip to content

Metrics Registration Fails Due to Empty allMetrics Slice #486

@Yashas-naidu

Description

@Yashas-naidu

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:

  1. Instantiate PortierisMetrics using NewMetrics().
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions