Class: Dumbstats::Graphite

Inherits:
Object
  • Object
show all
Includes:
Initialization
Defined in:
lib/dumbstats/graphite.rb

Overview

Agent to dump Buckets into Graphite.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Initialization

#update_from_hash!

Constructor Details

#initialize(*opts) ⇒ Graphite

Returns a new instance of Graphite.



17
18
19
20
# File 'lib/dumbstats/graphite.rb', line 17

def initialize *opts
  super
  @q = Queue.new
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/dumbstats/graphite.rb', line 9

def host
  @host
end

#nowObject

Returns the value of attribute now.



11
12
13
# File 'lib/dumbstats/graphite.rb', line 11

def now
  @now
end

#output_ioObject

Returns the value of attribute output_io.



15
16
17
# File 'lib/dumbstats/graphite.rb', line 15

def output_io
  @output_io
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/dumbstats/graphite.rb', line 9

def port
  @port
end

#prefixObject

Returns the value of attribute prefix.



11
12
13
# File 'lib/dumbstats/graphite.rb', line 11

def prefix
  @prefix
end

#runningObject

Returns the value of attribute running.



13
14
15
# File 'lib/dumbstats/graphite.rb', line 13

def running
  @running
end

#threadObject

Returns the value of attribute thread.



13
14
15
# File 'lib/dumbstats/graphite.rb', line 13

def thread
  @thread
end

Instance Method Details

#add!(name, value, now = nil, o = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/dumbstats/graphite.rb', line 26

def add! name, value, now = nil, o = nil
  o ||= EMPTY_Hash
  now ||= self.now || Time.now.utc
  name = encode_path(name)
  enqueue! "#{prefix}#{o[:prefix]}#{name}#{o[:suffix]} #{value} #{now.to_i}\n"
  self
end

#add_bucket!(b, opts = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dumbstats/graphite.rb', line 34

def add_bucket! b, opts = nil
  opts ||= EMPTY_Hash
  now = opts[:now]
  now ||= self.now || Time.now.utc
  if b.rate?
    add! b.name, b.rate, now, :prefix => opts[:prefix], :suffix => '.per_sec'
  else
    b.to_a.each do | k, v |
      next if a = opts[:ignore] and a.include?(k)
      add! b.name, v, now, :prefix => opts[:prefix], :suffix => '.' << encode_path(k)
    end
  end
end

#encode_path(name) ⇒ Object



22
23
24
# File 'lib/dumbstats/graphite.rb', line 22

def encode_path name
  name.to_s.gsub(/[^a-z0-9_]/i, '-')
end

#enqueue!(data) ⇒ Object



48
49
50
# File 'lib/dumbstats/graphite.rb', line 48

def enqueue! data
  @q << data
end

#run!Object



52
53
54
55
56
57
58
59
# File 'lib/dumbstats/graphite.rb', line 52

def run!
  @running = true
  @thread = Thread.current
  while @running
    send! @q.pop
  end
  self
end

#send!(data) ⇒ Object



61
62
63
# File 'lib/dumbstats/graphite.rb', line 61

def send! data
  output_io.write data
end

#socketObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dumbstats/graphite.rb', line 69

def socket
  unless @socket
    s = TCPSocket.new(host || '127.0.0.1', port || 2003) # CORRECT DEFAULT PORT?
    @socket = s
  end
  @socket
rescue ::Exception => exc
  STDERR.puts "#{self} socket: failed #{exc.inspect}\n  #{exc.backtrace * "\n  "}"
  sleep 10
  retry
end