go-ethereum/metrics/json.go
SexyERIC0723 9137152e19 metrics: replace time.Tick with time.NewTicker to prevent resource leaks
time.Tick() creates a ticker whose underlying resources can never be
released because there is no handle to call Stop(). This is flagged
by go vet (SA1015).

6 files in metrics/ fixed: debug.go, json.go, log.go, opentsdb.go,
syslog.go, writer.go.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-18 16:18:32 +00:00

33 lines
795 B
Go

package metrics
import (
"encoding/json"
"io"
"time"
)
// MarshalJSON returns a byte slice containing a JSON representation of all
// the metrics in the Registry.
func (r *StandardRegistry) MarshalJSON() ([]byte, error) {
return json.Marshal(r.GetAll())
}
// WriteJSON writes metrics from the given registry periodically to the
// specified io.Writer as JSON.
func WriteJSON(r Registry, d time.Duration, w io.Writer) {
ticker := time.NewTicker(d)
defer ticker.Stop()
for range ticker.C {
WriteJSONOnce(r, w)
}
}
// WriteJSONOnce writes metrics from the given registry to the specified
// io.Writer as JSON.
func WriteJSONOnce(r Registry, w io.Writer) {
json.NewEncoder(w).Encode(r)
}
func (r *PrefixedRegistry) MarshalJSON() ([]byte, error) {
return json.Marshal(r.GetAll())
}