Merge pull request #74080 from dims/ensure-prometheus-metrics-are-not-registered-twice

Ensure prometheus metrics are not registered twice
pull/564/head
Kubernetes Prow Robot 2019-02-18 11:59:30 -08:00 committed by GitHub
commit fcaa726e60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -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) {
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)
})
} }

View File

@ -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 (
) )
) )
var registerOnce sync.Once
func registerMetrics() { func registerMetrics() {
prometheus.MustRegister(openstackOperationsLatency) registerOnce.Do(func() {
prometheus.MustRegister(openstackAPIRequestErrors) prometheus.MustRegister(openstackOperationsLatency)
prometheus.MustRegister(openstackAPIRequestErrors)
})
} }