class Graphite

Attributes

host[RW]
port[RW]
time[RW]
type[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/simple-graphite.rb, line 8
def initialize(options = {})
  @host = options[:host]
  @type = options[:type] ||= :tcp
  @port = options[:port] ||= @type.eql?(:tcp) ? 2003 : 8125
  @time = Time.now.to_i
end
time_now() click to toggle source
# File lib/simple-graphite.rb, line 37
def self.time_now
  @time = Time.now.to_i
end

Public Instance Methods

hostname() click to toggle source
# File lib/simple-graphite.rb, line 33
def hostname
  ::Socket.gethostname
end
push_to_graphite() { |socket| ... } click to toggle source
# File lib/simple-graphite.rb, line 15
def push_to_graphite
  raise "You need to provide a hostname" if @host.nil?
  begin
    socket = Graphite::Socket.new @host, @port, @type
    yield socket
  ensure
    socket.close
  end
end
send_metrics(metrics_hash) click to toggle source
# File lib/simple-graphite.rb, line 25
def send_metrics(metrics_hash)
  current_time = time_now
  push_to_graphite do |graphite|
    graphite.puts((metrics_hash.map { |k,v| [k, v, current_time].join(' ') + "\n" }).join(''))
  end
  current_time
end
time_now() click to toggle source
# File lib/simple-graphite.rb, line 41
def time_now
  self.class.time_now
end