class Wavefront::Cli::Write

Push datapoints into Wavefront, via a proxy. This class deals in single points. It cannot batch, or deal with files or streams of data. This is because it depends on the very simple 'writer' class, which cannot be significantly changed, so as to maintain backward compatibility.

Public Instance Methods

prep_tags(tags) click to toggle source
# File lib/wavefront/cli/write.rb, line 85
def prep_tags(tags)
  #
  # Takes an array of key=value tags (as produced by docopt) and
  # turns it into an array of [key, value] arrays (as required
  # by various of our own methods). Anything not of the form
  # key=val is dropped.
  #
  return [] unless tags.is_a?(Array)
  tags.map { |t| t.split('=') }.select { |e| e.length == 2 }
end
run() click to toggle source
# File lib/wavefront/cli/write.rb, line 22
def run
  valid_value?(options[:'<value>'])
  valid_metric?(options[:'<metric>'])
  ts = options[:time] ? parse_time(options[:time]) : false

  [:proxy, :host].each do |h|
    fail Wavefront::Exception::InvalidHostname unless valid_host?(h)
  end

  write_opts = {
    agent_host:   options[:proxy],
    host_name:    options[:host],
    metric_name:  options[:'<metric>'],
    point_tags:   prep_tags(options[:tag]),
    timestamp:    ts,
    noop:         options[:noop]
  }

  write_metric(options[:'<value>'].to_f, options[:'<metric>'], write_opts)
end
valid_host?(hostname) click to toggle source
# File lib/wavefront/cli/write.rb, line 48
def valid_host?(hostname)
  #
  # quickly make sure a hostname looks vaguely sensible
  #
  hostname.match(/^[\w\.\-]+$/) && hostname.length < 1024
end
valid_metric?(metric) click to toggle source
# File lib/wavefront/cli/write.rb, line 68
def valid_metric?(metric)
  #
  # Apply some common-sense rules to metric paths. Check it's a
  # string, and that it has at least one dot in it. Don't allow
  # through odd characters or whitespace.
  #
  begin
    fail unless metric.is_a?(String) &&
                metric.split('.').length > 1 &&
                metric.match(/^[\w\-\._]+$/) &&
                metric.length < 1024
  rescue
    raise Wavefront::Exception::InvalidMetricName
  end
  true
end
valid_value?(value) click to toggle source
# File lib/wavefront/cli/write.rb, line 55
def valid_value?(value)
  #
  # Values, it seems, will always come in as strings. We need to
  # cast them to numbers. I don't think there's any reasonable way
  # to allow exponential notation.
  #
  unless value.is_a?(Numeric) || value.match(/^-?\d*\.?\d*$/) ||
         value.match(/^-?\d*\.?\d*e\d+$/)
    fail Wavefront::Exception::InvalidMetricValue
  end
  true
end
validate_opts() click to toggle source
# File lib/wavefront/cli/write.rb, line 15
def validate_opts
  #
  # Unlike all the API methods, we don't need a token here
  #
  abort 'Please supply a proxy endpoint.' unless options[:proxy]
end
write_metric(value, name, opts) click to toggle source
# File lib/wavefront/cli/write.rb, line 43
def write_metric(value, name, opts)
  wf = Wavefront::Writer.new(opts)
  wf.write(value, name, opts)
end