github.com/codingfuture/orig-energi3@v0.8.4/metrics/json.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  import (
    21  	"encoding/json"
    22  	"io"
    23  	"time"
    24  )
    25  
    26  // MarshalJSON returns a byte slice containing a JSON representation of all
    27  // the metrics in the Registry.
    28  func (r *StandardRegistry) MarshalJSON() ([]byte, error) {
    29  	return json.Marshal(r.GetAll())
    30  }
    31  
    32  // WriteJSON writes metrics from the given registry  periodically to the
    33  // specified io.Writer as JSON.
    34  func WriteJSON(r Registry, d time.Duration, w io.Writer) {
    35  	for range time.Tick(d) {
    36  		WriteJSONOnce(r, w)
    37  	}
    38  }
    39  
    40  // WriteJSONOnce writes metrics from the given registry to the specified
    41  // io.Writer as JSON.
    42  func WriteJSONOnce(r Registry, w io.Writer) {
    43  	json.NewEncoder(w).Encode(r)
    44  }
    45  
    46  func (p *PrefixedRegistry) MarshalJSON() ([]byte, error) {
    47  	return json.Marshal(p.GetAll())
    48  }