From 1bf652a5c7153767014dcbfdd7c6c2f8852f1bfa Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Wed, 23 Aug 2017 10:41:30 -0400 Subject: [PATCH] Register metrics in init --- .../persistentvolume/pv_controller_base.go | 2 -- pkg/volume/util/metrics.go | 21 +++++++++---------- .../operationexecutor/operation_executor.go | 2 -- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/pkg/controller/volume/persistentvolume/pv_controller_base.go b/pkg/controller/volume/persistentvolume/pv_controller_base.go index c4342bb8532f5..db24073055a45 100644 --- a/pkg/controller/volume/persistentvolume/pv_controller_base.go +++ b/pkg/controller/volume/persistentvolume/pv_controller_base.go @@ -75,8 +75,6 @@ func NewController(p ControllerParameters) (*PersistentVolumeController, error) eventRecorder = broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "persistentvolume-controller"}) } - util.RegisterMetrics() - controller := &PersistentVolumeController{ volumes: newPersistentVolumeOrderedIndex(), claims: cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc), diff --git a/pkg/volume/util/metrics.go b/pkg/volume/util/metrics.go index d3013b634d9e4..087bbfff41691 100644 --- a/pkg/volume/util/metrics.go +++ b/pkg/volume/util/metrics.go @@ -17,13 +17,12 @@ limitations under the License. package util import ( - "sync" "time" "github.com/prometheus/client_golang/prometheus" ) -var StorageOperationMetric = prometheus.NewHistogramVec( +var storageOperationMetric = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Name: "storage_operation_duration_seconds", Help: "Storage operation duration", @@ -31,7 +30,7 @@ var StorageOperationMetric = prometheus.NewHistogramVec( []string{"volume_plugin", "operation_name"}, ) -var StorageOperationErrorMetric = prometheus.NewCounterVec( +var storageOperationErrorMetric = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "storage_operation_errors_total", Help: "Storage operation errors", @@ -39,13 +38,13 @@ var StorageOperationErrorMetric = prometheus.NewCounterVec( []string{"volume_plugin", "operation_name"}, ) -var registerMetrics sync.Once +func init() { + registerMetrics() +} -func RegisterMetrics() { - registerMetrics.Do(func() { - prometheus.MustRegister(StorageOperationMetric) - prometheus.MustRegister(StorageOperationErrorMetric) - }) +func registerMetrics() { + prometheus.MustRegister(storageOperationMetric) + prometheus.MustRegister(storageOperationErrorMetric) } // OperationCompleteHook returns a hook to call when an operation is completed @@ -55,9 +54,9 @@ func OperationCompleteHook(plugin, operationName string) func(error) { timeTaken := time.Since(requestTime).Seconds() // Create metric with operation name and plugin name if err != nil { - StorageOperationErrorMetric.WithLabelValues(plugin, operationName).Inc() + storageOperationErrorMetric.WithLabelValues(plugin, operationName).Inc() } else { - StorageOperationMetric.WithLabelValues(plugin, operationName).Observe(timeTaken) + storageOperationMetric.WithLabelValues(plugin, operationName).Observe(timeTaken) } } return opComplete diff --git a/pkg/volume/util/operationexecutor/operation_executor.go b/pkg/volume/util/operationexecutor/operation_executor.go index 93d1033b10a89..21e8eecbdc740 100644 --- a/pkg/volume/util/operationexecutor/operation_executor.go +++ b/pkg/volume/util/operationexecutor/operation_executor.go @@ -125,8 +125,6 @@ type OperationExecutor interface { func NewOperationExecutor( operationGenerator OperationGenerator) OperationExecutor { - util.RegisterMetrics() - return &operationExecutor{ pendingOperations: nestedpendingoperations.NewNestedPendingOperations( true /* exponentialBackOffOnError */),