From 8fa332cce3cdf3e510c18ffe9682b920422bbd28 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 14 Feb 2019 09:01:14 -0500 Subject: [PATCH] Ensure prometheus metrics are not registered twice Change-Id: I0f05fae65689b1e22c18d2c46dc5125780e81024 --- pkg/cloudprovider/providers/aws/aws_metrics.go | 16 ++++++++++++---- .../providers/openstack/openstack_metrics.go | 14 +++++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/cloudprovider/providers/aws/aws_metrics.go b/pkg/cloudprovider/providers/aws/aws_metrics.go index aa434d9260..bb7958f04f 100644 --- a/pkg/cloudprovider/providers/aws/aws_metrics.go +++ b/pkg/cloudprovider/providers/aws/aws_metrics.go @@ -16,7 +16,11 @@ limitations under the License. package aws -import "github.com/prometheus/client_golang/prometheus" +import ( + "sync" + + "github.com/prometheus/client_golang/prometheus" +) var ( awsAPIMetric = prometheus.NewHistogramVec( @@ -53,8 +57,12 @@ func recordAWSThrottlesMetric(operation string) { awsAPIThrottlesMetric.With(prometheus.Labels{"operation_name": operation}).Inc() } +var registerOnce sync.Once + func registerMetrics() { - prometheus.MustRegister(awsAPIMetric) - prometheus.MustRegister(awsAPIErrorMetric) - prometheus.MustRegister(awsAPIThrottlesMetric) + registerOnce.Do(func() { + prometheus.MustRegister(awsAPIMetric) + prometheus.MustRegister(awsAPIErrorMetric) + prometheus.MustRegister(awsAPIThrottlesMetric) + }) } diff --git a/pkg/cloudprovider/providers/openstack/openstack_metrics.go b/pkg/cloudprovider/providers/openstack/openstack_metrics.go index ab8f9b6870..d24d657352 100644 --- a/pkg/cloudprovider/providers/openstack/openstack_metrics.go +++ b/pkg/cloudprovider/providers/openstack/openstack_metrics.go @@ -16,7 +16,11 @@ limitations under the License. package openstack -import "github.com/prometheus/client_golang/prometheus" +import ( + "sync" + + "github.com/prometheus/client_golang/prometheus" +) const ( openstackSubsystem = "openstack" @@ -44,7 +48,11 @@ var ( ) ) +var registerOnce sync.Once + func registerMetrics() { - prometheus.MustRegister(openstackOperationsLatency) - prometheus.MustRegister(openstackAPIRequestErrors) + registerOnce.Do(func() { + prometheus.MustRegister(openstackOperationsLatency) + prometheus.MustRegister(openstackAPIRequestErrors) + }) }