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.
38 39 40 41 |
# File 'lib/philiprehberger/metric/counter.rb', line 38 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 counter.
27 28 29 30 31 32 |
# File 'lib/philiprehberger/metric/counter.rb', line 27 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.
53 54 55 |
# File 'lib/philiprehberger/metric/counter.rb', line 53 def reset @mutex.synchronize { @values.clear } end |
#snapshot ⇒ Hash
Return a snapshot of all values.
46 47 48 |
# File 'lib/philiprehberger/metric/counter.rb', line 46 def snapshot @mutex.synchronize { @values.dup } end |
#type ⇒ String
Returns the metric type name.
58 59 60 |
# File 'lib/philiprehberger/metric/counter.rb', line 58 def type 'counter' end |