Codebase list golang-github-go-kit-kit / ab8826b
dogstatsd: Drop metrics according to sample rate (#884) Sample rate is for allowing to lower traffic generated by the statsd packets so the observations need to be dropped. statsd server will multiple the metrics with the inverse of sample rate to compenstate. Fixes #778 Martin Sucha authored 4 years ago Peter Bourgon committed 4 years ago
1 changed file(s) with 17 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1313 "context"
1414 "fmt"
1515 "io"
16 "math/rand"
1617 "strings"
1718 "sync"
1819 "sync/atomic"
7273 d.rates.Set(name, sampleRate)
7374 return &Counter{
7475 name: name,
75 obs: d.counters.Observe,
76 obs: sampleObservations(d.counters.Observe, sampleRate),
7677 }
7778 }
7879
9495 d.rates.Set(name, sampleRate)
9596 return &Timing{
9697 name: name,
97 obs: d.timings.Observe,
98 obs: sampleObservations(d.timings.Observe, sampleRate),
9899 }
99100 }
100101
104105 d.rates.Set(name, sampleRate)
105106 return &Histogram{
106107 name: name,
107 obs: d.histograms.Observe,
108 obs: sampleObservations(d.histograms.Observe, sampleRate),
108109 }
109110 }
110111
238239
239240 type observeFunc func(name string, lvs lv.LabelValues, value float64)
240241
242 // sampleObservations returns a modified observeFunc that samples observations.
243 func sampleObservations(obs observeFunc, sampleRate float64) observeFunc {
244 if sampleRate >= 1 {
245 return obs
246 }
247 return func(name string, lvs lv.LabelValues, value float64) {
248 if rand.Float64() > sampleRate {
249 return
250 }
251 obs(name, lvs, value)
252 }
253 }
254
241255 // Counter is a DogStatsD counter. Observations are forwarded to a Dogstatsd
242256 // object, and aggregated (summed) per timeseries.
243257 type Counter struct {