github.com/rudderlabs/rudder-go-kit@v0.30.0/stats/statsd_measurement.go (about) 1 package stats 2 3 import ( 4 "time" 5 6 "gopkg.in/alexcesaro/statsd.v2" 7 ) 8 9 // statsdMeasurement is the statsd-specific implementation of Measurement 10 type statsdMeasurement struct { 11 genericMeasurement 12 enabled bool 13 name string 14 client *statsdClient 15 } 16 17 // skip returns true if the stat should be skipped (stats disabled or client not ready) 18 // 19 // m.client.statsdMu.RLock should be held when calling this method. 20 func (m *statsdMeasurement) skip() bool { 21 return !m.enabled || !m.client.ready() 22 } 23 24 // statsdCounter represents a counter stat 25 type statsdCounter struct { 26 *statsdMeasurement 27 } 28 29 func (c *statsdCounter) Count(n int) { 30 c.client.statsdMu.RLock() 31 defer c.client.statsdMu.RUnlock() 32 if c.skip() { 33 return 34 } 35 c.client.statsd.Count(c.name, n) 36 } 37 38 // Increment increases the stat by 1. Is the Equivalent of Count(1). Only applies to CountType stats 39 func (c *statsdCounter) Increment() { 40 c.client.statsdMu.RLock() 41 defer c.client.statsdMu.RUnlock() 42 if c.skip() { 43 return 44 } 45 c.client.statsd.Increment(c.name) 46 } 47 48 // statsdGauge represents a gauge stat 49 type statsdGauge struct { 50 *statsdMeasurement 51 } 52 53 // Gauge records an absolute value for this stat. Only applies to GaugeType stats 54 func (g *statsdGauge) Gauge(value interface{}) { 55 g.client.statsdMu.RLock() 56 defer g.client.statsdMu.RUnlock() 57 if g.skip() { 58 return 59 } 60 g.client.statsd.Gauge(g.name, value) 61 } 62 63 // statsdTimer represents a timer stat 64 type statsdTimer struct { 65 *statsdMeasurement 66 timing *statsd.Timing 67 } 68 69 // Start starts a new timing for this stat. Only applies to TimerType stats 70 // Deprecated: Use concurrent safe SendTiming() instead 71 func (t *statsdTimer) Start() { 72 t.client.statsdMu.RLock() 73 defer t.client.statsdMu.RUnlock() 74 if t.skip() { 75 return 76 } 77 timing := t.client.statsd.NewTiming() 78 t.timing = &timing 79 } 80 81 // End send the time elapsed since the Start() call of this stat. Only applies to TimerType stats 82 // Deprecated: Use concurrent safe SendTiming() instead 83 func (t *statsdTimer) End() { 84 t.client.statsdMu.RLock() 85 defer t.client.statsdMu.RUnlock() 86 if t.skip() || t.timing == nil { 87 return 88 } 89 t.timing.Send(t.name) 90 } 91 92 // Since sends the time elapsed since duration start. Only applies to TimerType stats 93 func (t *statsdTimer) Since(start time.Time) { 94 t.SendTiming(time.Since(start)) 95 } 96 97 // SendTiming sends a timing for this stat. Only applies to TimerType stats 98 func (t *statsdTimer) SendTiming(duration time.Duration) { 99 t.client.statsdMu.RLock() 100 defer t.client.statsdMu.RUnlock() 101 if t.skip() { 102 return 103 } 104 t.client.statsd.Timing(t.name, int(duration/time.Millisecond)) 105 } 106 107 // RecordDuration records the duration of time between 108 // the call to this function and the execution of the function it returns. 109 // Only applies to TimerType stats 110 func (t *statsdTimer) RecordDuration() func() { 111 start := time.Now() 112 return func() { 113 t.Since(start) 114 } 115 } 116 117 // statsdHistogram represents a histogram stat 118 type statsdHistogram struct { 119 *statsdMeasurement 120 } 121 122 // Observe sends an observation 123 func (h *statsdHistogram) Observe(value float64) { 124 h.client.statsdMu.RLock() 125 defer h.client.statsdMu.RUnlock() 126 if h.skip() { 127 return 128 } 129 h.client.statsd.Histogram(h.name, value) 130 }