Class: Graphiterb::Accumulator

Inherits:
Object
  • Object
show all
Includes:
Utils::SystemInfo
Defined in:
lib/graphiterb/accumulator.rb

Overview

An accumulator that uses a Redis database as a fast store.

a = Accumulator.new
a.increment('my_value')

It’s assumed that the Redis database is local and on the default port, but pass in :host or :port (or any other options Redis.new understands) to change this.

By default incrementing ‘my_value’ which actually increment a counter stored at the key ‘graphiterb_accumulator:my_value:GRAPHITE_IDENTIFIER’.

See Graphiterb::Monitors::AccumulationsConsumer for the periodic monitor that will consume the accumulated counts.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::SystemInfo

#graphite_identifier, #hostname, #node_name

Constructor Details

#initialize(main_scope, options = {}) ⇒ Accumulator

Initialize a new Accumulator.

Takes the same options as Redis.new.

Also takes the :namespace option to change where the accumulations are stored. Different applications can use different accumulators with different namespaces in a single, shared Redis.

Parameters:

  • main_scope (String)
  • options (Hash) (defaults to: {})


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/graphiterb/accumulator.rb', line 48

def initialize main_scope, options={}
  require 'redis'
  begin
    require 'redis-namespace'
  rescue LoadError
    require 'redis/namespace'
  end
  @main_scope   = main_scope
  @redis        = Redis.new(options)
  @namespace    = options[:namespace] || 'graphiterb'
  @accumulators = Redis::Namespace.new(namespace, :redis => redis)
end

Instance Attribute Details

#accumulatorsObject

The Redis namespace (an object) used for the accumulators.



28
29
30
# File 'lib/graphiterb/accumulator.rb', line 28

def accumulators
  @accumulators
end

#main_scopeObject

The top-level Graphite scope inserted for each record.



31
32
33
# File 'lib/graphiterb/accumulator.rb', line 31

def main_scope
  @main_scope
end

#namespaceObject

The name of the Redis namespace (a string) in which accumulations are stored (defaults to ‘graphiterb’)



25
26
27
# File 'lib/graphiterb/accumulator.rb', line 25

def namespace
  @namespace
end

#redisObject

The Redis database.



21
22
23
# File 'lib/graphiterb/accumulator.rb', line 21

def redis
  @redis
end

Instance Method Details

#increment(*args) ⇒ Object

Increment the Graphite target args.

Parameters:

  • args (Array<String>, String)


74
75
76
# File 'lib/graphiterb/accumulator.rb', line 74

def increment *args
  accumulators.incr(scope(*args))
end

#increment_by(amount, *args) ⇒ Object

Increment the Graphite target args by the given amount.

The target will be automatically scoped, see Accumulator#scope.

Parameters:

  • amount (Integer)
  • args (Array<String>, String)


67
68
69
# File 'lib/graphiterb/accumulator.rb', line 67

def increment_by amount, *args
  accumulators.incrby(scope(*args), amount)
end

#scope(*args) ⇒ String

Return the scoped accumulator name.

This will be a valid string target that can be passed directly to Graphite.

a = Accumulator.new('scrapers')
a.scope('foo.bar', 'baz')
#=> 'scrapers.foo.bar.baz.ip-120.112.4.383'

Parameters:

  • args (Array<String>, String)

Returns:

  • (String)


89
90
91
# File 'lib/graphiterb/accumulator.rb', line 89

def scope *args
  [main_scope, args, graphite_identifier].flatten.compact.map(&:to_s).join('.')
end