github.com/ethereum/go-ethereum@v1.16.1/metrics/gauge_info.go (about)

     1  package metrics
     2  
     3  import (
     4  	"encoding/json"
     5  	"sync"
     6  )
     7  
     8  // GaugeInfoValue is a mapping of keys to values
     9  type GaugeInfoValue map[string]string
    10  
    11  func (val GaugeInfoValue) String() string {
    12  	data, _ := json.Marshal(val)
    13  	return string(data)
    14  }
    15  
    16  // GetOrRegisterGaugeInfo returns an existing GaugeInfo or constructs and registers a
    17  // new GaugeInfo.
    18  func GetOrRegisterGaugeInfo(name string, r Registry) *GaugeInfo {
    19  	return getOrRegister(name, NewGaugeInfo, r)
    20  }
    21  
    22  // NewGaugeInfo constructs a new GaugeInfo.
    23  func NewGaugeInfo() *GaugeInfo {
    24  	return &GaugeInfo{
    25  		value: GaugeInfoValue{},
    26  	}
    27  }
    28  
    29  // NewRegisteredGaugeInfo constructs and registers a new GaugeInfo.
    30  func NewRegisteredGaugeInfo(name string, r Registry) *GaugeInfo {
    31  	c := NewGaugeInfo()
    32  	if nil == r {
    33  		r = DefaultRegistry
    34  	}
    35  	r.Register(name, c)
    36  	return c
    37  }
    38  
    39  // GaugeInfoSnapshot is a read-only copy of another GaugeInfo.
    40  type GaugeInfoSnapshot GaugeInfoValue
    41  
    42  // Value returns the value at the time the snapshot was taken.
    43  func (g GaugeInfoSnapshot) Value() GaugeInfoValue { return GaugeInfoValue(g) }
    44  
    45  // GaugeInfo maintains a set of key/value mappings.
    46  type GaugeInfo struct {
    47  	mutex sync.Mutex
    48  	value GaugeInfoValue
    49  }
    50  
    51  // Snapshot returns a read-only copy of the gauge.
    52  func (g *GaugeInfo) Snapshot() GaugeInfoSnapshot {
    53  	return GaugeInfoSnapshot(g.value)
    54  }
    55  
    56  // Update updates the gauge's value.
    57  func (g *GaugeInfo) Update(v GaugeInfoValue) {
    58  	g.mutex.Lock()
    59  	defer g.mutex.Unlock()
    60  	g.value = v
    61  }