utils.go 439 B

1234567891011121314151617181920212223
  1. package statsd
  2. import (
  3. "math/rand"
  4. "sync"
  5. )
  6. func shouldSample(rate float64, r *rand.Rand, lock *sync.Mutex) bool {
  7. if rate >= 1 {
  8. return true
  9. }
  10. // sources created by rand.NewSource() (ie. w.random) are not thread safe.
  11. // TODO: use defer once the lowest Go version we support is 1.14 (defer
  12. // has an overhead before that).
  13. lock.Lock()
  14. if r.Float64() > rate {
  15. lock.Unlock()
  16. return false
  17. }
  18. lock.Unlock()
  19. return true
  20. }