github.com/verrazzano/verrazzano@v1.7.1/platform-operator/metricsexporter/metricsexporter.go (about) 1 // Copyright (c) 2022, 2023, 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 spi "github.com/verrazzano/verrazzano/pkg/controller/errors" 9 ) 10 11 type MetricsExporter struct { 12 internalConfig configuration 13 internalData data 14 } 15 16 // The alMetrics array will be automatically populated with all the metrics from each map. Metrics not included in a map can be added to thisMetric array for registration. 17 // The failedMetrics map will be automatically populated with all metrics which were not registered correctly. Metrics in thisMetric map will be retried periodically. 18 type configuration struct { 19 allMetrics []prometheus.Collector 20 failedMetrics map[prometheus.Collector]int 21 registry prometheus.Registerer 22 } 23 24 type data struct { 25 simpleCounterMetricMap map[metricName]*SimpleCounterMetric 26 simpleGaugeMetricMap map[metricName]*SimpleGaugeMetric 27 durationMetricMap map[metricName]*DurationMetric 28 componentHealth *ComponentHealth 29 componentInstallDuration *ComponentInstallDuration 30 componentUpgradeDuration *ComponentUpgradeDuration 31 } 32 type SimpleCounterMetric struct { 33 metric prometheus.Counter 34 } 35 36 // This member function increases a simpleCounterMetric by one 37 func (c *SimpleCounterMetric) Inc() { 38 c.metric.Inc() 39 } 40 41 // This member function increases a simpleCounterMetric by a user provided float64 number 42 func (c *SimpleCounterMetric) Add(num float64) { 43 c.metric.Add(num) 44 } 45 46 // This member function returns the underlying metric in a simpleCounterMetric 47 func (c *SimpleCounterMetric) Get() prometheus.Counter { 48 return c.metric 49 } 50 51 type SimpleGaugeMetric struct { 52 metric prometheus.Gauge 53 } 54 55 // This member function sets a SimpleGaugeMetric to a user provided float64 number 56 func (g *SimpleGaugeMetric) Set(num float64) { 57 g.metric.Set(num) 58 } 59 60 // This member function sets a SimpleGaugeMetric to the current time 61 func (g *SimpleGaugeMetric) SetToCurrentTime() { 62 g.metric.SetToCurrentTime() 63 } 64 65 // This member function increases a SimpleGaugeMetric by a user provided float64 number 66 func (g *SimpleGaugeMetric) Add(num float64) { 67 g.metric.Add(num) 68 } 69 70 // This member function returns the underlying metric in a simpleGaugeMetric 71 func (g *SimpleGaugeMetric) Get() prometheus.Gauge { 72 return g.metric 73 } 74 75 type DurationMetric struct { 76 metric prometheus.Summary 77 timer *prometheus.Timer 78 } 79 80 // This function creates a new timer, and starts the timer 81 func (d *DurationMetric) TimerStart() { 82 d.timer = prometheus.NewTimer(d.metric) 83 } 84 85 // This function stops the timer and record the Duration since the last call to TimerStart 86 func (d *DurationMetric) TimerStop() { 87 d.timer.ObserveDuration() 88 } 89 90 type ComponentHealth struct { 91 available *prometheus.GaugeVec 92 } 93 94 type ComponentInstallDuration struct { 95 installDuration *prometheus.GaugeVec 96 } 97 98 type ComponentUpgradeDuration struct { 99 upgradeDuration *prometheus.GaugeVec 100 } 101 102 // This member function returns the simpleGaugeMetric that holds the upgrade time for a component 103 func (c *ComponentHealth) SetComponentHealth(JSONname string, availability bool, isEnabled bool) (prometheus.Gauge, error) { 104 //isEnabled : true => 0, isEnabled : false => -1 105 enabledVal := -1 106 if isEnabled { 107 enabledVal = 0 108 } 109 //availability : true => 1, availability : false => 0 110 availableVal := 0 111 if availability { 112 availableVal = 1 113 } 114 //setting : enabled and available => 1, enabled and unavailable => 0, disabled = > -1 115 setting := enabledVal + availableVal 116 metric, err := c.available.GetMetricWithLabelValues(JSONname) 117 if err != nil { 118 return nil, err 119 } 120 metric.Set(float64(setting)) 121 return metric, nil 122 } 123 func IsMetricError(err error) bool { 124 if spi.IsUpdateConflict(err) || spi.IsRetryableError(err) { 125 return false 126 } 127 return true 128 }