github.com/Jeffail/benthos/v3@v3.65.0/public/service/metrics.go (about) 1 package service 2 3 import ( 4 "github.com/Jeffail/benthos/v3/lib/metrics" 5 ) 6 7 // Metrics allows plugin authors to emit custom metrics from components that are 8 // exported the same way as native Benthos metrics. It's safe to pass around a 9 // nil pointer for testing components. 10 type Metrics struct { 11 t metrics.Type 12 } 13 14 func newReverseAirGapMetrics(t metrics.Type) *Metrics { 15 return &Metrics{t} 16 } 17 18 // NewCounter creates a new counter metric with a name and variant list of label 19 // keys. 20 func (m *Metrics) NewCounter(name string, labelKeys ...string) *MetricCounter { 21 if m == nil { 22 return nil 23 } 24 cv := m.t.GetCounterVec(name, labelKeys) 25 return &MetricCounter{cv} 26 } 27 28 // NewTimer creates a new timer metric with a name and variant list of label 29 // keys. 30 func (m *Metrics) NewTimer(name string, labelKeys ...string) *MetricTimer { 31 if m == nil { 32 return nil 33 } 34 tv := m.t.GetTimerVec(name, labelKeys) 35 return &MetricTimer{tv} 36 } 37 38 // NewGauge creates a new gauge metric with a name and variant list of label 39 // keys. 40 func (m *Metrics) NewGauge(name string, labelKeys ...string) *MetricGauge { 41 if m == nil { 42 return nil 43 } 44 gv := m.t.GetGaugeVec(name, labelKeys) 45 return &MetricGauge{gv} 46 } 47 48 //------------------------------------------------------------------------------ 49 50 // MetricCounter represents a counter metric of a given name and labels. 51 type MetricCounter struct { 52 cv metrics.StatCounterVec 53 } 54 55 // Incr increments a counter metric by an amount, the number of label values 56 // must match the number and order of labels specified when the counter was 57 // created. 58 func (c *MetricCounter) Incr(count int64, labelValues ...string) { 59 if c == nil { 60 return 61 } 62 _ = c.cv.With(labelValues...).Incr(count) 63 } 64 65 // MetricTimer represents a timing metric of a given name and labels. 66 type MetricTimer struct { 67 tv metrics.StatTimerVec 68 } 69 70 // Timing adds a delta to a timing metric, the number of label values must match 71 // the number and order of labels specified when the timing was created. 72 func (t *MetricTimer) Timing(delta int64, labelValues ...string) { 73 if t == nil { 74 return 75 } 76 _ = t.tv.With(labelValues...).Timing(delta) 77 } 78 79 // MetricGauge represents a gauge metric of a given name and labels. 80 type MetricGauge struct { 81 gv metrics.StatGaugeVec 82 } 83 84 // Set a gauge metric, the number of label values must match the number and 85 // order of labels specified when the gauge was created. 86 func (g *MetricGauge) Set(value int64, labelValues ...string) { 87 if g == nil { 88 return 89 } 90 _ = g.gv.With(labelValues...).Set(value) 91 }