Class: Graphite::Logger
- Inherits:
-
Object
- Object
- Graphite::Logger
- Defined in:
- lib/graphite/logger.rb
Constant Summary collapse
- DEFAULT_PORT =
2003
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#initialize(server_host, logger = nil) ⇒ Logger
constructor
Initialize a new Graphite::Logger class; expects a string containing a DNS name for the Graphite server.
-
#log(time, measurements) ⇒ Object
Write a bunch of values to the server taken at the given time.
- #socket ⇒ Object
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
#logger ⇒ Object
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) = "" measurements.each do |key, value| raise "Measurement is not numeric" unless value.respond_to? :to_f << "#{key} #{value.to_f} #{time.to_i}\n" end logger.info("Graphite: #{}") if logger begin socket.write() rescue Errno::EPIPE @socket = nil retry end end |
#socket ⇒ Object
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 |