|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "sync" |
| 6 | +) |
| 7 | + |
| 8 | +// GaugeInfos hold a GaugeInfoValue value that can be set arbitrarily. |
| 9 | +type GaugeInfo interface { |
| 10 | + Snapshot() GaugeInfo |
| 11 | + Update(GaugeInfoValue) |
| 12 | + Value() GaugeInfoValue |
| 13 | +} |
| 14 | + |
| 15 | +// GaugeInfoValue is a mappng of (string) keys to (string) values |
| 16 | +type GaugeInfoValue map[string]string |
| 17 | + |
| 18 | +func (val GaugeInfoValue) String() string { |
| 19 | + data, _ := json.Marshal(val) |
| 20 | + return string(data) |
| 21 | +} |
| 22 | + |
| 23 | +// GetOrRegisterGaugeInfo returns an existing GaugeInfo or constructs and registers a |
| 24 | +// new StandardGaugeInfo. |
| 25 | +func GetOrRegisterGaugeInfo(name string, r Registry) GaugeInfo { |
| 26 | + if nil == r { |
| 27 | + r = DefaultRegistry |
| 28 | + } |
| 29 | + return r.GetOrRegister(name, NewGaugeInfo()).(GaugeInfo) |
| 30 | +} |
| 31 | + |
| 32 | +// NewGaugeInfo constructs a new StandardGaugeInfo. |
| 33 | +func NewGaugeInfo() GaugeInfo { |
| 34 | + if !Enabled { |
| 35 | + return NilGaugeInfo{} |
| 36 | + } |
| 37 | + return &StandardGaugeInfo{ |
| 38 | + value: GaugeInfoValue{}, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// NewRegisteredGaugeInfo constructs and registers a new StandardGaugeInfo. |
| 43 | +func NewRegisteredGaugeInfo(name string, r Registry) GaugeInfo { |
| 44 | + c := NewGaugeInfo() |
| 45 | + if nil == r { |
| 46 | + r = DefaultRegistry |
| 47 | + } |
| 48 | + r.Register(name, c) |
| 49 | + return c |
| 50 | +} |
| 51 | + |
| 52 | +// NewFunctionalGauge constructs a new FunctionalGauge. |
| 53 | +func NewFunctionalGaugeInfo(f func() GaugeInfoValue) GaugeInfo { |
| 54 | + if !Enabled { |
| 55 | + return NilGaugeInfo{} |
| 56 | + } |
| 57 | + return &FunctionalGaugeInfo{value: f} |
| 58 | +} |
| 59 | + |
| 60 | +// NewRegisteredFunctionalGauge constructs and registers a new StandardGauge. |
| 61 | +func NewRegisteredFunctionalGaugeInfo(name string, r Registry, f func() GaugeInfoValue) GaugeInfo { |
| 62 | + c := NewFunctionalGaugeInfo(f) |
| 63 | + if nil == r { |
| 64 | + r = DefaultRegistry |
| 65 | + } |
| 66 | + r.Register(name, c) |
| 67 | + return c |
| 68 | +} |
| 69 | + |
| 70 | +// GaugeInfoSnapshot is a read-only copy of another GaugeInfo. |
| 71 | +type GaugeInfoSnapshot GaugeInfoValue |
| 72 | + |
| 73 | +// Snapshot returns the snapshot. |
| 74 | +func (g GaugeInfoSnapshot) Snapshot() GaugeInfo { return g } |
| 75 | + |
| 76 | +// Update panics. |
| 77 | +func (GaugeInfoSnapshot) Update(GaugeInfoValue) { |
| 78 | + panic("Update called on a GaugeInfoSnapshot") |
| 79 | +} |
| 80 | + |
| 81 | +// Value returns the value at the time the snapshot was taken. |
| 82 | +func (g GaugeInfoSnapshot) Value() GaugeInfoValue { return GaugeInfoValue(g) } |
| 83 | + |
| 84 | +// NilGauge is a no-op Gauge. |
| 85 | +type NilGaugeInfo struct{} |
| 86 | + |
| 87 | +// Snapshot is a no-op. |
| 88 | +func (NilGaugeInfo) Snapshot() GaugeInfo { return NilGaugeInfo{} } |
| 89 | + |
| 90 | +// Update is a no-op. |
| 91 | +func (NilGaugeInfo) Update(v GaugeInfoValue) {} |
| 92 | + |
| 93 | +// Value is a no-op. |
| 94 | +func (NilGaugeInfo) Value() GaugeInfoValue { return GaugeInfoValue{} } |
| 95 | + |
| 96 | +// StandardGaugeInfo is the standard implementation of a GaugeInfo and uses |
| 97 | +// sync.Mutex to manage a single string value. |
| 98 | +type StandardGaugeInfo struct { |
| 99 | + mutex sync.Mutex |
| 100 | + value GaugeInfoValue |
| 101 | +} |
| 102 | + |
| 103 | +// Snapshot returns a read-only copy of the gauge. |
| 104 | +func (g *StandardGaugeInfo) Snapshot() GaugeInfo { |
| 105 | + return GaugeInfoSnapshot(g.Value()) |
| 106 | +} |
| 107 | + |
| 108 | +// Update updates the gauge's value. |
| 109 | +func (g *StandardGaugeInfo) Update(v GaugeInfoValue) { |
| 110 | + g.mutex.Lock() |
| 111 | + defer g.mutex.Unlock() |
| 112 | + g.value = v |
| 113 | +} |
| 114 | + |
| 115 | +// Value returns the gauge's current value. |
| 116 | +func (g *StandardGaugeInfo) Value() GaugeInfoValue { |
| 117 | + g.mutex.Lock() |
| 118 | + defer g.mutex.Unlock() |
| 119 | + return g.value |
| 120 | +} |
| 121 | + |
| 122 | +// FunctionalGaugeInfo returns value from given function |
| 123 | +type FunctionalGaugeInfo struct { |
| 124 | + value func() GaugeInfoValue |
| 125 | +} |
| 126 | + |
| 127 | +// Value returns the gauge's current value. |
| 128 | +func (g FunctionalGaugeInfo) Value() GaugeInfoValue { |
| 129 | + return g.value() |
| 130 | +} |
| 131 | + |
| 132 | +// Value returns the gauge's current value in JSON string format |
| 133 | +func (g FunctionalGaugeInfo) ValueJsonString() string { |
| 134 | + data, _ := json.Marshal(g.value()) |
| 135 | + return string(data) |
| 136 | +} |
| 137 | + |
| 138 | +// Snapshot returns the snapshot. |
| 139 | +func (g FunctionalGaugeInfo) Snapshot() GaugeInfo { return GaugeInfoSnapshot(g.Value()) } |
| 140 | + |
| 141 | +// Update panics. |
| 142 | +func (FunctionalGaugeInfo) Update(GaugeInfoValue) { |
| 143 | + panic("Update called on a FunctionalGaugeInfo") |
| 144 | +} |
0 commit comments