/
sampled.go
53 lines (43 loc) · 1.15 KB
/
sampled.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package metricsbp
import (
"github.com/reddit/baseplate.go/randbp"
"github.com/go-kit/kit/metrics"
)
// SampledCounter is a metrics.Counter implementation that actually sample the
// Add calls.
type SampledCounter struct {
Counter metrics.Counter
Rate float64
}
// With implements metrics.Counter.
func (c SampledCounter) With(tagValues ...string) metrics.Counter {
return SampledCounter{
Counter: c.Counter.With(tagValues...),
Rate: c.Rate,
}
}
// Add implements metrics.Counter.
func (c SampledCounter) Add(delta float64) {
if randbp.ShouldSampleWithRate(c.Rate) {
c.Counter.Add(delta)
}
}
// SampledHistogram is a metrics.Histogram implementation that actually sample
// the Observe calls.
type SampledHistogram struct {
Histogram metrics.Histogram
Rate float64
}
// With implements metrics.Histogram.
func (h SampledHistogram) With(labelValues ...string) metrics.Histogram {
return SampledHistogram{
Histogram: h.Histogram.With(labelValues...),
Rate: h.Rate,
}
}
// Observe implements metrics.Histogram.
func (h SampledHistogram) Observe(value float64) {
if randbp.ShouldSampleWithRate(h.Rate) {
h.Histogram.Observe(value)
}
}