Class: Graphite::Logger

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

Constant Summary collapse

DEFAULT_PORT =
2003

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_host, logger = nil) ⇒ Logger

Initialize a new Graphite::Logger class; expects a string containing a DNS name for the Graphite server. This hostname may optional include a port number, e.g. “graphite.example.com:3333”. If not specified, DEFAULT_PORT will be used. If you specify a Ruby-compatible logger object in the second parameter, a string containing the graphite message will be logged there before it is sent to the socket.



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

def initialize(server_host, logger = nil)
  @server = server_host
  @logger = logger
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#log(time, measurements) ⇒ Object

Write a bunch of values to the server taken at the given time



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

def log(time, measurements)
  message = ""
  measurements.each do |key, value|
    raise "Measurement is not numeric" unless value.respond_to? :to_f
    message << "#{key} #{value.to_f} #{time.to_i}\n"
  end
  logger.info("Graphite: #{message}") if logger
  begin
    socket.write(message)
  rescue Errno::EPIPE
    @socket = nil
	retry
  end	
end

#socketObject



19
20
21
22
23
24
25
26
# File 'lib/graphite/logger.rb', line 19

def socket
  if @socket.nil? || @socket.closed?
    host, port = @server.split(/:/)
    port ||= DEFAULT_PORT
    @socket = TCPSocket.new(host, port)
  end
  @socket
end