Class: Philiprehberger::Metric::Gauge
- Inherits:
-
Object
- Object
- Philiprehberger::Metric::Gauge
- Defined in:
- lib/philiprehberger/metric/gauge.rb
Overview
A gauge metric that can go up and down.
Instance Attribute Summary collapse
-
#help ⇒ String
readonly
The help description.
-
#name ⇒ String
readonly
The metric name.
Instance Method Summary collapse
-
#add(value, labels: {}) ⇒ void
Atomically add a value to the gauge.
-
#decrement(amount: 1, labels: {}) ⇒ void
Decrement the gauge.
-
#get(labels: {}) ⇒ Numeric
Get the current value for a set of labels.
-
#increment(amount: 1, labels: {}) ⇒ void
Increment the gauge.
-
#initialize(name, help: '') ⇒ Gauge
constructor
A new instance of Gauge.
-
#reset ⇒ void
Reset all values.
-
#set(value, labels: {}) ⇒ void
Set the gauge to a specific value.
-
#snapshot ⇒ Hash
Return a snapshot of all values.
-
#type ⇒ String
The metric type name.
Constructor Details
#initialize(name, help: '') ⇒ Gauge
Returns a new instance of Gauge.
15 16 17 18 19 20 |
# File 'lib/philiprehberger/metric/gauge.rb', line 15 def initialize(name, help: '') @name = name @help = help @mutex = Mutex.new @values = {} end |
Instance Attribute Details
#help ⇒ String (readonly)
Returns the help description.
11 12 13 |
# File 'lib/philiprehberger/metric/gauge.rb', line 11 def help @help end |
#name ⇒ String (readonly)
Returns the metric name.
8 9 10 |
# File 'lib/philiprehberger/metric/gauge.rb', line 8 def name @name end |
Instance Method Details
#add(value, labels: {}) ⇒ void
This method returns an undefined value.
Atomically add a value to the gauge. Negative values subtract.
58 59 60 61 62 63 |
# File 'lib/philiprehberger/metric/gauge.rb', line 58 def add(value, labels: {}) key = labels.sort.to_h @mutex.synchronize do @values[key] = (@values[key] || 0) + value end end |
#decrement(amount: 1, labels: {}) ⇒ void
This method returns an undefined value.
Decrement the gauge.
49 50 51 |
# File 'lib/philiprehberger/metric/gauge.rb', line 49 def decrement(amount: 1, labels: {}) increment(amount: -amount, labels: labels) end |
#get(labels: {}) ⇒ Numeric
Get the current value for a set of labels.
69 70 71 72 |
# File 'lib/philiprehberger/metric/gauge.rb', line 69 def get(labels: {}) key = labels.sort.to_h @mutex.synchronize { @values[key] || 0 } end |
#increment(amount: 1, labels: {}) ⇒ void
This method returns an undefined value.
Increment the gauge.
37 38 39 40 41 42 |
# File 'lib/philiprehberger/metric/gauge.rb', line 37 def increment(amount: 1, labels: {}) key = labels.sort.to_h @mutex.synchronize do @values[key] = (@values[key] || 0) + amount end end |
#reset ⇒ void
This method returns an undefined value.
Reset all values.
84 85 86 |
# File 'lib/philiprehberger/metric/gauge.rb', line 84 def reset @mutex.synchronize { @values.clear } end |
#set(value, labels: {}) ⇒ void
This method returns an undefined value.
Set the gauge to a specific value.
27 28 29 30 |
# File 'lib/philiprehberger/metric/gauge.rb', line 27 def set(value, labels: {}) key = labels.sort.to_h @mutex.synchronize { @values[key] = value } end |
#snapshot ⇒ Hash
Return a snapshot of all values.
77 78 79 |
# File 'lib/philiprehberger/metric/gauge.rb', line 77 def snapshot @mutex.synchronize { @values.dup } end |
#type ⇒ String
Returns the metric type name.
89 90 91 |
# File 'lib/philiprehberger/metric/gauge.rb', line 89 def type 'gauge' end |