|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | +) |
| 25 | + |
| 26 | +var StorageOperationMetric = prometheus.NewHistogramVec( |
| 27 | + prometheus.HistogramOpts{ |
| 28 | + Name: "storage_operation_duration_seconds", |
| 29 | + Help: "Storage operation duration", |
| 30 | + }, |
| 31 | + []string{"volume_plugin", "operation_name"}, |
| 32 | +) |
| 33 | + |
| 34 | +var StorageOperationErrorMetric = prometheus.NewCounterVec( |
| 35 | + prometheus.CounterOpts{ |
| 36 | + Name: "storage_operation_errors_total", |
| 37 | + Help: "Storage operation errors", |
| 38 | + }, |
| 39 | + []string{"volume_plugin", "operation_name"}, |
| 40 | +) |
| 41 | + |
| 42 | +var registerMetrics sync.Once |
| 43 | + |
| 44 | +func RegisterMetrics() { |
| 45 | + registerMetrics.Do(func() { |
| 46 | + prometheus.MustRegister(StorageOperationMetric) |
| 47 | + prometheus.MustRegister(StorageOperationErrorMetric) |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +// OperationCompleteHook returns a hook to call when an operation is completed |
| 52 | +func OperationCompleteHook(plugin, operationName string) func(error) { |
| 53 | + requestTime := time.Now() |
| 54 | + opComplete := func(err error) { |
| 55 | + timeTaken := time.Since(requestTime).Seconds() |
| 56 | + // Create metric with operation name and plugin name |
| 57 | + if err != nil { |
| 58 | + StorageOperationErrorMetric.WithLabelValues(plugin, operationName).Inc() |
| 59 | + } else { |
| 60 | + StorageOperationMetric.WithLabelValues(plugin, operationName).Observe(timeTaken) |
| 61 | + } |
| 62 | + } |
| 63 | + return opComplete |
| 64 | +} |
0 commit comments