github.com/lzy4123/fabric@v2.1.1+incompatible/common/metrics/statsd/provider.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package statsd 8 9 import ( 10 "github.com/go-kit/kit/metrics/statsd" 11 "github.com/hyperledger/fabric/common/metrics" 12 "github.com/hyperledger/fabric/common/metrics/internal/namer" 13 ) 14 15 const defaultFormat = "%{#fqname}" 16 17 type Provider struct { 18 Statsd *statsd.Statsd 19 } 20 21 func (p *Provider) NewCounter(o metrics.CounterOpts) metrics.Counter { 22 if o.StatsdFormat == "" { 23 o.StatsdFormat = defaultFormat 24 } 25 counter := &Counter{ 26 statsdProvider: p.Statsd, 27 namer: namer.NewCounterNamer(o), 28 } 29 30 if len(o.LabelNames) == 0 { 31 counter.Counter = p.Statsd.NewCounter(counter.namer.Format(), 1) 32 } 33 34 return counter 35 } 36 37 func (p *Provider) NewGauge(o metrics.GaugeOpts) metrics.Gauge { 38 if o.StatsdFormat == "" { 39 o.StatsdFormat = defaultFormat 40 } 41 gauge := &Gauge{ 42 statsdProvider: p.Statsd, 43 namer: namer.NewGaugeNamer(o), 44 } 45 46 if len(o.LabelNames) == 0 { 47 gauge.Gauge = p.Statsd.NewGauge(gauge.namer.Format()) 48 } 49 50 return gauge 51 } 52 53 func (p *Provider) NewHistogram(o metrics.HistogramOpts) metrics.Histogram { 54 if o.StatsdFormat == "" { 55 o.StatsdFormat = defaultFormat 56 } 57 histogram := &Histogram{ 58 statsdProvider: p.Statsd, 59 namer: namer.NewHistogramNamer(o), 60 } 61 62 if len(o.LabelNames) == 0 { 63 histogram.Timing = p.Statsd.NewTiming(histogram.namer.Format(), 1.0) 64 } 65 66 return histogram 67 } 68 69 type Counter struct { 70 Counter *statsd.Counter 71 namer *namer.Namer 72 statsdProvider *statsd.Statsd 73 } 74 75 func (c *Counter) Add(delta float64) { 76 if c.Counter == nil { 77 panic("label values must be provided by calling With") 78 } 79 c.Counter.Add(delta) 80 } 81 82 func (c *Counter) With(labelValues ...string) metrics.Counter { 83 name := c.namer.Format(labelValues...) 84 return &Counter{Counter: c.statsdProvider.NewCounter(name, 1)} 85 } 86 87 type Gauge struct { 88 Gauge *statsd.Gauge 89 namer *namer.Namer 90 statsdProvider *statsd.Statsd 91 } 92 93 func (g *Gauge) Add(delta float64) { 94 if g.Gauge == nil { 95 panic("label values must be provided by calling With") 96 } 97 g.Gauge.Add(delta) 98 } 99 100 func (g *Gauge) Set(value float64) { 101 if g.Gauge == nil { 102 panic("label values must be provided by calling With") 103 } 104 g.Gauge.Set(value) 105 } 106 107 func (g *Gauge) With(labelValues ...string) metrics.Gauge { 108 name := g.namer.Format(labelValues...) 109 return &Gauge{Gauge: g.statsdProvider.NewGauge(name)} 110 } 111 112 type Histogram struct { 113 Timing *statsd.Timing 114 namer *namer.Namer 115 statsdProvider *statsd.Statsd 116 } 117 118 func (h *Histogram) With(labelValues ...string) metrics.Histogram { 119 name := h.namer.Format(labelValues...) 120 return &Histogram{Timing: h.statsdProvider.NewTiming(name, 1)} 121 } 122 123 func (h *Histogram) Observe(value float64) { 124 if h.Timing == nil { 125 panic("label values must be provided by calling With") 126 } 127 h.Timing.Observe(value) 128 }