resetting_sample.go 716 B

123456789101112131415161718192021222324
  1. package metrics
  2. // ResettingSample converts an ordinary sample into one that resets whenever its
  3. // snapshot is retrieved. This will break for multi-monitor systems, but when only
  4. // a single metric is being pushed out, this ensure that low-frequency events don't
  5. // skew th charts indefinitely.
  6. func ResettingSample(sample Sample) Sample {
  7. return &resettingSample{
  8. Sample: sample,
  9. }
  10. }
  11. // resettingSample is a simple wrapper around a sample that resets it upon the
  12. // snapshot retrieval.
  13. type resettingSample struct {
  14. Sample
  15. }
  16. // Snapshot returns a read-only copy of the sample with the original reset.
  17. func (rs *resettingSample) Snapshot() Sample {
  18. s := rs.Sample.Snapshot()
  19. rs.Sample.Clear()
  20. return s
  21. }