Skip to content

Commit

Permalink
dogstatsd: Drop metrics according to sample rate (#884)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
martin-sucha authored and peterbourgon committed Jun 14, 2019
1 parent aba2075 commit ab8826b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions metrics/dogstatsd/dogstatsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"context"
"fmt"
"io"
"math/rand"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (d *Dogstatsd) NewCounter(name string, sampleRate float64) *Counter {
d.rates.Set(name, sampleRate)
return &Counter{
name: name,
obs: d.counters.Observe,
obs: sampleObservations(d.counters.Observe, sampleRate),
}
}

Expand All @@ -95,7 +96,7 @@ func (d *Dogstatsd) NewTiming(name string, sampleRate float64) *Timing {
d.rates.Set(name, sampleRate)
return &Timing{
name: name,
obs: d.timings.Observe,
obs: sampleObservations(d.timings.Observe, sampleRate),
}
}

Expand All @@ -105,7 +106,7 @@ func (d *Dogstatsd) NewHistogram(name string, sampleRate float64) *Histogram {
d.rates.Set(name, sampleRate)
return &Histogram{
name: name,
obs: d.histograms.Observe,
obs: sampleObservations(d.histograms.Observe, sampleRate),
}
}

Expand Down Expand Up @@ -239,6 +240,19 @@ func (d *Dogstatsd) tagValues(labelValues []string) string {

type observeFunc func(name string, lvs lv.LabelValues, value float64)

// sampleObservations returns a modified observeFunc that samples observations.
func sampleObservations(obs observeFunc, sampleRate float64) observeFunc {
if sampleRate >= 1 {
return obs
}
return func(name string, lvs lv.LabelValues, value float64) {
if rand.Float64() > sampleRate {
return
}
obs(name, lvs, value)
}
}

// Counter is a DogStatsD counter. Observations are forwarded to a Dogstatsd
// object, and aggregated (summed) per timeseries.
type Counter struct {
Expand Down

0 comments on commit ab8826b

Please sign in to comment.