Ensure prometheus metrics are not registered twice

Change-Id: I0f05fae65689b1e22c18d2c46dc5125780e81024
parent 07428f7e
...@@ -16,7 +16,11 @@ limitations under the License. ...@@ -16,7 +16,11 @@ limitations under the License.
package aws package aws
import "github.com/prometheus/client_golang/prometheus" import (
"sync"
"github.com/prometheus/client_golang/prometheus"
)
var ( var (
awsAPIMetric = prometheus.NewHistogramVec( awsAPIMetric = prometheus.NewHistogramVec(
...@@ -53,8 +57,12 @@ func recordAWSThrottlesMetric(operation string) { ...@@ -53,8 +57,12 @@ func recordAWSThrottlesMetric(operation string) {
awsAPIThrottlesMetric.With(prometheus.Labels{"operation_name": operation}).Inc() awsAPIThrottlesMetric.With(prometheus.Labels{"operation_name": operation}).Inc()
} }
var registerOnce sync.Once
func registerMetrics() { func registerMetrics() {
prometheus.MustRegister(awsAPIMetric) registerOnce.Do(func() {
prometheus.MustRegister(awsAPIErrorMetric) prometheus.MustRegister(awsAPIMetric)
prometheus.MustRegister(awsAPIThrottlesMetric) prometheus.MustRegister(awsAPIErrorMetric)
prometheus.MustRegister(awsAPIThrottlesMetric)
})
} }
...@@ -16,7 +16,11 @@ limitations under the License. ...@@ -16,7 +16,11 @@ limitations under the License.
package openstack package openstack
import "github.com/prometheus/client_golang/prometheus" import (
"sync"
"github.com/prometheus/client_golang/prometheus"
)
const ( const (
openstackSubsystem = "openstack" openstackSubsystem = "openstack"
...@@ -44,7 +48,11 @@ var ( ...@@ -44,7 +48,11 @@ var (
) )
) )
var registerOnce sync.Once
func registerMetrics() { func registerMetrics() {
prometheus.MustRegister(openstackOperationsLatency) registerOnce.Do(func() {
prometheus.MustRegister(openstackAPIRequestErrors) prometheus.MustRegister(openstackOperationsLatency)
prometheus.MustRegister(openstackAPIRequestErrors)
})
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment