gauge_float64.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package metrics
  2. import "sync"
  3. // GaugeFloat64s hold a float64 value that can be set arbitrarily.
  4. type GaugeFloat64 interface {
  5. Snapshot() GaugeFloat64
  6. Update(float64)
  7. Value() float64
  8. }
  9. // GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a
  10. // new StandardGaugeFloat64.
  11. func GetOrRegisterGaugeFloat64(name string, r Registry) GaugeFloat64 {
  12. if nil == r {
  13. r = DefaultRegistry
  14. }
  15. return r.GetOrRegister(name, NewGaugeFloat64()).(GaugeFloat64)
  16. }
  17. // NewGaugeFloat64 constructs a new StandardGaugeFloat64.
  18. func NewGaugeFloat64() GaugeFloat64 {
  19. if UseNilMetrics {
  20. return NilGaugeFloat64{}
  21. }
  22. return &StandardGaugeFloat64{
  23. value: 0.0,
  24. }
  25. }
  26. // NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64.
  27. func NewRegisteredGaugeFloat64(name string, r Registry) GaugeFloat64 {
  28. c := NewGaugeFloat64()
  29. if nil == r {
  30. r = DefaultRegistry
  31. }
  32. r.Register(name, c)
  33. return c
  34. }
  35. // NewFunctionalGauge constructs a new FunctionalGauge.
  36. func NewFunctionalGaugeFloat64(f func() float64) GaugeFloat64 {
  37. if UseNilMetrics {
  38. return NilGaugeFloat64{}
  39. }
  40. return &FunctionalGaugeFloat64{value: f}
  41. }
  42. // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
  43. func NewRegisteredFunctionalGaugeFloat64(name string, r Registry, f func() float64) GaugeFloat64 {
  44. c := NewFunctionalGaugeFloat64(f)
  45. if nil == r {
  46. r = DefaultRegistry
  47. }
  48. r.Register(name, c)
  49. return c
  50. }
  51. // GaugeFloat64Snapshot is a read-only copy of another GaugeFloat64.
  52. type GaugeFloat64Snapshot float64
  53. // Snapshot returns the snapshot.
  54. func (g GaugeFloat64Snapshot) Snapshot() GaugeFloat64 { return g }
  55. // Update panics.
  56. func (GaugeFloat64Snapshot) Update(float64) {
  57. panic("Update called on a GaugeFloat64Snapshot")
  58. }
  59. // Value returns the value at the time the snapshot was taken.
  60. func (g GaugeFloat64Snapshot) Value() float64 { return float64(g) }
  61. // NilGauge is a no-op Gauge.
  62. type NilGaugeFloat64 struct{}
  63. // Snapshot is a no-op.
  64. func (NilGaugeFloat64) Snapshot() GaugeFloat64 { return NilGaugeFloat64{} }
  65. // Update is a no-op.
  66. func (NilGaugeFloat64) Update(v float64) {}
  67. // Value is a no-op.
  68. func (NilGaugeFloat64) Value() float64 { return 0.0 }
  69. // StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses
  70. // sync.Mutex to manage a single float64 value.
  71. type StandardGaugeFloat64 struct {
  72. mutex sync.Mutex
  73. value float64
  74. }
  75. // Snapshot returns a read-only copy of the gauge.
  76. func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 {
  77. return GaugeFloat64Snapshot(g.Value())
  78. }
  79. // Update updates the gauge's value.
  80. func (g *StandardGaugeFloat64) Update(v float64) {
  81. g.mutex.Lock()
  82. defer g.mutex.Unlock()
  83. g.value = v
  84. }
  85. // Value returns the gauge's current value.
  86. func (g *StandardGaugeFloat64) Value() float64 {
  87. g.mutex.Lock()
  88. defer g.mutex.Unlock()
  89. return g.value
  90. }
  91. // FunctionalGaugeFloat64 returns value from given function
  92. type FunctionalGaugeFloat64 struct {
  93. value func() float64
  94. }
  95. // Value returns the gauge's current value.
  96. func (g FunctionalGaugeFloat64) Value() float64 {
  97. return g.value()
  98. }
  99. // Snapshot returns the snapshot.
  100. func (g FunctionalGaugeFloat64) Snapshot() GaugeFloat64 { return GaugeFloat64Snapshot(g.Value()) }
  101. // Update panics.
  102. func (FunctionalGaugeFloat64) Update(float64) {
  103. panic("Update called on a FunctionalGaugeFloat64")
  104. }