Class: WavefrontCli::Write

Inherits:
BaseWrite show all
Defined in:
lib/wavefront-cli/write.rb

Overview

Send points via a proxy. This inherits from the same base class as Report, but has to do a couple of things differently, as it speaks to a proxy rather than to the API.

Constant Summary

Constants included from Constants

Constants::ALL_PAGE_SIZE, Constants::DEFAULT_OPTS, Constants::HUMAN_TIME_FORMAT, Constants::HUMAN_TIME_FORMAT_MS

Instance Attribute Summary

Attributes inherited from BaseWrite

#fmt

Attributes inherited from Base

#klass, #klass_word, #options, #wf

Instance Method Summary collapse

Methods inherited from BaseWrite

#call_write, #do_file, #do_point, #enough_fields?, #expand_dist, #extract_path, #extract_source, #extract_tags, #extract_ts, #extract_value, #line_tags, #process_input, #process_input_file, #process_line, #read_stdin, #send_point, #tags_to_hash, #valid_format?, #valid_timestamp?

Methods inherited from Base

#check_status, #conds_to_query, #dispatch, #display, #display_api_error, #display_no_api_response, #do_delete, #do_describe, #do_import, #do_list, #do_search, #do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_set, #do_tags, #do_undelete, #do_update, #format_var, #handle_error, #handle_response, #hcl_fields, #import_to_create, #initialize, #load_display_class, #load_file, #load_from_stdin, #mk_opts, #no_api_response, #ok_exit, #options_and_exit, #parseable_output, #range_hash, #run, #search_key, #validate_id, #validate_input, #validate_tags, #validator_exception, #validator_method

Constructor Details

This class inherits a constructor from WavefrontCli::Base

Instance Method Details

#_sdk_classObject

I chose to prioritise UI consistency over internal elegance here. The write command doesn’t follow the age-old assumption that each command maps 1:1 to a similarly named SDK class. Write can use write or distribution.



40
41
42
43
# File 'lib/wavefront-cli/write.rb', line 40

def _sdk_class
  return 'Wavefront::Distribution' if distribution?
  'Wavefront::Write'
end

#close_connectionObject



86
87
88
# File 'lib/wavefront-cli/write.rb', line 86

def close_connection
  wf.close
end

#default_portObject



58
59
60
# File 'lib/wavefront-cli/write.rb', line 58

def default_port
  distribution? ? 40000 : 2878
end

#distribution?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/wavefront-cli/write.rb', line 45

def distribution?
  return true if options[:distribution]
  options[:infileformat] && options[:infileformat].include?('d')
end

#do_distributionObject

rubocop:disable Metrics/AbcSize



11
12
13
14
15
16
17
18
19
20
# File 'lib/wavefront-cli/write.rb', line 11

def do_distribution
  p = { path:     options[:'<metric>'],
        interval: options[:interval] || 'M',
        value:    mk_dist,
        tags:     tags_to_hash(options[:tag]) }

  p[:source] = options[:host] if options[:host]
  p[:ts] = parse_time(options[:time]) if options[:time]
  send_point(p)
end

#extra_optionsObject



31
32
33
# File 'lib/wavefront-cli/write.rb', line 31

def extra_options
  options[:using] ? { writer: options[:using] } : {}
end

#mk_credsObject



50
51
52
53
54
55
56
# File 'lib/wavefront-cli/write.rb', line 50

def mk_creds
  { proxy:    options[:proxy],
    port:     options[:port] || default_port,
    socket:   options[:socket],
    endpoint: options[:endpoint],
    token:    options[:token] }
end

#mk_distObject

Turn our user’s representation of a distribution into one which suits Wavefront. The SDK can do this for us.



26
27
28
29
# File 'lib/wavefront-cli/write.rb', line 26

def mk_dist
  xpanded = expand_dist(options[:'<val>'])
  wf.mk_distribution(xpanded.map(&:to_f))
end

#open_connectionObject



82
83
84
# File 'lib/wavefront-cli/write.rb', line 82

def open_connection
  wf.open
end

#validate_optsObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wavefront-cli/write.rb', line 62

def validate_opts
  validate_opts_file if options[:file]

  if options[:using] == 'unix'
    return true if options[:socket]
    raise(WavefrontCli::Exception::CredentialError, 'No socket path.')
  end

  return true if options[:proxy]
  raise(WavefrontCli::Exception::CredentialError, 'No proxy address.')
end

#validate_opts_fileObject



74
75
76
77
78
79
80
# File 'lib/wavefront-cli/write.rb', line 74

def validate_opts_file
  unless options[:metric] || (options.key?(:infileformat) &&
                              options[:infileformat].include?('m'))
    raise(WavefrontCli::Exception::InsufficientData,
          "Supply a metric path in the file or with '-m'.")
  end
end