github.com/verrazzano/verrazzano@v1.7.0/application-operator/metricsexporter/metricsexporter.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package metricsexporter
     5  
     6  import (
     7  	"github.com/prometheus/client_golang/prometheus"
     8  	"go.uber.org/zap"
     9  )
    10  
    11  var (
    12  	MetricsExp           = metricsExporter{}
    13  	DefaultLabelFunction func(index int64) string
    14  	TestDelegate         = metricsDelegate{}
    15  )
    16  
    17  type metricsExporter struct {
    18  	internalConfig configuration
    19  	internalData   data
    20  }
    21  type configuration struct {
    22  	// This Metric array will be automatically populated with all the metrics from each map.
    23  	// Metrics not included in a map can be added to thisMetric array for registration.
    24  	allMetrics []prometheus.Collector
    25  	// This Metric map will be automatically populated with all metrics which were not registered correctly.
    26  	// Metrics in thisMetric map will be retried periodically.
    27  	failedMetrics map[prometheus.Collector]int
    28  	registry      prometheus.Registerer
    29  }
    30  type data struct {
    31  	simpleCounterMetricMap map[metricName]*SimpleCounterMetric
    32  	durationMetricMap      map[metricName]*DurationMetrics
    33  }
    34  type metricsDelegate struct {
    35  }
    36  
    37  // Counter Metrics
    38  type SimpleCounterMetric struct {
    39  	metric prometheus.Counter
    40  }
    41  
    42  func (c *SimpleCounterMetric) Inc(log *zap.SugaredLogger, err error) {
    43  	c.metric.Inc()
    44  
    45  }
    46  func (c *SimpleCounterMetric) Add(num float64) {
    47  	c.metric.Add(num)
    48  }
    49  func (c *SimpleCounterMetric) Get() prometheus.Counter {
    50  	return c.metric
    51  }
    52  
    53  // Duration Metrics
    54  type DurationMetrics struct {
    55  	timer  *prometheus.Timer
    56  	metric prometheus.Summary
    57  }
    58  
    59  // Creates a new timer, and starts the timer
    60  func (d *DurationMetrics) TimerStart() {
    61  	d.timer = prometheus.NewTimer(d.metric)
    62  }
    63  
    64  // Stops the timer and record the Duration since the last call to TimerStart
    65  func (d *DurationMetrics) TimerStop() {
    66  	d.timer.ObserveDuration()
    67  
    68  }