Skip to content

Commit

Permalink
Register metrics in init
Browse files Browse the repository at this point in the history
  • Loading branch information
wongma7 committed Aug 23, 2017
1 parent ae998cb commit 1bf652a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
2 changes: 0 additions & 2 deletions pkg/controller/volume/persistentvolume/pv_controller_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 10 additions & 11 deletions pkg/volume/util/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,34 @@ 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",
},
[]string{"volume_plugin", "operation_name"},
)

var StorageOperationErrorMetric = prometheus.NewCounterVec(
var storageOperationErrorMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "storage_operation_errors_total",
Help: "Storage operation errors",
},
[]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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions pkg/volume/util/operationexecutor/operation_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ type OperationExecutor interface {
func NewOperationExecutor(
operationGenerator OperationGenerator) OperationExecutor {

util.RegisterMetrics()

return &operationExecutor{
pendingOperations: nestedpendingoperations.NewNestedPendingOperations(
true /* exponentialBackOffOnError */),
Expand Down

0 comments on commit 1bf652a

Please sign in to comment.