Class: Philiprehberger::Metric::Gauge

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

Overview

A gauge metric that can go up and down.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Gauge.

Parameters:

  • name (String)

    the metric name

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

    the help description



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

#helpString (readonly)

Returns the help description.

Returns:

  • (String)

    the help description



11
12
13
# File 'lib/philiprehberger/metric/gauge.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/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.

Parameters:

  • value (Numeric)

    the amount to add (may be negative)

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

    optional labels



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.

Parameters:

  • amount (Numeric) (defaults to: 1)

    the amount to decrement by (default: 1)

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

    optional labels



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.

Parameters:

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

    the label set

Returns:

  • (Numeric)

    the current gauge value



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.

Parameters:

  • amount (Numeric) (defaults to: 1)

    the amount to increment by (default: 1)

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

    optional labels



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

#resetvoid

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.

Parameters:

  • value (Numeric)

    the value to set

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

    optional labels



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

#snapshotHash

Return a snapshot of all values.

Returns:

  • (Hash)

    labels => value pairs



77
78
79
# File 'lib/philiprehberger/metric/gauge.rb', line 77

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

#typeString

Returns the metric type name.

Returns:

  • (String)

    the metric type name



89
90
91
# File 'lib/philiprehberger/metric/gauge.rb', line 89

def type
  'gauge'
end