Class: Wavefront::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront/writer.rb

Constant Summary collapse

DEFAULT_AGENT_HOST =
'localhost'
DEFAULT_PORT =
2878
DEFAULT_HOSTNAME =
%x{hostname -f}.chomp

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Writer

Returns a new instance of Writer.



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

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

Instance Method Details

#write(metric_value, metric_name = @metric_name, host = @host_name, point_tags = @point_tags, timestamp = Time.now) ⇒ Object



42
43
44
45
46
47
# File 'lib/wavefront/writer.rb', line 42

def write(metric_value, metric_name=@metric_name, host=@host_name, point_tags=@point_tags, timestamp=Time.now)
  raise Wavefront::Exception::EmptyMetricName if metric_name.empty?
  tags = point_tags.empty? ? '' : point_tags.map{|k,v| "#{k}=\"#{v}\""}.join(' ')
  append = tags.empty? ? "host=#{host}" : "host=#{host} #{tags}"
  @socket.puts "#{metric_name} #{metric_value} #{timestamp.to_i} #{append}"
end