class Wavefront::Writer

Constants

DEFAULT_AGENT_HOST
DEFAULT_HOSTNAME
DEFAULT_PORT

Public Class Methods

new(options = {}) click to toggle source
# File lib/wavefront/writer.rb, line 30
def initialize(options = {})
  options[:agent_host] ||= DEFAULT_AGENT_HOST
  options[:agent_port] ||= DEFAULT_PORT
  options[:host_name] ||= DEFAULT_HOSTNAME
  options[:metric_name] ||= ''
  options[:point_tags] ||= {}

  @host_name = options[:host_name]
  @metric_name = options[:metric_name]
  @point_tags = options[:point_tags]

  @socket = get_socket(options[:agent_host], options[:agent_port])
end

Public Instance Methods

write(metric_value, metric_name = @metric_name, options = {}) click to toggle source
# File lib/wavefront/writer.rb, line 44
def write(metric_value, metric_name = @metric_name, options = {})
  options[:host_name] ||= @host_name
  options[:point_tags] ||= @point_tags
  options[:timestamp] ||= Time.now

  if metric_name.empty?
    raise Wavefront::Exception::EmptyMetricName
  end

  if options[:point_tags].empty?
    append = "host=#{options[:host_name]}"
  else
    tags = options[:point_tags].map { |k, v| "#{k}=\"#{v}\"" }.join(' ')
    append = "host=#{options[:host_name]} #{tags}"
  end

  str = [metric_name, metric_value, options[:timestamp].to_i,
                append].join(' ')

  if options[:noop]
    puts "metric to send: #{str}"
  else
    @socket.puts(str)
  end
end

Private Instance Methods

get_socket(host, port) click to toggle source
# File lib/wavefront/writer.rb, line 72
def get_socket(host, port)
  TCPSocket.new(host, port)
end