github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/promutils/labeled/gauge.go (about) 1 package labeled 2 3 import ( 4 "context" 5 6 "github.com/lyft/flytestdlib/contextutils" 7 "github.com/lyft/flytestdlib/promutils" 8 "github.com/prometheus/client_golang/prometheus" 9 ) 10 11 // Represents a gauge labeled with values from the context. See labeled.SetMetricsKeys for more information 12 type Gauge struct { 13 *prometheus.GaugeVec 14 15 prometheus.Gauge 16 additionalLabels []contextutils.Key 17 } 18 19 // Inc increments the gauge by 1. Use Add to increment by arbitrary values. The data point will be 20 // labeled with values from context. See labeled.SetMetricsKeys for information about to configure that. 21 func (g Gauge) Inc(ctx context.Context) { 22 gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...)) 23 if err != nil { 24 panic(err.Error()) 25 } 26 gauge.Inc() 27 28 if g.Gauge != nil { 29 g.Gauge.Inc() 30 } 31 } 32 33 // Add adds the given value to the Gauge. (The value can be negative, resulting in a decrease of the Gauge.) 34 // The data point will be labeled with values from context. See labeled.SetMetricsKeys for information about to configure that. 35 func (g Gauge) Add(ctx context.Context, v float64) { 36 gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...)) 37 if err != nil { 38 panic(err.Error()) 39 } 40 gauge.Add(v) 41 42 if g.Gauge != nil { 43 g.Gauge.Add(v) 44 } 45 } 46 47 // Set sets the Gauge to an arbitrary value. 48 // The data point will be labeled with values from context. See labeled.SetMetricsKeys for information about to configure that. 49 func (g Gauge) Set(ctx context.Context, v float64) { 50 gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...)) 51 if err != nil { 52 panic(err.Error()) 53 } 54 gauge.Set(v) 55 56 if g.Gauge != nil { 57 g.Gauge.Set(v) 58 } 59 } 60 61 // Dec decrements the level by 1. Use Sub to decrement by arbitrary values. The data point will be 62 // labeled with values from context. See labeled.SetMetricsKeys for information about to configure that. 63 func (g Gauge) Dec(ctx context.Context) { 64 gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...)) 65 if err != nil { 66 panic(err.Error()) 67 } 68 gauge.Dec() 69 70 if g.Gauge != nil { 71 g.Gauge.Dec() 72 } 73 } 74 75 // Sub adds the given value to the Gauge. The value can be negative, resulting in an increase of the Gauge. 76 // The data point will be labeled with values from context. See labeled.SetMetricsKeys for information about to configure that. 77 func (g Gauge) Sub(ctx context.Context, v float64) { 78 gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...)) 79 if err != nil { 80 panic(err.Error()) 81 } 82 gauge.Sub(v) 83 84 if g.Gauge != nil { 85 g.Gauge.Sub(v) 86 } 87 } 88 89 // SetToCurrentTime sets the Gauge to the current Unix time in seconds. 90 func (g Gauge) SetToCurrentTime(ctx context.Context) { 91 gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, metricKeys...)) 92 if err != nil { 93 panic(err.Error()) 94 } 95 gauge.SetToCurrentTime() 96 97 if g.Gauge != nil { 98 g.Gauge.SetToCurrentTime() 99 } 100 } 101 102 // Creates a new labeled gauge. Label keys must be set before instantiating. If the unlabeled option is given, 103 // this object will also instantiate and emit another gauge with the given name with an _unlabeled suffix. 104 // See labeled.SetMetricsKeys for information about to configure that. 105 func NewGauge(name, description string, scope promutils.Scope, opts ...MetricOption) Gauge { 106 if len(metricKeys) == 0 { 107 panic(ErrNeverSet) 108 } 109 110 g := Gauge{} 111 for _, opt := range opts { 112 if _, emitUnlabeledMetric := opt.(EmitUnlabeledMetricOption); emitUnlabeledMetric { 113 g.Gauge = scope.MustNewGauge(GetUnlabeledMetricName(name), description) 114 } else if additionalLabels, casted := opt.(AdditionalLabelsOption); casted { 115 g.GaugeVec = scope.MustNewGaugeVec(name, description, append(metricStringKeys, additionalLabels.Labels...)...) 116 g.additionalLabels = contextutils.MetricKeysFromStrings(additionalLabels.Labels) 117 } 118 } 119 120 if g.GaugeVec == nil { 121 g.GaugeVec = scope.MustNewGaugeVec(name, description, metricStringKeys...) 122 } 123 124 return g 125 }