Class: Wavefront::Cli::Write

Inherits:
Wavefront::Cli show all
Includes:
Wavefront::Constants, Mixins
Defined in:
lib/wavefront/cli/write.rb

Overview

backward compatibility.

Constant Summary

Constants included from Wavefront::Constants

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

Instance Attribute Summary

Attributes inherited from Wavefront::Cli

#arguments, #noop, #options

Instance Method Summary collapse

Methods included from Mixins

#call_delete, #call_get, #call_post, #hash_to_qs, #interpolate_schema, #load_file, #parse_time, #time_to_ms, #uri_concat

Methods inherited from Wavefront::Cli

#initialize

Constructor Details

This class inherits a constructor from Wavefront::Cli

Instance Method Details

#prep_tags(tags) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# 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

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# 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) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
# 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_optsObject



15
16
17
18
19
20
# 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) ⇒ Object



43
44
45
46
# 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