github.com/Jeffail/benthos/v3@v3.65.0/lib/metrics/wrap_flat.go (about) 1 package metrics 2 3 import ( 4 "github.com/Jeffail/benthos/v3/lib/log" 5 ) 6 7 //------------------------------------------------------------------------------ 8 9 // flatStat is a representation of a single metric stat. Interactions with this 10 // stat are thread safe. 11 type flatStat struct { 12 path string 13 f Flat 14 } 15 16 // Incr increments a metric by an amount. 17 func (f *flatStat) Incr(count int64) error { 18 f.f.Incr(f.path, count) 19 return nil 20 } 21 22 // Decr decrements a metric by an amount. 23 func (f *flatStat) Decr(count int64) error { 24 f.f.Decr(f.path, count) 25 return nil 26 } 27 28 // Timing sets a timing metric. 29 func (f *flatStat) Timing(delta int64) error { 30 f.f.Timing(f.path, delta) 31 return nil 32 } 33 34 // Set sets a gauge metric. 35 func (f *flatStat) Set(value int64) error { 36 f.f.Gauge(f.path, value) 37 return nil 38 } 39 40 //------------------------------------------------------------------------------ 41 42 // wrappedFlat implements the entire Type interface around a Flat type. 43 type wrappedFlat struct { 44 f Flat 45 } 46 47 // WrapFlat creates a Type around a Flat implementation. 48 func WrapFlat(f Flat) Type { 49 return &wrappedFlat{ 50 f: f, 51 } 52 } 53 54 //------------------------------------------------------------------------------ 55 56 // GetCounter returns a stat counter object for a path. 57 func (h *wrappedFlat) GetCounter(path string) StatCounter { 58 return &flatStat{ 59 path: path, 60 f: h.f, 61 } 62 } 63 64 // GetCounterVec returns a stat counter object for a path with the labels 65 // discarded. 66 func (h *wrappedFlat) GetCounterVec(path string, n []string) StatCounterVec { 67 return fakeCounterVec(func([]string) StatCounter { 68 return &flatStat{ 69 path: path, 70 f: h.f, 71 } 72 }) 73 } 74 75 // GetTimer returns a stat timer object for a path. 76 func (h *wrappedFlat) GetTimer(path string) StatTimer { 77 return &flatStat{ 78 path: path, 79 f: h.f, 80 } 81 } 82 83 // GetTimerVec returns a stat timer object for a path with the labels 84 // discarded. 85 func (h *wrappedFlat) GetTimerVec(path string, n []string) StatTimerVec { 86 return fakeTimerVec(func([]string) StatTimer { 87 return &flatStat{ 88 path: path, 89 f: h.f, 90 } 91 }) 92 } 93 94 // GetGauge returns a stat gauge object for a path. 95 func (h *wrappedFlat) GetGauge(path string) StatGauge { 96 return &flatStat{ 97 path: path, 98 f: h.f, 99 } 100 } 101 102 // GetGaugeVec returns a stat timer object for a path with the labels 103 // discarded. 104 func (h *wrappedFlat) GetGaugeVec(path string, n []string) StatGaugeVec { 105 return fakeGaugeVec(func([]string) StatGauge { 106 return &flatStat{ 107 path: path, 108 f: h.f, 109 } 110 }) 111 } 112 113 // SetLogger does nothing. 114 func (h *wrappedFlat) SetLogger(log log.Modular) { 115 } 116 117 // Close stops the wrappedFlat object from aggregating metrics and cleans up 118 // resources. 119 func (h *wrappedFlat) Close() error { 120 return h.f.Close() 121 } 122 123 //------------------------------------------------------------------------------