gauge_float64.go 3.3 KB

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