json.go 749 B

1234567891011121314151617181920212223242526272829303132
  1. package metrics
  2. import (
  3. "encoding/json"
  4. "io"
  5. "time"
  6. )
  7. // MarshalJSON returns a byte slice containing a JSON representation of all
  8. // the metrics in the Registry.
  9. func (r *StandardRegistry) MarshalJSON() ([]byte, error) {
  10. return json.Marshal(r.GetAll())
  11. }
  12. // WriteJSON writes metrics from the given registry periodically to the
  13. // specified io.Writer as JSON.
  14. func WriteJSON(r Registry, d time.Duration, w io.Writer) {
  15. for range time.Tick(d) {
  16. WriteJSONOnce(r, w)
  17. }
  18. }
  19. // WriteJSONOnce writes metrics from the given registry to the specified
  20. // io.Writer as JSON.
  21. func WriteJSONOnce(r Registry, w io.Writer) {
  22. json.NewEncoder(w).Encode(r)
  23. }
  24. func (p *PrefixedRegistry) MarshalJSON() ([]byte, error) {
  25. return json.Marshal(p.GetAll())
  26. }