github.xiaoq7.com/operator-framework/operator-sdk@v0.8.2/pkg/ansible/metrics/metrics.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metrics
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/prometheus/client_golang/prometheus"
    21  	"sigs.k8s.io/controller-runtime/pkg/metrics"
    22  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    23  )
    24  
    25  const (
    26  	subsystem = "ansible_operator"
    27  )
    28  
    29  var (
    30  	reconcileResults = prometheus.NewGaugeVec(
    31  		prometheus.GaugeOpts{
    32  			Subsystem: subsystem,
    33  			Name:      "reconcile_result",
    34  			Help:      "Gauge of reconciles and their results.",
    35  		},
    36  		[]string{
    37  			"GVK",
    38  			"result",
    39  		})
    40  
    41  	reconciles = prometheus.NewHistogramVec(
    42  		prometheus.HistogramOpts{
    43  			Subsystem: subsystem,
    44  			Name:      "reconciles",
    45  			Help:      "How long in seconds a reconcile takes.",
    46  		},
    47  		[]string{
    48  			"GVK",
    49  		})
    50  )
    51  
    52  func init() {
    53  	metrics.Registry.MustRegister(reconcileResults)
    54  	metrics.Registry.MustRegister(reconciles)
    55  }
    56  
    57  // We will never want to panic our app because of metric saving.
    58  // Therefore, we will recover our panics here and error log them
    59  // for later diagnosis but will never fail the app.
    60  func recoverMetricPanic() {
    61  	if r := recover(); r != nil {
    62  		logf.Log.WithName("metrics").Error(fmt.Errorf("%v", r),
    63  			"Recovering from metric function")
    64  	}
    65  }
    66  
    67  func ReconcileSucceeded(gvk string) {
    68  	defer recoverMetricPanic()
    69  	reconcileResults.WithLabelValues(gvk, "succeeded").Inc()
    70  }
    71  
    72  func ReconcileFailed(gvk string) {
    73  	// TODO: consider taking in a failure reason
    74  	defer recoverMetricPanic()
    75  	reconcileResults.WithLabelValues(gvk, "failed").Inc()
    76  }
    77  
    78  func ReconcileTimer(gvk string) *prometheus.Timer {
    79  	defer recoverMetricPanic()
    80  	return prometheus.NewTimer(prometheus.ObserverFunc(func(duration float64) {
    81  		reconciles.WithLabelValues(gvk).Observe(duration)
    82  	}))
    83  }