Class: Wavefront::Writer

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

Constant Summary collapse

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

Constants included from Constants

Constants::ALERT_FORMATS, Constants::DASH_FORMATS, Constants::DEFAULT_ALERT_FORMAT, Constants::DEFAULT_DASH_FORMAT, Constants::DEFAULT_FORMAT, Constants::DEFAULT_HOST, Constants::DEFAULT_INFILE_FORMAT, Constants::DEFAULT_OBSOLETE_METRICS, Constants::DEFAULT_OPTS, Constants::DEFAULT_PERIOD_SECONDS, Constants::DEFAULT_PREFIX_LENGTH, Constants::DEFAULT_PROXY, Constants::DEFAULT_PROXY_PORT, Constants::DEFAULT_SOURCE_FORMAT, Constants::DEFAULT_STRICT, Constants::EVENT_LEVELS, Constants::EVENT_STATE_DIR, Constants::FORMATS, Constants::GRANULARITIES, Constants::SOURCE_FORMATS

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Writer

Returns a new instance of Writer.



30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Method Details

#write(metric_value, metric_name = @metric_name, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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