github.com/devkononov/go-func@v0.0.0-20190722084534-f14392d369a7/metrics/json.go (about)

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