Class: Wavefront::Write
- Inherits:
-
Object
- Object
- Wavefront::Write
- Includes:
- Validators
- Defined in:
- lib/wavefront-sdk/write.rb
Overview
This class helps you send points to Wavefront. It is extended by the Write and Report classes, which respectively handle point ingestion by a proxy and directly to the API.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#creds ⇒ Object
readonly
Returns the value of attribute creds.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Instance Method Summary collapse
-
#close ⇒ Object
Wrapper to the writer class’s #close method.
-
#hash_to_wf(point) ⇒ Object
Convert a validated point to a string conforming to community.wavefront.com/docs/DOC-1031.
-
#initialize(creds = {}, opts = {}) ⇒ Write
constructor
Construct an object which gives the user an interface for writing points to Wavefront.
-
#open ⇒ Object
Wrapper to the writer class’s #open method.
-
#paths_to_deltas(points) ⇒ Array[Hash]
Prefix all paths in a points array (as passed to #write_delta() with a delta symbol.
-
#point_array(point) ⇒ Object
Make an array which can be used by #hash_to_wf.
-
#raw(points, openclose = true) ⇒ Object
Send raw data to a Wavefront proxy, optionally automatically opening and closing the connection.
-
#send_point(point) ⇒ Object
Wrapper for the writer class’s #send_point method.
- #setup_options(user, defaults) ⇒ Object
-
#validation ⇒ Object
The method used to validate the data we wish to write.
-
#write(points = [], openclose = true, prefix = nil) ⇒ Object
A wrapper to the writer class’s #write method.
-
#write_delta(points, openclose = true) ⇒ Object
A wrapper method around #write() which guarantees all points will be sent as deltas.
Methods included from Validators
#wf_alert_id?, #wf_alert_severity?, #wf_cloudintegration_id?, #wf_dashboard_id?, #wf_derivedmetric_id?, #wf_distribution?, #wf_distribution_count?, #wf_distribution_interval?, #wf_distribution_values?, #wf_epoch?, #wf_event_id?, #wf_granularity?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_point?, #wf_point_tag?, #wf_point_tags?, #wf_proxy_id?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_source_id?, #wf_string?, #wf_tag?, #wf_ts?, #wf_user_id?, #wf_value?, #wf_version?, #wf_webhook_id?
Constructor Details
#initialize(creds = {}, opts = {}) ⇒ Write
Construct an object which gives the user an interface for writing points to Wavefront. The actual writing is handled by
- a Wavefront::Writer
-
subclass.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/wavefront-sdk/write.rb', line 42 def initialize(creds = {}, opts = {}) defaults = { tags: nil, writer: :socket, noop: false, novalidate: false, verbose: false, debug: false } @opts = (opts, defaults) @creds = creds (opts[:tags]) if opts[:tags] @logger = Wavefront::Logger.new(opts) @writer = setup_writer end |
Instance Attribute Details
#creds ⇒ Object (readonly)
Returns the value of attribute creds.
16 17 18 |
# File 'lib/wavefront-sdk/write.rb', line 16 def creds @creds end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
16 17 18 |
# File 'lib/wavefront-sdk/write.rb', line 16 def logger @logger end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
16 17 18 |
# File 'lib/wavefront-sdk/write.rb', line 16 def opts @opts end |
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
16 17 18 |
# File 'lib/wavefront-sdk/write.rb', line 16 def writer @writer end |
Instance Method Details
#close ⇒ Object
Wrapper to the writer class’s #close method.
70 71 72 |
# File 'lib/wavefront-sdk/write.rb', line 70 def close writer.close end |
#hash_to_wf(point) ⇒ Object
Convert a validated point to a string conforming to community.wavefront.com/docs/DOC-1031. No validation is done here.
155 156 157 158 159 160 |
# File 'lib/wavefront-sdk/write.rb', line 155 def hash_to_wf(point) format('%s %s %s source=%s %s %s', *point_array(point)).squeeze(' ').strip rescue StandardError raise Wavefront::Exception::InvalidPoint end |
#open ⇒ Object
Wrapper to the writer class’s #open method. Using this you can manually open a connection and re-use it.
64 65 66 |
# File 'lib/wavefront-sdk/write.rb', line 64 def open writer.open end |
#paths_to_deltas(points) ⇒ Array[Hash]
Prefix all paths in a points array (as passed to #write_delta() with a delta symbol
102 103 104 |
# File 'lib/wavefront-sdk/write.rb', line 102 def paths_to_deltas(points) [points].flatten.map { |p| p.tap { p[:path] = DELTA + p[:path] } } end |
#point_array(point) ⇒ Object
Make an array which can be used by #hash_to_wf.
167 168 169 170 171 172 173 174 |
# File 'lib/wavefront-sdk/write.rb', line 167 def point_array(point) [point[:path] || raise, point[:value] || raise, point.fetch(:ts, nil), point.fetch(:source, HOSTNAME), point[:tags] && point[:tags].to_wf_tag, opts[:tags] && opts[:tags].to_wf_tag] end |
#raw(points, openclose = true) ⇒ Object
Send raw data to a Wavefront proxy, optionally automatically opening and closing the connection. (Or not, if that does not make sense in the context of the writer.)
132 133 134 135 136 137 138 139 140 |
# File 'lib/wavefront-sdk/write.rb', line 132 def raw(points, openclose = true) writer.open if openclose && writer.respond_to?(:open) begin [points].flatten.each { |p| writer.send_point(p) } ensure writer.close if openclose && writer.respond_to?(:close) end end |
#send_point(point) ⇒ Object
Wrapper for the writer class’s #send_point method
110 111 112 113 114 115 116 117 118 |
# File 'lib/wavefront-sdk/write.rb', line 110 def send_point(point) if opts[:noop] logger.log "Would send: #{point}" return end logger.log("Sending: #{point}", :debug) writer.send_point(point) end |
#setup_options(user, defaults) ⇒ Object
57 58 59 |
# File 'lib/wavefront-sdk/write.rb', line 57 def (user, defaults) defaults.merge(user) end |
#validation ⇒ Object
The method used to validate the data we wish to write.
144 145 146 |
# File 'lib/wavefront-sdk/write.rb', line 144 def validation :wf_point? end |
#write(points = [], openclose = true, prefix = nil) ⇒ Object
A wrapper to the writer class’s #write method. Writers implement this method differently, Check the appropriate class documentation for @return information etc. The signature is always the same.
79 80 81 |
# File 'lib/wavefront-sdk/write.rb', line 79 def write(points = [], openclose = true, prefix = nil) writer.write(points, openclose, prefix) end |
#write_delta(points, openclose = true) ⇒ Object
A wrapper method around #write() which guarantees all points will be sent as deltas. You can still manually prefix any metric with a delta symbol and use #write(), but depending on your use-case, this method may be safer. It’s easy to forget the delta.
92 93 94 |
# File 'lib/wavefront-sdk/write.rb', line 92 def write_delta(points, openclose = true) write(paths_to_deltas(points), openclose) end |