Skip to content

Commit bb963bf

Browse files
committed
fix: always reset all metrics before writing them when calling WriteTo
This prevents the Dogstatsd client to consume an ever-increasing amount of memory when it fails to write somme metrics. Without this fix, if counters can't be written, timings and histograms are not reset.
1 parent d7cefca commit bb963bf

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

metrics/dogstatsd/dogstatsd.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ func (d *Dogstatsd) SendLoop(ctx context.Context, c <-chan time.Time, network, a
141141
// lost if there is a problem with the write. Clients should be sure to call
142142
// WriteTo regularly, ideally through the WriteLoop or SendLoop helper methods.
143143
func (d *Dogstatsd) WriteTo(w io.Writer) (count int64, err error) {
144-
var n int
145-
146-
d.counters.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
144+
var (
145+
n int
146+
counters = d.counters.Reset()
147+
timings = d.timings.Reset()
148+
histograms = d.histograms.Reset()
149+
)
150+
151+
counters.Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
147152
n, err = fmt.Fprintf(w, "%s%s:%f|c%s%s\n", d.prefix, name, sum(values), sampling(d.rates.Get(name)), d.tagValues(lvs))
148153
if err != nil {
149154
return false
@@ -168,7 +173,7 @@ func (d *Dogstatsd) WriteTo(w io.Writer) (count int64, err error) {
168173
}
169174
d.mtx.RUnlock()
170175

171-
d.timings.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
176+
timings.Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
172177
sampleRate := d.rates.Get(name)
173178
for _, value := range values {
174179
n, err = fmt.Fprintf(w, "%s%s:%f|ms%s%s\n", d.prefix, name, value, sampling(sampleRate), d.tagValues(lvs))
@@ -183,7 +188,7 @@ func (d *Dogstatsd) WriteTo(w io.Writer) (count int64, err error) {
183188
return count, err
184189
}
185190

186-
d.histograms.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
191+
histograms.Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
187192
sampleRate := d.rates.Get(name)
188193
for _, value := range values {
189194
n, err = fmt.Fprintf(w, "%s%s:%f|h%s%s\n", d.prefix, name, value, sampling(sampleRate), d.tagValues(lvs))

0 commit comments

Comments
 (0)