Class: Vmpooler::Graphite

Inherits:
Object
  • Object
show all
Defined in:
lib/vmpooler/graphite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Graphite

Returns a new instance of Graphite.



7
8
9
10
11
12
13
14
15
# File 'lib/vmpooler/graphite.rb', line 7

def initialize(params = {})
  if params['server'].nil? || params['server'].empty?
    raise ArgumentError, "Graphite server is required. Config: #{params.inspect}"
  end

  @server = params['server']
  @port   = params['port'] || 2003
  @prefix = params['prefix'] || 'vmpooler'
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/vmpooler/graphite.rb', line 5

def port
  @port
end

#prefixObject (readonly)

Returns the value of attribute prefix.



5
6
7
# File 'lib/vmpooler/graphite.rb', line 5

def prefix
  @prefix
end

#serverObject (readonly)

Returns the value of attribute server.



5
6
7
# File 'lib/vmpooler/graphite.rb', line 5

def server
  @server
end

Instance Method Details

#gauge(label, value) ⇒ Object



21
22
23
# File 'lib/vmpooler/graphite.rb', line 21

def gauge(label, value)
  log label, value
end

#increment(label) ⇒ Object



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

def increment(label)
  log label, 1
end

#log(path, value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vmpooler/graphite.rb', line 29

def log(path, value)
  Thread.new do
    socket = TCPSocket.new(server, port)
    begin
      socket.puts "#{prefix}.#{path} #{value} #{Time.now.to_i}"
    ensure
      socket.close
    end
  end
rescue => err
  $stderr.puts "Failure logging #{path} to graphite server [#{server}:#{port}]: #{err}"
end

#timing(label, duration) ⇒ Object



25
26
27
# File 'lib/vmpooler/graphite.rb', line 25

def timing(label, duration)
  log label, duration
end