Class: Yabeda::Counter

Inherits:
Metric
  • Object
show all
Defined in:
lib/yabeda/counter.rb

Overview

Growing-only counter

Instance Attribute Summary

Attributes inherited from Metric

#adapter, #aggregation, #comment, #group, #name, #per, #tags, #unit, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Metric

#adapters, #get, #increment_value, #initialize, #inspect

Constructor Details

This class inherits a constructor from Yabeda::Metric

Class Method Details

.parse_args(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yabeda/counter.rb', line 22

def self.parse_args(*args)
  case args.size
  when 0 # increment()
    [EMPTY_TAGS, 1]
  when 1 # increment(by: 5) or increment(tags)
    if args[0].key?(:by)
      [EMPTY_TAGS, args.fetch(:by)]
    else
      [args[0], 1]
    end
  when 2 # increment(tags, by: 5)
    [args[0], args[1].fetch(:by, 1)]
  else
    raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..2)"
  end
end

Instance Method Details

#increment(tags = {}, by: 1) ⇒ Object

Increment the counter for given set of tags by the given increment value

Parameters:

  • tags (defaults to: {})

    HashSymbol=>#to_s tags to identify the counter

  • by (Integer) (defaults to: 1)

    strictly positive increment value



10
11
12
13
14
15
16
17
18
# File 'lib/yabeda/counter.rb', line 10

def increment(*args)
  tags, by = self.class.parse_args(*args)
  all_tags = ::Yabeda::Tags.build(tags, group)
  value = increment_value(all_tags, by: by)
  adapters.each_value do |adapter|
    adapter.perform_counter_increment!(self, all_tags, by)
  end
  value
end