Class: Philiprehberger::Metric::Counter
- Inherits:
-
Object
- Object
- Philiprehberger::Metric::Counter
- Defined in:
- lib/philiprehberger/metric/counter.rb
Overview
A monotonically increasing counter metric.
Instance Attribute Summary collapse
-
#help ⇒ String
readonly
The help description.
-
#name ⇒ String
readonly
The metric name.
Instance Method Summary collapse
-
#get(labels: {}) ⇒ Numeric
Get the current value for a set of labels.
-
#increment(amount: 1, labels: {}) ⇒ void
Increment the counter.
-
#initialize(name, help: '') ⇒ Counter
constructor
A new instance of Counter.
-
#reset ⇒ void
Reset all values.
-
#snapshot ⇒ Hash
Return a snapshot of all values.
-
#type ⇒ String
The metric type name.
Constructor Details
#initialize(name, help: '') ⇒ Counter
Returns a new instance of Counter.
15 16 17 18 19 20 |
# File 'lib/philiprehberger/metric/counter.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/counter.rb', line 11 def help @help end |
#name ⇒ String (readonly)
Returns the metric name.
8 9 10 |
# File 'lib/philiprehberger/metric/counter.rb', line 8 def name @name end |
Instance Method Details
#get(labels: {}) ⇒ Numeric
Get the current value for a set of labels.
44 45 46 47 |
# File 'lib/philiprehberger/metric/counter.rb', line 44 def get(labels: {}) key = labels.sort.to_h @mutex.synchronize { @values[key] || 0 } end |
#increment(amount: 1, labels: {}) ⇒ void
31 32 33 34 35 36 37 38 |
# File 'lib/philiprehberger/metric/counter.rb', line 31 def increment(amount: 1, labels: {}) raise Error, 'Counter cannot decrease' if amount.negative? 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.
59 60 61 |
# File 'lib/philiprehberger/metric/counter.rb', line 59 def reset @mutex.synchronize { @values.clear } end |
#snapshot ⇒ Hash
Return a snapshot of all values.
52 53 54 |
# File 'lib/philiprehberger/metric/counter.rb', line 52 def snapshot @mutex.synchronize { @values.dup } end |
#type ⇒ String
Returns the metric type name.
64 65 66 |
# File 'lib/philiprehberger/metric/counter.rb', line 64 def type 'counter' end |