Class: Philiprehberger::Metric::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/metric/counter.rb

Overview

A monotonically increasing counter metric.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, help: '') ⇒ Counter

Returns a new instance of Counter.

Parameters:

  • name (String)

    the metric name

  • help (String) (defaults to: '')

    the help description



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

#helpString (readonly)

Returns the help description.

Returns:

  • (String)

    the help description



11
12
13
# File 'lib/philiprehberger/metric/counter.rb', line 11

def help
  @help
end

#nameString (readonly)

Returns the metric name.

Returns:

  • (String)

    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.

Parameters:

  • labels (Hash) (defaults to: {})

    the label set

Returns:

  • (Numeric)

    the current counter value



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.

Parameters:

  • amount (Numeric) (defaults to: 1)

    the amount to increment by (default: 1)

  • labels (Hash) (defaults to: {})

    optional labels for dimensional metrics



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

#resetvoid

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

#snapshotHash

Return a snapshot of all values.

Returns:

  • (Hash)

    labels => value pairs



46
47
48
# File 'lib/philiprehberger/metric/counter.rb', line 46

def snapshot
  @mutex.synchronize { @values.dup }
end

#typeString

Returns the metric type name.

Returns:

  • (String)

    the metric type name



58
59
60
# File 'lib/philiprehberger/metric/counter.rb', line 58

def type
  'counter'
end